3 * Implementation of GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2011 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
29 #include "relational.h"
31 #include "operators.h"
47 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(indexed, exprseq,
48 print_func<print_context>(&indexed::do_print).
49 print_func<print_latex>(&indexed::do_print_latex).
50 print_func<print_tree>(&indexed::do_print_tree))
53 // default constructor
56 indexed::indexed() : symtree(not_symmetric())
64 indexed::indexed(const ex & b) : inherited(b), symtree(not_symmetric())
69 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(not_symmetric())
74 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(not_symmetric())
79 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(not_symmetric())
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())
89 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(symm)
94 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(symm)
99 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 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(not_symmetric())
106 seq.insert(seq.end(), v.begin(), v.end());
110 indexed::indexed(const ex & b, const symmetry & symm, const exvector & v) : inherited(b), symtree(symm)
112 seq.insert(seq.end(), v.begin(), v.end());
116 indexed::indexed(const symmetry & symm, const exprseq & es) : inherited(es), symtree(symm)
120 indexed::indexed(const symmetry & symm, const exvector & v, bool discardable) : inherited(v, discardable), symtree(symm)
124 indexed::indexed(const symmetry & symm, std::auto_ptr<exvector> vp) : inherited(vp), symtree(symm)
132 void indexed::read_archive(const archive_node &n, lst &sym_lst)
134 inherited::read_archive(n, sym_lst);
135 if (!n.find_ex("symmetry", symtree, sym_lst)) {
136 // GiNaC versions <= 0.9.0 had an unsigned "symmetry" property
138 n.find_unsigned("symmetry", symm);
147 symtree = not_symmetric();
150 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
153 GINAC_BIND_UNARCHIVER(indexed);
155 void indexed::archive(archive_node &n) const
157 inherited::archive(n);
158 n.add_ex("symmetry", symtree);
162 // functions overriding virtual functions from base classes
165 void indexed::printindices(const print_context & c, unsigned level) const
167 if (seq.size() > 1) {
169 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
171 if (is_a<print_latex>(c)) {
173 // TeX output: group by variance
175 bool covariant = true;
177 while (it != itend) {
178 bool cur_covariant = (is_a<varidx>(*it) ? ex_to<varidx>(*it).is_covariant() : true);
179 if (first || cur_covariant != covariant) { // Variance changed
180 // The empty {} prevents indices from ending up on top of each other
183 covariant = cur_covariant;
199 while (it != itend) {
207 void indexed::print_indexed(const print_context & c, const char *openbrace, const char *closebrace, unsigned level) const
209 if (precedence() <= level)
210 c.s << openbrace << '(';
212 seq[0].print(c, precedence());
214 printindices(c, level);
215 if (precedence() <= level)
216 c.s << ')' << closebrace;
219 void indexed::do_print(const print_context & c, unsigned level) const
221 print_indexed(c, "", "", level);
224 void indexed::do_print_latex(const print_latex & c, unsigned level) const
226 print_indexed(c, "{", "}", level);
229 void indexed::do_print_tree(const print_tree & c, unsigned level) const
231 c.s << std::string(level, ' ') << class_name() << " @" << this
232 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
233 << ", " << seq.size()-1 << " indices"
234 << ", symmetry=" << symtree << std::endl;
235 seq[0].print(c, level + c.delta_indent);
236 printindices(c, level + c.delta_indent);
239 bool indexed::info(unsigned inf) const
241 if (inf == info_flags::indexed) return true;
242 if (inf == info_flags::has_indices) return seq.size() > 1;
243 return inherited::info(inf);
246 struct idx_is_not : public std::binary_function<ex, unsigned, bool> {
247 bool operator() (const ex & e, unsigned inf) const {
248 return !(ex_to<idx>(e).get_value().info(inf));
252 bool indexed::all_index_values_are(unsigned inf) const
254 // No indices? Then no property can be fulfilled
259 return find_if(seq.begin() + 1, seq.end(), bind2nd(idx_is_not(), inf)) == seq.end();
262 int indexed::compare_same_type(const basic & other) const
264 GINAC_ASSERT(is_a<indexed>(other));
265 return inherited::compare_same_type(other);
268 ex indexed::eval(int level) const
270 // First evaluate children, then we will end up here again
272 return indexed(ex_to<symmetry>(symtree), evalchildren(level));
274 const ex &base = seq[0];
276 // If the base object is 0, the whole object is 0
280 // If the base object is a product, pull out the numeric factor
281 if (is_exactly_a<mul>(base) && is_exactly_a<numeric>(base.op(base.nops() - 1))) {
283 ex f = ex_to<numeric>(base.op(base.nops() - 1));
285 return f * thiscontainer(v);
288 if((typeid(*this) == typeid(indexed)) && seq.size()==1)
291 // Canonicalize indices according to the symmetry properties
292 if (seq.size() > 2) {
294 GINAC_ASSERT(is_exactly_a<symmetry>(symtree));
295 int sig = canonicalize(v.begin() + 1, ex_to<symmetry>(symtree));
296 if (sig != std::numeric_limits<int>::max()) {
297 // Something has changed while sorting indices, more evaluations later
300 return ex(sig) * thiscontainer(v);
304 // Let the class of the base object perform additional evaluations
305 return ex_to<basic>(base).eval_indexed(*this);
308 ex indexed::real_part() const
310 if(op(0).info(info_flags::real))
312 return real_part_function(*this).hold();
315 ex indexed::imag_part() const
317 if(op(0).info(info_flags::real))
319 return imag_part_function(*this).hold();
322 ex indexed::thiscontainer(const exvector & v) const
324 return indexed(ex_to<symmetry>(symtree), v);
327 ex indexed::thiscontainer(std::auto_ptr<exvector> vp) const
329 return indexed(ex_to<symmetry>(symtree), vp);
332 unsigned indexed::return_type() const
334 if(is_a<matrix>(op(0)))
335 return return_types::commutative;
337 return op(0).return_type();
340 ex indexed::expand(unsigned options) const
342 GINAC_ASSERT(seq.size() > 0);
344 if (options & expand_options::expand_indexed) {
345 ex newbase = seq[0].expand(options);
346 if (is_exactly_a<add>(newbase)) {
348 for (size_t i=0; i<newbase.nops(); i++) {
350 s[0] = newbase.op(i);
351 sum += thiscontainer(s).expand(options);
355 if (!are_ex_trivially_equal(newbase, seq[0])) {
358 return ex_to<indexed>(thiscontainer(s)).inherited::expand(options);
361 return inherited::expand(options);
365 // virtual functions which can be overridden by derived classes
371 // non-virtual functions in this class
374 /** Check whether all indices are of class idx and validate the symmetry
375 * tree. This function is used internally to make sure that all constructed
376 * indexed objects really carry indices and not some other classes. */
377 void indexed::validate() const
379 GINAC_ASSERT(seq.size() > 0);
380 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
381 while (it != itend) {
383 throw(std::invalid_argument("indices of indexed object must be of type idx"));
387 if (!symtree.is_zero()) {
388 if (!is_exactly_a<symmetry>(symtree))
389 throw(std::invalid_argument("symmetry of indexed object must be of type symmetry"));
390 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
394 /** Implementation of ex::diff() for an indexed object always returns 0.
397 ex indexed::derivative(const symbol & s) const
406 struct idx_is_equal_ignore_dim : public std::binary_function<ex, ex, bool> {
407 bool operator() (const ex &lh, const ex &rh) const
413 // Replacing the dimension might cause an error (e.g. with
414 // index classes that only work in a fixed number of dimensions)
415 return lh.is_equal(ex_to<idx>(rh).replace_dim(ex_to<idx>(lh).get_dim()));
422 /** Check whether two sorted index vectors are consistent (i.e. equal). */
423 static bool indices_consistent(const exvector & v1, const exvector & v2)
425 // Number of indices must be the same
426 if (v1.size() != v2.size())
429 return equal(v1.begin(), v1.end(), v2.begin(), idx_is_equal_ignore_dim());
432 exvector indexed::get_indices() const
434 GINAC_ASSERT(seq.size() >= 1);
435 return exvector(seq.begin() + 1, seq.end());
438 exvector indexed::get_dummy_indices() const
440 exvector free_indices, dummy_indices;
441 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
442 return dummy_indices;
445 exvector indexed::get_dummy_indices(const indexed & other) const
447 exvector indices = get_free_indices();
448 exvector other_indices = other.get_free_indices();
449 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
450 exvector dummy_indices;
451 find_dummy_indices(indices, dummy_indices);
452 return dummy_indices;
455 bool indexed::has_dummy_index_for(const ex & i) const
457 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
458 while (it != itend) {
459 if (is_dummy_pair(*it, i))
466 exvector indexed::get_free_indices() const
468 exvector free_indices, dummy_indices;
469 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
473 exvector add::get_free_indices() const
475 exvector free_indices;
476 for (size_t i=0; i<nops(); i++) {
478 free_indices = op(i).get_free_indices();
480 exvector free_indices_of_term = op(i).get_free_indices();
481 if (!indices_consistent(free_indices, free_indices_of_term))
482 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
488 exvector mul::get_free_indices() const
490 // Concatenate free indices of all factors
492 for (size_t 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 ncmul::get_free_indices() const
505 // Concatenate free indices of all factors
507 for (size_t i=0; i<nops(); i++) {
508 exvector free_indices_of_factor = op(i).get_free_indices();
509 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
512 // And remove the dummy indices
513 exvector free_indices, dummy_indices;
514 find_free_and_dummy(un, free_indices, dummy_indices);
518 struct is_summation_idx : public std::unary_function<ex, bool> {
519 bool operator()(const ex & e)
521 return is_dummy_pair(e, e);
525 exvector integral::get_free_indices() const
527 if (a.get_free_indices().size() || b.get_free_indices().size())
528 throw (std::runtime_error("integral::get_free_indices: boundary values should not have free indices"));
529 return f.get_free_indices();
532 template<class T> size_t number_of_type(const exvector&v)
535 for(exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
536 if(is_exactly_a<T>(*i))
541 /** Rename dummy indices in an expression.
543 * @param e Expression to work on
544 * @param local_dummy_indices The set of dummy indices that appear in the
546 * @param global_dummy_indices The set of dummy indices that have appeared
547 * before and which we would like to use in "e", too. This gets updated
549 template<class T> static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
551 size_t global_size = number_of_type<T>(global_dummy_indices),
552 local_size = number_of_type<T>(local_dummy_indices);
554 // Any local dummy indices at all?
558 if (global_size < local_size) {
560 // More local indices than we encountered before, add the new ones
562 size_t old_global_size = global_size;
563 int remaining = local_size - global_size;
564 exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
565 while (it != itend && remaining > 0) {
566 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()) {
567 global_dummy_indices.push_back(*it);
574 // If this is the first set of local indices, do nothing
575 if (old_global_size == 0)
578 GINAC_ASSERT(local_size <= global_size);
580 // Construct vectors of index symbols
581 exvector local_syms, global_syms;
582 local_syms.reserve(local_size);
583 global_syms.reserve(local_size);
584 for (size_t i=0; local_syms.size()!=local_size; i++)
585 if(is_exactly_a<T>(local_dummy_indices[i]))
586 local_syms.push_back(local_dummy_indices[i].op(0));
587 shaker_sort(local_syms.begin(), local_syms.end(), ex_is_less(), ex_swap());
588 for (size_t i=0; global_syms.size()!=local_size; i++) // don't use more global symbols than necessary
589 if(is_exactly_a<T>(global_dummy_indices[i]))
590 global_syms.push_back(global_dummy_indices[i].op(0));
591 shaker_sort(global_syms.begin(), global_syms.end(), ex_is_less(), ex_swap());
593 // Remove common indices
594 exvector local_uniq, global_uniq;
595 set_difference(local_syms.begin(), local_syms.end(), global_syms.begin(), global_syms.end(), std::back_insert_iterator<exvector>(local_uniq), ex_is_less());
596 set_difference(global_syms.begin(), global_syms.end(), local_syms.begin(), local_syms.end(), std::back_insert_iterator<exvector>(global_uniq), ex_is_less());
598 // Replace remaining non-common local index symbols by global ones
599 if (local_uniq.empty())
602 while (global_uniq.size() > local_uniq.size())
603 global_uniq.pop_back();
604 return e.subs(lst(local_uniq.begin(), local_uniq.end()), lst(global_uniq.begin(), global_uniq.end()), subs_options::no_pattern);
608 /** Given a set of indices, extract those of class varidx. */
609 static void find_variant_indices(const exvector & v, exvector & variant_indices)
611 exvector::const_iterator it1, itend;
612 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
613 if (is_exactly_a<varidx>(*it1))
614 variant_indices.push_back(*it1);
618 /** Raise/lower dummy indices in a single indexed objects to canonicalize their
621 * @param e Object to work on
622 * @param variant_dummy_indices The set of indices that might need repositioning (will be changed by this function)
623 * @param moved_indices The set of indices that have been repositioned (will be changed by this function)
624 * @return true if 'e' was changed */
625 bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices)
627 bool something_changed = false;
629 // Find dummy symbols that occur twice in the same indexed object.
630 exvector local_var_dummies;
631 local_var_dummies.reserve(e.nops()/2);
632 for (size_t i=1; i<e.nops(); ++i) {
633 if (!is_a<varidx>(e.op(i)))
635 for (size_t j=i+1; j<e.nops(); ++j) {
636 if (is_dummy_pair(e.op(i), e.op(j))) {
637 local_var_dummies.push_back(e.op(i));
638 for (exvector::iterator k = variant_dummy_indices.begin();
639 k!=variant_dummy_indices.end(); ++k) {
640 if (e.op(i).op(0) == k->op(0)) {
641 variant_dummy_indices.erase(k);
650 // In the case where a dummy symbol occurs twice in the same indexed object
651 // we try all posibilities of raising/lowering and keep the least one in
652 // the sense of ex_is_less.
654 size_t numpossibs = 1 << local_var_dummies.size();
655 for (size_t i=0; i<numpossibs; ++i) {
657 for (size_t j=0; j<local_var_dummies.size(); ++j) {
660 ex curr_idx = local_var_dummies[j];
661 ex curr_toggle = ex_to<varidx>(curr_idx).toggle_variance();
662 m[curr_idx] = curr_toggle;
663 m[curr_toggle] = curr_idx;
665 try_e = e.subs(m, subs_options::no_pattern);
667 if(ex_is_less()(try_e, optimal_e))
669 something_changed = true;
674 if (!is_a<indexed>(e))
677 exvector seq = ex_to<indexed>(e).seq;
679 // If a dummy index is encountered for the first time in the
680 // product, pull it up, otherwise, pull it down
681 for (exvector::iterator it2 = seq.begin()+1, it2end = seq.end();
682 it2 != it2end; ++it2) {
683 if (!is_exactly_a<varidx>(*it2))
686 exvector::iterator vit, vitend;
687 for (vit = variant_dummy_indices.begin(), vitend = variant_dummy_indices.end(); vit != vitend; ++vit) {
688 if (it2->op(0).is_equal(vit->op(0))) {
689 if (ex_to<varidx>(*it2).is_covariant()) {
691 * N.B. we don't want to use
694 * *it2 == ex_to<varidx>(*it2).toggle_variance(),
695 * ex_to<varidx>(*it2).toggle_variance() == *it2
696 * ), subs_options::no_pattern);
698 * since this can trigger non-trivial repositioning of indices,
699 * e.g. due to non-trivial symmetry properties of e, thus
700 * invalidating iterators
702 *it2 = ex_to<varidx>(*it2).toggle_variance();
703 something_changed = true;
705 moved_indices.push_back(*vit);
706 variant_dummy_indices.erase(vit);
711 for (vit = moved_indices.begin(), vitend = moved_indices.end(); vit != vitend; ++vit) {
712 if (it2->op(0).is_equal(vit->op(0))) {
713 if (ex_to<varidx>(*it2).is_contravariant()) {
714 *it2 = ex_to<varidx>(*it2).toggle_variance();
715 something_changed = true;
724 if (something_changed)
725 e = ex_to<indexed>(e).thiscontainer(seq);
727 return something_changed;
730 /* Ordering that only compares the base expressions of indexed objects. */
731 struct ex_base_is_less : public std::binary_function<ex, ex, bool> {
732 bool operator() (const ex &lh, const ex &rh) const
734 return (is_a<indexed>(lh) ? lh.op(0) : lh).compare(is_a<indexed>(rh) ? rh.op(0) : rh) < 0;
738 /* An auxiliary function used by simplify_indexed() and expand_dummy_sum()
739 * It returns an exvector of factors from the supplied product */
740 static void product_to_exvector(const ex & e, exvector & v, bool & non_commutative)
742 // Remember whether the product was commutative or noncommutative
743 // (because we chop it into factors and need to reassemble later)
744 non_commutative = is_exactly_a<ncmul>(e);
746 // Collect factors in an exvector, store squares twice
747 v.reserve(e.nops() * 2);
749 if (is_exactly_a<power>(e)) {
750 // We only get called for simple squares, split a^2 -> a*a
751 GINAC_ASSERT(e.op(1).is_equal(_ex2));
752 v.push_back(e.op(0));
753 v.push_back(e.op(0));
755 for (size_t i=0; i<e.nops(); i++) {
757 if (is_exactly_a<power>(f) && f.op(1).is_equal(_ex2)) {
758 v.push_back(f.op(0));
759 v.push_back(f.op(0));
760 } else if (is_exactly_a<ncmul>(f)) {
761 // Noncommutative factor found, split it as well
762 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
763 for (size_t j=0; j<f.nops(); j++)
764 v.push_back(f.op(j));
771 template<class T> ex idx_symmetrization(const ex& r,const exvector& local_dummy_indices)
772 { exvector dummy_syms;
773 dummy_syms.reserve(r.nops());
774 for (exvector::const_iterator it = local_dummy_indices.begin(); it != local_dummy_indices.end(); ++it)
775 if(is_exactly_a<T>(*it))
776 dummy_syms.push_back(it->op(0));
777 if(dummy_syms.size() < 2)
779 ex q=symmetrize(r, dummy_syms);
783 // Forward declaration needed in absence of friend injection, C.f. [namespace.memdef]:
784 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
786 /** Simplify product of indexed expressions (commutative, noncommutative and
787 * simple squares), return list of free indices. */
788 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
790 // Collect factors in an exvector
793 // Remember whether the product was commutative or noncommutative
794 // (because we chop it into factors and need to reassemble later)
795 bool non_commutative;
796 product_to_exvector(e, v, non_commutative);
798 // Perform contractions
799 bool something_changed = false;
800 bool has_nonsymmetric = false;
801 GINAC_ASSERT(v.size() > 1);
802 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
803 for (it1 = v.begin(); it1 != next_to_last; it1++) {
806 if (!is_a<indexed>(*it1))
809 bool first_noncommutative = (it1->return_type() != return_types::commutative);
810 bool first_nonsymmetric = ex_to<symmetry>(ex_to<indexed>(*it1).get_symmetry()).has_nonsymmetric();
812 // Indexed factor found, get free indices and look for contraction
814 exvector free1, dummy1;
815 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
817 exvector::iterator it2;
818 for (it2 = it1 + 1; it2 != itend; it2++) {
820 if (!is_a<indexed>(*it2))
823 bool second_noncommutative = (it2->return_type() != return_types::commutative);
825 // Find free indices of second factor and merge them with free
826 // indices of first factor
828 find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
829 un.insert(un.end(), free1.begin(), free1.end());
831 // Check whether the two factors share dummy indices
832 exvector free, dummy;
833 find_free_and_dummy(un, free, dummy);
834 size_t num_dummies = dummy.size();
835 if (num_dummies == 0)
838 // At least one dummy index, is it a defined scalar product?
839 bool contracted = false;
840 if (free.empty() && it1->nops()==2 && it2->nops()==2) {
842 ex dim = minimal_dim(
843 ex_to<idx>(it1->op(1)).get_dim(),
844 ex_to<idx>(it2->op(1)).get_dim()
847 // User-defined scalar product?
848 if (sp.is_defined(*it1, *it2, dim)) {
850 // Yes, substitute it
851 *it1 = sp.evaluate(*it1, *it2, dim);
853 goto contraction_done;
857 // Try to contract the first one with the second one
858 contracted = ex_to<basic>(it1->op(0)).contract_with(it1, it2, v);
861 // That didn't work; maybe the second object knows how to
862 // contract itself with the first one
863 contracted = ex_to<basic>(it2->op(0)).contract_with(it2, it1, v);
867 if (first_noncommutative || second_noncommutative
868 || is_exactly_a<add>(*it1) || is_exactly_a<add>(*it2)
869 || is_exactly_a<mul>(*it1) || is_exactly_a<mul>(*it2)
870 || is_exactly_a<ncmul>(*it1) || is_exactly_a<ncmul>(*it2)) {
872 // One of the factors became a sum or product:
873 // re-expand expression and run again
874 // Non-commutative products are always re-expanded to give
875 // eval_ncmul() the chance to re-order and canonicalize
877 ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
878 return simplify_indexed(r, free_indices, dummy_indices, sp);
881 // Both objects may have new indices now or they might
882 // even not be indexed objects any more, so we have to
884 something_changed = true;
887 else if (!has_nonsymmetric &&
888 (first_nonsymmetric ||
889 ex_to<symmetry>(ex_to<indexed>(*it2).get_symmetry()).has_nonsymmetric())) {
890 has_nonsymmetric = 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 if (has_nonsymmetric) {
946 ex q = idx_symmetrization<idx>(r, local_dummy_indices);
948 free_indices.clear();
951 q = idx_symmetrization<varidx>(q, local_dummy_indices);
953 free_indices.clear();
956 q = idx_symmetrization<spinidx>(q, local_dummy_indices);
958 free_indices.clear();
963 // Dummy index renaming
964 r = rename_dummy_indices<idx>(r, dummy_indices, local_dummy_indices);
965 r = rename_dummy_indices<varidx>(r, dummy_indices, local_dummy_indices);
966 r = rename_dummy_indices<spinidx>(r, dummy_indices, local_dummy_indices);
968 // Product of indexed object with a scalar?
969 if (is_exactly_a<mul>(r) && r.nops() == 2
970 && is_exactly_a<numeric>(r.op(1)) && is_a<indexed>(r.op(0)))
971 return ex_to<basic>(r.op(0).op(0)).scalar_mul_indexed(r.op(0), ex_to<numeric>(r.op(1)));
976 /** This structure stores the original and symmetrized versions of terms
977 * obtained during the simplification of sums. */
980 terminfo(const ex & orig_, const ex & symm_) : orig(orig_), symm(symm_) {}
982 ex orig; /**< original term */
983 ex symm; /**< symmtrized term */
986 class terminfo_is_less {
988 bool operator() (const terminfo & ti1, const terminfo & ti2) const
990 return (ti1.symm.compare(ti2.symm) < 0);
994 /** This structure stores the individual symmetrized terms obtained during
995 * the simplification of sums. */
998 symminfo() : num(0) {}
1000 symminfo(const ex & symmterm_, const ex & orig_, size_t num_) : orig(orig_), num(num_)
1002 if (is_exactly_a<mul>(symmterm_) && is_exactly_a<numeric>(symmterm_.op(symmterm_.nops()-1))) {
1003 coeff = symmterm_.op(symmterm_.nops()-1);
1004 symmterm = symmterm_ / coeff;
1007 symmterm = symmterm_;
1011 ex symmterm; /**< symmetrized term */
1012 ex coeff; /**< coefficient of symmetrized term */
1013 ex orig; /**< original term */
1014 size_t num; /**< how many symmetrized terms resulted from the original term */
1017 class symminfo_is_less_by_symmterm {
1019 bool operator() (const symminfo & si1, const symminfo & si2) const
1021 return (si1.symmterm.compare(si2.symmterm) < 0);
1025 class symminfo_is_less_by_orig {
1027 bool operator() (const symminfo & si1, const symminfo & si2) const
1029 return (si1.orig.compare(si2.orig) < 0);
1033 bool hasindex(const ex &x, const ex &sym)
1035 if(is_a<idx>(x) && x.op(0)==sym)
1038 for(size_t i=0; i<x.nops(); ++i)
1039 if(hasindex(x.op(i), sym))
1044 /** Simplify indexed expression, return list of free indices. */
1045 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
1047 // Expand the expression
1048 ex e_expanded = e.expand();
1050 // Simplification of single indexed object: just find the free indices
1051 // and perform dummy index renaming/repositioning
1052 if (is_a<indexed>(e_expanded)) {
1054 // Find the dummy indices
1055 const indexed &i = ex_to<indexed>(e_expanded);
1056 exvector local_dummy_indices;
1057 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
1059 // Filter out the dummy indices with variance
1060 exvector variant_dummy_indices;
1061 find_variant_indices(local_dummy_indices, variant_dummy_indices);
1063 // Any indices with variance present at all?
1064 if (!variant_dummy_indices.empty()) {
1066 // Yes, reposition them
1067 exvector moved_indices;
1068 reposition_dummy_indices(e_expanded, variant_dummy_indices, moved_indices);
1071 // Rename the dummy indices
1072 e_expanded = rename_dummy_indices<idx>(e_expanded, dummy_indices, local_dummy_indices);
1073 e_expanded = rename_dummy_indices<varidx>(e_expanded, dummy_indices, local_dummy_indices);
1074 e_expanded = rename_dummy_indices<spinidx>(e_expanded, dummy_indices, local_dummy_indices);
1078 // Simplification of sum = sum of simplifications, check consistency of
1079 // free indices in each term
1080 if (is_exactly_a<add>(e_expanded)) {
1083 free_indices.clear();
1085 for (size_t i=0; i<e_expanded.nops(); i++) {
1086 exvector free_indices_of_term;
1087 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
1088 if (!term.is_zero()) {
1090 free_indices = free_indices_of_term;
1094 if (!indices_consistent(free_indices, free_indices_of_term)) {
1095 std::ostringstream s;
1096 s << "simplify_indexed: inconsistent indices in sum: ";
1097 s << exprseq(free_indices) << " vs. " << exprseq(free_indices_of_term);
1098 throw (std::runtime_error(s.str()));
1100 if (is_a<indexed>(sum) && is_a<indexed>(term))
1101 sum = ex_to<basic>(sum.op(0)).add_indexed(sum, term);
1108 // If the sum turns out to be zero, we are finished
1109 if (sum.is_zero()) {
1110 free_indices.clear();
1114 // More than one term and more than one dummy index?
1115 size_t num_terms_orig = (is_exactly_a<add>(sum) ? sum.nops() : 1);
1116 if (num_terms_orig < 2 || dummy_indices.size() < 2)
1119 // Chop the sum into terms and symmetrize each one over the dummy
1121 std::vector<terminfo> terms;
1122 for (size_t i=0; i<sum.nops(); i++) {
1123 const ex & term = sum.op(i);
1124 exvector dummy_indices_of_term;
1125 dummy_indices_of_term.reserve(dummy_indices.size());
1126 for(exvector::iterator i=dummy_indices.begin(); i!=dummy_indices.end(); ++i)
1127 if(hasindex(term,i->op(0)))
1128 dummy_indices_of_term.push_back(*i);
1129 ex term_symm = idx_symmetrization<idx>(term, dummy_indices_of_term);
1130 term_symm = idx_symmetrization<varidx>(term_symm, dummy_indices_of_term);
1131 term_symm = idx_symmetrization<spinidx>(term_symm, dummy_indices_of_term);
1132 if (term_symm.is_zero())
1134 terms.push_back(terminfo(term, term_symm));
1137 // Sort by symmetrized terms
1138 std::sort(terms.begin(), terms.end(), terminfo_is_less());
1140 // Combine equal symmetrized terms
1141 std::vector<terminfo> terms_pass2;
1142 for (std::vector<terminfo>::const_iterator i=terms.begin(); i!=terms.end(); ) {
1144 std::vector<terminfo>::const_iterator j = i + 1;
1145 while (j != terms.end() && j->symm == i->symm) {
1149 terms_pass2.push_back(terminfo(i->orig * num, i->symm * num));
1153 // If there is only one term left, we are finished
1154 if (terms_pass2.size() == 1)
1155 return terms_pass2[0].orig;
1157 // Chop the symmetrized terms into subterms
1158 std::vector<symminfo> sy;
1159 for (std::vector<terminfo>::const_iterator i=terms_pass2.begin(); i!=terms_pass2.end(); ++i) {
1160 if (is_exactly_a<add>(i->symm)) {
1161 size_t num = i->symm.nops();
1162 for (size_t j=0; j<num; j++)
1163 sy.push_back(symminfo(i->symm.op(j), i->orig, num));
1165 sy.push_back(symminfo(i->symm, i->orig, 1));
1168 // Sort by symmetrized subterms
1169 std::sort(sy.begin(), sy.end(), symminfo_is_less_by_symmterm());
1171 // Combine equal symmetrized subterms
1172 std::vector<symminfo> sy_pass2;
1174 for (std::vector<symminfo>::const_iterator i=sy.begin(); i!=sy.end(); ) {
1176 // Combine equal terms
1177 std::vector<symminfo>::const_iterator j = i + 1;
1178 if (j != sy.end() && j->symmterm == i->symmterm) {
1180 // More than one term, collect the coefficients
1181 ex coeff = i->coeff;
1182 while (j != sy.end() && j->symmterm == i->symmterm) {
1187 // Add combined term to result
1188 if (!coeff.is_zero())
1189 result.push_back(coeff * i->symmterm);
1193 // Single term, store for second pass
1194 sy_pass2.push_back(*i);
1200 // Were there any remaining terms that didn't get combined?
1201 if (sy_pass2.size() > 0) {
1203 // Yes, sort by their original terms
1204 std::sort(sy_pass2.begin(), sy_pass2.end(), symminfo_is_less_by_orig());
1206 for (std::vector<symminfo>::const_iterator i=sy_pass2.begin(); i!=sy_pass2.end(); ) {
1208 // How many symmetrized terms of this original term are left?
1210 std::vector<symminfo>::const_iterator j = i + 1;
1211 while (j != sy_pass2.end() && j->orig == i->orig) {
1216 if (num == i->num) {
1218 // All terms left, then add the original term to the result
1219 result.push_back(i->orig);
1223 // Some terms were combined with others, add up the remaining symmetrized terms
1224 std::vector<symminfo>::const_iterator k;
1225 for (k=i; k!=j; k++)
1226 result.push_back(k->coeff * k->symmterm);
1233 // Add all resulting terms
1234 ex sum_symm = (new add(result))->setflag(status_flags::dynallocated);
1235 if (sum_symm.is_zero())
1236 free_indices.clear();
1240 // Simplification of products
1241 if (is_exactly_a<mul>(e_expanded)
1242 || is_exactly_a<ncmul>(e_expanded)
1243 || (is_exactly_a<power>(e_expanded) && is_a<indexed>(e_expanded.op(0)) && e_expanded.op(1).is_equal(_ex2)))
1244 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
1246 // Cannot do anything
1247 free_indices.clear();
1251 /** Simplify/canonicalize expression containing indexed objects. This
1252 * performs contraction of dummy indices where possible and checks whether
1253 * the free indices in sums are consistent.
1255 * @param options Simplification options (currently unused)
1256 * @return simplified expression */
1257 ex ex::simplify_indexed(unsigned options) const
1259 exvector free_indices, dummy_indices;
1261 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1264 /** Simplify/canonicalize expression containing indexed objects. This
1265 * performs contraction of dummy indices where possible, checks whether
1266 * the free indices in sums are consistent, and automatically replaces
1267 * scalar products by known values if desired.
1269 * @param sp Scalar products to be replaced automatically
1270 * @param options Simplification options (currently unused)
1271 * @return simplified expression */
1272 ex ex::simplify_indexed(const scalar_products & sp, unsigned options) const
1274 exvector free_indices, dummy_indices;
1275 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1278 /** Symmetrize expression over its free indices. */
1279 ex ex::symmetrize() const
1281 return GiNaC::symmetrize(*this, get_free_indices());
1284 /** Antisymmetrize expression over its free indices. */
1285 ex ex::antisymmetrize() const
1287 return GiNaC::antisymmetrize(*this, get_free_indices());
1290 /** Symmetrize expression by cyclic permutation over its free indices. */
1291 ex ex::symmetrize_cyclic() const
1293 return GiNaC::symmetrize_cyclic(*this, get_free_indices());
1300 spmapkey::spmapkey(const ex & v1_, const ex & v2_, const ex & dim_) : dim(dim_)
1302 // If indexed, extract base objects
1303 ex s1 = is_a<indexed>(v1_) ? v1_.op(0) : v1_;
1304 ex s2 = is_a<indexed>(v2_) ? v2_.op(0) : v2_;
1306 // Enforce canonical order in pair
1307 if (s1.compare(s2) > 0) {
1316 bool spmapkey::operator==(const spmapkey &other) const
1318 if (!v1.is_equal(other.v1))
1320 if (!v2.is_equal(other.v2))
1322 if (is_a<wildcard>(dim) || is_a<wildcard>(other.dim))
1325 return dim.is_equal(other.dim);
1328 bool spmapkey::operator<(const spmapkey &other) const
1330 int cmp = v1.compare(other.v1);
1333 cmp = v2.compare(other.v2);
1337 // Objects are equal, now check dimensions
1338 if (is_a<wildcard>(dim) || is_a<wildcard>(other.dim))
1341 return dim.compare(other.dim) < 0;
1344 void spmapkey::debugprint() const
1346 std::cerr << "(" << v1 << "," << v2 << "," << dim << ")";
1349 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
1351 spm[spmapkey(v1, v2)] = sp;
1354 void scalar_products::add(const ex & v1, const ex & v2, const ex & dim, const ex & sp)
1356 spm[spmapkey(v1, v2, dim)] = sp;
1359 void scalar_products::add_vectors(const lst & l, const ex & dim)
1361 // Add all possible pairs of products
1362 for (lst::const_iterator it1 = l.begin(); it1 != l.end(); ++it1)
1363 for (lst::const_iterator it2 = l.begin(); it2 != l.end(); ++it2)
1364 add(*it1, *it2, *it1 * *it2);
1367 void scalar_products::clear()
1372 /** Check whether scalar product pair is defined. */
1373 bool scalar_products::is_defined(const ex & v1, const ex & v2, const ex & dim) const
1375 return spm.find(spmapkey(v1, v2, dim)) != spm.end();
1378 /** Return value of defined scalar product pair. */
1379 ex scalar_products::evaluate(const ex & v1, const ex & v2, const ex & dim) const
1381 return spm.find(spmapkey(v1, v2, dim))->second;
1384 void scalar_products::debugprint() const
1386 std::cerr << "map size=" << spm.size() << std::endl;
1387 spmap::const_iterator i = spm.begin(), end = spm.end();
1389 const spmapkey & k = i->first;
1390 std::cerr << "item key=";
1392 std::cerr << ", value=" << i->second << std::endl;
1397 exvector get_all_dummy_indices_safely(const ex & e)
1399 if (is_a<indexed>(e))
1400 return ex_to<indexed>(e).get_dummy_indices();
1401 else if (is_a<power>(e) && e.op(1)==2) {
1402 return e.op(0).get_free_indices();
1404 else if (is_a<mul>(e) || is_a<ncmul>(e)) {
1406 exvector free_indices;
1407 for (std::size_t i = 0; i < e.nops(); ++i) {
1408 exvector dummies_of_factor = get_all_dummy_indices_safely(e.op(i));
1409 dummies.insert(dummies.end(), dummies_of_factor.begin(),
1410 dummies_of_factor.end());
1411 exvector free_of_factor = e.op(i).get_free_indices();
1412 free_indices.insert(free_indices.begin(), free_of_factor.begin(),
1413 free_of_factor.end());
1415 exvector free_out, dummy_out;
1416 find_free_and_dummy(free_indices.begin(), free_indices.end(), free_out,
1418 dummies.insert(dummies.end(), dummy_out.begin(), dummy_out.end());
1421 else if(is_a<add>(e)) {
1423 for(std::size_t i = 0; i < e.nops(); ++i) {
1424 exvector dummies_of_term = get_all_dummy_indices_safely(e.op(i));
1425 sort(dummies_of_term.begin(), dummies_of_term.end());
1427 set_union(result.begin(), result.end(), dummies_of_term.begin(),
1428 dummies_of_term.end(), std::back_inserter<exvector>(new_vec),
1430 result.swap(new_vec);
1437 /** Returns all dummy indices from the exvector */
1438 exvector get_all_dummy_indices(const ex & e)
1442 product_to_exvector(e, p, nc);
1443 exvector::const_iterator ip = p.begin(), ipend = p.end();
1445 while (ip != ipend) {
1446 if (is_a<indexed>(*ip)) {
1447 v1 = ex_to<indexed>(*ip).get_dummy_indices();
1448 v.insert(v.end(), v1.begin(), v1.end());
1449 exvector::const_iterator ip1 = ip+1;
1450 while (ip1 != ipend) {
1451 if (is_a<indexed>(*ip1)) {
1452 v1 = ex_to<indexed>(*ip).get_dummy_indices(ex_to<indexed>(*ip1));
1453 v.insert(v.end(), v1.begin(), v1.end());
1463 lst rename_dummy_indices_uniquely(const exvector & va, const exvector & vb)
1465 exvector common_indices;
1466 set_intersection(va.begin(), va.end(), vb.begin(), vb.end(), std::back_insert_iterator<exvector>(common_indices), ex_is_less());
1467 if (common_indices.empty()) {
1468 return lst(lst(), lst());
1470 exvector new_indices, old_indices;
1471 old_indices.reserve(2*common_indices.size());
1472 new_indices.reserve(2*common_indices.size());
1473 exvector::const_iterator ip = common_indices.begin(), ipend = common_indices.end();
1474 while (ip != ipend) {
1475 ex newsym=(new symbol)->setflag(status_flags::dynallocated);
1477 if(is_exactly_a<spinidx>(*ip))
1478 newidx = (new spinidx(newsym, ex_to<spinidx>(*ip).get_dim(),
1479 ex_to<spinidx>(*ip).is_covariant(),
1480 ex_to<spinidx>(*ip).is_dotted()))
1481 -> setflag(status_flags::dynallocated);
1482 else if (is_exactly_a<varidx>(*ip))
1483 newidx = (new varidx(newsym, ex_to<varidx>(*ip).get_dim(),
1484 ex_to<varidx>(*ip).is_covariant()))
1485 -> setflag(status_flags::dynallocated);
1487 newidx = (new idx(newsym, ex_to<idx>(*ip).get_dim()))
1488 -> setflag(status_flags::dynallocated);
1489 old_indices.push_back(*ip);
1490 new_indices.push_back(newidx);
1491 if(is_a<varidx>(*ip)) {
1492 old_indices.push_back(ex_to<varidx>(*ip).toggle_variance());
1493 new_indices.push_back(ex_to<varidx>(newidx).toggle_variance());
1497 return lst(lst(old_indices.begin(), old_indices.end()), lst(new_indices.begin(), new_indices.end()));
1501 ex rename_dummy_indices_uniquely(const exvector & va, const exvector & vb, const ex & b)
1503 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1504 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);
1507 ex rename_dummy_indices_uniquely(const ex & a, const ex & b)
1509 exvector va = get_all_dummy_indices_safely(a);
1510 if (va.size() > 0) {
1511 exvector vb = get_all_dummy_indices_safely(b);
1512 if (vb.size() > 0) {
1513 sort(va.begin(), va.end(), ex_is_less());
1514 sort(vb.begin(), vb.end(), ex_is_less());
1515 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1516 if (indices_subs.op(0).nops() > 0)
1517 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);
1523 ex rename_dummy_indices_uniquely(exvector & va, const ex & b, bool modify_va)
1525 if (va.size() > 0) {
1526 exvector vb = get_all_dummy_indices_safely(b);
1527 if (vb.size() > 0) {
1528 sort(vb.begin(), vb.end(), ex_is_less());
1529 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1530 if (indices_subs.op(0).nops() > 0) {
1532 for (lst::const_iterator i = ex_to<lst>(indices_subs.op(1)).begin(); i != ex_to<lst>(indices_subs.op(1)).end(); ++i)
1534 exvector uncommon_indices;
1535 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());
1536 exvector::const_iterator ip = uncommon_indices.begin(), ipend = uncommon_indices.end();
1537 while (ip != ipend) {
1541 sort(va.begin(), va.end(), ex_is_less());
1543 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);
1550 ex expand_dummy_sum(const ex & e, bool subs_idx)
1552 ex e_expanded = e.expand();
1553 pointer_to_map_function_1arg<bool> fcn(expand_dummy_sum, subs_idx);
1554 if (is_a<add>(e_expanded) || is_a<lst>(e_expanded) || is_a<matrix>(e_expanded)) {
1555 return e_expanded.map(fcn);
1556 } else if (is_a<ncmul>(e_expanded) || is_a<mul>(e_expanded) || is_a<power>(e_expanded) || is_a<indexed>(e_expanded)) {
1558 if (is_a<indexed>(e_expanded))
1559 v = ex_to<indexed>(e_expanded).get_dummy_indices();
1561 v = get_all_dummy_indices(e_expanded);
1562 ex result = e_expanded;
1563 for(exvector::const_iterator it=v.begin(); it!=v.end(); ++it) {
1565 if (ex_to<idx>(nu).get_dim().info(info_flags::nonnegint)) {
1566 int idim = ex_to<numeric>(ex_to<idx>(nu).get_dim()).to_int();
1568 for (int i=0; i < idim; i++) {
1569 if (subs_idx && is_a<varidx>(nu)) {
1570 ex other = ex_to<varidx>(nu).toggle_variance();
1571 en += result.subs(lst(
1573 other == idx(i, idim)
1576 en += result.subs( nu.op(0) == i );
1588 } // namespace GiNaC