3 * Implementation of GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "relational.h"
35 #include "operators.h"
46 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(indexed, exprseq,
47 print_func<print_context>(&indexed::do_print).
48 print_func<print_latex>(&indexed::do_print_latex).
49 print_func<print_tree>(&indexed::do_print_tree))
52 // default constructor
55 indexed::indexed() : symtree(not_symmetric())
57 tinfo_key = &indexed::tinfo_static;
64 indexed::indexed(const ex & b) : inherited(b), symtree(not_symmetric())
66 tinfo_key = &indexed::tinfo_static;
70 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(not_symmetric())
72 tinfo_key = &indexed::tinfo_static;
76 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(not_symmetric())
78 tinfo_key = &indexed::tinfo_static;
82 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(not_symmetric())
84 tinfo_key = &indexed::tinfo_static;
88 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())
90 tinfo_key = &indexed::tinfo_static;
94 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(symm)
96 tinfo_key = &indexed::tinfo_static;
100 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(symm)
102 tinfo_key = &indexed::tinfo_static;
106 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)
108 tinfo_key = &indexed::tinfo_static;
112 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(not_symmetric())
114 seq.insert(seq.end(), v.begin(), v.end());
115 tinfo_key = &indexed::tinfo_static;
119 indexed::indexed(const ex & b, const symmetry & symm, const exvector & v) : inherited(b), symtree(symm)
121 seq.insert(seq.end(), v.begin(), v.end());
122 tinfo_key = &indexed::tinfo_static;
126 indexed::indexed(const symmetry & symm, const exprseq & es) : inherited(es), symtree(symm)
128 tinfo_key = &indexed::tinfo_static;
131 indexed::indexed(const symmetry & symm, const exvector & v, bool discardable) : inherited(v, discardable), symtree(symm)
133 tinfo_key = &indexed::tinfo_static;
136 indexed::indexed(const symmetry & symm, std::auto_ptr<exvector> vp) : inherited(vp), symtree(symm)
138 tinfo_key = &indexed::tinfo_static;
145 indexed::indexed(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
147 if (!n.find_ex("symmetry", symtree, sym_lst)) {
148 // GiNaC versions <= 0.9.0 had an unsigned "symmetry" property
150 n.find_unsigned("symmetry", symm);
159 symtree = not_symmetric();
162 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
166 void indexed::archive(archive_node &n) const
168 inherited::archive(n);
169 n.add_ex("symmetry", symtree);
172 DEFAULT_UNARCHIVE(indexed)
175 // functions overriding virtual functions from base classes
178 void indexed::printindices(const print_context & c, unsigned level) const
180 if (seq.size() > 1) {
182 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
184 if (is_a<print_latex>(c)) {
186 // TeX output: group by variance
188 bool covariant = true;
190 while (it != itend) {
191 bool cur_covariant = (is_a<varidx>(*it) ? ex_to<varidx>(*it).is_covariant() : true);
192 if (first || cur_covariant != covariant) { // Variance changed
193 // The empty {} prevents indices from ending up on top of each other
196 covariant = cur_covariant;
212 while (it != itend) {
220 void indexed::print_indexed(const print_context & c, const char *openbrace, const char *closebrace, unsigned level) const
222 if (precedence() <= level)
223 c.s << openbrace << '(';
225 seq[0].print(c, precedence());
227 printindices(c, level);
228 if (precedence() <= level)
229 c.s << ')' << closebrace;
232 void indexed::do_print(const print_context & c, unsigned level) const
234 print_indexed(c, "", "", level);
237 void indexed::do_print_latex(const print_latex & c, unsigned level) const
239 print_indexed(c, "{", "}", level);
242 void indexed::do_print_tree(const print_tree & c, unsigned level) const
244 c.s << std::string(level, ' ') << class_name() << " @" << this
245 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
246 << ", " << seq.size()-1 << " indices"
247 << ", symmetry=" << symtree << std::endl;
248 seq[0].print(c, level + c.delta_indent);
249 printindices(c, level + c.delta_indent);
252 bool indexed::info(unsigned inf) const
254 if (inf == info_flags::indexed) return true;
255 if (inf == info_flags::has_indices) return seq.size() > 1;
256 return inherited::info(inf);
259 struct idx_is_not : public std::binary_function<ex, unsigned, bool> {
260 bool operator() (const ex & e, unsigned inf) const {
261 return !(ex_to<idx>(e).get_value().info(inf));
265 bool indexed::all_index_values_are(unsigned inf) const
267 // No indices? Then no property can be fulfilled
272 return find_if(seq.begin() + 1, seq.end(), bind2nd(idx_is_not(), inf)) == seq.end();
275 int indexed::compare_same_type(const basic & other) const
277 GINAC_ASSERT(is_a<indexed>(other));
278 return inherited::compare_same_type(other);
281 ex indexed::eval(int level) const
283 // First evaluate children, then we will end up here again
285 return indexed(ex_to<symmetry>(symtree), evalchildren(level));
287 const ex &base = seq[0];
289 // If the base object is 0, the whole object is 0
293 // If the base object is a product, pull out the numeric factor
294 if (is_exactly_a<mul>(base) && is_exactly_a<numeric>(base.op(base.nops() - 1))) {
296 ex f = ex_to<numeric>(base.op(base.nops() - 1));
298 return f * thiscontainer(v);
301 if(this->tinfo()==&indexed::tinfo_static && seq.size()==1)
304 // Canonicalize indices according to the symmetry properties
305 if (seq.size() > 2) {
307 GINAC_ASSERT(is_exactly_a<symmetry>(symtree));
308 int sig = canonicalize(v.begin() + 1, ex_to<symmetry>(symtree));
309 if (sig != INT_MAX) {
310 // Something has changed while sorting indices, more evaluations later
313 return ex(sig) * thiscontainer(v);
317 // Let the class of the base object perform additional evaluations
318 return ex_to<basic>(base).eval_indexed(*this);
321 ex indexed::real_part() const
323 if(op(0).info(info_flags::real))
325 return real_part_function(*this).hold();
328 ex indexed::imag_part() const
330 if(op(0).info(info_flags::real))
332 return imag_part_function(*this).hold();
335 ex indexed::thiscontainer(const exvector & v) const
337 return indexed(ex_to<symmetry>(symtree), v);
340 ex indexed::thiscontainer(std::auto_ptr<exvector> vp) const
342 return indexed(ex_to<symmetry>(symtree), vp);
345 unsigned indexed::return_type() const
347 if(is_a<matrix>(op(0)))
348 return return_types::commutative;
350 return op(0).return_type();
353 ex indexed::expand(unsigned options) const
355 GINAC_ASSERT(seq.size() > 0);
357 if (options & expand_options::expand_indexed) {
358 ex newbase = seq[0].expand(options);
359 if (is_exactly_a<add>(newbase)) {
361 for (size_t i=0; i<newbase.nops(); i++) {
363 s[0] = newbase.op(i);
364 sum += thiscontainer(s).expand(options);
368 if (!are_ex_trivially_equal(newbase, seq[0])) {
371 return ex_to<indexed>(thiscontainer(s)).inherited::expand(options);
374 return inherited::expand(options);
378 // virtual functions which can be overridden by derived classes
384 // non-virtual functions in this class
387 /** Check whether all indices are of class idx and validate the symmetry
388 * tree. This function is used internally to make sure that all constructed
389 * indexed objects really carry indices and not some other classes. */
390 void indexed::validate() const
392 GINAC_ASSERT(seq.size() > 0);
393 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
394 while (it != itend) {
396 throw(std::invalid_argument("indices of indexed object must be of type idx"));
400 if (!symtree.is_zero()) {
401 if (!is_exactly_a<symmetry>(symtree))
402 throw(std::invalid_argument("symmetry of indexed object must be of type symmetry"));
403 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
407 /** Implementation of ex::diff() for an indexed object always returns 0.
410 ex indexed::derivative(const symbol & s) const
419 struct idx_is_equal_ignore_dim : public std::binary_function<ex, ex, bool> {
420 bool operator() (const ex &lh, const ex &rh) const
426 // Replacing the dimension might cause an error (e.g. with
427 // index classes that only work in a fixed number of dimensions)
428 return lh.is_equal(ex_to<idx>(rh).replace_dim(ex_to<idx>(lh).get_dim()));
435 /** Check whether two sorted index vectors are consistent (i.e. equal). */
436 static bool indices_consistent(const exvector & v1, const exvector & v2)
438 // Number of indices must be the same
439 if (v1.size() != v2.size())
442 return equal(v1.begin(), v1.end(), v2.begin(), idx_is_equal_ignore_dim());
445 exvector indexed::get_indices() const
447 GINAC_ASSERT(seq.size() >= 1);
448 return exvector(seq.begin() + 1, seq.end());
451 exvector indexed::get_dummy_indices() const
453 exvector free_indices, dummy_indices;
454 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
455 return dummy_indices;
458 exvector indexed::get_dummy_indices(const indexed & other) const
460 exvector indices = get_free_indices();
461 exvector other_indices = other.get_free_indices();
462 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
463 exvector dummy_indices;
464 find_dummy_indices(indices, dummy_indices);
465 return dummy_indices;
468 bool indexed::has_dummy_index_for(const ex & i) const
470 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
471 while (it != itend) {
472 if (is_dummy_pair(*it, i))
479 exvector indexed::get_free_indices() 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() const
488 exvector free_indices;
489 for (size_t 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() const
503 // Concatenate free indices of all factors
505 for (size_t 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() const
518 // Concatenate free indices of all factors
520 for (size_t 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 struct is_summation_idx : public std::unary_function<ex, bool> {
532 bool operator()(const ex & e)
534 return is_dummy_pair(e, e);
538 exvector integral::get_free_indices() const
540 if (a.get_free_indices().size() || b.get_free_indices().size())
541 throw (std::runtime_error("integral::get_free_indices: boundary values should not have free indices"));
542 return f.get_free_indices();
545 template<class T> size_t number_of_type(const exvector&v)
548 for(exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
549 if(is_exactly_a<T>(*i))
554 /** Rename dummy indices in an expression.
556 * @param e Expression to work on
557 * @param local_dummy_indices The set of dummy indices that appear in the
559 * @param global_dummy_indices The set of dummy indices that have appeared
560 * before and which we would like to use in "e", too. This gets updated
562 template<class T> static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
564 size_t global_size = number_of_type<T>(global_dummy_indices),
565 local_size = number_of_type<T>(local_dummy_indices);
567 // Any local dummy indices at all?
571 if (global_size < local_size) {
573 // More local indices than we encountered before, add the new ones
575 size_t old_global_size = global_size;
576 int remaining = local_size - global_size;
577 exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
578 while (it != itend && remaining > 0) {
579 if (is_exactly_a<T>(*it) && find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(idx_is_equal_ignore_dim(), *it)) == global_dummy_indices.end()) {
580 global_dummy_indices.push_back(*it);
587 // If this is the first set of local indices, do nothing
588 if (old_global_size == 0)
591 GINAC_ASSERT(local_size <= global_size);
593 // Construct vectors of index symbols
594 exvector local_syms, global_syms;
595 local_syms.reserve(local_size);
596 global_syms.reserve(local_size);
597 for (size_t i=0; local_syms.size()!=local_size; i++)
598 if(is_exactly_a<T>(local_dummy_indices[i]))
599 local_syms.push_back(local_dummy_indices[i].op(0));
600 shaker_sort(local_syms.begin(), local_syms.end(), ex_is_less(), ex_swap());
601 for (size_t i=0; global_syms.size()!=local_size; i++) // don't use more global symbols than necessary
602 if(is_exactly_a<T>(global_dummy_indices[i]))
603 global_syms.push_back(global_dummy_indices[i].op(0));
604 shaker_sort(global_syms.begin(), global_syms.end(), ex_is_less(), ex_swap());
606 // Remove common indices
607 exvector local_uniq, global_uniq;
608 set_difference(local_syms.begin(), local_syms.end(), global_syms.begin(), global_syms.end(), std::back_insert_iterator<exvector>(local_uniq), ex_is_less());
609 set_difference(global_syms.begin(), global_syms.end(), local_syms.begin(), local_syms.end(), std::back_insert_iterator<exvector>(global_uniq), ex_is_less());
611 // Replace remaining non-common local index symbols by global ones
612 if (local_uniq.empty())
615 while (global_uniq.size() > local_uniq.size())
616 global_uniq.pop_back();
617 return e.subs(lst(local_uniq.begin(), local_uniq.end()), lst(global_uniq.begin(), global_uniq.end()), subs_options::no_pattern);
621 /** Given a set of indices, extract those of class varidx. */
622 static void find_variant_indices(const exvector & v, exvector & variant_indices)
624 exvector::const_iterator it1, itend;
625 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
626 if (is_exactly_a<varidx>(*it1))
627 variant_indices.push_back(*it1);
631 /** Raise/lower dummy indices in a single indexed objects to canonicalize their
634 * @param e Object to work on
635 * @param variant_dummy_indices The set of indices that might need repositioning (will be changed by this function)
636 * @param moved_indices The set of indices that have been repositioned (will be changed by this function)
637 * @return true if 'e' was changed */
638 bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices)
640 bool something_changed = false;
642 // Canonicalize wrt indices that are dummies within e. I.e., their
643 // symbol occurs twice in an index of e. This is only done if there
644 // is a cyclic symmetry because in that case it may happen that after
645 // raising/lowering an index the indices get reshuffled by ::eval in
646 // such a way that the iterators no longer point to the right objects.
647 if (ex_to<symmetry>(ex_to<indexed>(e).get_symmetry()).has_cyclic()) {
648 // Get dummy pairs of varidxes within the indexed object in e.
649 exvector local_var_dummies;
650 local_var_dummies.reserve(e.nops()/2);
651 for (size_t i=1; i<e.nops(); ++i) {
652 if (!is_a<varidx>(e.op(i)))
654 for (size_t j=i+1; j<e.nops(); ++j) {
655 if (is_dummy_pair(e.op(i), e.op(j))) {
656 local_var_dummies.push_back(e.op(i));
657 for (exvector::iterator k = variant_dummy_indices.begin();
658 k!=variant_dummy_indices.end(); ++k) {
659 if (e.op(i).op(0) == k->op(0)) {
660 variant_dummy_indices.erase(k);
668 // Try all posibilities of raising/lowering and keep the least one in
669 // the sense of ex_is_less.
671 size_t numpossibs = 1 << local_var_dummies.size();
672 for (size_t i=0; i<numpossibs; ++i) {
674 for (size_t j=0; j<local_var_dummies.size(); ++j) {
677 ex curr_idx = local_var_dummies[j];
678 ex curr_toggle = ex_to<varidx>(curr_idx).toggle_variance();
679 m[curr_idx] = curr_toggle;
680 m[curr_toggle] = curr_idx;
682 try_e = e.subs(m, subs_options::no_pattern);
684 if(ex_is_less()(try_e, optimal_e))
686 something_changed = true;
692 // If a dummy index is encountered for the first time in the
693 // product, pull it up, otherwise, pull it down
694 exvector::const_iterator it2, it2start, it2end;
695 for (it2start = ex_to<indexed>(e).seq.begin(), it2end = ex_to<indexed>(e).seq.end(), it2 = it2start + 1; it2 != it2end; ++it2) {
696 if (!is_exactly_a<varidx>(*it2))
699 exvector::iterator vit, vitend;
700 for (vit = variant_dummy_indices.begin(), vitend = variant_dummy_indices.end(); vit != vitend; ++vit) {
701 if (it2->op(0).is_equal(vit->op(0))) {
702 if (ex_to<varidx>(*it2).is_covariant()) {
704 *it2 == ex_to<varidx>(*it2).toggle_variance(),
705 ex_to<varidx>(*it2).toggle_variance() == *it2
706 ), subs_options::no_pattern);
707 something_changed = true;
708 it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
709 it2start = ex_to<indexed>(e).seq.begin();
710 it2end = ex_to<indexed>(e).seq.end();
712 moved_indices.push_back(*vit);
713 variant_dummy_indices.erase(vit);
718 for (vit = moved_indices.begin(), vitend = moved_indices.end(); vit != vitend; ++vit) {
719 if (it2->op(0).is_equal(vit->op(0))) {
720 if (ex_to<varidx>(*it2).is_contravariant()) {
721 e = e.subs(*it2 == ex_to<varidx>(*it2).toggle_variance(), subs_options::no_pattern);
722 something_changed = true;
723 it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
724 it2start = ex_to<indexed>(e).seq.begin();
725 it2end = ex_to<indexed>(e).seq.end();
734 return something_changed;
737 /* Ordering that only compares the base expressions of indexed objects. */
738 struct ex_base_is_less : public std::binary_function<ex, ex, bool> {
739 bool operator() (const ex &lh, const ex &rh) const
741 return (is_a<indexed>(lh) ? lh.op(0) : lh).compare(is_a<indexed>(rh) ? rh.op(0) : rh) < 0;
745 /* An auxiliary function used by simplify_indexed() and expand_dummy_sum()
746 * It returns an exvector of factors from the supplied product */
747 static void product_to_exvector(const ex & e, exvector & v, bool & non_commutative)
749 // Remember whether the product was commutative or noncommutative
750 // (because we chop it into factors and need to reassemble later)
751 non_commutative = is_exactly_a<ncmul>(e);
753 // Collect factors in an exvector, store squares twice
754 v.reserve(e.nops() * 2);
756 if (is_exactly_a<power>(e)) {
757 // We only get called for simple squares, split a^2 -> a*a
758 GINAC_ASSERT(e.op(1).is_equal(_ex2));
759 v.push_back(e.op(0));
760 v.push_back(e.op(0));
762 for (size_t i=0; i<e.nops(); i++) {
764 if (is_exactly_a<power>(f) && f.op(1).is_equal(_ex2)) {
765 v.push_back(f.op(0));
766 v.push_back(f.op(0));
767 } else if (is_exactly_a<ncmul>(f)) {
768 // Noncommutative factor found, split it as well
769 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
770 for (size_t j=0; j<f.nops(); j++)
771 v.push_back(f.op(j));
778 template<class T> ex idx_symmetrization(const ex& r,const exvector& local_dummy_indices)
779 { exvector dummy_syms;
780 dummy_syms.reserve(r.nops());
781 for (exvector::const_iterator it = local_dummy_indices.begin(); it != local_dummy_indices.end(); ++it)
782 if(is_exactly_a<T>(*it))
783 dummy_syms.push_back(it->op(0));
784 if(dummy_syms.size() < 2)
786 ex q=symmetrize(r, dummy_syms);
790 // Forward declaration needed in absence of friend injection, C.f. [namespace.memdef]:
791 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
793 /** Simplify product of indexed expressions (commutative, noncommutative and
794 * simple squares), return list of free indices. */
795 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
797 // Collect factors in an exvector
800 // Remember whether the product was commutative or noncommutative
801 // (because we chop it into factors and need to reassemble later)
802 bool non_commutative;
803 product_to_exvector(e, v, non_commutative);
805 // Perform contractions
806 bool something_changed = false;
807 GINAC_ASSERT(v.size() > 1);
808 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
809 for (it1 = v.begin(); it1 != next_to_last; it1++) {
812 if (!is_a<indexed>(*it1))
815 bool first_noncommutative = (it1->return_type() != return_types::commutative);
817 // Indexed factor found, get free indices and look for contraction
819 exvector free1, dummy1;
820 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
822 exvector::iterator it2;
823 for (it2 = it1 + 1; it2 != itend; it2++) {
825 if (!is_a<indexed>(*it2))
828 bool second_noncommutative = (it2->return_type() != return_types::commutative);
830 // Find free indices of second factor and merge them with free
831 // indices of first factor
833 find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
834 un.insert(un.end(), free1.begin(), free1.end());
836 // Check whether the two factors share dummy indices
837 exvector free, dummy;
838 find_free_and_dummy(un, free, dummy);
839 size_t num_dummies = dummy.size();
840 if (num_dummies == 0)
843 // At least one dummy index, is it a defined scalar product?
844 bool contracted = false;
845 if (free.empty() && it1->nops()==2 && it2->nops()==2) {
847 ex dim = minimal_dim(
848 ex_to<idx>(it1->op(1)).get_dim(),
849 ex_to<idx>(it2->op(1)).get_dim()
852 // User-defined scalar product?
853 if (sp.is_defined(*it1, *it2, dim)) {
855 // Yes, substitute it
856 *it1 = sp.evaluate(*it1, *it2, dim);
858 goto contraction_done;
862 // Try to contract the first one with the second one
863 contracted = ex_to<basic>(it1->op(0)).contract_with(it1, it2, v);
866 // That didn't work; maybe the second object knows how to
867 // contract itself with the first one
868 contracted = ex_to<basic>(it2->op(0)).contract_with(it2, it1, v);
872 if (first_noncommutative || second_noncommutative
873 || is_exactly_a<add>(*it1) || is_exactly_a<add>(*it2)
874 || is_exactly_a<mul>(*it1) || is_exactly_a<mul>(*it2)
875 || is_exactly_a<ncmul>(*it1) || is_exactly_a<ncmul>(*it2)) {
877 // One of the factors became a sum or product:
878 // re-expand expression and run again
879 // Non-commutative products are always re-expanded to give
880 // eval_ncmul() the chance to re-order and canonicalize
882 ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
883 return simplify_indexed(r, free_indices, dummy_indices, sp);
886 // Both objects may have new indices now or they might
887 // even not be indexed objects any more, so we have to
889 something_changed = true;
895 // Find free indices (concatenate them all and call find_free_and_dummy())
896 // and all dummy indices that appear
897 exvector un, individual_dummy_indices;
898 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
899 exvector free_indices_of_factor;
900 if (is_a<indexed>(*it1)) {
901 exvector dummy_indices_of_factor;
902 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);
903 individual_dummy_indices.insert(individual_dummy_indices.end(), dummy_indices_of_factor.begin(), dummy_indices_of_factor.end());
905 free_indices_of_factor = it1->get_free_indices();
906 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
908 exvector local_dummy_indices;
909 find_free_and_dummy(un, free_indices, local_dummy_indices);
910 local_dummy_indices.insert(local_dummy_indices.end(), individual_dummy_indices.begin(), individual_dummy_indices.end());
912 // Filter out the dummy indices with variance
913 exvector variant_dummy_indices;
914 find_variant_indices(local_dummy_indices, variant_dummy_indices);
916 // Any indices with variance present at all?
917 if (!variant_dummy_indices.empty()) {
919 // Yes, bring the product into a canonical order that only depends on
920 // the base expressions of indexed objects
921 if (!non_commutative)
922 std::sort(v.begin(), v.end(), ex_base_is_less());
924 exvector moved_indices;
926 // Iterate over all indexed objects in the product
927 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
928 if (!is_a<indexed>(*it1))
931 if (reposition_dummy_indices(*it1, variant_dummy_indices, moved_indices))
932 something_changed = true;
937 if (something_changed)
938 r = non_commutative ? ex(ncmul(v, true)) : ex(mul(v));
942 // The result should be symmetric with respect to exchange of dummy
943 // indices, so if the symmetrization vanishes, the whole expression is
944 // zero. This detects things like eps.i.j.k * p.j * p.k = 0.
945 ex q = idx_symmetrization<idx>(r, local_dummy_indices);
947 free_indices.clear();
950 q = idx_symmetrization<varidx>(q, local_dummy_indices);
952 free_indices.clear();
955 q = idx_symmetrization<spinidx>(q, local_dummy_indices);
957 free_indices.clear();
961 // Dummy index renaming
962 r = rename_dummy_indices<idx>(r, dummy_indices, local_dummy_indices);
963 r = rename_dummy_indices<varidx>(r, dummy_indices, local_dummy_indices);
964 r = rename_dummy_indices<spinidx>(r, dummy_indices, local_dummy_indices);
966 // Product of indexed object with a scalar?
967 if (is_exactly_a<mul>(r) && r.nops() == 2
968 && is_exactly_a<numeric>(r.op(1)) && is_a<indexed>(r.op(0)))
969 return ex_to<basic>(r.op(0).op(0)).scalar_mul_indexed(r.op(0), ex_to<numeric>(r.op(1)));
974 /** This structure stores the original and symmetrized versions of terms
975 * obtained during the simplification of sums. */
978 terminfo(const ex & orig_, const ex & symm_) : orig(orig_), symm(symm_) {}
980 ex orig; /**< original term */
981 ex symm; /**< symmtrized term */
984 class terminfo_is_less {
986 bool operator() (const terminfo & ti1, const terminfo & ti2) const
988 return (ti1.symm.compare(ti2.symm) < 0);
992 /** This structure stores the individual symmetrized terms obtained during
993 * the simplification of sums. */
996 symminfo() : num(0) {}
998 symminfo(const ex & symmterm_, const ex & orig_, size_t num_) : orig(orig_), num(num_)
1000 if (is_exactly_a<mul>(symmterm_) && is_exactly_a<numeric>(symmterm_.op(symmterm_.nops()-1))) {
1001 coeff = symmterm_.op(symmterm_.nops()-1);
1002 symmterm = symmterm_ / coeff;
1005 symmterm = symmterm_;
1009 ex symmterm; /**< symmetrized term */
1010 ex coeff; /**< coefficient of symmetrized term */
1011 ex orig; /**< original term */
1012 size_t num; /**< how many symmetrized terms resulted from the original term */
1015 class symminfo_is_less_by_symmterm {
1017 bool operator() (const symminfo & si1, const symminfo & si2) const
1019 return (si1.symmterm.compare(si2.symmterm) < 0);
1023 class symminfo_is_less_by_orig {
1025 bool operator() (const symminfo & si1, const symminfo & si2) const
1027 return (si1.orig.compare(si2.orig) < 0);
1031 bool hasindex(const ex &x, const ex &sym)
1033 if(is_a<idx>(x) && x.op(0)==sym)
1036 for(size_t i=0; i<x.nops(); ++i)
1037 if(hasindex(x.op(i), sym))
1042 /** Simplify indexed expression, return list of free indices. */
1043 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
1045 // Expand the expression
1046 ex e_expanded = e.expand();
1048 // Simplification of single indexed object: just find the free indices
1049 // and perform dummy index renaming/repositioning
1050 if (is_a<indexed>(e_expanded)) {
1052 // Find the dummy indices
1053 const indexed &i = ex_to<indexed>(e_expanded);
1054 exvector local_dummy_indices;
1055 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
1057 // Filter out the dummy indices with variance
1058 exvector variant_dummy_indices;
1059 find_variant_indices(local_dummy_indices, variant_dummy_indices);
1061 // Any indices with variance present at all?
1062 if (!variant_dummy_indices.empty()) {
1064 // Yes, reposition them
1065 exvector moved_indices;
1066 reposition_dummy_indices(e_expanded, variant_dummy_indices, moved_indices);
1069 // Rename the dummy indices
1070 e_expanded = rename_dummy_indices<idx>(e_expanded, dummy_indices, local_dummy_indices);
1071 e_expanded = rename_dummy_indices<varidx>(e_expanded, dummy_indices, local_dummy_indices);
1072 e_expanded = rename_dummy_indices<spinidx>(e_expanded, dummy_indices, local_dummy_indices);
1076 // Simplification of sum = sum of simplifications, check consistency of
1077 // free indices in each term
1078 if (is_exactly_a<add>(e_expanded)) {
1081 free_indices.clear();
1083 for (size_t i=0; i<e_expanded.nops(); i++) {
1084 exvector free_indices_of_term;
1085 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
1086 if (!term.is_zero()) {
1088 free_indices = free_indices_of_term;
1092 if (!indices_consistent(free_indices, free_indices_of_term)) {
1093 std::ostringstream s;
1094 s << "simplify_indexed: inconsistent indices in sum: ";
1095 s << exprseq(free_indices) << " vs. " << exprseq(free_indices_of_term);
1096 throw (std::runtime_error(s.str()));
1098 if (is_a<indexed>(sum) && is_a<indexed>(term))
1099 sum = ex_to<basic>(sum.op(0)).add_indexed(sum, term);
1106 // If the sum turns out to be zero, we are finished
1107 if (sum.is_zero()) {
1108 free_indices.clear();
1112 // More than one term and more than one dummy index?
1113 size_t num_terms_orig = (is_exactly_a<add>(sum) ? sum.nops() : 1);
1114 if (num_terms_orig < 2 || dummy_indices.size() < 2)
1117 // Chop the sum into terms and symmetrize each one over the dummy
1119 std::vector<terminfo> terms;
1120 for (size_t i=0; i<sum.nops(); i++) {
1121 const ex & term = sum.op(i);
1122 exvector dummy_indices_of_term;
1123 dummy_indices_of_term.reserve(dummy_indices.size());
1124 for(exvector::iterator i=dummy_indices.begin(); i!=dummy_indices.end(); ++i)
1125 if(hasindex(term,i->op(0)))
1126 dummy_indices_of_term.push_back(*i);
1127 ex term_symm = idx_symmetrization<idx>(term, dummy_indices_of_term);
1128 term_symm = idx_symmetrization<varidx>(term_symm, dummy_indices_of_term);
1129 term_symm = idx_symmetrization<spinidx>(term_symm, dummy_indices_of_term);
1130 if (term_symm.is_zero())
1132 terms.push_back(terminfo(term, term_symm));
1135 // Sort by symmetrized terms
1136 std::sort(terms.begin(), terms.end(), terminfo_is_less());
1138 // Combine equal symmetrized terms
1139 std::vector<terminfo> terms_pass2;
1140 for (std::vector<terminfo>::const_iterator i=terms.begin(); i!=terms.end(); ) {
1142 std::vector<terminfo>::const_iterator j = i + 1;
1143 while (j != terms.end() && j->symm == i->symm) {
1147 terms_pass2.push_back(terminfo(i->orig * num, i->symm * num));
1151 // If there is only one term left, we are finished
1152 if (terms_pass2.size() == 1)
1153 return terms_pass2[0].orig;
1155 // Chop the symmetrized terms into subterms
1156 std::vector<symminfo> sy;
1157 for (std::vector<terminfo>::const_iterator i=terms_pass2.begin(); i!=terms_pass2.end(); ++i) {
1158 if (is_exactly_a<add>(i->symm)) {
1159 size_t num = i->symm.nops();
1160 for (size_t j=0; j<num; j++)
1161 sy.push_back(symminfo(i->symm.op(j), i->orig, num));
1163 sy.push_back(symminfo(i->symm, i->orig, 1));
1166 // Sort by symmetrized subterms
1167 std::sort(sy.begin(), sy.end(), symminfo_is_less_by_symmterm());
1169 // Combine equal symmetrized subterms
1170 std::vector<symminfo> sy_pass2;
1172 for (std::vector<symminfo>::const_iterator i=sy.begin(); i!=sy.end(); ) {
1174 // Combine equal terms
1175 std::vector<symminfo>::const_iterator j = i + 1;
1176 if (j != sy.end() && j->symmterm == i->symmterm) {
1178 // More than one term, collect the coefficients
1179 ex coeff = i->coeff;
1180 while (j != sy.end() && j->symmterm == i->symmterm) {
1185 // Add combined term to result
1186 if (!coeff.is_zero())
1187 result.push_back(coeff * i->symmterm);
1191 // Single term, store for second pass
1192 sy_pass2.push_back(*i);
1198 // Were there any remaining terms that didn't get combined?
1199 if (sy_pass2.size() > 0) {
1201 // Yes, sort by their original terms
1202 std::sort(sy_pass2.begin(), sy_pass2.end(), symminfo_is_less_by_orig());
1204 for (std::vector<symminfo>::const_iterator i=sy_pass2.begin(); i!=sy_pass2.end(); ) {
1206 // How many symmetrized terms of this original term are left?
1208 std::vector<symminfo>::const_iterator j = i + 1;
1209 while (j != sy_pass2.end() && j->orig == i->orig) {
1214 if (num == i->num) {
1216 // All terms left, then add the original term to the result
1217 result.push_back(i->orig);
1221 // Some terms were combined with others, add up the remaining symmetrized terms
1222 std::vector<symminfo>::const_iterator k;
1223 for (k=i; k!=j; k++)
1224 result.push_back(k->coeff * k->symmterm);
1231 // Add all resulting terms
1232 ex sum_symm = (new add(result))->setflag(status_flags::dynallocated);
1233 if (sum_symm.is_zero())
1234 free_indices.clear();
1238 // Simplification of products
1239 if (is_exactly_a<mul>(e_expanded)
1240 || is_exactly_a<ncmul>(e_expanded)
1241 || (is_exactly_a<power>(e_expanded) && is_a<indexed>(e_expanded.op(0)) && e_expanded.op(1).is_equal(_ex2)))
1242 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
1244 // Cannot do anything
1245 free_indices.clear();
1249 /** Simplify/canonicalize expression containing indexed objects. This
1250 * performs contraction of dummy indices where possible and checks whether
1251 * the free indices in sums are consistent.
1253 * @param options Simplification options (currently unused)
1254 * @return simplified expression */
1255 ex ex::simplify_indexed(unsigned options) const
1257 exvector free_indices, dummy_indices;
1259 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1262 /** Simplify/canonicalize expression containing indexed objects. This
1263 * performs contraction of dummy indices where possible, checks whether
1264 * the free indices in sums are consistent, and automatically replaces
1265 * scalar products by known values if desired.
1267 * @param sp Scalar products to be replaced automatically
1268 * @param options Simplification options (currently unused)
1269 * @return simplified expression */
1270 ex ex::simplify_indexed(const scalar_products & sp, unsigned options) const
1272 exvector free_indices, dummy_indices;
1273 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1276 /** Symmetrize expression over its free indices. */
1277 ex ex::symmetrize() const
1279 return GiNaC::symmetrize(*this, get_free_indices());
1282 /** Antisymmetrize expression over its free indices. */
1283 ex ex::antisymmetrize() const
1285 return GiNaC::antisymmetrize(*this, get_free_indices());
1288 /** Symmetrize expression by cyclic permutation over its free indices. */
1289 ex ex::symmetrize_cyclic() const
1291 return GiNaC::symmetrize_cyclic(*this, get_free_indices());
1298 spmapkey::spmapkey(const ex & v1_, const ex & v2_, const ex & dim_) : dim(dim_)
1300 // If indexed, extract base objects
1301 ex s1 = is_a<indexed>(v1_) ? v1_.op(0) : v1_;
1302 ex s2 = is_a<indexed>(v2_) ? v2_.op(0) : v2_;
1304 // Enforce canonical order in pair
1305 if (s1.compare(s2) > 0) {
1314 bool spmapkey::operator==(const spmapkey &other) const
1316 if (!v1.is_equal(other.v1))
1318 if (!v2.is_equal(other.v2))
1320 if (is_a<wildcard>(dim) || is_a<wildcard>(other.dim))
1323 return dim.is_equal(other.dim);
1326 bool spmapkey::operator<(const spmapkey &other) const
1328 int cmp = v1.compare(other.v1);
1331 cmp = v2.compare(other.v2);
1335 // Objects are equal, now check dimensions
1336 if (is_a<wildcard>(dim) || is_a<wildcard>(other.dim))
1339 return dim.compare(other.dim) < 0;
1342 void spmapkey::debugprint() const
1344 std::cerr << "(" << v1 << "," << v2 << "," << dim << ")";
1347 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
1349 spm[spmapkey(v1, v2)] = sp;
1352 void scalar_products::add(const ex & v1, const ex & v2, const ex & dim, const ex & sp)
1354 spm[spmapkey(v1, v2, dim)] = sp;
1357 void scalar_products::add_vectors(const lst & l, const ex & dim)
1359 // Add all possible pairs of products
1360 for (lst::const_iterator it1 = l.begin(); it1 != l.end(); ++it1)
1361 for (lst::const_iterator it2 = l.begin(); it2 != l.end(); ++it2)
1362 add(*it1, *it2, *it1 * *it2);
1365 void scalar_products::clear()
1370 /** Check whether scalar product pair is defined. */
1371 bool scalar_products::is_defined(const ex & v1, const ex & v2, const ex & dim) const
1373 return spm.find(spmapkey(v1, v2, dim)) != spm.end();
1376 /** Return value of defined scalar product pair. */
1377 ex scalar_products::evaluate(const ex & v1, const ex & v2, const ex & dim) const
1379 return spm.find(spmapkey(v1, v2, dim))->second;
1382 void scalar_products::debugprint() const
1384 std::cerr << "map size=" << spm.size() << std::endl;
1385 spmap::const_iterator i = spm.begin(), end = spm.end();
1387 const spmapkey & k = i->first;
1388 std::cerr << "item key=";
1390 std::cerr << ", value=" << i->second << std::endl;
1395 exvector get_all_dummy_indices_safely(const ex & e)
1397 if (is_a<indexed>(e))
1398 return ex_to<indexed>(e).get_dummy_indices();
1399 else if (is_a<power>(e) && e.op(1)==2) {
1400 return e.op(0).get_free_indices();
1402 else if (is_a<mul>(e) || is_a<ncmul>(e)) {
1404 exvector free_indices;
1405 for (int i=0; i<e.nops(); ++i) {
1406 exvector dummies_of_factor = get_all_dummy_indices_safely(e.op(i));
1407 dummies.insert(dummies.end(), dummies_of_factor.begin(),
1408 dummies_of_factor.end());
1409 exvector free_of_factor = e.op(i).get_free_indices();
1410 free_indices.insert(free_indices.begin(), free_of_factor.begin(),
1411 free_of_factor.end());
1413 exvector free_out, dummy_out;
1414 find_free_and_dummy(free_indices.begin(), free_indices.end(), free_out,
1416 dummies.insert(dummies.end(), dummy_out.begin(), dummy_out.end());
1419 else if(is_a<add>(e)) {
1421 for(int i=0; i<e.nops(); ++i) {
1422 exvector dummies_of_term = get_all_dummy_indices_safely(e.op(i));
1423 sort(dummies_of_term.begin(), dummies_of_term.end());
1425 set_union(result.begin(), result.end(), dummies_of_term.begin(),
1426 dummies_of_term.end(), std::back_inserter<exvector>(new_vec),
1428 result.swap(new_vec);
1435 /** Returns all dummy indices from the exvector */
1436 exvector get_all_dummy_indices(const ex & e)
1440 product_to_exvector(e, p, nc);
1441 exvector::const_iterator ip = p.begin(), ipend = p.end();
1443 while (ip != ipend) {
1444 if (is_a<indexed>(*ip)) {
1445 v1 = ex_to<indexed>(*ip).get_dummy_indices();
1446 v.insert(v.end(), v1.begin(), v1.end());
1447 exvector::const_iterator ip1 = ip+1;
1448 while (ip1 != ipend) {
1449 if (is_a<indexed>(*ip1)) {
1450 v1 = ex_to<indexed>(*ip).get_dummy_indices(ex_to<indexed>(*ip1));
1451 v.insert(v.end(), v1.begin(), v1.end());
1461 lst rename_dummy_indices_uniquely(const exvector & va, const exvector & vb)
1463 exvector common_indices;
1464 set_intersection(va.begin(), va.end(), vb.begin(), vb.end(), std::back_insert_iterator<exvector>(common_indices), ex_is_less());
1465 if (common_indices.empty()) {
1466 return lst(lst(), lst());
1468 exvector new_indices, old_indices;
1469 old_indices.reserve(2*common_indices.size());
1470 new_indices.reserve(2*common_indices.size());
1471 exvector::const_iterator ip = common_indices.begin(), ipend = common_indices.end();
1472 while (ip != ipend) {
1473 ex newsym=(new symbol)->setflag(status_flags::dynallocated);
1475 if(is_exactly_a<spinidx>(*ip))
1476 newidx = (new spinidx(newsym, ex_to<spinidx>(*ip).get_dim(),
1477 ex_to<spinidx>(*ip).is_covariant(),
1478 ex_to<spinidx>(*ip).is_dotted()))
1479 -> setflag(status_flags::dynallocated);
1480 else if (is_exactly_a<varidx>(*ip))
1481 newidx = (new varidx(newsym, ex_to<varidx>(*ip).get_dim(),
1482 ex_to<varidx>(*ip).is_covariant()))
1483 -> setflag(status_flags::dynallocated);
1485 newidx = (new idx(newsym, ex_to<idx>(*ip).get_dim()))
1486 -> setflag(status_flags::dynallocated);
1487 old_indices.push_back(*ip);
1488 new_indices.push_back(newidx);
1489 if(is_a<varidx>(*ip)) {
1490 old_indices.push_back(ex_to<varidx>(*ip).toggle_variance());
1491 new_indices.push_back(ex_to<varidx>(newidx).toggle_variance());
1495 return lst(lst(old_indices.begin(), old_indices.end()), lst(new_indices.begin(), new_indices.end()));
1499 ex rename_dummy_indices_uniquely(const exvector & va, const exvector & vb, const ex & b)
1501 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1502 return (indices_subs.op(0).nops()>0 ? b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming) : b);
1505 ex rename_dummy_indices_uniquely(const ex & a, const ex & b)
1507 exvector va = get_all_dummy_indices_safely(a);
1508 if (va.size() > 0) {
1509 exvector vb = get_all_dummy_indices_safely(b);
1510 if (vb.size() > 0) {
1511 sort(va.begin(), va.end(), ex_is_less());
1512 sort(vb.begin(), vb.end(), ex_is_less());
1513 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1514 if (indices_subs.op(0).nops() > 0)
1515 return b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming);
1521 ex rename_dummy_indices_uniquely(exvector & va, const ex & b, bool modify_va)
1523 if (va.size() > 0) {
1524 exvector vb = get_all_dummy_indices_safely(b);
1525 if (vb.size() > 0) {
1526 sort(vb.begin(), vb.end(), ex_is_less());
1527 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1528 if (indices_subs.op(0).nops() > 0) {
1530 for (lst::const_iterator i = ex_to<lst>(indices_subs.op(1)).begin(); i != ex_to<lst>(indices_subs.op(1)).end(); ++i)
1532 exvector uncommon_indices;
1533 set_difference(vb.begin(), vb.end(), indices_subs.op(0).begin(), indices_subs.op(0).end(), std::back_insert_iterator<exvector>(uncommon_indices), ex_is_less());
1534 exvector::const_iterator ip = uncommon_indices.begin(), ipend = uncommon_indices.end();
1535 while (ip != ipend) {
1539 sort(va.begin(), va.end(), ex_is_less());
1541 return b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming);
1548 ex expand_dummy_sum(const ex & e, bool subs_idx)
1550 ex e_expanded = e.expand();
1551 pointer_to_map_function_1arg<bool> fcn(expand_dummy_sum, subs_idx);
1552 if (is_a<add>(e_expanded) || is_a<lst>(e_expanded) || is_a<matrix>(e_expanded)) {
1553 return e_expanded.map(fcn);
1554 } else if (is_a<ncmul>(e_expanded) || is_a<mul>(e_expanded) || is_a<power>(e_expanded) || is_a<indexed>(e_expanded)) {
1556 if (is_a<indexed>(e_expanded))
1557 v = ex_to<indexed>(e_expanded).get_dummy_indices();
1559 v = get_all_dummy_indices(e_expanded);
1560 ex result = e_expanded;
1561 for(exvector::const_iterator it=v.begin(); it!=v.end(); ++it) {
1563 if (ex_to<idx>(nu).get_dim().info(info_flags::nonnegint)) {
1564 int idim = ex_to<numeric>(ex_to<idx>(nu).get_dim()).to_int();
1566 for (int i=0; i < idim; i++) {
1567 if (subs_idx && is_a<varidx>(nu)) {
1568 ex other = ex_to<varidx>(nu).toggle_variance();
1569 en += result.subs(lst(
1571 other == idx(i, idim)
1574 en += result.subs( nu.op(0) == i );
1586 } // namespace GiNaC