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
39 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
42 // default constructor, destructor, copy constructor assignment operator and helpers
45 indexed::indexed() : symmetry(unknown)
47 debugmsg("indexed default constructor", LOGLEVEL_CONSTRUCT);
48 tinfo_key = TINFO_indexed;
51 void indexed::copy(const indexed & other)
53 inherited::copy(other);
54 symmetry = other.symmetry;
57 DEFAULT_DESTROY(indexed)
63 indexed::indexed(const ex & b) : inherited(b), symmetry(unknown)
65 debugmsg("indexed constructor from ex", LOGLEVEL_CONSTRUCT);
66 tinfo_key = TINFO_indexed;
67 assert_all_indices_of_type_idx();
70 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symmetry(unknown)
72 debugmsg("indexed constructor from ex,ex", LOGLEVEL_CONSTRUCT);
73 tinfo_key = TINFO_indexed;
74 assert_all_indices_of_type_idx();
77 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(unknown)
79 debugmsg("indexed constructor from ex,ex,ex", LOGLEVEL_CONSTRUCT);
80 tinfo_key = TINFO_indexed;
81 assert_all_indices_of_type_idx();
84 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(unknown)
86 debugmsg("indexed constructor from ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
87 tinfo_key = TINFO_indexed;
88 assert_all_indices_of_type_idx();
91 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)
93 debugmsg("indexed constructor from ex,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_indexed;
95 assert_all_indices_of_type_idx();
98 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(symm)
100 debugmsg("indexed constructor from ex,symmetry,ex,ex", LOGLEVEL_CONSTRUCT);
101 tinfo_key = TINFO_indexed;
102 assert_all_indices_of_type_idx();
105 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(symm)
107 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex", LOGLEVEL_CONSTRUCT);
108 tinfo_key = TINFO_indexed;
109 assert_all_indices_of_type_idx();
112 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)
114 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
115 tinfo_key = TINFO_indexed;
116 assert_all_indices_of_type_idx();
119 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symmetry(unknown)
121 debugmsg("indexed constructor from ex,exvector", LOGLEVEL_CONSTRUCT);
122 seq.insert(seq.end(), v.begin(), v.end());
123 tinfo_key = TINFO_indexed;
124 assert_all_indices_of_type_idx();
127 indexed::indexed(const ex & b, symmetry_type symm, const exvector & v) : inherited(b), symmetry(symm)
129 debugmsg("indexed constructor from ex,symmetry,exvector", LOGLEVEL_CONSTRUCT);
130 seq.insert(seq.end(), v.begin(), v.end());
131 tinfo_key = TINFO_indexed;
132 assert_all_indices_of_type_idx();
135 indexed::indexed(symmetry_type symm, const exprseq & es) : inherited(es), symmetry(symm)
137 debugmsg("indexed constructor from symmetry,exprseq", LOGLEVEL_CONSTRUCT);
138 tinfo_key = TINFO_indexed;
139 assert_all_indices_of_type_idx();
142 indexed::indexed(symmetry_type symm, const exvector & v, bool discardable) : inherited(v, discardable), symmetry(symm)
144 debugmsg("indexed constructor from symmetry,exvector", LOGLEVEL_CONSTRUCT);
145 tinfo_key = TINFO_indexed;
146 assert_all_indices_of_type_idx();
149 indexed::indexed(symmetry_type symm, exvector * vp) : inherited(vp), symmetry(symm)
151 debugmsg("indexed constructor from symmetry,exvector *", LOGLEVEL_CONSTRUCT);
152 tinfo_key = TINFO_indexed;
153 assert_all_indices_of_type_idx();
160 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
162 debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
164 if (!(n.find_unsigned("symmetry", symm)))
165 throw (std::runtime_error("unknown indexed symmetry type in archive"));
168 void indexed::archive(archive_node &n) const
170 inherited::archive(n);
171 n.add_unsigned("symmetry", symmetry);
174 DEFAULT_UNARCHIVE(indexed)
177 // functions overriding virtual functions from bases classes
180 void indexed::print(const print_context & c, unsigned level) const
182 debugmsg("indexed print", LOGLEVEL_PRINT);
183 GINAC_ASSERT(seq.size() > 0);
185 if (is_of_type(c, print_tree)) {
187 c.s << std::string(level, ' ') << class_name()
188 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
189 << ", " << seq.size()-1 << " indices";
191 case symmetric: c.s << ", symmetric"; break;
192 case antisymmetric: c.s << ", antisymmetric"; break;
196 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
197 seq[0].print(c, level + delta_indent);
198 printindices(c, level + delta_indent);
202 const ex & base = seq[0];
203 bool need_parens = is_ex_exactly_of_type(base, add) || is_ex_exactly_of_type(base, mul)
204 || is_ex_exactly_of_type(base, ncmul) || is_ex_exactly_of_type(base, power);
210 printindices(c, level);
214 bool indexed::info(unsigned inf) const
216 if (inf == info_flags::indexed) return true;
217 if (inf == info_flags::has_indices) return seq.size() > 1;
218 return inherited::info(inf);
221 bool indexed::all_index_values_are(unsigned inf) const
223 // No indices? Then no property can be fulfilled
228 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
229 while (it != itend) {
230 GINAC_ASSERT(is_ex_of_type(*it, idx));
231 if (!ex_to_idx(*it).get_value().info(inf))
238 int indexed::compare_same_type(const basic & other) const
240 GINAC_ASSERT(is_of_type(other, indexed));
241 return inherited::compare_same_type(other);
244 // The main difference between sort_index_vector() and canonicalize_indices()
245 // is that the latter takes the symmetry of the object into account. Once we
246 // implement mixed symmetries, canonicalize_indices() will only be able to
247 // reorder index pairs with known symmetry properties, while sort_index_vector()
248 // always sorts the whole vector.
250 /** Bring a vector of indices into a canonic order (don't care about the
251 * symmetry of the objects carrying the indices). Dummy indices will lie
252 * next to each other after the sorting.
254 * @param v Index vector to be sorted */
255 static void sort_index_vector(exvector &v)
257 // Nothing to sort if less than 2 elements
261 // Simple bubble sort algorithm should be sufficient for the small
262 // number of indices expected
263 exvector::iterator it1 = v.begin(), itend = v.end(), next_to_last_idx = itend - 1;
264 while (it1 != next_to_last_idx) {
265 exvector::iterator it2 = it1 + 1;
266 while (it2 != itend) {
267 if (it1->compare(*it2) > 0)
275 /** Bring a vector of indices into a canonic order. This operation only makes
276 * sense if the object carrying these indices is either symmetric or totally
277 * antisymmetric with respect to the indices.
279 * @param itbegin Start of index vector
280 * @param itend End of index vector
281 * @param antisymm Whether the object is antisymmetric
282 * @return the sign introduced by the reordering of the indices if the object
283 * is antisymmetric (or 0 if two equal indices are encountered). For
284 * symmetric objects, this is always +1. If the index vector was
285 * already in a canonic order this function returns INT_MAX. */
286 static int canonicalize_indices(exvector::iterator itbegin, exvector::iterator itend, bool antisymm)
288 bool something_changed = false;
291 // Simple bubble sort algorithm should be sufficient for the small
292 // number of indices expected
293 exvector::iterator it1 = itbegin, next_to_last_idx = itend - 1;
294 while (it1 != next_to_last_idx) {
295 exvector::iterator it2 = it1 + 1;
296 while (it2 != itend) {
297 int cmpval = it1->compare(*it2);
300 something_changed = true;
303 } else if (cmpval == 0 && antisymm) {
304 something_changed = true;
312 return something_changed ? sig : INT_MAX;
315 ex indexed::eval(int level) const
317 // First evaluate children, then we will end up here again
319 return indexed(symmetry, evalchildren(level));
321 const ex &base = seq[0];
323 // If the base object is 0, the whole object is 0
327 // If the base object is a product, pull out the numeric factor
328 if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
330 ex f = ex_to_numeric(base.op(base.nops() - 1));
332 return f * thisexprseq(v);
335 // Canonicalize indices according to the symmetry properties
336 if (seq.size() > 2 && (symmetry != unknown && symmetry != mixed)) {
338 int sig = canonicalize_indices(v.begin() + 1, v.end(), symmetry == antisymmetric);
339 if (sig != INT_MAX) {
340 // Something has changed while sorting indices, more evaluations later
343 return ex(sig) * thisexprseq(v);
347 // Let the class of the base object perform additional evaluations
348 return base.bp->eval_indexed(*this);
351 int indexed::degree(const ex & s) const
353 return is_equal(*s.bp) ? 1 : 0;
356 int indexed::ldegree(const ex & s) const
358 return is_equal(*s.bp) ? 1 : 0;
361 ex indexed::coeff(const ex & s, int n) const
364 return n==1 ? _ex1() : _ex0();
366 return n==0 ? ex(*this) : _ex0();
369 ex indexed::thisexprseq(const exvector & v) const
371 return indexed(symmetry, v);
374 ex indexed::thisexprseq(exvector * vp) const
376 return indexed(symmetry, vp);
379 ex indexed::expand(unsigned options) const
381 GINAC_ASSERT(seq.size() > 0);
383 if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
385 // expand_indexed expands (a+b).i -> a.i + b.i
386 const ex & base = seq[0];
388 for (unsigned i=0; i<base.nops(); i++) {
391 sum += thisexprseq(s).expand();
396 return inherited::expand(options);
400 // virtual functions which can be overridden by derived classes
406 // non-virtual functions in this class
409 void indexed::printindices(const print_context & c, unsigned level) const
411 if (seq.size() > 1) {
412 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
413 while (it != itend) {
420 /** Check whether all indices are of class idx. This function is used
421 * internally to make sure that all constructed indexed objects really
422 * carry indices and not some other classes. */
423 void indexed::assert_all_indices_of_type_idx(void) const
425 GINAC_ASSERT(seq.size() > 0);
426 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
427 while (it != itend) {
428 if (!is_ex_of_type(*it, idx))
429 throw(std::invalid_argument("indices of indexed object must be of type idx"));
438 /** Check whether two sorted index vectors are consistent (i.e. equal). */
439 static bool indices_consistent(const exvector & v1, const exvector & v2)
441 // Number of indices must be the same
442 if (v1.size() != v2.size())
445 // And also the indices themselves
446 exvector::const_iterator ait = v1.begin(), aitend = v1.end(),
447 bit = v2.begin(), bitend = v2.end();
448 while (ait != aitend) {
449 if (!ait->is_equal(*bit))
456 exvector indexed::get_indices(void) const
458 GINAC_ASSERT(seq.size() >= 1);
459 return exvector(seq.begin() + 1, seq.end());
462 exvector indexed::get_dummy_indices(void) const
464 exvector free_indices, dummy_indices;
465 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
466 return dummy_indices;
469 exvector indexed::get_dummy_indices(const indexed & other) const
471 exvector indices = get_free_indices();
472 exvector other_indices = other.get_free_indices();
473 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
474 exvector dummy_indices;
475 find_dummy_indices(indices, dummy_indices);
476 return dummy_indices;
479 exvector indexed::get_free_indices(void) const
481 exvector free_indices, dummy_indices;
482 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
486 exvector add::get_free_indices(void) const
488 exvector free_indices;
489 for (unsigned i=0; i<nops(); i++) {
491 free_indices = op(i).get_free_indices();
493 exvector free_indices_of_term = op(i).get_free_indices();
494 if (!indices_consistent(free_indices, free_indices_of_term))
495 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
501 exvector mul::get_free_indices(void) const
503 // Concatenate free indices of all factors
505 for (unsigned i=0; i<nops(); i++) {
506 exvector free_indices_of_factor = op(i).get_free_indices();
507 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
510 // And remove the dummy indices
511 exvector free_indices, dummy_indices;
512 find_free_and_dummy(un, free_indices, dummy_indices);
516 exvector ncmul::get_free_indices(void) const
518 // Concatenate free indices of all factors
520 for (unsigned i=0; i<nops(); i++) {
521 exvector free_indices_of_factor = op(i).get_free_indices();
522 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
525 // And remove the dummy indices
526 exvector free_indices, dummy_indices;
527 find_free_and_dummy(un, free_indices, dummy_indices);
531 exvector power::get_free_indices(void) const
533 // Return free indices of basis
534 return basis.get_free_indices();
537 /** Simplify product of indexed expressions (commutative, noncommutative and
538 * simple squares), return list of free indices. */
539 ex simplify_indexed_product(const ex & e, exvector & free_indices, const scalar_products & sp)
541 // Remember whether the product was commutative or noncommutative
542 // (because we chop it into factors and need to reassemble later)
543 bool non_commutative = is_ex_exactly_of_type(e, ncmul);
545 // Collect factors in an exvector, store squares twice
547 v.reserve(e.nops() * 2);
549 if (is_ex_exactly_of_type(e, power)) {
550 // We only get called for simple squares, split a^2 -> a*a
551 GINAC_ASSERT(e.op(1).is_equal(_ex2()));
552 v.push_back(e.op(0));
553 v.push_back(e.op(0));
555 for (int i=0; i<e.nops(); i++) {
557 if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2())) {
558 v.push_back(f.op(0));
559 v.push_back(f.op(0));
560 } else if (is_ex_exactly_of_type(f, ncmul)) {
561 // Noncommutative factor found, split it as well
562 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
563 for (int j=0; j<f.nops(); j++)
564 v.push_back(f.op(j));
570 // Perform contractions
571 bool something_changed = false;
572 GINAC_ASSERT(v.size() > 1);
573 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
574 for (it1 = v.begin(); it1 != next_to_last; it1++) {
577 if (!is_ex_of_type(*it1, indexed))
580 // Indexed factor found, get free indices and look for contraction
582 exvector free1, dummy1;
583 find_free_and_dummy(ex_to_indexed(*it1).seq.begin() + 1, ex_to_indexed(*it1).seq.end(), free1, dummy1);
585 exvector::iterator it2;
586 for (it2 = it1 + 1; it2 != itend; it2++) {
588 if (!is_ex_of_type(*it2, indexed))
591 // Find free indices of second factor and merge them with free
592 // indices of first factor
594 find_free_and_dummy(ex_to_indexed(*it2).seq.begin() + 1, ex_to_indexed(*it2).seq.end(), un, dummy1);
595 un.insert(un.end(), free1.begin(), free1.end());
597 // Check whether the two factors share dummy indices
598 exvector free, dummy;
599 find_free_and_dummy(un, free, dummy);
600 if (dummy.size() == 0)
603 // At least one dummy index, is it a defined scalar product?
604 bool contracted = false;
605 if (free.size() == 0) {
606 if (sp.is_defined(*it1, *it2)) {
607 *it1 = sp.evaluate(*it1, *it2);
609 goto contraction_done;
613 // Contraction of symmetric with antisymmetric object is zero
614 if ((ex_to_indexed(*it1).symmetry == indexed::symmetric &&
615 ex_to_indexed(*it2).symmetry == indexed::antisymmetric
616 || ex_to_indexed(*it1).symmetry == indexed::antisymmetric &&
617 ex_to_indexed(*it2).symmetry == indexed::symmetric)
618 && dummy.size() > 1) {
619 free_indices.clear();
623 // Try to contract the first one with the second one
624 contracted = it1->op(0).bp->contract_with(it1, it2, v);
627 // That didn't work; maybe the second object knows how to
628 // contract itself with the first one
629 contracted = it2->op(0).bp->contract_with(it2, it1, v);
633 if (is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
634 || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)) {
636 // One of the factors became a sum or product:
637 // re-expand expression and run again
638 ex r = non_commutative ? ex(ncmul(v)) : ex(mul(v));
639 return simplify_indexed(r, free_indices, sp);
642 // Both objects may have new indices now or they might
643 // even not be indexed objects any more, so we have to
645 something_changed = true;
651 // Find free indices (concatenate them all and call find_free_and_dummy())
652 exvector un, dummy_indices;
653 it1 = v.begin(); itend = v.end();
654 while (it1 != itend) {
655 exvector free_indices_of_factor = it1->get_free_indices();
656 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
659 find_free_and_dummy(un, free_indices, dummy_indices);
662 if (something_changed)
663 r = non_commutative ? ex(ncmul(v)) : ex(mul(v));
667 // Product of indexed object with a scalar?
668 if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
669 && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
670 return r.op(0).op(0).bp->scalar_mul_indexed(r.op(0), ex_to_numeric(r.op(1)));
675 /** Simplify indexed expression, return list of free indices. */
676 ex simplify_indexed(const ex & e, exvector & free_indices, const scalar_products & sp)
678 // Expand the expression
679 ex e_expanded = e.expand();
681 // Simplification of single indexed object: just find the free indices
682 if (is_ex_of_type(e_expanded, indexed)) {
683 const indexed &i = ex_to_indexed(e_expanded);
684 exvector dummy_indices;
685 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, dummy_indices);
689 // Simplification of sum = sum of simplifications, check consistency of
690 // free indices in each term
691 if (is_ex_exactly_of_type(e_expanded, add)) {
694 free_indices.clear();
696 for (unsigned i=0; i<e_expanded.nops(); i++) {
697 exvector free_indices_of_term;
698 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, sp);
699 if (!term.is_zero()) {
701 free_indices = free_indices_of_term;
705 if (!indices_consistent(free_indices, free_indices_of_term))
706 throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
707 if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
708 sum = sum.op(0).bp->add_indexed(sum, term);
718 // Simplification of products
719 if (is_ex_exactly_of_type(e_expanded, mul)
720 || is_ex_exactly_of_type(e_expanded, ncmul)
721 || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2())))
722 return simplify_indexed_product(e_expanded, free_indices, sp);
724 // Cannot do anything
725 free_indices.clear();
729 ex simplify_indexed(const ex & e)
731 exvector free_indices;
733 return simplify_indexed(e, free_indices, sp);
736 ex simplify_indexed(const ex & e, const scalar_products & sp)
738 exvector free_indices;
739 return simplify_indexed(e, free_indices, sp);
746 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
748 spm[make_key(v1, v2)] = sp;
751 void scalar_products::clear(void)
756 /** Check whether scalar product pair is defined. */
757 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
759 return spm.find(make_key(v1, v2)) != spm.end();
762 /** Return value of defined scalar product pair. */
763 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
765 return spm.find(make_key(v1, v2))->second;
768 void scalar_products::debugprint(void) const
770 std::cerr << "map size=" << spm.size() << std::endl;
771 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
772 const spmapkey & k = cit->first;
773 std::cerr << "item key=(" << k.first << "," << k.second;
774 std::cerr << "), value=" << cit->second << std::endl;
778 /** Make key from object pair. */
779 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
781 // If indexed, extract base objects
782 ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
783 ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
785 // Enforce canonical order in pair
786 if (s1.compare(s2) > 0)
787 return spmapkey(s2, s1);
789 return spmapkey(s1, s2);