3 * Implementation of GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2016 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) : inherited(v), symtree(symm)
124 indexed::indexed(const symmetry & symm, exvector && v) : inherited(std::move(v)), 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 auto 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 bool indexed::all_index_values_are(unsigned inf) const
248 // No indices? Then no property can be fulfilled
253 return find_if(seq.begin() + 1, seq.end(),
254 [inf](const ex & e) { return !(ex_to<idx>(e).get_value().info(inf)); }) == seq.end();
257 int indexed::compare_same_type(const basic & other) const
259 GINAC_ASSERT(is_a<indexed>(other));
260 return inherited::compare_same_type(other);
263 ex indexed::eval() const
265 const ex &base = seq[0];
267 // If the base object is 0, the whole object is 0
271 // If the base object is a product, pull out the numeric factor
272 if (is_exactly_a<mul>(base) && is_exactly_a<numeric>(base.op(base.nops() - 1))) {
274 ex f = ex_to<numeric>(base.op(base.nops() - 1));
276 return f * thiscontainer(v);
279 if((typeid(*this) == typeid(indexed)) && seq.size()==1)
282 // Canonicalize indices according to the symmetry properties
283 if (seq.size() > 2) {
285 GINAC_ASSERT(is_exactly_a<symmetry>(symtree));
286 int sig = canonicalize(v.begin() + 1, ex_to<symmetry>(symtree));
287 if (sig != std::numeric_limits<int>::max()) {
288 // Something has changed while sorting indices, more evaluations later
291 return ex(sig) * thiscontainer(v);
295 // Let the class of the base object perform additional evaluations
296 return ex_to<basic>(base).eval_indexed(*this);
299 ex indexed::real_part() const
301 if(op(0).info(info_flags::real))
303 return real_part_function(*this).hold();
306 ex indexed::imag_part() const
308 if(op(0).info(info_flags::real))
310 return imag_part_function(*this).hold();
313 ex indexed::thiscontainer(const exvector & v) const
315 return indexed(ex_to<symmetry>(symtree), v);
318 ex indexed::thiscontainer(exvector && v) const
320 return indexed(ex_to<symmetry>(symtree), std::move(v));
323 unsigned indexed::return_type() const
325 if(is_a<matrix>(op(0)))
326 return return_types::commutative;
328 return op(0).return_type();
331 ex indexed::expand(unsigned options) const
333 GINAC_ASSERT(seq.size() > 0);
335 if (options & expand_options::expand_indexed) {
336 ex newbase = seq[0].expand(options);
337 if (is_exactly_a<add>(newbase)) {
339 for (size_t i=0; i<newbase.nops(); i++) {
341 s[0] = newbase.op(i);
342 sum += thiscontainer(s).expand(options);
346 if (!are_ex_trivially_equal(newbase, seq[0])) {
349 return ex_to<indexed>(thiscontainer(s)).inherited::expand(options);
352 return inherited::expand(options);
356 // virtual functions which can be overridden by derived classes
362 // non-virtual functions in this class
365 /** Check whether all indices are of class idx and validate the symmetry
366 * tree. This function is used internally to make sure that all constructed
367 * indexed objects really carry indices and not some other classes. */
368 void indexed::validate() const
370 GINAC_ASSERT(seq.size() > 0);
371 auto it = seq.begin() + 1, itend = seq.end();
372 while (it != itend) {
374 throw(std::invalid_argument("indices of indexed object must be of type idx"));
378 if (!symtree.is_zero()) {
379 if (!is_exactly_a<symmetry>(symtree))
380 throw(std::invalid_argument("symmetry of indexed object must be of type symmetry"));
381 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
385 /** Implementation of ex::diff() for an indexed object always returns 0.
388 ex indexed::derivative(const symbol & s) const
397 struct idx_is_equal_ignore_dim : public std::binary_function<ex, ex, bool> {
398 bool operator() (const ex &lh, const ex &rh) const
404 // Replacing the dimension might cause an error (e.g. with
405 // index classes that only work in a fixed number of dimensions)
406 return lh.is_equal(ex_to<idx>(rh).replace_dim(ex_to<idx>(lh).get_dim()));
413 /** Check whether two sorted index vectors are consistent (i.e. equal). */
414 static bool indices_consistent(const exvector & v1, const exvector & v2)
416 // Number of indices must be the same
417 if (v1.size() != v2.size())
420 return equal(v1.begin(), v1.end(), v2.begin(), idx_is_equal_ignore_dim());
423 exvector indexed::get_indices() const
425 GINAC_ASSERT(seq.size() >= 1);
426 return exvector(seq.begin() + 1, seq.end());
429 exvector indexed::get_dummy_indices() const
431 exvector free_indices, dummy_indices;
432 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
433 return dummy_indices;
436 exvector indexed::get_dummy_indices(const indexed & other) const
438 exvector indices = get_free_indices();
439 exvector other_indices = other.get_free_indices();
440 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
441 exvector dummy_indices;
442 find_dummy_indices(indices, dummy_indices);
443 return dummy_indices;
446 bool indexed::has_dummy_index_for(const ex & i) const
448 auto it = seq.begin() + 1, itend = seq.end();
449 while (it != itend) {
450 if (is_dummy_pair(*it, i))
457 exvector indexed::get_free_indices() const
459 exvector free_indices, dummy_indices;
460 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
464 exvector add::get_free_indices() const
466 exvector free_indices;
467 for (size_t i=0; i<nops(); i++) {
469 free_indices = op(i).get_free_indices();
471 exvector free_indices_of_term = op(i).get_free_indices();
472 if (!indices_consistent(free_indices, free_indices_of_term))
473 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
479 exvector mul::get_free_indices() const
481 // Concatenate free indices of all factors
483 for (size_t i=0; i<nops(); i++) {
484 exvector free_indices_of_factor = op(i).get_free_indices();
485 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
488 // And remove the dummy indices
489 exvector free_indices, dummy_indices;
490 find_free_and_dummy(un, free_indices, dummy_indices);
494 exvector ncmul::get_free_indices() const
496 // Concatenate free indices of all factors
498 for (size_t i=0; i<nops(); i++) {
499 exvector free_indices_of_factor = op(i).get_free_indices();
500 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
503 // And remove the dummy indices
504 exvector free_indices, dummy_indices;
505 find_free_and_dummy(un, free_indices, dummy_indices);
509 struct is_summation_idx : public std::unary_function<ex, bool> {
510 bool operator()(const ex & e)
512 return is_dummy_pair(e, e);
516 exvector integral::get_free_indices() const
518 if (a.get_free_indices().size() || b.get_free_indices().size())
519 throw (std::runtime_error("integral::get_free_indices: boundary values should not have free indices"));
520 return f.get_free_indices();
523 template<class T> size_t number_of_type(const exvector&v)
527 if (is_exactly_a<T>(it))
532 /** Rename dummy indices in an expression.
534 * @param e Expression to work on
535 * @param local_dummy_indices The set of dummy indices that appear in the
537 * @param global_dummy_indices The set of dummy indices that have appeared
538 * before and which we would like to use in "e", too. This gets updated
540 template<class T> static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
542 size_t global_size = number_of_type<T>(global_dummy_indices),
543 local_size = number_of_type<T>(local_dummy_indices);
545 // Any local dummy indices at all?
549 if (global_size < local_size) {
551 // More local indices than we encountered before, add the new ones
553 size_t old_global_size = global_size;
554 int remaining = local_size - global_size;
555 auto it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
556 while (it != itend && remaining > 0) {
557 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()) {
558 global_dummy_indices.push_back(*it);
565 // If this is the first set of local indices, do nothing
566 if (old_global_size == 0)
569 GINAC_ASSERT(local_size <= global_size);
571 // Construct vectors of index symbols
572 exvector local_syms, global_syms;
573 local_syms.reserve(local_size);
574 global_syms.reserve(local_size);
575 for (size_t i=0; local_syms.size()!=local_size; i++)
576 if(is_exactly_a<T>(local_dummy_indices[i]))
577 local_syms.push_back(local_dummy_indices[i].op(0));
578 shaker_sort(local_syms.begin(), local_syms.end(), ex_is_less(), ex_swap());
579 for (size_t i=0; global_syms.size()!=local_size; i++) // don't use more global symbols than necessary
580 if(is_exactly_a<T>(global_dummy_indices[i]))
581 global_syms.push_back(global_dummy_indices[i].op(0));
582 shaker_sort(global_syms.begin(), global_syms.end(), ex_is_less(), ex_swap());
584 // Remove common indices
585 exvector local_uniq, global_uniq;
586 set_difference(local_syms.begin(), local_syms.end(), global_syms.begin(), global_syms.end(), std::back_insert_iterator<exvector>(local_uniq), ex_is_less());
587 set_difference(global_syms.begin(), global_syms.end(), local_syms.begin(), local_syms.end(), std::back_insert_iterator<exvector>(global_uniq), ex_is_less());
589 // Replace remaining non-common local index symbols by global ones
590 if (local_uniq.empty())
593 while (global_uniq.size() > local_uniq.size())
594 global_uniq.pop_back();
595 return e.subs(lst(local_uniq.begin(), local_uniq.end()), lst(global_uniq.begin(), global_uniq.end()), subs_options::no_pattern);
599 /** Given a set of indices, extract those of class varidx. */
600 static void find_variant_indices(const exvector & v, exvector & variant_indices)
602 exvector::const_iterator it1, itend;
603 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
604 if (is_exactly_a<varidx>(*it1))
605 variant_indices.push_back(*it1);
609 /** Raise/lower dummy indices in a single indexed objects to canonicalize their
612 * @param e Object to work on
613 * @param variant_dummy_indices The set of indices that might need repositioning (will be changed by this function)
614 * @param moved_indices The set of indices that have been repositioned (will be changed by this function)
615 * @return true if 'e' was changed */
616 bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices)
618 bool something_changed = false;
620 // Find dummy symbols that occur twice in the same indexed object.
621 exvector local_var_dummies;
622 local_var_dummies.reserve(e.nops()/2);
623 for (size_t i=1; i<e.nops(); ++i) {
624 if (!is_a<varidx>(e.op(i)))
626 for (size_t j=i+1; j<e.nops(); ++j) {
627 if (is_dummy_pair(e.op(i), e.op(j))) {
628 local_var_dummies.push_back(e.op(i));
629 for (auto k = variant_dummy_indices.begin(); k!=variant_dummy_indices.end(); ++k) {
630 if (e.op(i).op(0) == k->op(0)) {
631 variant_dummy_indices.erase(k);
640 // In the case where a dummy symbol occurs twice in the same indexed object
641 // we try all possibilities of raising/lowering and keep the least one in
642 // the sense of ex_is_less.
644 size_t numpossibs = 1 << local_var_dummies.size();
645 for (size_t i=0; i<numpossibs; ++i) {
647 for (size_t j=0; j<local_var_dummies.size(); ++j) {
650 ex curr_idx = local_var_dummies[j];
651 ex curr_toggle = ex_to<varidx>(curr_idx).toggle_variance();
652 m[curr_idx] = curr_toggle;
653 m[curr_toggle] = curr_idx;
655 try_e = e.subs(m, subs_options::no_pattern);
657 if(ex_is_less()(try_e, optimal_e))
659 something_changed = true;
664 if (!is_a<indexed>(e))
667 exvector seq = ex_to<indexed>(e).seq;
669 // If a dummy index is encountered for the first time in the
670 // product, pull it up, otherwise, pull it down
671 for (auto it2 = seq.begin()+1, it2end = seq.end(); it2 != it2end; ++it2) {
672 if (!is_exactly_a<varidx>(*it2))
675 exvector::iterator vit, vitend;
676 for (vit = variant_dummy_indices.begin(), vitend = variant_dummy_indices.end(); vit != vitend; ++vit) {
677 if (it2->op(0).is_equal(vit->op(0))) {
678 if (ex_to<varidx>(*it2).is_covariant()) {
680 * N.B. we don't want to use
683 * *it2 == ex_to<varidx>(*it2).toggle_variance(),
684 * ex_to<varidx>(*it2).toggle_variance() == *it2
685 * }, subs_options::no_pattern);
687 * since this can trigger non-trivial repositioning of indices,
688 * e.g. due to non-trivial symmetry properties of e, thus
689 * invalidating iterators
691 *it2 = ex_to<varidx>(*it2).toggle_variance();
692 something_changed = true;
694 moved_indices.push_back(*vit);
695 variant_dummy_indices.erase(vit);
700 for (vit = moved_indices.begin(), vitend = moved_indices.end(); vit != vitend; ++vit) {
701 if (it2->op(0).is_equal(vit->op(0))) {
702 if (ex_to<varidx>(*it2).is_contravariant()) {
703 *it2 = ex_to<varidx>(*it2).toggle_variance();
704 something_changed = true;
713 if (something_changed)
714 e = ex_to<indexed>(e).thiscontainer(seq);
716 return something_changed;
719 /* Ordering that only compares the base expressions of indexed objects. */
720 struct ex_base_is_less : public std::binary_function<ex, ex, bool> {
721 bool operator() (const ex &lh, const ex &rh) const
723 return (is_a<indexed>(lh) ? lh.op(0) : lh).compare(is_a<indexed>(rh) ? rh.op(0) : rh) < 0;
727 /* An auxiliary function used by simplify_indexed() and expand_dummy_sum()
728 * It returns an exvector of factors from the supplied product */
729 static void product_to_exvector(const ex & e, exvector & v, bool & non_commutative)
731 // Remember whether the product was commutative or noncommutative
732 // (because we chop it into factors and need to reassemble later)
733 non_commutative = is_exactly_a<ncmul>(e);
735 // Collect factors in an exvector, store squares twice
736 v.reserve(e.nops() * 2);
738 if (is_exactly_a<power>(e)) {
739 // We only get called for simple squares, split a^2 -> a*a
740 GINAC_ASSERT(e.op(1).is_equal(_ex2));
741 v.push_back(e.op(0));
742 v.push_back(e.op(0));
744 for (size_t i=0; i<e.nops(); i++) {
746 if (is_exactly_a<power>(f) && f.op(1).is_equal(_ex2)) {
747 v.push_back(f.op(0));
748 v.push_back(f.op(0));
749 } else if (is_exactly_a<ncmul>(f)) {
750 // Noncommutative factor found, split it as well
751 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
752 for (size_t j=0; j<f.nops(); j++)
753 v.push_back(f.op(j));
760 template<class T> ex idx_symmetrization(const ex& r,const exvector& local_dummy_indices)
761 { exvector dummy_syms;
762 dummy_syms.reserve(r.nops());
763 for (auto & it : local_dummy_indices)
764 if(is_exactly_a<T>(it))
765 dummy_syms.push_back(it.op(0));
766 if(dummy_syms.size() < 2)
768 ex q=symmetrize(r, dummy_syms);
772 // Forward declaration needed in absence of friend injection, C.f. [namespace.memdef]:
773 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
775 /** Simplify product of indexed expressions (commutative, noncommutative and
776 * simple squares), return list of free indices. */
777 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
779 // Collect factors in an exvector
782 // Remember whether the product was commutative or noncommutative
783 // (because we chop it into factors and need to reassemble later)
784 bool non_commutative;
785 product_to_exvector(e, v, non_commutative);
787 // Perform contractions
788 bool something_changed = false;
789 bool has_nonsymmetric = false;
790 GINAC_ASSERT(v.size() > 1);
791 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
792 for (it1 = v.begin(); it1 != next_to_last; it1++) {
795 if (!is_a<indexed>(*it1))
798 bool first_noncommutative = (it1->return_type() != return_types::commutative);
799 bool first_nonsymmetric = ex_to<symmetry>(ex_to<indexed>(*it1).get_symmetry()).has_nonsymmetric();
801 // Indexed factor found, get free indices and look for contraction
803 exvector free1, dummy1;
804 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
806 exvector::iterator it2;
807 for (it2 = it1 + 1; it2 != itend; it2++) {
809 if (!is_a<indexed>(*it2))
812 bool second_noncommutative = (it2->return_type() != return_types::commutative);
814 // Find free indices of second factor and merge them with free
815 // indices of first factor
817 find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
818 un.insert(un.end(), free1.begin(), free1.end());
820 // Check whether the two factors share dummy indices
821 exvector free, dummy;
822 find_free_and_dummy(un, free, dummy);
823 size_t num_dummies = dummy.size();
824 if (num_dummies == 0)
827 // At least one dummy index, is it a defined scalar product?
828 bool contracted = false;
829 if (free.empty() && it1->nops()==2 && it2->nops()==2) {
831 ex dim = minimal_dim(
832 ex_to<idx>(it1->op(1)).get_dim(),
833 ex_to<idx>(it2->op(1)).get_dim()
836 // User-defined scalar product?
837 if (sp.is_defined(*it1, *it2, dim)) {
839 // Yes, substitute it
840 *it1 = sp.evaluate(*it1, *it2, dim);
842 goto contraction_done;
846 // Try to contract the first one with the second one
847 contracted = ex_to<basic>(it1->op(0)).contract_with(it1, it2, v);
850 // That didn't work; maybe the second object knows how to
851 // contract itself with the first one
852 contracted = ex_to<basic>(it2->op(0)).contract_with(it2, it1, v);
856 if (first_noncommutative || second_noncommutative
857 || is_exactly_a<add>(*it1) || is_exactly_a<add>(*it2)
858 || is_exactly_a<mul>(*it1) || is_exactly_a<mul>(*it2)
859 || is_exactly_a<ncmul>(*it1) || is_exactly_a<ncmul>(*it2)) {
861 // One of the factors became a sum or product:
862 // re-expand expression and run again
863 // Non-commutative products are always re-expanded to give
864 // eval_ncmul() the chance to re-order and canonicalize
866 bool is_a_product = (is_exactly_a<mul>(*it1) || is_exactly_a<ncmul>(*it1)) &&
867 (is_exactly_a<mul>(*it2) || is_exactly_a<ncmul>(*it2));
868 ex r = (non_commutative ? ex(ncmul(std::move(v))) : ex(mul(std::move(v))));
870 // If new expression is a product we can call this function again,
871 // otherwise we need to pass argument to simplify_indexed() to be expanded
873 return simplify_indexed_product(r, free_indices, dummy_indices, sp);
875 return simplify_indexed(r, free_indices, dummy_indices, sp);
878 // Both objects may have new indices now or they might
879 // even not be indexed objects any more, so we have to
881 something_changed = true;
884 else if (!has_nonsymmetric &&
885 (first_nonsymmetric ||
886 ex_to<symmetry>(ex_to<indexed>(*it2).get_symmetry()).has_nonsymmetric())) {
887 has_nonsymmetric = true;
892 // Find free indices (concatenate them all and call find_free_and_dummy())
893 // and all dummy indices that appear
894 exvector un, individual_dummy_indices;
895 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
896 exvector free_indices_of_factor;
897 if (is_a<indexed>(*it1)) {
898 exvector dummy_indices_of_factor;
899 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);
900 individual_dummy_indices.insert(individual_dummy_indices.end(), dummy_indices_of_factor.begin(), dummy_indices_of_factor.end());
902 free_indices_of_factor = it1->get_free_indices();
903 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
905 exvector local_dummy_indices;
906 find_free_and_dummy(un, free_indices, local_dummy_indices);
907 local_dummy_indices.insert(local_dummy_indices.end(), individual_dummy_indices.begin(), individual_dummy_indices.end());
909 // Filter out the dummy indices with variance
910 exvector variant_dummy_indices;
911 find_variant_indices(local_dummy_indices, variant_dummy_indices);
913 // Any indices with variance present at all?
914 if (!variant_dummy_indices.empty()) {
916 // Yes, bring the product into a canonical order that only depends on
917 // the base expressions of indexed objects
918 if (!non_commutative)
919 std::sort(v.begin(), v.end(), ex_base_is_less());
921 exvector moved_indices;
923 // Iterate over all indexed objects in the product
924 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
925 if (!is_a<indexed>(*it1))
928 if (reposition_dummy_indices(*it1, variant_dummy_indices, moved_indices))
929 something_changed = true;
934 if (something_changed)
935 r = non_commutative ? ex(ncmul(std::move(v))) : ex(mul(std::move(v)));
939 // The result should be symmetric with respect to exchange of dummy
940 // indices, so if the symmetrization vanishes, the whole expression is
941 // zero. This detects things like eps.i.j.k * p.j * p.k = 0.
942 if (has_nonsymmetric) {
943 ex q = idx_symmetrization<idx>(r, local_dummy_indices);
945 free_indices.clear();
948 q = idx_symmetrization<varidx>(q, local_dummy_indices);
950 free_indices.clear();
953 q = idx_symmetrization<spinidx>(q, local_dummy_indices);
955 free_indices.clear();
960 // Dummy index renaming
961 r = rename_dummy_indices<idx>(r, dummy_indices, local_dummy_indices);
962 r = rename_dummy_indices<varidx>(r, dummy_indices, local_dummy_indices);
963 r = rename_dummy_indices<spinidx>(r, dummy_indices, local_dummy_indices);
965 // Product of indexed object with a scalar?
966 if (is_exactly_a<mul>(r) && r.nops() == 2
967 && is_exactly_a<numeric>(r.op(1)) && is_a<indexed>(r.op(0)))
968 return ex_to<basic>(r.op(0).op(0)).scalar_mul_indexed(r.op(0), ex_to<numeric>(r.op(1)));
973 /** This structure stores the original and symmetrized versions of terms
974 * obtained during the simplification of sums. */
977 terminfo(const ex & orig_, const ex & symm_) : orig(orig_), symm(symm_) {}
979 ex orig; /**< original term */
980 ex symm; /**< symmetrized term */
983 class terminfo_is_less {
985 bool operator() (const terminfo & ti1, const terminfo & ti2) const
987 return (ti1.symm.compare(ti2.symm) < 0);
991 /** This structure stores the individual symmetrized terms obtained during
992 * the simplification of sums. */
995 symminfo() : num(0) {}
997 symminfo(const ex & symmterm_, const ex & orig_, size_t num_) : orig(orig_), num(num_)
999 if (is_exactly_a<mul>(symmterm_) && is_exactly_a<numeric>(symmterm_.op(symmterm_.nops()-1))) {
1000 coeff = symmterm_.op(symmterm_.nops()-1);
1001 symmterm = symmterm_ / coeff;
1004 symmterm = symmterm_;
1008 ex symmterm; /**< symmetrized term */
1009 ex coeff; /**< coefficient of symmetrized term */
1010 ex orig; /**< original term */
1011 size_t num; /**< how many symmetrized terms resulted from the original term */
1014 class symminfo_is_less_by_symmterm {
1016 bool operator() (const symminfo & si1, const symminfo & si2) const
1018 return (si1.symmterm.compare(si2.symmterm) < 0);
1022 class symminfo_is_less_by_orig {
1024 bool operator() (const symminfo & si1, const symminfo & si2) const
1026 return (si1.orig.compare(si2.orig) < 0);
1030 bool hasindex(const ex &x, const ex &sym)
1032 if(is_a<idx>(x) && x.op(0)==sym)
1035 for(size_t i=0; i<x.nops(); ++i)
1036 if(hasindex(x.op(i), sym))
1041 /** Simplify indexed expression, return list of free indices. */
1042 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
1044 // Expand the expression
1045 ex e_expanded = e.expand();
1047 // Simplification of single indexed object: just find the free indices
1048 // and perform dummy index renaming/repositioning
1049 if (is_a<indexed>(e_expanded)) {
1051 // Find the dummy indices
1052 const indexed &i = ex_to<indexed>(e_expanded);
1053 exvector local_dummy_indices;
1054 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
1056 // Filter out the dummy indices with variance
1057 exvector variant_dummy_indices;
1058 find_variant_indices(local_dummy_indices, variant_dummy_indices);
1060 // Any indices with variance present at all?
1061 if (!variant_dummy_indices.empty()) {
1063 // Yes, reposition them
1064 exvector moved_indices;
1065 reposition_dummy_indices(e_expanded, variant_dummy_indices, moved_indices);
1068 // Rename the dummy indices
1069 e_expanded = rename_dummy_indices<idx>(e_expanded, dummy_indices, local_dummy_indices);
1070 e_expanded = rename_dummy_indices<varidx>(e_expanded, dummy_indices, local_dummy_indices);
1071 e_expanded = rename_dummy_indices<spinidx>(e_expanded, dummy_indices, local_dummy_indices);
1075 // Simplification of sum = sum of simplifications, check consistency of
1076 // free indices in each term
1077 if (is_exactly_a<add>(e_expanded)) {
1080 free_indices.clear();
1082 for (size_t i=0; i<e_expanded.nops(); i++) {
1083 exvector free_indices_of_term;
1084 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
1085 if (!term.is_zero()) {
1087 free_indices = free_indices_of_term;
1091 if (!indices_consistent(free_indices, free_indices_of_term)) {
1092 std::ostringstream s;
1093 s << "simplify_indexed: inconsistent indices in sum: ";
1094 s << exprseq(free_indices) << " vs. " << exprseq(free_indices_of_term);
1095 throw (std::runtime_error(s.str()));
1097 if (is_a<indexed>(sum) && is_a<indexed>(term))
1098 sum = ex_to<basic>(sum.op(0)).add_indexed(sum, term);
1105 // If the sum turns out to be zero, we are finished
1106 if (sum.is_zero()) {
1107 free_indices.clear();
1111 // More than one term and more than one dummy index?
1112 size_t num_terms_orig = (is_exactly_a<add>(sum) ? sum.nops() : 1);
1113 if (num_terms_orig < 2 || dummy_indices.size() < 2)
1116 // Chop the sum into terms and symmetrize each one over the dummy
1118 std::vector<terminfo> terms;
1119 for (size_t i=0; i<sum.nops(); i++) {
1120 const ex & term = sum.op(i);
1121 exvector dummy_indices_of_term;
1122 dummy_indices_of_term.reserve(dummy_indices.size());
1123 for (auto & i : dummy_indices)
1124 if (hasindex(term,i.op(0)))
1125 dummy_indices_of_term.push_back(i);
1126 ex term_symm = idx_symmetrization<idx>(term, dummy_indices_of_term);
1127 term_symm = idx_symmetrization<varidx>(term_symm, dummy_indices_of_term);
1128 term_symm = idx_symmetrization<spinidx>(term_symm, dummy_indices_of_term);
1129 if (term_symm.is_zero())
1131 terms.push_back(terminfo(term, term_symm));
1134 // Sort by symmetrized terms
1135 std::sort(terms.begin(), terms.end(), terminfo_is_less());
1137 // Combine equal symmetrized terms
1138 std::vector<terminfo> terms_pass2;
1139 for (std::vector<terminfo>::const_iterator i=terms.begin(); i!=terms.end(); ) {
1142 while (j != terms.end() && j->symm == i->symm) {
1146 terms_pass2.push_back(terminfo(i->orig * num, i->symm * num));
1150 // If there is only one term left, we are finished
1151 if (terms_pass2.size() == 1)
1152 return terms_pass2[0].orig;
1154 // Chop the symmetrized terms into subterms
1155 std::vector<symminfo> sy;
1156 for (auto & i : terms_pass2) {
1157 if (is_exactly_a<add>(i.symm)) {
1158 size_t num = i.symm.nops();
1159 for (size_t j=0; j<num; j++)
1160 sy.push_back(symminfo(i.symm.op(j), i.orig, num));
1162 sy.push_back(symminfo(i.symm, i.orig, 1));
1165 // Sort by symmetrized subterms
1166 std::sort(sy.begin(), sy.end(), symminfo_is_less_by_symmterm());
1168 // Combine equal symmetrized subterms
1169 std::vector<symminfo> sy_pass2;
1171 for (auto i=sy.begin(); i!=sy.end(); ) {
1173 // Combine equal terms
1175 if (j != sy.end() && j->symmterm == i->symmterm) {
1177 // More than one term, collect the coefficients
1178 ex coeff = i->coeff;
1179 while (j != sy.end() && j->symmterm == i->symmterm) {
1184 // Add combined term to result
1185 if (!coeff.is_zero())
1186 result.push_back(coeff * i->symmterm);
1190 // Single term, store for second pass
1191 sy_pass2.push_back(*i);
1197 // Were there any remaining terms that didn't get combined?
1198 if (sy_pass2.size() > 0) {
1200 // Yes, sort by their original terms
1201 std::sort(sy_pass2.begin(), sy_pass2.end(), symminfo_is_less_by_orig());
1203 for (std::vector<symminfo>::const_iterator i=sy_pass2.begin(); i!=sy_pass2.end(); ) {
1205 // How many symmetrized terms of this original term are left?
1208 while (j != sy_pass2.end() && j->orig == i->orig) {
1213 if (num == i->num) {
1215 // All terms left, then add the original term to the result
1216 result.push_back(i->orig);
1220 // Some terms were combined with others, add up the remaining symmetrized terms
1221 std::vector<symminfo>::const_iterator k;
1222 for (k=i; k!=j; k++)
1223 result.push_back(k->coeff * k->symmterm);
1230 // Add all resulting terms
1231 ex sum_symm = dynallocate<add>(result);
1232 if (sum_symm.is_zero())
1233 free_indices.clear();
1237 // Simplification of products
1238 if (is_exactly_a<mul>(e_expanded)
1239 || is_exactly_a<ncmul>(e_expanded)
1240 || (is_exactly_a<power>(e_expanded) && is_a<indexed>(e_expanded.op(0)) && e_expanded.op(1).is_equal(_ex2)))
1241 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
1243 // Cannot do anything
1244 free_indices.clear();
1248 /** Simplify/canonicalize expression containing indexed objects. This
1249 * performs contraction of dummy indices where possible and checks whether
1250 * the free indices in sums are consistent.
1252 * @param options Simplification options (currently unused)
1253 * @return simplified expression */
1254 ex ex::simplify_indexed(unsigned options) const
1256 exvector free_indices, dummy_indices;
1258 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1261 /** Simplify/canonicalize expression containing indexed objects. This
1262 * performs contraction of dummy indices where possible, checks whether
1263 * the free indices in sums are consistent, and automatically replaces
1264 * scalar products by known values if desired.
1266 * @param sp Scalar products to be replaced automatically
1267 * @param options Simplification options (currently unused)
1268 * @return simplified expression */
1269 ex ex::simplify_indexed(const scalar_products & sp, unsigned options) const
1271 exvector free_indices, dummy_indices;
1272 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1275 /** Symmetrize expression over its free indices. */
1276 ex ex::symmetrize() const
1278 return GiNaC::symmetrize(*this, get_free_indices());
1281 /** Antisymmetrize expression over its free indices. */
1282 ex ex::antisymmetrize() const
1284 return GiNaC::antisymmetrize(*this, get_free_indices());
1287 /** Symmetrize expression by cyclic permutation over its free indices. */
1288 ex ex::symmetrize_cyclic() const
1290 return GiNaC::symmetrize_cyclic(*this, get_free_indices());
1297 spmapkey::spmapkey(const ex & v1_, const ex & v2_, const ex & dim_) : dim(dim_)
1299 // If indexed, extract base objects
1300 ex s1 = is_a<indexed>(v1_) ? v1_.op(0) : v1_;
1301 ex s2 = is_a<indexed>(v2_) ? v2_.op(0) : v2_;
1303 // Enforce canonical order in pair
1304 if (s1.compare(s2) > 0) {
1313 bool spmapkey::operator==(const spmapkey &other) const
1315 if (!v1.is_equal(other.v1))
1317 if (!v2.is_equal(other.v2))
1319 if (is_a<wildcard>(dim) || is_a<wildcard>(other.dim))
1322 return dim.is_equal(other.dim);
1325 bool spmapkey::operator<(const spmapkey &other) const
1327 int cmp = v1.compare(other.v1);
1330 cmp = v2.compare(other.v2);
1334 // Objects are equal, now check dimensions
1335 if (is_a<wildcard>(dim) || is_a<wildcard>(other.dim))
1338 return dim.compare(other.dim) < 0;
1341 void spmapkey::debugprint() const
1343 std::cerr << "(" << v1 << "," << v2 << "," << dim << ")";
1346 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
1348 spm[spmapkey(v1, v2)] = sp;
1351 void scalar_products::add(const ex & v1, const ex & v2, const ex & dim, const ex & sp)
1353 spm[spmapkey(v1, v2, dim)] = sp;
1356 void scalar_products::add_vectors(const lst & l, const ex & dim)
1358 // Add all possible pairs of products
1359 for (auto & it1 : l)
1360 for (auto & it2 : l)
1361 add(it1, it2, it1 * it2);
1364 void scalar_products::clear()
1369 /** Check whether scalar product pair is defined. */
1370 bool scalar_products::is_defined(const ex & v1, const ex & v2, const ex & dim) const
1372 return spm.find(spmapkey(v1, v2, dim)) != spm.end();
1375 /** Return value of defined scalar product pair. */
1376 ex scalar_products::evaluate(const ex & v1, const ex & v2, const ex & dim) const
1378 return spm.find(spmapkey(v1, v2, dim))->second;
1381 void scalar_products::debugprint() const
1383 std::cerr << "map size=" << spm.size() << std::endl;
1384 for (auto & it : spm) {
1385 const spmapkey & k = it.first;
1386 std::cerr << "item key=";
1388 std::cerr << ", value=" << it.second << std::endl;
1392 exvector get_all_dummy_indices_safely(const ex & e)
1394 if (is_a<indexed>(e))
1395 return ex_to<indexed>(e).get_dummy_indices();
1396 else if (is_a<power>(e) && e.op(1)==2) {
1397 return e.op(0).get_free_indices();
1399 else if (is_a<mul>(e) || is_a<ncmul>(e)) {
1401 exvector free_indices;
1402 for (std::size_t i = 0; i < e.nops(); ++i) {
1403 exvector dummies_of_factor = get_all_dummy_indices_safely(e.op(i));
1404 dummies.insert(dummies.end(), dummies_of_factor.begin(),
1405 dummies_of_factor.end());
1406 exvector free_of_factor = e.op(i).get_free_indices();
1407 free_indices.insert(free_indices.begin(), free_of_factor.begin(),
1408 free_of_factor.end());
1410 exvector free_out, dummy_out;
1411 find_free_and_dummy(free_indices.begin(), free_indices.end(), free_out,
1413 dummies.insert(dummies.end(), dummy_out.begin(), dummy_out.end());
1416 else if(is_a<add>(e)) {
1418 for(std::size_t i = 0; i < e.nops(); ++i) {
1419 exvector dummies_of_term = get_all_dummy_indices_safely(e.op(i));
1420 sort(dummies_of_term.begin(), dummies_of_term.end());
1422 set_union(result.begin(), result.end(), dummies_of_term.begin(),
1423 dummies_of_term.end(), std::back_inserter<exvector>(new_vec),
1425 result.swap(new_vec);
1432 /** Returns all dummy indices from the exvector */
1433 exvector get_all_dummy_indices(const ex & e)
1437 product_to_exvector(e, p, nc);
1438 auto ip = p.begin(), ipend = p.end();
1440 while (ip != ipend) {
1441 if (is_a<indexed>(*ip)) {
1442 v1 = ex_to<indexed>(*ip).get_dummy_indices();
1443 v.insert(v.end(), v1.begin(), v1.end());
1445 while (ip1 != ipend) {
1446 if (is_a<indexed>(*ip1)) {
1447 v1 = ex_to<indexed>(*ip).get_dummy_indices(ex_to<indexed>(*ip1));
1448 v.insert(v.end(), v1.begin(), v1.end());
1458 lst rename_dummy_indices_uniquely(const exvector & va, const exvector & vb)
1460 exvector common_indices;
1461 set_intersection(va.begin(), va.end(), vb.begin(), vb.end(), std::back_insert_iterator<exvector>(common_indices), ex_is_less());
1462 if (common_indices.empty()) {
1463 return lst{lst{}, lst{}};
1465 exvector new_indices, old_indices;
1466 old_indices.reserve(2*common_indices.size());
1467 new_indices.reserve(2*common_indices.size());
1468 exvector::const_iterator ip = common_indices.begin(), ipend = common_indices.end();
1469 while (ip != ipend) {
1470 ex newsym = dynallocate<symbol>();
1472 if(is_exactly_a<spinidx>(*ip))
1473 newidx = dynallocate<spinidx>(newsym, ex_to<spinidx>(*ip).get_dim(),
1474 ex_to<spinidx>(*ip).is_covariant(),
1475 ex_to<spinidx>(*ip).is_dotted());
1476 else if (is_exactly_a<varidx>(*ip))
1477 newidx = dynallocate<varidx>(newsym, ex_to<varidx>(*ip).get_dim(),
1478 ex_to<varidx>(*ip).is_covariant());
1480 newidx = dynallocate<idx>(newsym, ex_to<idx>(*ip).get_dim());
1481 old_indices.push_back(*ip);
1482 new_indices.push_back(newidx);
1483 if(is_a<varidx>(*ip)) {
1484 old_indices.push_back(ex_to<varidx>(*ip).toggle_variance());
1485 new_indices.push_back(ex_to<varidx>(newidx).toggle_variance());
1489 return lst{lst(old_indices.begin(), old_indices.end()), lst(new_indices.begin(), new_indices.end())};
1493 ex rename_dummy_indices_uniquely(const exvector & va, const exvector & vb, const ex & b)
1495 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1496 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);
1499 ex rename_dummy_indices_uniquely(const ex & a, const ex & b)
1501 exvector va = get_all_dummy_indices_safely(a);
1502 if (va.size() > 0) {
1503 exvector vb = get_all_dummy_indices_safely(b);
1504 if (vb.size() > 0) {
1505 sort(va.begin(), va.end(), ex_is_less());
1506 sort(vb.begin(), vb.end(), ex_is_less());
1507 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1508 if (indices_subs.op(0).nops() > 0)
1509 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);
1515 ex rename_dummy_indices_uniquely(exvector & va, const ex & b, bool modify_va)
1517 if (va.size() > 0) {
1518 exvector vb = get_all_dummy_indices_safely(b);
1519 if (vb.size() > 0) {
1520 sort(vb.begin(), vb.end(), ex_is_less());
1521 lst indices_subs = rename_dummy_indices_uniquely(va, vb);
1522 if (indices_subs.op(0).nops() > 0) {
1524 for (auto & i : ex_to<lst>(indices_subs.op(1)))
1526 exvector uncommon_indices;
1527 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());
1528 for (auto & ip : uncommon_indices)
1530 sort(va.begin(), va.end(), ex_is_less());
1532 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);
1539 ex expand_dummy_sum(const ex & e, bool subs_idx)
1541 ex e_expanded = e.expand();
1542 pointer_to_map_function_1arg<bool> fcn(expand_dummy_sum, subs_idx);
1543 if (is_a<add>(e_expanded) || is_a<lst>(e_expanded) || is_a<matrix>(e_expanded)) {
1544 return e_expanded.map(fcn);
1545 } else if (is_a<ncmul>(e_expanded) || is_a<mul>(e_expanded) || is_a<power>(e_expanded) || is_a<indexed>(e_expanded)) {
1547 if (is_a<indexed>(e_expanded))
1548 v = ex_to<indexed>(e_expanded).get_dummy_indices();
1550 v = get_all_dummy_indices(e_expanded);
1551 ex result = e_expanded;
1552 for (const auto & nu : v) {
1553 if (ex_to<idx>(nu).get_dim().info(info_flags::nonnegint)) {
1554 int idim = ex_to<numeric>(ex_to<idx>(nu).get_dim()).to_int();
1556 for (int i=0; i < idim; i++) {
1557 if (subs_idx && is_a<varidx>(nu)) {
1558 ex other = ex_to<varidx>(nu).toggle_variance();
1559 en += result.subs(lst{
1561 other == idx(i, idim)
1564 en += result.subs( nu.op(0) == i );
1576 } // namespace GiNaC