3 * Implementation of GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2003 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
33 #include "relational.h"
42 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
45 // default ctor, dtor, copy ctor, assignment operator and helpers
48 indexed::indexed() : symtree(sy_none())
50 tinfo_key = TINFO_indexed;
53 void indexed::copy(const indexed & other)
55 inherited::copy(other);
56 symtree = other.symtree;
59 DEFAULT_DESTROY(indexed)
65 indexed::indexed(const ex & b) : inherited(b), symtree(sy_none())
67 tinfo_key = TINFO_indexed;
71 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(sy_none())
73 tinfo_key = TINFO_indexed;
77 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(sy_none())
79 tinfo_key = TINFO_indexed;
83 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(sy_none())
85 tinfo_key = TINFO_indexed;
89 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symtree(sy_none())
91 tinfo_key = TINFO_indexed;
95 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(symm)
97 tinfo_key = TINFO_indexed;
101 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(symm)
103 tinfo_key = TINFO_indexed;
107 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symtree(symm)
109 tinfo_key = TINFO_indexed;
113 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(sy_none())
115 seq.insert(seq.end(), v.begin(), v.end());
116 tinfo_key = TINFO_indexed;
120 indexed::indexed(const ex & b, const symmetry & symm, const exvector & v) : inherited(b), symtree(symm)
122 seq.insert(seq.end(), v.begin(), v.end());
123 tinfo_key = TINFO_indexed;
127 indexed::indexed(const symmetry & symm, const exprseq & es) : inherited(es), symtree(symm)
129 tinfo_key = TINFO_indexed;
132 indexed::indexed(const symmetry & symm, const exvector & v, bool discardable) : inherited(v, discardable), symtree(symm)
134 tinfo_key = TINFO_indexed;
137 indexed::indexed(const symmetry & symm, exvector * vp) : inherited(vp), symtree(symm)
139 tinfo_key = TINFO_indexed;
146 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
148 if (!n.find_ex("symmetry", symtree, sym_lst)) {
149 // GiNaC versions <= 0.9.0 had an unsigned "symmetry" property
151 n.find_unsigned("symmetry", symm);
163 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
167 void indexed::archive(archive_node &n) const
169 inherited::archive(n);
170 n.add_ex("symmetry", symtree);
173 DEFAULT_UNARCHIVE(indexed)
176 // functions overriding virtual functions from base classes
179 void indexed::print(const print_context & c, unsigned level) const
181 GINAC_ASSERT(seq.size() > 0);
183 if (is_a<print_tree>(c)) {
185 c.s << std::string(level, ' ') << class_name()
186 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
187 << ", " << seq.size()-1 << " indices"
188 << ", symmetry=" << symtree << std::endl;
189 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
190 seq[0].print(c, level + delta_indent);
191 printindices(c, level + delta_indent);
195 bool is_tex = is_a<print_latex>(c);
196 const ex & base = seq[0];
198 if (precedence() <= level)
199 c.s << (is_tex ? "{(" : "(");
202 base.print(c, precedence());
205 printindices(c, level);
206 if (precedence() <= level)
207 c.s << (is_tex ? ")}" : ")");
211 bool indexed::info(unsigned inf) const
213 if (inf == info_flags::indexed) return true;
214 if (inf == info_flags::has_indices) return seq.size() > 1;
215 return inherited::info(inf);
218 struct idx_is_not : public std::binary_function<ex, unsigned, bool> {
219 bool operator() (const ex & e, unsigned inf) const {
220 return !(ex_to<idx>(e).get_value().info(inf));
224 bool indexed::all_index_values_are(unsigned inf) const
226 // No indices? Then no property can be fulfilled
231 return find_if(seq.begin() + 1, seq.end(), bind2nd(idx_is_not(), inf)) == seq.end();
234 int indexed::compare_same_type(const basic & other) const
236 GINAC_ASSERT(is_a<indexed>(other));
237 return inherited::compare_same_type(other);
240 ex indexed::eval(int level) const
242 // First evaluate children, then we will end up here again
244 return indexed(ex_to<symmetry>(symtree), evalchildren(level));
246 const ex &base = seq[0];
248 // If the base object is 0, the whole object is 0
252 // If the base object is a product, pull out the numeric factor
253 if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
255 ex f = ex_to<numeric>(base.op(base.nops() - 1));
257 return f * thisexprseq(v);
260 // Canonicalize indices according to the symmetry properties
261 if (seq.size() > 2) {
263 GINAC_ASSERT(is_exactly_a<symmetry>(symtree));
264 int sig = canonicalize(v.begin() + 1, ex_to<symmetry>(symtree));
265 if (sig != INT_MAX) {
266 // Something has changed while sorting indices, more evaluations later
269 return ex(sig) * thisexprseq(v);
273 // Let the class of the base object perform additional evaluations
274 return ex_to<basic>(base).eval_indexed(*this);
277 ex indexed::thisexprseq(const exvector & v) const
279 return indexed(ex_to<symmetry>(symtree), v);
282 ex indexed::thisexprseq(exvector * vp) const
284 return indexed(ex_to<symmetry>(symtree), vp);
287 ex indexed::expand(unsigned options) const
289 GINAC_ASSERT(seq.size() > 0);
291 if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
293 // expand_indexed expands (a+b).i -> a.i + b.i
294 const ex & base = seq[0];
296 for (unsigned i=0; i<base.nops(); i++) {
299 sum += thisexprseq(s).expand();
304 return inherited::expand(options);
308 // virtual functions which can be overridden by derived classes
314 // non-virtual functions in this class
317 void indexed::printindices(const print_context & c, unsigned level) const
319 if (seq.size() > 1) {
321 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
323 if (is_a<print_latex>(c)) {
325 // TeX output: group by variance
327 bool covariant = true;
329 while (it != itend) {
330 bool cur_covariant = (is_ex_of_type(*it, varidx) ? ex_to<varidx>(*it).is_covariant() : true);
331 if (first || cur_covariant != covariant) { // Variance changed
332 // The empty {} prevents indices from ending up on top of each other
335 covariant = cur_covariant;
351 while (it != itend) {
359 /** Check whether all indices are of class idx and validate the symmetry
360 * tree. This function is used internally to make sure that all constructed
361 * indexed objects really carry indices and not some other classes. */
362 void indexed::validate(void) const
364 GINAC_ASSERT(seq.size() > 0);
365 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
366 while (it != itend) {
367 if (!is_ex_of_type(*it, idx))
368 throw(std::invalid_argument("indices of indexed object must be of type idx"));
372 if (!symtree.is_zero()) {
373 if (!is_ex_exactly_of_type(symtree, symmetry))
374 throw(std::invalid_argument("symmetry of indexed object must be of type symmetry"));
375 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
379 /** Implementation of ex::diff() for an indexed object always returns 0.
382 ex indexed::derivative(const symbol & s) const
391 struct idx_is_equal_ignore_dim : public std::binary_function<ex, ex, bool> {
392 bool operator() (const ex &lh, const ex &rh) const
398 // Replacing the dimension might cause an error (e.g. with
399 // index classes that only work in a fixed number of dimensions)
400 return lh.is_equal(ex_to<idx>(rh).replace_dim(ex_to<idx>(lh).get_dim()));
407 /** Check whether two sorted index vectors are consistent (i.e. equal). */
408 static bool indices_consistent(const exvector & v1, const exvector & v2)
410 // Number of indices must be the same
411 if (v1.size() != v2.size())
414 return equal(v1.begin(), v1.end(), v2.begin(), idx_is_equal_ignore_dim());
417 exvector indexed::get_indices(void) const
419 GINAC_ASSERT(seq.size() >= 1);
420 return exvector(seq.begin() + 1, seq.end());
423 exvector indexed::get_dummy_indices(void) const
425 exvector free_indices, dummy_indices;
426 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
427 return dummy_indices;
430 exvector indexed::get_dummy_indices(const indexed & other) const
432 exvector indices = get_free_indices();
433 exvector other_indices = other.get_free_indices();
434 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
435 exvector dummy_indices;
436 find_dummy_indices(indices, dummy_indices);
437 return dummy_indices;
440 bool indexed::has_dummy_index_for(const ex & i) const
442 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
443 while (it != itend) {
444 if (is_dummy_pair(*it, i))
451 exvector indexed::get_free_indices(void) const
453 exvector free_indices, dummy_indices;
454 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
458 exvector add::get_free_indices(void) const
460 exvector free_indices;
461 for (unsigned i=0; i<nops(); i++) {
463 free_indices = op(i).get_free_indices();
465 exvector free_indices_of_term = op(i).get_free_indices();
466 if (!indices_consistent(free_indices, free_indices_of_term))
467 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
473 exvector mul::get_free_indices(void) const
475 // Concatenate free indices of all factors
477 for (unsigned i=0; i<nops(); i++) {
478 exvector free_indices_of_factor = op(i).get_free_indices();
479 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
482 // And remove the dummy indices
483 exvector free_indices, dummy_indices;
484 find_free_and_dummy(un, free_indices, dummy_indices);
488 exvector ncmul::get_free_indices(void) const
490 // Concatenate free indices of all factors
492 for (unsigned i=0; i<nops(); i++) {
493 exvector free_indices_of_factor = op(i).get_free_indices();
494 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
497 // And remove the dummy indices
498 exvector free_indices, dummy_indices;
499 find_free_and_dummy(un, free_indices, dummy_indices);
503 exvector power::get_free_indices(void) const
505 // Return free indices of basis
506 return basis.get_free_indices();
509 /** Rename dummy indices in an expression.
511 * @param e Expression to work on
512 * @param local_dummy_indices The set of dummy indices that appear in the
514 * @param global_dummy_indices The set of dummy indices that have appeared
515 * before and which we would like to use in "e", too. This gets updated
517 static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
519 unsigned global_size = global_dummy_indices.size(),
520 local_size = local_dummy_indices.size();
522 // Any local dummy indices at all?
526 if (global_size < local_size) {
528 // More local indices than we encountered before, add the new ones
530 int old_global_size = global_size;
531 int remaining = local_size - global_size;
532 exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
533 while (it != itend && remaining > 0) {
534 if (find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(op0_is_equal(), *it)) == global_dummy_indices.end()) {
535 global_dummy_indices.push_back(*it);
542 // If this is the first set of local indices, do nothing
543 if (old_global_size == 0)
546 GINAC_ASSERT(local_size <= global_size);
548 // Construct lists of index symbols
549 exlist local_syms, global_syms;
550 for (unsigned i=0; i<local_size; i++)
551 local_syms.push_back(local_dummy_indices[i].op(0));
552 shaker_sort(local_syms.begin(), local_syms.end(), ex_is_less(), ex_swap());
553 for (unsigned i=0; i<local_size; i++) // don't use more global symbols than necessary
554 global_syms.push_back(global_dummy_indices[i].op(0));
555 shaker_sort(global_syms.begin(), global_syms.end(), ex_is_less(), ex_swap());
557 // Remove common indices
558 exlist local_uniq, global_uniq;
559 set_difference(local_syms.begin(), local_syms.end(), global_syms.begin(), global_syms.end(), std::back_insert_iterator<exlist>(local_uniq), ex_is_less());
560 set_difference(global_syms.begin(), global_syms.end(), local_syms.begin(), local_syms.end(), std::back_insert_iterator<exlist>(global_uniq), ex_is_less());
562 // Replace remaining non-common local index symbols by global ones
563 if (local_uniq.empty())
566 while (global_uniq.size() > local_uniq.size())
567 global_uniq.pop_back();
568 return e.subs(lst(local_uniq), lst(global_uniq));
572 /** Given a set of indices, extract those of class varidx. */
573 static void find_variant_indices(const exvector & v, exvector & variant_indices)
575 exvector::const_iterator it1, itend;
576 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
577 if (is_exactly_a<varidx>(*it1))
578 variant_indices.push_back(*it1);
582 /** Raise/lower dummy indices in a single indexed objects to canonicalize their
585 * @param e Object to work on
586 * @param variant_dummy_indices The set of indices that might need repositioning (will be changed by this function)
587 * @param moved_indices The set of indices that have been repositioned (will be changed by this function)
588 * @return true if 'e' was changed */
589 bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices)
591 bool something_changed = false;
593 // If a dummy index is encountered for the first time in the
594 // product, pull it up, otherwise, pull it down
595 exvector::const_iterator it2, it2start, it2end;
596 for (it2start = ex_to<indexed>(e).seq.begin(), it2end = ex_to<indexed>(e).seq.end(), it2 = it2start + 1; it2 != it2end; ++it2) {
597 if (!is_exactly_a<varidx>(*it2))
600 exvector::iterator vit, vitend;
601 for (vit = variant_dummy_indices.begin(), vitend = variant_dummy_indices.end(); vit != vitend; ++vit) {
602 if (it2->op(0).is_equal(vit->op(0))) {
603 if (ex_to<varidx>(*it2).is_covariant()) {
605 *it2 == ex_to<varidx>(*it2).toggle_variance(),
606 ex_to<varidx>(*it2).toggle_variance() == *it2
608 something_changed = true;
609 it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
610 it2start = ex_to<indexed>(e).seq.begin();
611 it2end = ex_to<indexed>(e).seq.end();
613 moved_indices.push_back(*vit);
614 variant_dummy_indices.erase(vit);
619 for (vit = moved_indices.begin(), vitend = moved_indices.end(); vit != vitend; ++vit) {
620 if (it2->op(0).is_equal(vit->op(0))) {
621 if (ex_to<varidx>(*it2).is_contravariant()) {
622 e = e.subs(*it2 == ex_to<varidx>(*it2).toggle_variance());
623 something_changed = true;
624 it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
625 it2start = ex_to<indexed>(e).seq.begin();
626 it2end = ex_to<indexed>(e).seq.end();
635 return something_changed;
638 /* Ordering that only compares the base expressions of indexed objects. */
639 struct ex_base_is_less : public std::binary_function<ex, ex, bool> {
640 bool operator() (const ex &lh, const ex &rh) const
642 return (is_a<indexed>(lh) ? lh.op(0) : lh).compare(is_a<indexed>(rh) ? rh.op(0) : rh) < 0;
646 /** Simplify product of indexed expressions (commutative, noncommutative and
647 * simple squares), return list of free indices. */
648 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
650 // Remember whether the product was commutative or noncommutative
651 // (because we chop it into factors and need to reassemble later)
652 bool non_commutative = is_ex_exactly_of_type(e, ncmul);
654 // Collect factors in an exvector, store squares twice
656 v.reserve(e.nops() * 2);
658 if (is_ex_exactly_of_type(e, power)) {
659 // We only get called for simple squares, split a^2 -> a*a
660 GINAC_ASSERT(e.op(1).is_equal(_ex2));
661 v.push_back(e.op(0));
662 v.push_back(e.op(0));
664 for (unsigned i=0; i<e.nops(); i++) {
666 if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2)) {
667 v.push_back(f.op(0));
668 v.push_back(f.op(0));
669 } else if (is_ex_exactly_of_type(f, ncmul)) {
670 // Noncommutative factor found, split it as well
671 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
672 for (unsigned j=0; j<f.nops(); j++)
673 v.push_back(f.op(j));
679 // Perform contractions
680 bool something_changed = false;
681 GINAC_ASSERT(v.size() > 1);
682 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
683 for (it1 = v.begin(); it1 != next_to_last; it1++) {
686 if (!is_ex_of_type(*it1, indexed))
689 bool first_noncommutative = (it1->return_type() != return_types::commutative);
691 // Indexed factor found, get free indices and look for contraction
693 exvector free1, dummy1;
694 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
696 exvector::iterator it2;
697 for (it2 = it1 + 1; it2 != itend; it2++) {
699 if (!is_ex_of_type(*it2, indexed))
702 bool second_noncommutative = (it2->return_type() != return_types::commutative);
704 // Find free indices of second factor and merge them with free
705 // indices of first factor
707 find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
708 un.insert(un.end(), free1.begin(), free1.end());
710 // Check whether the two factors share dummy indices
711 exvector free, dummy;
712 find_free_and_dummy(un, free, dummy);
713 unsigned num_dummies = dummy.size();
714 if (num_dummies == 0)
717 // At least one dummy index, is it a defined scalar product?
718 bool contracted = false;
720 if (sp.is_defined(*it1, *it2)) {
721 *it1 = sp.evaluate(*it1, *it2);
723 goto contraction_done;
727 // Try to contract the first one with the second one
728 contracted = ex_to<basic>(it1->op(0)).contract_with(it1, it2, v);
731 // That didn't work; maybe the second object knows how to
732 // contract itself with the first one
733 contracted = ex_to<basic>(it2->op(0)).contract_with(it2, it1, v);
737 if (first_noncommutative || second_noncommutative
738 || is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
739 || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)
740 || is_ex_exactly_of_type(*it1, ncmul) || is_ex_exactly_of_type(*it2, ncmul)) {
742 // One of the factors became a sum or product:
743 // re-expand expression and run again
744 // Non-commutative products are always re-expanded to give
745 // simplify_ncmul() the chance to re-order and canonicalize
747 ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
748 return simplify_indexed(r, free_indices, dummy_indices, sp);
751 // Both objects may have new indices now or they might
752 // even not be indexed objects any more, so we have to
754 something_changed = true;
760 // Find free indices (concatenate them all and call find_free_and_dummy())
761 // and all dummy indices that appear
762 exvector un, individual_dummy_indices;
763 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
764 exvector free_indices_of_factor;
765 if (is_ex_of_type(*it1, indexed)) {
766 exvector dummy_indices_of_factor;
767 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free_indices_of_factor, dummy_indices_of_factor);
768 individual_dummy_indices.insert(individual_dummy_indices.end(), dummy_indices_of_factor.begin(), dummy_indices_of_factor.end());
770 free_indices_of_factor = it1->get_free_indices();
771 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
773 exvector local_dummy_indices;
774 find_free_and_dummy(un, free_indices, local_dummy_indices);
775 local_dummy_indices.insert(local_dummy_indices.end(), individual_dummy_indices.begin(), individual_dummy_indices.end());
777 // Filter out the dummy indices with variance
778 exvector variant_dummy_indices;
779 find_variant_indices(local_dummy_indices, variant_dummy_indices);
781 // Any indices with variance present at all?
782 if (!variant_dummy_indices.empty()) {
784 // Yes, bring the product into a canonical order that only depends on
785 // the base expressions of indexed objects
786 if (!non_commutative)
787 std::sort(v.begin(), v.end(), ex_base_is_less());
789 exvector moved_indices;
791 // Iterate over all indexed objects in the product
792 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
793 if (!is_ex_of_type(*it1, indexed))
796 if (reposition_dummy_indices(*it1, variant_dummy_indices, moved_indices))
797 something_changed = true;
802 if (something_changed)
803 r = non_commutative ? ex(ncmul(v, true)) : ex(mul(v));
807 // The result should be symmetric with respect to exchange of dummy
808 // indices, so if the symmetrization vanishes, the whole expression is
809 // zero. This detects things like eps.i.j.k * p.j * p.k = 0.
810 if (local_dummy_indices.size() >= 2) {
812 for (exvector::size_type i=0; i<local_dummy_indices.size(); i++)
813 dummy_syms.append(local_dummy_indices[i].op(0));
814 if (r.symmetrize(dummy_syms).is_zero()) {
815 free_indices.clear();
820 // Dummy index renaming
821 r = rename_dummy_indices(r, dummy_indices, local_dummy_indices);
823 // Product of indexed object with a scalar?
824 if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
825 && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
826 return ex_to<basic>(r.op(0).op(0)).scalar_mul_indexed(r.op(0), ex_to<numeric>(r.op(1)));
831 /** This structure stores the original and symmetrized versions of terms
832 * obtained during the simplification of sums. */
835 terminfo(const ex & orig_, const ex & symm_) : orig(orig_), symm(symm_) {}
837 ex orig; /**< original term */
838 ex symm; /**< symmtrized term */
841 class terminfo_is_less {
843 bool operator() (const terminfo & ti1, const terminfo & ti2) const
845 return (ti1.symm.compare(ti2.symm) < 0);
849 /** This structure stores the individual symmetrized terms obtained during
850 * the simplification of sums. */
853 symminfo() : num(0) {}
855 symminfo(const ex & symmterm_, const ex & orig_, unsigned num_) : orig(orig_), num(num_)
857 if (is_exactly_a<mul>(symmterm_) && is_exactly_a<numeric>(symmterm_.op(symmterm_.nops()-1))) {
858 coeff = symmterm_.op(symmterm_.nops()-1);
859 symmterm = symmterm_ / coeff;
862 symmterm = symmterm_;
866 ex symmterm; /**< symmetrized term */
867 ex coeff; /**< coefficient of symmetrized term */
868 ex orig; /**< original term */
869 unsigned num; /**< how many symmetrized terms resulted from the original term */
872 class symminfo_is_less_by_symmterm {
874 bool operator() (const symminfo & si1, const symminfo & si2) const
876 return (si1.symmterm.compare(si2.symmterm) < 0);
880 class symminfo_is_less_by_orig {
882 bool operator() (const symminfo & si1, const symminfo & si2) const
884 return (si1.orig.compare(si2.orig) < 0);
888 /** Simplify indexed expression, return list of free indices. */
889 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
891 // Expand the expression
892 ex e_expanded = e.expand();
894 // Simplification of single indexed object: just find the free indices
895 // and perform dummy index renaming/repositioning
896 if (is_ex_of_type(e_expanded, indexed)) {
898 // Find the dummy indices
899 const indexed &i = ex_to<indexed>(e_expanded);
900 exvector local_dummy_indices;
901 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
903 // Filter out the dummy indices with variance
904 exvector variant_dummy_indices;
905 find_variant_indices(local_dummy_indices, variant_dummy_indices);
907 // Any indices with variance present at all?
908 if (!variant_dummy_indices.empty()) {
910 // Yes, reposition them
911 exvector moved_indices;
912 reposition_dummy_indices(e_expanded, variant_dummy_indices, moved_indices);
915 // Rename the dummy indices
916 return rename_dummy_indices(e_expanded, dummy_indices, local_dummy_indices);
919 // Simplification of sum = sum of simplifications, check consistency of
920 // free indices in each term
921 if (is_ex_exactly_of_type(e_expanded, add)) {
924 free_indices.clear();
926 for (unsigned i=0; i<e_expanded.nops(); i++) {
927 exvector free_indices_of_term;
928 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
929 if (!term.is_zero()) {
931 free_indices = free_indices_of_term;
935 if (!indices_consistent(free_indices, free_indices_of_term)) {
936 std::ostringstream s;
937 s << "simplify_indexed: inconsistent indices in sum: ";
938 s << exprseq(free_indices) << " vs. " << exprseq(free_indices_of_term);
939 throw (std::runtime_error(s.str()));
941 if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
942 sum = ex_to<basic>(sum.op(0)).add_indexed(sum, term);
949 // If the sum turns out to be zero, we are finished
951 free_indices.clear();
955 // More than one term and more than one dummy index?
956 int num_terms_orig = (is_exactly_a<add>(sum) ? sum.nops() : 1);
957 if (num_terms_orig < 2 || dummy_indices.size() < 2)
960 // Yes, construct list of all dummy index symbols
962 for (exvector::size_type i=0; i<dummy_indices.size(); i++)
963 dummy_syms.append(dummy_indices[i].op(0));
965 // Chop the sum into terms and symmetrize each one over the dummy
967 std::vector<terminfo> terms;
968 for (unsigned i=0; i<sum.nops(); i++) {
969 const ex & term = sum.op(i);
970 ex term_symm = term.symmetrize(dummy_syms);
971 if (term_symm.is_zero())
973 terms.push_back(terminfo(term, term_symm));
976 // Sort by symmetrized terms
977 std::sort(terms.begin(), terms.end(), terminfo_is_less());
979 // Combine equal symmetrized terms
980 std::vector<terminfo> terms_pass2;
981 for (std::vector<terminfo>::const_iterator i=terms.begin(); i!=terms.end(); ) {
983 std::vector<terminfo>::const_iterator j = i + 1;
984 while (j != terms.end() && j->symm == i->symm) {
988 terms_pass2.push_back(terminfo(i->orig * num, i->symm * num));
992 // If there is only one term left, we are finished
993 if (terms_pass2.size() == 1)
994 return terms_pass2[0].orig;
996 // Chop the symmetrized terms into subterms
997 std::vector<symminfo> sy;
998 for (std::vector<terminfo>::const_iterator i=terms_pass2.begin(); i!=terms_pass2.end(); ++i) {
999 if (is_exactly_a<add>(i->symm)) {
1000 unsigned num = i->symm.nops();
1001 for (unsigned j=0; j<num; j++)
1002 sy.push_back(symminfo(i->symm.op(j), i->orig, num));
1004 sy.push_back(symminfo(i->symm, i->orig, 1));
1007 // Sort by symmetrized subterms
1008 std::sort(sy.begin(), sy.end(), symminfo_is_less_by_symmterm());
1010 // Combine equal symmetrized subterms
1011 std::vector<symminfo> sy_pass2;
1013 for (std::vector<symminfo>::const_iterator i=sy.begin(); i!=sy.end(); ) {
1015 // Combine equal terms
1016 std::vector<symminfo>::const_iterator j = i + 1;
1017 if (j != sy.end() && j->symmterm == i->symmterm) {
1019 // More than one term, collect the coefficients
1020 ex coeff = i->coeff;
1021 while (j != sy.end() && j->symmterm == i->symmterm) {
1026 // Add combined term to result
1027 if (!coeff.is_zero())
1028 result.push_back(coeff * i->symmterm);
1032 // Single term, store for second pass
1033 sy_pass2.push_back(*i);
1039 // Were there any remaining terms that didn't get combined?
1040 if (sy_pass2.size() > 0) {
1042 // Yes, sort by their original terms
1043 std::sort(sy_pass2.begin(), sy_pass2.end(), symminfo_is_less_by_orig());
1045 for (std::vector<symminfo>::const_iterator i=sy_pass2.begin(); i!=sy_pass2.end(); ) {
1047 // How many symmetrized terms of this original term are left?
1049 std::vector<symminfo>::const_iterator j = i + 1;
1050 while (j != sy_pass2.end() && j->orig == i->orig) {
1055 if (num == i->num) {
1057 // All terms left, then add the original term to the result
1058 result.push_back(i->orig);
1062 // Some terms were combined with others, add up the remaining symmetrized terms
1063 std::vector<symminfo>::const_iterator k;
1064 for (k=i; k!=j; k++)
1065 result.push_back(k->coeff * k->symmterm);
1072 // Add all resulting terms
1073 ex sum_symm = (new add(result))->setflag(status_flags::dynallocated);
1074 if (sum_symm.is_zero())
1075 free_indices.clear();
1079 // Simplification of products
1080 if (is_ex_exactly_of_type(e_expanded, mul)
1081 || is_ex_exactly_of_type(e_expanded, ncmul)
1082 || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2)))
1083 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
1085 // Cannot do anything
1086 free_indices.clear();
1090 /** Simplify/canonicalize expression containing indexed objects. This
1091 * performs contraction of dummy indices where possible and checks whether
1092 * the free indices in sums are consistent.
1094 * @return simplified expression */
1095 ex ex::simplify_indexed(void) const
1097 exvector free_indices, dummy_indices;
1099 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1102 /** Simplify/canonicalize expression containing indexed objects. This
1103 * performs contraction of dummy indices where possible, checks whether
1104 * the free indices in sums are consistent, and automatically replaces
1105 * scalar products by known values if desired.
1107 * @param sp Scalar products to be replaced automatically
1108 * @return simplified expression */
1109 ex ex::simplify_indexed(const scalar_products & sp) const
1111 exvector free_indices, dummy_indices;
1112 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1115 /** Symmetrize expression over its free indices. */
1116 ex ex::symmetrize(void) const
1118 return GiNaC::symmetrize(*this, get_free_indices());
1121 /** Antisymmetrize expression over its free indices. */
1122 ex ex::antisymmetrize(void) const
1124 return GiNaC::antisymmetrize(*this, get_free_indices());
1127 /** Symmetrize expression by cyclic permutation over its free indices. */
1128 ex ex::symmetrize_cyclic(void) const
1130 return GiNaC::symmetrize_cyclic(*this, get_free_indices());
1137 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
1139 spm[make_key(v1, v2)] = sp;
1142 void scalar_products::add_vectors(const lst & l)
1144 // Add all possible pairs of products
1145 unsigned num = l.nops();
1146 for (unsigned i=0; i<num; i++) {
1148 for (unsigned j=0; j<num; j++) {
1155 void scalar_products::clear(void)
1160 /** Check whether scalar product pair is defined. */
1161 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
1163 return spm.find(make_key(v1, v2)) != spm.end();
1166 /** Return value of defined scalar product pair. */
1167 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
1169 return spm.find(make_key(v1, v2))->second;
1172 void scalar_products::debugprint(void) const
1174 std::cerr << "map size=" << spm.size() << std::endl;
1175 spmap::const_iterator i = spm.begin(), end = spm.end();
1177 const spmapkey & k = i->first;
1178 std::cerr << "item key=(" << k.first << "," << k.second;
1179 std::cerr << "), value=" << i->second << std::endl;
1184 /** Make key from object pair. */
1185 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
1187 // If indexed, extract base objects
1188 ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
1189 ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
1191 // Enforce canonical order in pair
1192 if (s1.compare(s2) > 0)
1193 return spmapkey(s2, s1);
1195 return spmapkey(s1, s2);
1198 } // namespace GiNaC