3 * Implementation of GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2004 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"
35 #include "operators.h"
42 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(indexed, exprseq,
43 print_func<print_context>(&indexed::do_print).
44 print_func<print_latex>(&indexed::do_print_latex).
45 print_func<print_tree>(&indexed::do_print_tree))
48 // default constructor
51 indexed::indexed() : symtree(not_symmetric())
53 tinfo_key = TINFO_indexed;
60 indexed::indexed(const ex & b) : inherited(b), symtree(not_symmetric())
62 tinfo_key = TINFO_indexed;
66 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(not_symmetric())
68 tinfo_key = TINFO_indexed;
72 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(not_symmetric())
74 tinfo_key = TINFO_indexed;
78 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(not_symmetric())
80 tinfo_key = TINFO_indexed;
84 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symtree(not_symmetric())
86 tinfo_key = TINFO_indexed;
90 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(symm)
92 tinfo_key = TINFO_indexed;
96 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(symm)
98 tinfo_key = TINFO_indexed;
102 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)
104 tinfo_key = TINFO_indexed;
108 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(not_symmetric())
110 seq.insert(seq.end(), v.begin(), v.end());
111 tinfo_key = TINFO_indexed;
115 indexed::indexed(const ex & b, const symmetry & symm, const exvector & v) : inherited(b), symtree(symm)
117 seq.insert(seq.end(), v.begin(), v.end());
118 tinfo_key = TINFO_indexed;
122 indexed::indexed(const symmetry & symm, const exprseq & es) : inherited(es), symtree(symm)
124 tinfo_key = TINFO_indexed;
127 indexed::indexed(const symmetry & symm, const exvector & v, bool discardable) : inherited(v, discardable), symtree(symm)
129 tinfo_key = TINFO_indexed;
132 indexed::indexed(const symmetry & symm, std::auto_ptr<exvector> vp) : inherited(vp), symtree(symm)
134 tinfo_key = TINFO_indexed;
141 indexed::indexed(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
143 if (!n.find_ex("symmetry", symtree, sym_lst)) {
144 // GiNaC versions <= 0.9.0 had an unsigned "symmetry" property
146 n.find_unsigned("symmetry", symm);
155 symtree = not_symmetric();
158 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
162 void indexed::archive(archive_node &n) const
164 inherited::archive(n);
165 n.add_ex("symmetry", symtree);
168 DEFAULT_UNARCHIVE(indexed)
171 // functions overriding virtual functions from base classes
174 void indexed::printindices(const print_context & c, unsigned level) const
176 if (seq.size() > 1) {
178 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
180 if (is_a<print_latex>(c)) {
182 // TeX output: group by variance
184 bool covariant = true;
186 while (it != itend) {
187 bool cur_covariant = (is_a<varidx>(*it) ? ex_to<varidx>(*it).is_covariant() : true);
188 if (first || cur_covariant != covariant) { // Variance changed
189 // The empty {} prevents indices from ending up on top of each other
192 covariant = cur_covariant;
208 while (it != itend) {
216 void indexed::print_indexed(const print_context & c, const char *openbrace, const char *closebrace, unsigned level) const
218 if (precedence() <= level)
219 c.s << openbrace << '(';
221 seq[0].print(c, precedence());
223 printindices(c, level);
224 if (precedence() <= level)
225 c.s << ')' << closebrace;
228 void indexed::do_print(const print_context & c, unsigned level) const
230 print_indexed(c, "", "", level);
233 void indexed::do_print_latex(const print_latex & c, unsigned level) const
235 print_indexed(c, "{", "}", level);
238 void indexed::do_print_tree(const print_tree & c, unsigned level) const
240 c.s << std::string(level, ' ') << class_name() << " @" << this
241 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
242 << ", " << seq.size()-1 << " indices"
243 << ", symmetry=" << symtree << std::endl;
244 seq[0].print(c, level + c.delta_indent);
245 printindices(c, level + c.delta_indent);
248 bool indexed::info(unsigned inf) const
250 if (inf == info_flags::indexed) return true;
251 if (inf == info_flags::has_indices) return seq.size() > 1;
252 return inherited::info(inf);
255 struct idx_is_not : public std::binary_function<ex, unsigned, bool> {
256 bool operator() (const ex & e, unsigned inf) const {
257 return !(ex_to<idx>(e).get_value().info(inf));
261 bool indexed::all_index_values_are(unsigned inf) const
263 // No indices? Then no property can be fulfilled
268 return find_if(seq.begin() + 1, seq.end(), bind2nd(idx_is_not(), inf)) == seq.end();
271 int indexed::compare_same_type(const basic & other) const
273 GINAC_ASSERT(is_a<indexed>(other));
274 return inherited::compare_same_type(other);
277 ex indexed::eval(int level) const
279 // First evaluate children, then we will end up here again
281 return indexed(ex_to<symmetry>(symtree), evalchildren(level));
283 const ex &base = seq[0];
285 // If the base object is 0, the whole object is 0
289 // If the base object is a product, pull out the numeric factor
290 if (is_exactly_a<mul>(base) && is_exactly_a<numeric>(base.op(base.nops() - 1))) {
292 ex f = ex_to<numeric>(base.op(base.nops() - 1));
294 return f * thiscontainer(v);
297 // Canonicalize indices according to the symmetry properties
298 if (seq.size() > 2) {
300 GINAC_ASSERT(is_exactly_a<symmetry>(symtree));
301 int sig = canonicalize(v.begin() + 1, ex_to<symmetry>(symtree));
302 if (sig != INT_MAX) {
303 // Something has changed while sorting indices, more evaluations later
306 return ex(sig) * thiscontainer(v);
310 // Let the class of the base object perform additional evaluations
311 return ex_to<basic>(base).eval_indexed(*this);
314 ex indexed::thiscontainer(const exvector & v) const
316 return indexed(ex_to<symmetry>(symtree), v);
319 ex indexed::thiscontainer(std::auto_ptr<exvector> vp) const
321 return indexed(ex_to<symmetry>(symtree), vp);
324 ex indexed::expand(unsigned options) const
326 GINAC_ASSERT(seq.size() > 0);
328 if (options & expand_options::expand_indexed) {
329 ex newbase = seq[0].expand(options);
330 if (is_exactly_a<add>(newbase)) {
332 for (size_t i=0; i<newbase.nops(); i++) {
334 s[0] = newbase.op(i);
335 sum += thiscontainer(s).expand(options);
339 if (!are_ex_trivially_equal(newbase, seq[0])) {
342 return ex_to<indexed>(thiscontainer(s)).inherited::expand(options);
345 return inherited::expand(options);
349 // virtual functions which can be overridden by derived classes
355 // non-virtual functions in this class
358 /** Check whether all indices are of class idx and validate the symmetry
359 * tree. This function is used internally to make sure that all constructed
360 * indexed objects really carry indices and not some other classes. */
361 void indexed::validate() const
363 GINAC_ASSERT(seq.size() > 0);
364 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
365 while (it != itend) {
367 throw(std::invalid_argument("indices of indexed object must be of type idx"));
371 if (!symtree.is_zero()) {
372 if (!is_exactly_a<symmetry>(symtree))
373 throw(std::invalid_argument("symmetry of indexed object must be of type symmetry"));
374 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
378 /** Implementation of ex::diff() for an indexed object always returns 0.
381 ex indexed::derivative(const symbol & s) const
390 struct idx_is_equal_ignore_dim : public std::binary_function<ex, ex, bool> {
391 bool operator() (const ex &lh, const ex &rh) const
397 // Replacing the dimension might cause an error (e.g. with
398 // index classes that only work in a fixed number of dimensions)
399 return lh.is_equal(ex_to<idx>(rh).replace_dim(ex_to<idx>(lh).get_dim()));
406 /** Check whether two sorted index vectors are consistent (i.e. equal). */
407 static bool indices_consistent(const exvector & v1, const exvector & v2)
409 // Number of indices must be the same
410 if (v1.size() != v2.size())
413 return equal(v1.begin(), v1.end(), v2.begin(), idx_is_equal_ignore_dim());
416 exvector indexed::get_indices() const
418 GINAC_ASSERT(seq.size() >= 1);
419 return exvector(seq.begin() + 1, seq.end());
422 exvector indexed::get_dummy_indices() const
424 exvector free_indices, dummy_indices;
425 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
426 return dummy_indices;
429 exvector indexed::get_dummy_indices(const indexed & other) const
431 exvector indices = get_free_indices();
432 exvector other_indices = other.get_free_indices();
433 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
434 exvector dummy_indices;
435 find_dummy_indices(indices, dummy_indices);
436 return dummy_indices;
439 bool indexed::has_dummy_index_for(const ex & i) const
441 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
442 while (it != itend) {
443 if (is_dummy_pair(*it, i))
450 exvector indexed::get_free_indices() const
452 exvector free_indices, dummy_indices;
453 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
457 exvector add::get_free_indices() const
459 exvector free_indices;
460 for (size_t i=0; i<nops(); i++) {
462 free_indices = op(i).get_free_indices();
464 exvector free_indices_of_term = op(i).get_free_indices();
465 if (!indices_consistent(free_indices, free_indices_of_term))
466 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
472 exvector mul::get_free_indices() const
474 // Concatenate free indices of all factors
476 for (size_t i=0; i<nops(); i++) {
477 exvector free_indices_of_factor = op(i).get_free_indices();
478 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
481 // And remove the dummy indices
482 exvector free_indices, dummy_indices;
483 find_free_and_dummy(un, free_indices, dummy_indices);
487 exvector ncmul::get_free_indices() const
489 // Concatenate free indices of all factors
491 for (size_t i=0; i<nops(); i++) {
492 exvector free_indices_of_factor = op(i).get_free_indices();
493 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
496 // And remove the dummy indices
497 exvector free_indices, dummy_indices;
498 find_free_and_dummy(un, free_indices, dummy_indices);
502 struct is_summation_idx : public std::unary_function<ex, bool> {
503 bool operator()(const ex & e)
505 return is_dummy_pair(e, e);
509 exvector power::get_free_indices() const
511 // Get free indices of basis
512 exvector basis_indices = basis.get_free_indices();
514 if (exponent.info(info_flags::even)) {
515 // If the exponent is an even number, then any "free" index that
516 // forms a dummy pair with itself is actually a summation index
517 exvector really_free;
518 std::remove_copy_if(basis_indices.begin(), basis_indices.end(),
519 std::back_inserter(really_free), is_summation_idx());
522 return basis_indices;
525 /** Rename dummy indices in an expression.
527 * @param e Expression to work on
528 * @param local_dummy_indices The set of dummy indices that appear in the
530 * @param global_dummy_indices The set of dummy indices that have appeared
531 * before and which we would like to use in "e", too. This gets updated
533 static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
535 size_t global_size = global_dummy_indices.size(),
536 local_size = local_dummy_indices.size();
538 // Any local dummy indices at all?
542 if (global_size < local_size) {
544 // More local indices than we encountered before, add the new ones
546 size_t old_global_size = global_size;
547 int remaining = local_size - global_size;
548 exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
549 while (it != itend && remaining > 0) {
550 if (find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(op0_is_equal(), *it)) == global_dummy_indices.end()) {
551 global_dummy_indices.push_back(*it);
558 // If this is the first set of local indices, do nothing
559 if (old_global_size == 0)
562 GINAC_ASSERT(local_size <= global_size);
564 // Construct vectors of index symbols
565 exvector local_syms, global_syms;
566 local_syms.reserve(local_size);
567 global_syms.reserve(local_size);
568 for (size_t i=0; i<local_size; i++)
569 local_syms.push_back(local_dummy_indices[i].op(0));
570 shaker_sort(local_syms.begin(), local_syms.end(), ex_is_less(), ex_swap());
571 for (size_t i=0; i<local_size; i++) // don't use more global symbols than necessary
572 global_syms.push_back(global_dummy_indices[i].op(0));
573 shaker_sort(global_syms.begin(), global_syms.end(), ex_is_less(), ex_swap());
575 // Remove common indices
576 exvector local_uniq, global_uniq;
577 set_difference(local_syms.begin(), local_syms.end(), global_syms.begin(), global_syms.end(), std::back_insert_iterator<exvector>(local_uniq), ex_is_less());
578 set_difference(global_syms.begin(), global_syms.end(), local_syms.begin(), local_syms.end(), std::back_insert_iterator<exvector>(global_uniq), ex_is_less());
580 // Replace remaining non-common local index symbols by global ones
581 if (local_uniq.empty())
584 while (global_uniq.size() > local_uniq.size())
585 global_uniq.pop_back();
586 return e.subs(lst(local_uniq.begin(), local_uniq.end()), lst(global_uniq.begin(), global_uniq.end()), subs_options::no_pattern);
590 /** Given a set of indices, extract those of class varidx. */
591 static void find_variant_indices(const exvector & v, exvector & variant_indices)
593 exvector::const_iterator it1, itend;
594 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
595 if (is_exactly_a<varidx>(*it1))
596 variant_indices.push_back(*it1);
600 /** Raise/lower dummy indices in a single indexed objects to canonicalize their
603 * @param e Object to work on
604 * @param variant_dummy_indices The set of indices that might need repositioning (will be changed by this function)
605 * @param moved_indices The set of indices that have been repositioned (will be changed by this function)
606 * @return true if 'e' was changed */
607 bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices)
609 bool something_changed = false;
611 // If a dummy index is encountered for the first time in the
612 // product, pull it up, otherwise, pull it down
613 exvector::const_iterator it2, it2start, it2end;
614 for (it2start = ex_to<indexed>(e).seq.begin(), it2end = ex_to<indexed>(e).seq.end(), it2 = it2start + 1; it2 != it2end; ++it2) {
615 if (!is_exactly_a<varidx>(*it2))
618 exvector::iterator vit, vitend;
619 for (vit = variant_dummy_indices.begin(), vitend = variant_dummy_indices.end(); vit != vitend; ++vit) {
620 if (it2->op(0).is_equal(vit->op(0))) {
621 if (ex_to<varidx>(*it2).is_covariant()) {
623 *it2 == ex_to<varidx>(*it2).toggle_variance(),
624 ex_to<varidx>(*it2).toggle_variance() == *it2
625 ), subs_options::no_pattern);
626 something_changed = true;
627 it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
628 it2start = ex_to<indexed>(e).seq.begin();
629 it2end = ex_to<indexed>(e).seq.end();
631 moved_indices.push_back(*vit);
632 variant_dummy_indices.erase(vit);
637 for (vit = moved_indices.begin(), vitend = moved_indices.end(); vit != vitend; ++vit) {
638 if (it2->op(0).is_equal(vit->op(0))) {
639 if (ex_to<varidx>(*it2).is_contravariant()) {
640 e = e.subs(*it2 == ex_to<varidx>(*it2).toggle_variance(), subs_options::no_pattern);
641 something_changed = true;
642 it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
643 it2start = ex_to<indexed>(e).seq.begin();
644 it2end = ex_to<indexed>(e).seq.end();
653 return something_changed;
656 /* Ordering that only compares the base expressions of indexed objects. */
657 struct ex_base_is_less : public std::binary_function<ex, ex, bool> {
658 bool operator() (const ex &lh, const ex &rh) const
660 return (is_a<indexed>(lh) ? lh.op(0) : lh).compare(is_a<indexed>(rh) ? rh.op(0) : rh) < 0;
664 /** Simplify product of indexed expressions (commutative, noncommutative and
665 * simple squares), return list of free indices. */
666 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
668 // Remember whether the product was commutative or noncommutative
669 // (because we chop it into factors and need to reassemble later)
670 bool non_commutative = is_exactly_a<ncmul>(e);
672 // Collect factors in an exvector, store squares twice
674 v.reserve(e.nops() * 2);
676 if (is_exactly_a<power>(e)) {
677 // We only get called for simple squares, split a^2 -> a*a
678 GINAC_ASSERT(e.op(1).is_equal(_ex2));
679 v.push_back(e.op(0));
680 v.push_back(e.op(0));
682 for (size_t i=0; i<e.nops(); i++) {
684 if (is_exactly_a<power>(f) && f.op(1).is_equal(_ex2)) {
685 v.push_back(f.op(0));
686 v.push_back(f.op(0));
687 } else if (is_exactly_a<ncmul>(f)) {
688 // Noncommutative factor found, split it as well
689 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
690 for (size_t j=0; j<f.nops(); j++)
691 v.push_back(f.op(j));
697 // Perform contractions
698 bool something_changed = false;
699 GINAC_ASSERT(v.size() > 1);
700 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
701 for (it1 = v.begin(); it1 != next_to_last; it1++) {
704 if (!is_a<indexed>(*it1))
707 bool first_noncommutative = (it1->return_type() != return_types::commutative);
709 // Indexed factor found, get free indices and look for contraction
711 exvector free1, dummy1;
712 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
714 exvector::iterator it2;
715 for (it2 = it1 + 1; it2 != itend; it2++) {
717 if (!is_a<indexed>(*it2))
720 bool second_noncommutative = (it2->return_type() != return_types::commutative);
722 // Find free indices of second factor and merge them with free
723 // indices of first factor
725 find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
726 un.insert(un.end(), free1.begin(), free1.end());
728 // Check whether the two factors share dummy indices
729 exvector free, dummy;
730 find_free_and_dummy(un, free, dummy);
731 size_t num_dummies = dummy.size();
732 if (num_dummies == 0)
735 // At least one dummy index, is it a defined scalar product?
736 bool contracted = false;
739 // Find minimal dimension of all indices of both factors
740 exvector::const_iterator dit = ex_to<indexed>(*it1).seq.begin() + 1, ditend = ex_to<indexed>(*it1).seq.end();
741 ex dim = ex_to<idx>(*dit).get_dim();
743 for (; dit != ditend; ++dit) {
744 dim = minimal_dim(dim, ex_to<idx>(*dit).get_dim());
746 dit = ex_to<indexed>(*it2).seq.begin() + 1;
747 ditend = ex_to<indexed>(*it2).seq.end();
748 for (; dit != ditend; ++dit) {
749 dim = minimal_dim(dim, ex_to<idx>(*dit).get_dim());
752 // User-defined scalar product?
753 if (sp.is_defined(*it1, *it2, dim)) {
755 // Yes, substitute it
756 *it1 = sp.evaluate(*it1, *it2, dim);
758 goto contraction_done;
762 // Try to contract the first one with the second one
763 contracted = ex_to<basic>(it1->op(0)).contract_with(it1, it2, v);
766 // That didn't work; maybe the second object knows how to
767 // contract itself with the first one
768 contracted = ex_to<basic>(it2->op(0)).contract_with(it2, it1, v);
772 if (first_noncommutative || second_noncommutative
773 || is_exactly_a<add>(*it1) || is_exactly_a<add>(*it2)
774 || is_exactly_a<mul>(*it1) || is_exactly_a<mul>(*it2)
775 || is_exactly_a<ncmul>(*it1) || is_exactly_a<ncmul>(*it2)) {
777 // One of the factors became a sum or product:
778 // re-expand expression and run again
779 // Non-commutative products are always re-expanded to give
780 // eval_ncmul() the chance to re-order and canonicalize
782 ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
783 return simplify_indexed(r, free_indices, dummy_indices, sp);
786 // Both objects may have new indices now or they might
787 // even not be indexed objects any more, so we have to
789 something_changed = true;
795 // Find free indices (concatenate them all and call find_free_and_dummy())
796 // and all dummy indices that appear
797 exvector un, individual_dummy_indices;
798 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
799 exvector free_indices_of_factor;
800 if (is_a<indexed>(*it1)) {
801 exvector dummy_indices_of_factor;
802 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);
803 individual_dummy_indices.insert(individual_dummy_indices.end(), dummy_indices_of_factor.begin(), dummy_indices_of_factor.end());
805 free_indices_of_factor = it1->get_free_indices();
806 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
808 exvector local_dummy_indices;
809 find_free_and_dummy(un, free_indices, local_dummy_indices);
810 local_dummy_indices.insert(local_dummy_indices.end(), individual_dummy_indices.begin(), individual_dummy_indices.end());
812 // Filter out the dummy indices with variance
813 exvector variant_dummy_indices;
814 find_variant_indices(local_dummy_indices, variant_dummy_indices);
816 // Any indices with variance present at all?
817 if (!variant_dummy_indices.empty()) {
819 // Yes, bring the product into a canonical order that only depends on
820 // the base expressions of indexed objects
821 if (!non_commutative)
822 std::sort(v.begin(), v.end(), ex_base_is_less());
824 exvector moved_indices;
826 // Iterate over all indexed objects in the product
827 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
828 if (!is_a<indexed>(*it1))
831 if (reposition_dummy_indices(*it1, variant_dummy_indices, moved_indices))
832 something_changed = true;
837 if (something_changed)
838 r = non_commutative ? ex(ncmul(v, true)) : ex(mul(v));
842 // The result should be symmetric with respect to exchange of dummy
843 // indices, so if the symmetrization vanishes, the whole expression is
844 // zero. This detects things like eps.i.j.k * p.j * p.k = 0.
845 if (local_dummy_indices.size() >= 2) {
847 dummy_syms.reserve(local_dummy_indices.size());
848 for (exvector::const_iterator it = local_dummy_indices.begin(); it != local_dummy_indices.end(); ++it)
849 dummy_syms.push_back(it->op(0));
850 if (symmetrize(r, dummy_syms).is_zero()) {
851 free_indices.clear();
856 // Dummy index renaming
857 r = rename_dummy_indices(r, dummy_indices, local_dummy_indices);
859 // Product of indexed object with a scalar?
860 if (is_exactly_a<mul>(r) && r.nops() == 2
861 && is_exactly_a<numeric>(r.op(1)) && is_a<indexed>(r.op(0)))
862 return ex_to<basic>(r.op(0).op(0)).scalar_mul_indexed(r.op(0), ex_to<numeric>(r.op(1)));
867 /** This structure stores the original and symmetrized versions of terms
868 * obtained during the simplification of sums. */
871 terminfo(const ex & orig_, const ex & symm_) : orig(orig_), symm(symm_) {}
873 ex orig; /**< original term */
874 ex symm; /**< symmtrized term */
877 class terminfo_is_less {
879 bool operator() (const terminfo & ti1, const terminfo & ti2) const
881 return (ti1.symm.compare(ti2.symm) < 0);
885 /** This structure stores the individual symmetrized terms obtained during
886 * the simplification of sums. */
889 symminfo() : num(0) {}
891 symminfo(const ex & symmterm_, const ex & orig_, size_t num_) : orig(orig_), num(num_)
893 if (is_exactly_a<mul>(symmterm_) && is_exactly_a<numeric>(symmterm_.op(symmterm_.nops()-1))) {
894 coeff = symmterm_.op(symmterm_.nops()-1);
895 symmterm = symmterm_ / coeff;
898 symmterm = symmterm_;
902 ex symmterm; /**< symmetrized term */
903 ex coeff; /**< coefficient of symmetrized term */
904 ex orig; /**< original term */
905 size_t num; /**< how many symmetrized terms resulted from the original term */
908 class symminfo_is_less_by_symmterm {
910 bool operator() (const symminfo & si1, const symminfo & si2) const
912 return (si1.symmterm.compare(si2.symmterm) < 0);
916 class symminfo_is_less_by_orig {
918 bool operator() (const symminfo & si1, const symminfo & si2) const
920 return (si1.orig.compare(si2.orig) < 0);
924 /** Simplify indexed expression, return list of free indices. */
925 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
927 // Expand the expression
928 ex e_expanded = e.expand();
930 // Simplification of single indexed object: just find the free indices
931 // and perform dummy index renaming/repositioning
932 if (is_a<indexed>(e_expanded)) {
934 // Find the dummy indices
935 const indexed &i = ex_to<indexed>(e_expanded);
936 exvector local_dummy_indices;
937 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
939 // Filter out the dummy indices with variance
940 exvector variant_dummy_indices;
941 find_variant_indices(local_dummy_indices, variant_dummy_indices);
943 // Any indices with variance present at all?
944 if (!variant_dummy_indices.empty()) {
946 // Yes, reposition them
947 exvector moved_indices;
948 reposition_dummy_indices(e_expanded, variant_dummy_indices, moved_indices);
951 // Rename the dummy indices
952 return rename_dummy_indices(e_expanded, dummy_indices, local_dummy_indices);
955 // Simplification of sum = sum of simplifications, check consistency of
956 // free indices in each term
957 if (is_exactly_a<add>(e_expanded)) {
960 free_indices.clear();
962 for (size_t i=0; i<e_expanded.nops(); i++) {
963 exvector free_indices_of_term;
964 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
965 if (!term.is_zero()) {
967 free_indices = free_indices_of_term;
971 if (!indices_consistent(free_indices, free_indices_of_term)) {
972 std::ostringstream s;
973 s << "simplify_indexed: inconsistent indices in sum: ";
974 s << exprseq(free_indices) << " vs. " << exprseq(free_indices_of_term);
975 throw (std::runtime_error(s.str()));
977 if (is_a<indexed>(sum) && is_a<indexed>(term))
978 sum = ex_to<basic>(sum.op(0)).add_indexed(sum, term);
985 // If the sum turns out to be zero, we are finished
987 free_indices.clear();
991 // More than one term and more than one dummy index?
992 size_t num_terms_orig = (is_exactly_a<add>(sum) ? sum.nops() : 1);
993 if (num_terms_orig < 2 || dummy_indices.size() < 2)
996 // Yes, construct vector of all dummy index symbols
998 dummy_syms.reserve(dummy_indices.size());
999 for (exvector::const_iterator it = dummy_indices.begin(); it != dummy_indices.end(); ++it)
1000 dummy_syms.push_back(it->op(0));
1002 // Chop the sum into terms and symmetrize each one over the dummy
1004 std::vector<terminfo> terms;
1005 for (size_t i=0; i<sum.nops(); i++) {
1006 const ex & term = sum.op(i);
1007 ex term_symm = symmetrize(term, dummy_syms);
1008 if (term_symm.is_zero())
1010 terms.push_back(terminfo(term, term_symm));
1013 // Sort by symmetrized terms
1014 std::sort(terms.begin(), terms.end(), terminfo_is_less());
1016 // Combine equal symmetrized terms
1017 std::vector<terminfo> terms_pass2;
1018 for (std::vector<terminfo>::const_iterator i=terms.begin(); i!=terms.end(); ) {
1020 std::vector<terminfo>::const_iterator j = i + 1;
1021 while (j != terms.end() && j->symm == i->symm) {
1025 terms_pass2.push_back(terminfo(i->orig * num, i->symm * num));
1029 // If there is only one term left, we are finished
1030 if (terms_pass2.size() == 1)
1031 return terms_pass2[0].orig;
1033 // Chop the symmetrized terms into subterms
1034 std::vector<symminfo> sy;
1035 for (std::vector<terminfo>::const_iterator i=terms_pass2.begin(); i!=terms_pass2.end(); ++i) {
1036 if (is_exactly_a<add>(i->symm)) {
1037 size_t num = i->symm.nops();
1038 for (size_t j=0; j<num; j++)
1039 sy.push_back(symminfo(i->symm.op(j), i->orig, num));
1041 sy.push_back(symminfo(i->symm, i->orig, 1));
1044 // Sort by symmetrized subterms
1045 std::sort(sy.begin(), sy.end(), symminfo_is_less_by_symmterm());
1047 // Combine equal symmetrized subterms
1048 std::vector<symminfo> sy_pass2;
1050 for (std::vector<symminfo>::const_iterator i=sy.begin(); i!=sy.end(); ) {
1052 // Combine equal terms
1053 std::vector<symminfo>::const_iterator j = i + 1;
1054 if (j != sy.end() && j->symmterm == i->symmterm) {
1056 // More than one term, collect the coefficients
1057 ex coeff = i->coeff;
1058 while (j != sy.end() && j->symmterm == i->symmterm) {
1063 // Add combined term to result
1064 if (!coeff.is_zero())
1065 result.push_back(coeff * i->symmterm);
1069 // Single term, store for second pass
1070 sy_pass2.push_back(*i);
1076 // Were there any remaining terms that didn't get combined?
1077 if (sy_pass2.size() > 0) {
1079 // Yes, sort by their original terms
1080 std::sort(sy_pass2.begin(), sy_pass2.end(), symminfo_is_less_by_orig());
1082 for (std::vector<symminfo>::const_iterator i=sy_pass2.begin(); i!=sy_pass2.end(); ) {
1084 // How many symmetrized terms of this original term are left?
1086 std::vector<symminfo>::const_iterator j = i + 1;
1087 while (j != sy_pass2.end() && j->orig == i->orig) {
1092 if (num == i->num) {
1094 // All terms left, then add the original term to the result
1095 result.push_back(i->orig);
1099 // Some terms were combined with others, add up the remaining symmetrized terms
1100 std::vector<symminfo>::const_iterator k;
1101 for (k=i; k!=j; k++)
1102 result.push_back(k->coeff * k->symmterm);
1109 // Add all resulting terms
1110 ex sum_symm = (new add(result))->setflag(status_flags::dynallocated);
1111 if (sum_symm.is_zero())
1112 free_indices.clear();
1116 // Simplification of products
1117 if (is_exactly_a<mul>(e_expanded)
1118 || is_exactly_a<ncmul>(e_expanded)
1119 || (is_exactly_a<power>(e_expanded) && is_a<indexed>(e_expanded.op(0)) && e_expanded.op(1).is_equal(_ex2)))
1120 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
1122 // Cannot do anything
1123 free_indices.clear();
1127 /** Simplify/canonicalize expression containing indexed objects. This
1128 * performs contraction of dummy indices where possible and checks whether
1129 * the free indices in sums are consistent.
1131 * @return simplified expression */
1132 ex ex::simplify_indexed(unsigned options) const
1134 exvector free_indices, dummy_indices;
1136 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1139 /** Simplify/canonicalize expression containing indexed objects. This
1140 * performs contraction of dummy indices where possible, checks whether
1141 * the free indices in sums are consistent, and automatically replaces
1142 * scalar products by known values if desired.
1144 * @param sp Scalar products to be replaced automatically
1145 * @return simplified expression */
1146 ex ex::simplify_indexed(const scalar_products & sp, unsigned options) const
1148 exvector free_indices, dummy_indices;
1149 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1152 /** Symmetrize expression over its free indices. */
1153 ex ex::symmetrize() const
1155 return GiNaC::symmetrize(*this, get_free_indices());
1158 /** Antisymmetrize expression over its free indices. */
1159 ex ex::antisymmetrize() const
1161 return GiNaC::antisymmetrize(*this, get_free_indices());
1164 /** Symmetrize expression by cyclic permutation over its free indices. */
1165 ex ex::symmetrize_cyclic() const
1167 return GiNaC::symmetrize_cyclic(*this, get_free_indices());
1174 spmapkey::spmapkey(const ex & v1_, const ex & v2_, const ex & dim_) : dim(dim_)
1176 // If indexed, extract base objects
1177 ex s1 = is_a<indexed>(v1_) ? v1_.op(0) : v1_;
1178 ex s2 = is_a<indexed>(v2_) ? v2_.op(0) : v2_;
1180 // Enforce canonical order in pair
1181 if (s1.compare(s2) > 0) {
1190 bool spmapkey::operator==(const spmapkey &other) const
1192 if (!v1.is_equal(other.v1))
1194 if (!v2.is_equal(other.v2))
1196 if (is_a<wildcard>(dim) || is_a<wildcard>(other.dim))
1199 return dim.is_equal(other.dim);
1202 bool spmapkey::operator<(const spmapkey &other) const
1204 int cmp = v1.compare(other.v1);
1207 cmp = v2.compare(other.v2);
1211 // Objects are equal, now check dimensions
1212 if (is_a<wildcard>(dim) || is_a<wildcard>(other.dim))
1215 return dim.compare(other.dim) < 0;
1218 void spmapkey::debugprint() const
1220 std::cerr << "(" << v1 << "," << v2 << "," << dim << ")";
1223 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
1225 spm[spmapkey(v1, v2)] = sp;
1228 void scalar_products::add(const ex & v1, const ex & v2, const ex & dim, const ex & sp)
1230 spm[spmapkey(v1, v2, dim)] = sp;
1233 void scalar_products::add_vectors(const lst & l, const ex & dim)
1235 // Add all possible pairs of products
1236 for (lst::const_iterator it1 = l.begin(); it1 != l.end(); ++it1)
1237 for (lst::const_iterator it2 = l.begin(); it2 != l.end(); ++it2)
1238 add(*it1, *it2, *it1 * *it2);
1241 void scalar_products::clear()
1246 /** Check whether scalar product pair is defined. */
1247 bool scalar_products::is_defined(const ex & v1, const ex & v2, const ex & dim) const
1249 return spm.find(spmapkey(v1, v2, dim)) != spm.end();
1252 /** Return value of defined scalar product pair. */
1253 ex scalar_products::evaluate(const ex & v1, const ex & v2, const ex & dim) const
1255 return spm.find(spmapkey(v1, v2, dim))->second;
1258 void scalar_products::debugprint() const
1260 std::cerr << "map size=" << spm.size() << std::endl;
1261 spmap::const_iterator i = spm.begin(), end = spm.end();
1263 const spmapkey & k = i->first;
1264 std::cerr << "item key=";
1266 std::cerr << ", value=" << i->second << std::endl;
1271 } // namespace GiNaC