3 * Implementation of GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
41 // default constructor, destructor, copy constructor assignment operator and helpers
44 indexed::indexed() : symmetry(unknown)
46 debugmsg("indexed default constructor", LOGLEVEL_CONSTRUCT);
47 tinfo_key = TINFO_indexed;
50 void indexed::copy(const indexed & other)
52 inherited::copy(other);
53 symmetry = other.symmetry;
56 DEFAULT_DESTROY(indexed)
62 indexed::indexed(const ex & b) : inherited(b), symmetry(unknown)
64 debugmsg("indexed constructor from ex", LOGLEVEL_CONSTRUCT);
65 tinfo_key = TINFO_indexed;
66 assert_all_indices_of_type_idx();
69 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symmetry(unknown)
71 debugmsg("indexed constructor from ex,ex", LOGLEVEL_CONSTRUCT);
72 tinfo_key = TINFO_indexed;
73 assert_all_indices_of_type_idx();
76 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(unknown)
78 debugmsg("indexed constructor from ex,ex,ex", LOGLEVEL_CONSTRUCT);
79 tinfo_key = TINFO_indexed;
80 assert_all_indices_of_type_idx();
83 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(unknown)
85 debugmsg("indexed constructor from ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
86 tinfo_key = TINFO_indexed;
87 assert_all_indices_of_type_idx();
90 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symmetry(unknown)
92 debugmsg("indexed constructor from ex,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
93 tinfo_key = TINFO_indexed;
94 assert_all_indices_of_type_idx();
97 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(symm)
99 debugmsg("indexed constructor from ex,symmetry,ex,ex", LOGLEVEL_CONSTRUCT);
100 tinfo_key = TINFO_indexed;
101 assert_all_indices_of_type_idx();
104 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(symm)
106 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex", LOGLEVEL_CONSTRUCT);
107 tinfo_key = TINFO_indexed;
108 assert_all_indices_of_type_idx();
111 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symmetry(symm)
113 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
114 tinfo_key = TINFO_indexed;
115 assert_all_indices_of_type_idx();
118 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symmetry(unknown)
120 debugmsg("indexed constructor from ex,exvector", LOGLEVEL_CONSTRUCT);
121 seq.insert(seq.end(), v.begin(), v.end());
122 tinfo_key = TINFO_indexed;
123 assert_all_indices_of_type_idx();
126 indexed::indexed(const ex & b, symmetry_type symm, const exvector & v) : inherited(b), symmetry(symm)
128 debugmsg("indexed constructor from ex,symmetry,exvector", LOGLEVEL_CONSTRUCT);
129 seq.insert(seq.end(), v.begin(), v.end());
130 tinfo_key = TINFO_indexed;
131 assert_all_indices_of_type_idx();
134 indexed::indexed(symmetry_type symm, const exprseq & es) : inherited(es), symmetry(symm)
136 debugmsg("indexed constructor from symmetry,exprseq", LOGLEVEL_CONSTRUCT);
137 tinfo_key = TINFO_indexed;
138 assert_all_indices_of_type_idx();
141 indexed::indexed(symmetry_type symm, const exvector & v, bool discardable) : inherited(v, discardable), symmetry(symm)
143 debugmsg("indexed constructor from symmetry,exvector", LOGLEVEL_CONSTRUCT);
144 tinfo_key = TINFO_indexed;
145 assert_all_indices_of_type_idx();
148 indexed::indexed(symmetry_type symm, exvector * vp) : inherited(vp), symmetry(symm)
150 debugmsg("indexed constructor from symmetry,exvector *", LOGLEVEL_CONSTRUCT);
151 tinfo_key = TINFO_indexed;
152 assert_all_indices_of_type_idx();
159 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
161 debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
163 if (!(n.find_unsigned("symmetry", symm)))
164 throw (std::runtime_error("unknown indexed symmetry type in archive"));
167 void indexed::archive(archive_node &n) const
169 inherited::archive(n);
170 n.add_unsigned("symmetry", symmetry);
173 DEFAULT_UNARCHIVE(indexed)
176 // functions overriding virtual functions from bases classes
179 void indexed::printraw(std::ostream & os) const
181 debugmsg("indexed printraw", LOGLEVEL_PRINT);
182 GINAC_ASSERT(seq.size() > 0);
184 os << class_name() << "(";
188 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
191 void indexed::printtree(std::ostream & os, unsigned indent) const
193 debugmsg("indexed printtree", LOGLEVEL_PRINT);
194 GINAC_ASSERT(seq.size() > 0);
196 os << std::string(indent, ' ') << class_name() << ", " << seq.size()-1 << " indices";
197 os << ",hash=" << hashvalue << ",flags=" << flags << std::endl;
198 printtreeindices(os, indent);
201 void indexed::print(std::ostream & os, unsigned upper_precedence) const
203 debugmsg("indexed print", LOGLEVEL_PRINT);
204 GINAC_ASSERT(seq.size() > 0);
206 const ex & base = seq[0];
207 bool need_parens = is_ex_exactly_of_type(base, add) || is_ex_exactly_of_type(base, mul)
208 || is_ex_exactly_of_type(base, ncmul) || is_ex_exactly_of_type(base, power);
217 bool indexed::info(unsigned inf) const
219 if (inf == info_flags::indexed) return true;
220 if (inf == info_flags::has_indices) return seq.size() > 1;
221 return inherited::info(inf);
224 bool indexed::all_index_values_are(unsigned inf) const
226 // No indices? Then no property can be fulfilled
231 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
232 while (it != itend) {
233 GINAC_ASSERT(is_ex_of_type(*it, idx));
234 if (!ex_to_idx(*it).get_value().info(inf))
241 int indexed::compare_same_type(const basic & other) const
243 GINAC_ASSERT(is_of_type(other, indexed));
244 return inherited::compare_same_type(other);
247 // The main difference between sort_index_vector() and canonicalize_indices()
248 // is that the latter takes the symmetry of the object into account. Once we
249 // implement mixed symmetries, canonicalize_indices() will only be able to
250 // reorder index pairs with known symmetry properties, while sort_index_vector()
251 // always sorts the whole vector.
253 /** Bring a vector of indices into a canonic order (don't care about the
254 * symmetry of the objects carrying the indices). Dummy indices will lie
255 * next to each other after the sorting.
257 * @param v Index vector to be sorted */
258 static void sort_index_vector(exvector &v)
260 // Nothing to sort if less than 2 elements
264 // Simple bubble sort algorithm should be sufficient for the small
265 // number of indices expected
266 exvector::iterator it1 = v.begin(), itend = v.end(), next_to_last_idx = itend - 1;
267 while (it1 != next_to_last_idx) {
268 exvector::iterator it2 = it1 + 1;
269 while (it2 != itend) {
270 if (it1->compare(*it2) > 0)
278 /** Bring a vector of indices into a canonic order. This operation only makes
279 * sense if the object carrying these indices is either symmetric or totally
280 * antisymmetric with respect to the indices.
282 * @param itbegin Start of index vector
283 * @param itend End of index vector
284 * @param antisymm Whether the object is antisymmetric
285 * @return the sign introduced by the reordering of the indices if the object
286 * is antisymmetric (or 0 if two equal indices are encountered). For
287 * symmetric objects, this is always +1. If the index vector was
288 * already in a canonic order this function returns INT_MAX. */
289 static int canonicalize_indices(exvector::iterator itbegin, exvector::iterator itend, bool antisymm)
291 bool something_changed = false;
294 // Simple bubble sort algorithm should be sufficient for the small
295 // number of indices expected
296 exvector::iterator it1 = itbegin, next_to_last_idx = itend - 1;
297 while (it1 != next_to_last_idx) {
298 exvector::iterator it2 = it1 + 1;
299 while (it2 != itend) {
300 int cmpval = it1->compare(*it2);
303 something_changed = true;
306 } else if (cmpval == 0 && antisymm) {
307 something_changed = true;
315 return something_changed ? sig : INT_MAX;
318 ex indexed::eval(int level) const
320 // First evaluate children, then we will end up here again
322 return indexed(symmetry, evalchildren(level));
324 const ex &base = seq[0];
326 // If the base object is 0, the whole object is 0
330 // If the base object is a product, pull out the numeric factor
331 if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
333 ex f = ex_to_numeric(base.op(base.nops() - 1));
335 return f * thisexprseq(v);
338 // Canonicalize indices according to the symmetry properties
339 if (seq.size() > 2 && (symmetry != unknown && symmetry != mixed)) {
341 int sig = canonicalize_indices(v.begin() + 1, v.end(), symmetry == antisymmetric);
342 if (sig != INT_MAX) {
343 // Something has changed while sorting indices, more evaluations later
346 return ex(sig) * thisexprseq(v);
350 // Let the class of the base object perform additional evaluations
351 return base.bp->eval_indexed(*this);
354 int indexed::degree(const ex & s) const
356 return is_equal(*s.bp) ? 1 : 0;
359 int indexed::ldegree(const ex & s) const
361 return is_equal(*s.bp) ? 1 : 0;
364 ex indexed::coeff(const ex & s, int n) const
367 return n==1 ? _ex1() : _ex0();
369 return n==0 ? ex(*this) : _ex0();
372 ex indexed::subs(const lst & ls, const lst & lr) const
374 GINAC_ASSERT(ls.nops() == lr.nops());
376 for (unsigned i=0; i<ls.nops(); i++) {
377 if (is_ex_of_type(ls.op(i), indexed) &&
378 compare_same_type(ex_to_indexed(ls.op(i)))==0)
382 return inherited::subs(ls, lr);
385 ex indexed::thisexprseq(const exvector & v) const
387 return indexed(symmetry, v);
390 ex indexed::thisexprseq(exvector * vp) const
392 return indexed(symmetry, vp);
395 ex indexed::expand(unsigned options) const
397 GINAC_ASSERT(seq.size() > 0);
399 if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
401 // expand_indexed expands (a+b).i -> a.i + b.i
402 const ex & base = seq[0];
404 for (unsigned i=0; i<base.nops(); i++) {
407 sum += thisexprseq(s).expand();
412 return inherited::expand(options);
416 // virtual functions which can be overridden by derived classes
422 // non-virtual functions in this class
425 void indexed::printrawindices(std::ostream & os) const
427 if (seq.size() > 1) {
428 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
429 while (it != itend) {
438 void indexed::printtreeindices(std::ostream & os, unsigned indent) const
440 if (seq.size() > 1) {
441 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
442 while (it != itend) {
443 os << std::string(indent + delta_indent, ' ');
451 void indexed::printindices(std::ostream & os) const
453 if (seq.size() > 1) {
454 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
455 while (it != itend) {
462 /** Check whether all indices are of class idx. This function is used
463 * internally to make sure that all constructed indexed objects really
464 * carry indices and not some other classes. */
465 void indexed::assert_all_indices_of_type_idx(void) const
467 GINAC_ASSERT(seq.size() > 0);
468 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
469 while (it != itend) {
470 if (!is_ex_of_type(*it, idx))
471 throw(std::invalid_argument("indices of indexed object must be of type idx"));
480 /** Check whether two sorted index vectors are consistent (i.e. equal). */
481 static bool indices_consistent(const exvector & v1, const exvector & v2)
483 // Number of indices must be the same
484 if (v1.size() != v2.size())
487 // And also the indices themselves
488 exvector::const_iterator ait = v1.begin(), aitend = v1.end(),
489 bit = v2.begin(), bitend = v2.end();
490 while (ait != aitend) {
491 if (!ait->is_equal(*bit))
498 exvector indexed::get_indices(void) const
500 GINAC_ASSERT(seq.size() >= 1);
501 return exvector(seq.begin() + 1, seq.end());
504 exvector indexed::get_dummy_indices(void) const
506 exvector free_indices, dummy_indices;
507 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
508 return dummy_indices;
511 exvector indexed::get_dummy_indices(const indexed & other) const
513 exvector indices = get_free_indices();
514 exvector other_indices = other.get_free_indices();
515 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
516 exvector dummy_indices;
517 find_dummy_indices(indices, dummy_indices);
518 return dummy_indices;
521 exvector indexed::get_free_indices(void) const
523 exvector free_indices, dummy_indices;
524 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
528 exvector add::get_free_indices(void) const
530 exvector free_indices;
531 for (unsigned i=0; i<nops(); i++) {
533 free_indices = op(i).get_free_indices();
535 exvector free_indices_of_term = op(i).get_free_indices();
536 if (!indices_consistent(free_indices, free_indices_of_term))
537 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
543 exvector mul::get_free_indices(void) const
545 // Concatenate free indices of all factors
547 for (unsigned i=0; i<nops(); i++) {
548 exvector free_indices_of_factor = op(i).get_free_indices();
549 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
552 // And remove the dummy indices
553 exvector free_indices, dummy_indices;
554 find_free_and_dummy(un, free_indices, dummy_indices);
558 exvector ncmul::get_free_indices(void) const
560 // Concatenate free indices of all factors
562 for (unsigned i=0; i<nops(); i++) {
563 exvector free_indices_of_factor = op(i).get_free_indices();
564 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
567 // And remove the dummy indices
568 exvector free_indices, dummy_indices;
569 find_free_and_dummy(un, free_indices, dummy_indices);
573 exvector power::get_free_indices(void) const
575 // Return free indices of basis
576 return basis.get_free_indices();
579 /** Simplify product of indexed expressions (commutative, noncommutative and
580 * simple squares), return list of free indices. */
581 ex simplify_indexed_product(const ex & e, exvector & free_indices, const scalar_products & sp)
583 // Remember whether the product was commutative or noncommutative
584 // (because we chop it into factors and need to reassemble later)
585 bool non_commutative = is_ex_exactly_of_type(e, ncmul);
587 // Collect factors in an exvector, store squares twice
589 v.reserve(e.nops() * 2);
591 if (is_ex_exactly_of_type(e, power)) {
592 // We only get called for simple squares, split a^2 -> a*a
593 GINAC_ASSERT(e.op(1).is_equal(_ex2()));
594 v.push_back(e.op(0));
595 v.push_back(e.op(0));
597 for (int i=0; i<e.nops(); i++) {
599 if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2())) {
600 v.push_back(f.op(0));
601 v.push_back(f.op(0));
602 } else if (is_ex_exactly_of_type(f, ncmul)) {
603 // Noncommutative factor found, split it as well
604 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
605 for (int j=0; j<f.nops(); j++)
606 v.push_back(f.op(j));
612 // Perform contractions
613 bool something_changed = false;
614 GINAC_ASSERT(v.size() > 1);
615 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
616 for (it1 = v.begin(); it1 != next_to_last; it1++) {
619 if (!is_ex_of_type(*it1, indexed))
622 // Indexed factor found, get free indices and look for contraction
624 exvector free1, dummy1;
625 find_free_and_dummy(ex_to_indexed(*it1).seq.begin() + 1, ex_to_indexed(*it1).seq.end(), free1, dummy1);
627 exvector::iterator it2;
628 for (it2 = it1 + 1; it2 != itend; it2++) {
630 if (!is_ex_of_type(*it2, indexed))
633 // Find free indices of second factor and merge them with free
634 // indices of first factor
636 find_free_and_dummy(ex_to_indexed(*it2).seq.begin() + 1, ex_to_indexed(*it2).seq.end(), un, dummy1);
637 un.insert(un.end(), free1.begin(), free1.end());
639 // Check whether the two factors share dummy indices
640 exvector free, dummy;
641 find_free_and_dummy(un, free, dummy);
642 if (dummy.size() == 0)
645 // At least one dummy index, is it a defined scalar product?
646 bool contracted = false;
647 if (free.size() == 0) {
648 if (sp.is_defined(*it1, *it2)) {
649 *it1 = sp.evaluate(*it1, *it2);
651 goto contraction_done;
655 // Contraction of symmetric with antisymmetric object is zero
656 if ((ex_to_indexed(*it1).symmetry == indexed::symmetric &&
657 ex_to_indexed(*it2).symmetry == indexed::antisymmetric
658 || ex_to_indexed(*it1).symmetry == indexed::antisymmetric &&
659 ex_to_indexed(*it2).symmetry == indexed::symmetric)
660 && dummy.size() > 1) {
661 free_indices.clear();
665 // Try to contract the first one with the second one
666 contracted = it1->op(0).bp->contract_with(it1, it2, v);
669 // That didn't work; maybe the second object knows how to
670 // contract itself with the first one
671 contracted = it2->op(0).bp->contract_with(it2, it1, v);
675 if (is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
676 || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)) {
678 // One of the factors became a sum or product:
679 // re-expand expression and run again
680 ex r = non_commutative ? ex(ncmul(v)) : ex(mul(v));
681 return simplify_indexed(r, free_indices, sp);
684 // Both objects may have new indices now or they might
685 // even not be indexed objects any more, so we have to
687 something_changed = true;
693 // Find free indices (concatenate them all and call find_free_and_dummy())
694 exvector un, dummy_indices;
695 it1 = v.begin(); itend = v.end();
696 while (it1 != itend) {
697 exvector free_indices_of_factor = it1->get_free_indices();
698 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
701 find_free_and_dummy(un, free_indices, dummy_indices);
704 if (something_changed)
705 r = non_commutative ? ex(ncmul(v)) : ex(mul(v));
709 // Product of indexed object with a scalar?
710 if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
711 && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
712 return r.op(0).op(0).bp->scalar_mul_indexed(r.op(0), ex_to_numeric(r.op(1)));
717 /** Simplify indexed expression, return list of free indices. */
718 ex simplify_indexed(const ex & e, exvector & free_indices, const scalar_products & sp)
720 // Expand the expression
721 ex e_expanded = e.expand();
723 // Simplification of single indexed object: just find the free indices
724 if (is_ex_of_type(e_expanded, indexed)) {
725 const indexed &i = ex_to_indexed(e_expanded);
726 exvector dummy_indices;
727 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, dummy_indices);
731 // Simplification of sum = sum of simplifications, check consistency of
732 // free indices in each term
733 if (is_ex_exactly_of_type(e_expanded, add)) {
736 free_indices.clear();
738 for (unsigned i=0; i<e_expanded.nops(); i++) {
739 exvector free_indices_of_term;
740 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, sp);
741 if (!term.is_zero()) {
743 free_indices = free_indices_of_term;
747 if (!indices_consistent(free_indices, free_indices_of_term))
748 throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
749 if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
750 sum = sum.op(0).bp->add_indexed(sum, term);
760 // Simplification of products
761 if (is_ex_exactly_of_type(e_expanded, mul)
762 || is_ex_exactly_of_type(e_expanded, ncmul)
763 || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2())))
764 return simplify_indexed_product(e_expanded, free_indices, sp);
766 // Cannot do anything
767 free_indices.clear();
771 ex simplify_indexed(const ex & e)
773 exvector free_indices;
775 return simplify_indexed(e, free_indices, sp);
778 ex simplify_indexed(const ex & e, const scalar_products & sp)
780 exvector free_indices;
781 return simplify_indexed(e, free_indices, sp);
788 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
790 spm[make_key(v1, v2)] = sp;
793 void scalar_products::clear(void)
798 /** Check whether scalar product pair is defined. */
799 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
801 return spm.find(make_key(v1, v2)) != spm.end();
804 /** Return value of defined scalar product pair. */
805 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
807 return spm.find(make_key(v1, v2))->second;
810 void scalar_products::debugprint(void) const
812 std::cerr << "map size=" << spm.size() << std::endl;
813 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
814 const spmapkey & k = cit->first;
815 std::cerr << "item key=(" << k.first << "," << k.second;
816 std::cerr << "), value=" << cit->second << std::endl;
820 /** Make key from object pair. */
821 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
823 // If indexed, extract base objects
824 ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
825 ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
827 // Enforce canonical order in pair
828 if (s1.compare(s2) > 0)
829 return spmapkey(s2, s1);
831 return spmapkey(s1, s2);