3 * Implementation of GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
44 // default constructor, destructor, copy constructor assignment operator and helpers
47 indexed::indexed() : symmetry(unknown)
49 debugmsg("indexed default constructor", LOGLEVEL_CONSTRUCT);
50 tinfo_key = TINFO_indexed;
53 void indexed::copy(const indexed & other)
55 inherited::copy(other);
56 symmetry = other.symmetry;
59 DEFAULT_DESTROY(indexed)
65 indexed::indexed(const ex & b) : inherited(b), symmetry(unknown)
67 debugmsg("indexed constructor from ex", LOGLEVEL_CONSTRUCT);
68 tinfo_key = TINFO_indexed;
69 assert_all_indices_of_type_idx();
72 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symmetry(unknown)
74 debugmsg("indexed constructor from ex,ex", LOGLEVEL_CONSTRUCT);
75 tinfo_key = TINFO_indexed;
76 assert_all_indices_of_type_idx();
79 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(unknown)
81 debugmsg("indexed constructor from ex,ex,ex", LOGLEVEL_CONSTRUCT);
82 tinfo_key = TINFO_indexed;
83 assert_all_indices_of_type_idx();
86 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(unknown)
88 debugmsg("indexed constructor from ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
89 tinfo_key = TINFO_indexed;
90 assert_all_indices_of_type_idx();
93 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symmetry(unknown)
95 debugmsg("indexed constructor from ex,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
96 tinfo_key = TINFO_indexed;
97 assert_all_indices_of_type_idx();
100 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(symm)
102 debugmsg("indexed constructor from ex,symmetry,ex,ex", LOGLEVEL_CONSTRUCT);
103 tinfo_key = TINFO_indexed;
104 assert_all_indices_of_type_idx();
107 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(symm)
109 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex", LOGLEVEL_CONSTRUCT);
110 tinfo_key = TINFO_indexed;
111 assert_all_indices_of_type_idx();
114 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symmetry(symm)
116 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
117 tinfo_key = TINFO_indexed;
118 assert_all_indices_of_type_idx();
121 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symmetry(unknown)
123 debugmsg("indexed constructor from ex,exvector", LOGLEVEL_CONSTRUCT);
124 seq.insert(seq.end(), v.begin(), v.end());
125 tinfo_key = TINFO_indexed;
126 assert_all_indices_of_type_idx();
129 indexed::indexed(const ex & b, symmetry_type symm, const exvector & v) : inherited(b), symmetry(symm)
131 debugmsg("indexed constructor from ex,symmetry,exvector", LOGLEVEL_CONSTRUCT);
132 seq.insert(seq.end(), v.begin(), v.end());
133 tinfo_key = TINFO_indexed;
134 assert_all_indices_of_type_idx();
137 indexed::indexed(symmetry_type symm, const exprseq & es) : inherited(es), symmetry(symm)
139 debugmsg("indexed constructor from symmetry,exprseq", LOGLEVEL_CONSTRUCT);
140 tinfo_key = TINFO_indexed;
141 assert_all_indices_of_type_idx();
144 indexed::indexed(symmetry_type symm, const exvector & v, bool discardable) : inherited(v, discardable), symmetry(symm)
146 debugmsg("indexed constructor from symmetry,exvector", LOGLEVEL_CONSTRUCT);
147 tinfo_key = TINFO_indexed;
148 assert_all_indices_of_type_idx();
151 indexed::indexed(symmetry_type symm, exvector * vp) : inherited(vp), symmetry(symm)
153 debugmsg("indexed constructor from symmetry,exvector *", LOGLEVEL_CONSTRUCT);
154 tinfo_key = TINFO_indexed;
155 assert_all_indices_of_type_idx();
162 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
164 debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
166 if (!(n.find_unsigned("symmetry", symm)))
167 throw (std::runtime_error("unknown indexed symmetry type in archive"));
170 void indexed::archive(archive_node &n) const
172 inherited::archive(n);
173 n.add_unsigned("symmetry", symmetry);
176 DEFAULT_UNARCHIVE(indexed)
179 // functions overriding virtual functions from bases classes
182 void indexed::print(const print_context & c, unsigned level) const
184 debugmsg("indexed print", LOGLEVEL_PRINT);
185 GINAC_ASSERT(seq.size() > 0);
187 if (is_of_type(c, print_tree)) {
189 c.s << std::string(level, ' ') << class_name()
190 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
191 << ", " << seq.size()-1 << " indices";
193 case symmetric: c.s << ", symmetric"; break;
194 case antisymmetric: c.s << ", antisymmetric"; break;
198 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
199 seq[0].print(c, level + delta_indent);
200 printindices(c, level + delta_indent);
204 bool is_tex = is_of_type(c, print_latex);
205 const ex & base = seq[0];
206 bool need_parens = is_ex_exactly_of_type(base, add) || is_ex_exactly_of_type(base, mul)
207 || is_ex_exactly_of_type(base, ncmul) || is_ex_exactly_of_type(base, power)
208 || is_ex_of_type(base, indexed);
218 printindices(c, level);
222 bool indexed::info(unsigned inf) const
224 if (inf == info_flags::indexed) return true;
225 if (inf == info_flags::has_indices) return seq.size() > 1;
226 return inherited::info(inf);
229 struct idx_is_not : public binary_function<ex, unsigned, bool> {
230 bool operator() (const ex & e, unsigned inf) const {
231 return !(ex_to_idx(e).get_value().info(inf));
235 bool indexed::all_index_values_are(unsigned inf) const
237 // No indices? Then no property can be fulfilled
242 return find_if(seq.begin() + 1, seq.end(), bind2nd(idx_is_not(), inf)) == seq.end();
245 int indexed::compare_same_type(const basic & other) const
247 GINAC_ASSERT(is_of_type(other, indexed));
248 return inherited::compare_same_type(other);
251 // The main difference between sort_index_vector() and canonicalize_indices()
252 // is that the latter takes the symmetry of the object into account. Once we
253 // implement mixed symmetries, canonicalize_indices() will only be able to
254 // reorder index pairs with known symmetry properties, while sort_index_vector()
255 // always sorts the whole vector.
257 /** Bring a vector of indices into a canonic order. This operation only makes
258 * sense if the object carrying these indices is either symmetric or totally
259 * antisymmetric with respect to the indices.
261 * @param itbegin Start of index vector
262 * @param itend End of index vector
263 * @param antisymm Whether the object is antisymmetric
264 * @return the sign introduced by the reordering of the indices if the object
265 * is antisymmetric (or 0 if two equal indices are encountered). For
266 * symmetric objects, this is always +1. If the index vector was
267 * already in a canonic order this function returns INT_MAX. */
268 static int canonicalize_indices(exvector::iterator itbegin, exvector::iterator itend, bool antisymm)
270 bool something_changed = false;
273 // Simple bubble sort algorithm should be sufficient for the small
274 // number of indices expected
275 exvector::iterator it1 = itbegin, next_to_last_idx = itend - 1;
276 while (it1 != next_to_last_idx) {
277 exvector::iterator it2 = it1 + 1;
278 while (it2 != itend) {
279 int cmpval = it1->compare(*it2);
282 something_changed = true;
285 } else if (cmpval == 0 && antisymm) {
286 something_changed = true;
294 return something_changed ? sig : INT_MAX;
297 ex indexed::eval(int level) const
299 // First evaluate children, then we will end up here again
301 return indexed(symmetry, evalchildren(level));
303 const ex &base = seq[0];
305 // If the base object is 0, the whole object is 0
309 // If the base object is a product, pull out the numeric factor
310 if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
312 ex f = ex_to_numeric(base.op(base.nops() - 1));
314 return f * thisexprseq(v);
317 // Canonicalize indices according to the symmetry properties
318 if (seq.size() > 2 && (symmetry != unknown && symmetry != mixed)) {
320 int sig = canonicalize_indices(v.begin() + 1, v.end(), symmetry == antisymmetric);
321 if (sig != INT_MAX) {
322 // Something has changed while sorting indices, more evaluations later
325 return ex(sig) * thisexprseq(v);
329 // Let the class of the base object perform additional evaluations
330 return base.bp->eval_indexed(*this);
333 int indexed::degree(const ex & s) const
335 return is_equal(*s.bp) ? 1 : 0;
338 int indexed::ldegree(const ex & s) const
340 return is_equal(*s.bp) ? 1 : 0;
343 ex indexed::coeff(const ex & s, int n) const
346 return n==1 ? _ex1() : _ex0();
348 return n==0 ? ex(*this) : _ex0();
351 ex indexed::thisexprseq(const exvector & v) const
353 return indexed(symmetry, v);
356 ex indexed::thisexprseq(exvector * vp) const
358 return indexed(symmetry, vp);
361 ex indexed::expand(unsigned options) const
363 GINAC_ASSERT(seq.size() > 0);
365 if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
367 // expand_indexed expands (a+b).i -> a.i + b.i
368 const ex & base = seq[0];
370 for (unsigned i=0; i<base.nops(); i++) {
373 sum += thisexprseq(s).expand();
378 return inherited::expand(options);
382 // virtual functions which can be overridden by derived classes
388 // non-virtual functions in this class
391 void indexed::printindices(const print_context & c, unsigned level) const
393 if (seq.size() > 1) {
395 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
397 if (is_of_type(c, print_latex)) {
399 // TeX output: group by variance
401 bool covariant = true;
403 while (it != itend) {
404 bool cur_covariant = (is_ex_of_type(*it, varidx) ? ex_to_varidx(*it).is_covariant() : true);
405 if (first || cur_covariant != covariant) {
408 covariant = cur_covariant;
424 while (it != itend) {
432 /** Check whether all indices are of class idx. This function is used
433 * internally to make sure that all constructed indexed objects really
434 * carry indices and not some other classes. */
435 void indexed::assert_all_indices_of_type_idx(void) const
437 GINAC_ASSERT(seq.size() > 0);
438 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
439 while (it != itend) {
440 if (!is_ex_of_type(*it, idx))
441 throw(std::invalid_argument("indices of indexed object must be of type idx"));
450 /** Check whether two sorted index vectors are consistent (i.e. equal). */
451 static bool indices_consistent(const exvector & v1, const exvector & v2)
453 // Number of indices must be the same
454 if (v1.size() != v2.size())
457 return equal(v1.begin(), v1.end(), v2.begin(), ex_is_equal());
460 exvector indexed::get_indices(void) const
462 GINAC_ASSERT(seq.size() >= 1);
463 return exvector(seq.begin() + 1, seq.end());
466 exvector indexed::get_dummy_indices(void) const
468 exvector free_indices, dummy_indices;
469 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
470 return dummy_indices;
473 exvector indexed::get_dummy_indices(const indexed & other) const
475 exvector indices = get_free_indices();
476 exvector other_indices = other.get_free_indices();
477 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
478 exvector dummy_indices;
479 find_dummy_indices(indices, dummy_indices);
480 return dummy_indices;
483 bool indexed::has_dummy_index_for(const ex & i) const
485 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
486 while (it != itend) {
487 if (is_dummy_pair(*it, i))
494 exvector indexed::get_free_indices(void) const
496 exvector free_indices, dummy_indices;
497 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
501 exvector add::get_free_indices(void) const
503 exvector free_indices;
504 for (unsigned i=0; i<nops(); i++) {
506 free_indices = op(i).get_free_indices();
508 exvector free_indices_of_term = op(i).get_free_indices();
509 if (!indices_consistent(free_indices, free_indices_of_term))
510 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
516 exvector mul::get_free_indices(void) const
518 // Concatenate free indices of all factors
520 for (unsigned 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 exvector ncmul::get_free_indices(void) const
533 // Concatenate free indices of all factors
535 for (unsigned i=0; i<nops(); i++) {
536 exvector free_indices_of_factor = op(i).get_free_indices();
537 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
540 // And remove the dummy indices
541 exvector free_indices, dummy_indices;
542 find_free_and_dummy(un, free_indices, dummy_indices);
546 exvector power::get_free_indices(void) const
548 // Return free indices of basis
549 return basis.get_free_indices();
552 /** Rename dummy indices in an expression.
554 * @param e Expression to be worked on
555 * @param local_dummy_indices The set of dummy indices that appear in the
557 * @param global_dummy_indices The set of dummy indices that have appeared
558 * before and which we would like to use in "e", too. This gets updated
560 static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
562 int global_size = global_dummy_indices.size(),
563 local_size = local_dummy_indices.size();
565 // Any local dummy indices at all?
569 if (global_size < local_size) {
571 // More local indices than we encountered before, add the new ones
573 int remaining = local_size - global_size;
574 exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
575 while (it != itend && remaining > 0) {
576 if (find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(ex_is_equal(), *it)) == global_dummy_indices.end()) {
577 global_dummy_indices.push_back(*it);
585 // Replace index symbols in expression
586 GINAC_ASSERT(local_size <= global_size);
587 bool all_equal = true;
588 lst local_syms, global_syms;
589 for (unsigned i=0; i<local_size; i++) {
590 ex loc_sym = local_dummy_indices[i].op(0);
591 ex glob_sym = global_dummy_indices[i].op(0);
592 if (!loc_sym.is_equal(glob_sym)) {
594 local_syms.append(loc_sym);
595 global_syms.append(glob_sym);
601 return e.subs(local_syms, global_syms);
604 /** Simplify product of indexed expressions (commutative, noncommutative and
605 * simple squares), return list of free indices. */
606 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
608 // Remember whether the product was commutative or noncommutative
609 // (because we chop it into factors and need to reassemble later)
610 bool non_commutative = is_ex_exactly_of_type(e, ncmul);
612 // Collect factors in an exvector, store squares twice
614 v.reserve(e.nops() * 2);
616 if (is_ex_exactly_of_type(e, power)) {
617 // We only get called for simple squares, split a^2 -> a*a
618 GINAC_ASSERT(e.op(1).is_equal(_ex2()));
619 v.push_back(e.op(0));
620 v.push_back(e.op(0));
622 for (int i=0; i<e.nops(); i++) {
624 if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2())) {
625 v.push_back(f.op(0));
626 v.push_back(f.op(0));
627 } else if (is_ex_exactly_of_type(f, ncmul)) {
628 // Noncommutative factor found, split it as well
629 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
630 for (int j=0; j<f.nops(); j++)
631 v.push_back(f.op(j));
637 // Perform contractions
638 bool something_changed = false;
639 GINAC_ASSERT(v.size() > 1);
640 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
641 for (it1 = v.begin(); it1 != next_to_last; it1++) {
644 if (!is_ex_of_type(*it1, indexed))
647 bool first_noncommutative = (it1->return_type() != return_types::commutative);
649 // Indexed factor found, get free indices and look for contraction
651 exvector free1, dummy1;
652 find_free_and_dummy(ex_to_indexed(*it1).seq.begin() + 1, ex_to_indexed(*it1).seq.end(), free1, dummy1);
654 exvector::iterator it2;
655 for (it2 = it1 + 1; it2 != itend; it2++) {
657 if (!is_ex_of_type(*it2, indexed))
660 bool second_noncommutative = (it2->return_type() != return_types::commutative);
662 // Find free indices of second factor and merge them with free
663 // indices of first factor
665 find_free_and_dummy(ex_to_indexed(*it2).seq.begin() + 1, ex_to_indexed(*it2).seq.end(), un, dummy1);
666 un.insert(un.end(), free1.begin(), free1.end());
668 // Check whether the two factors share dummy indices
669 exvector free, dummy;
670 find_free_and_dummy(un, free, dummy);
671 if (dummy.size() == 0)
674 // At least one dummy index, is it a defined scalar product?
675 bool contracted = false;
676 if (free.size() == 0) {
677 if (sp.is_defined(*it1, *it2)) {
678 *it1 = sp.evaluate(*it1, *it2);
680 goto contraction_done;
684 // Contraction of symmetric with antisymmetric object is zero
685 if ((ex_to_indexed(*it1).symmetry == indexed::symmetric &&
686 ex_to_indexed(*it2).symmetry == indexed::antisymmetric
687 || ex_to_indexed(*it1).symmetry == indexed::antisymmetric &&
688 ex_to_indexed(*it2).symmetry == indexed::symmetric)
689 && dummy.size() > 1) {
690 free_indices.clear();
694 // Try to contract the first one with the second one
695 contracted = it1->op(0).bp->contract_with(it1, it2, v);
698 // That didn't work; maybe the second object knows how to
699 // contract itself with the first one
700 contracted = it2->op(0).bp->contract_with(it2, it1, v);
704 if (first_noncommutative || second_noncommutative
705 || is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
706 || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)
707 || is_ex_exactly_of_type(*it1, ncmul) || is_ex_exactly_of_type(*it2, ncmul)) {
709 // One of the factors became a sum or product:
710 // re-expand expression and run again
711 // Non-commutative products are always re-expanded to give
712 // simplify_ncmul() the chance to re-order and canonicalize
714 ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
715 return simplify_indexed(r, free_indices, dummy_indices, sp);
718 // Both objects may have new indices now or they might
719 // even not be indexed objects any more, so we have to
721 something_changed = true;
727 // Find free indices (concatenate them all and call find_free_and_dummy())
728 // and all dummy indices that appear
729 exvector un, individual_dummy_indices;
730 it1 = v.begin(); itend = v.end();
731 while (it1 != itend) {
732 exvector free_indices_of_factor;
733 if (is_ex_of_type(*it1, indexed)) {
734 exvector dummy_indices_of_factor;
735 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);
736 individual_dummy_indices.insert(individual_dummy_indices.end(), dummy_indices_of_factor.begin(), dummy_indices_of_factor.end());
738 free_indices_of_factor = it1->get_free_indices();
739 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
742 exvector local_dummy_indices;
743 find_free_and_dummy(un, free_indices, local_dummy_indices);
744 local_dummy_indices.insert(local_dummy_indices.end(), individual_dummy_indices.begin(), individual_dummy_indices.end());
747 if (something_changed)
748 r = non_commutative ? ex(ncmul(v, true)) : ex(mul(v));
752 // Dummy index renaming
753 r = rename_dummy_indices(r, dummy_indices, local_dummy_indices);
755 // Product of indexed object with a scalar?
756 if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
757 && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
758 return r.op(0).op(0).bp->scalar_mul_indexed(r.op(0), ex_to_numeric(r.op(1)));
763 /** Simplify indexed expression, return list of free indices. */
764 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
766 // Expand the expression
767 ex e_expanded = e.expand();
769 // Simplification of single indexed object: just find the free indices
770 // and perform dummy index renaming
771 if (is_ex_of_type(e_expanded, indexed)) {
772 const indexed &i = ex_to_indexed(e_expanded);
773 exvector local_dummy_indices;
774 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
775 return rename_dummy_indices(e_expanded, dummy_indices, local_dummy_indices);
778 // Simplification of sum = sum of simplifications, check consistency of
779 // free indices in each term
780 if (is_ex_exactly_of_type(e_expanded, add)) {
783 free_indices.clear();
785 for (unsigned i=0; i<e_expanded.nops(); i++) {
786 exvector free_indices_of_term;
787 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
788 if (!term.is_zero()) {
790 free_indices = free_indices_of_term;
794 if (!indices_consistent(free_indices, free_indices_of_term))
795 throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
796 if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
797 sum = sum.op(0).bp->add_indexed(sum, term);
807 // Simplification of products
808 if (is_ex_exactly_of_type(e_expanded, mul)
809 || is_ex_exactly_of_type(e_expanded, ncmul)
810 || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2())))
811 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
813 // Cannot do anything
814 free_indices.clear();
818 ex simplify_indexed(const ex & e)
820 exvector free_indices, dummy_indices;
822 return simplify_indexed(e, free_indices, dummy_indices, sp);
825 ex simplify_indexed(const ex & e, const scalar_products & sp)
827 exvector free_indices, dummy_indices;
828 return simplify_indexed(e, free_indices, dummy_indices, sp);
831 ex symmetrize(const ex & e)
833 return symmetrize(e, e.get_free_indices());
836 ex antisymmetrize(const ex & e)
838 return antisymmetrize(e, e.get_free_indices());
845 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
847 spm[make_key(v1, v2)] = sp;
850 void scalar_products::add_vectors(const lst & l)
852 // Add all possible pairs of products
853 unsigned num = l.nops();
854 for (unsigned i=0; i<num; i++) {
856 for (unsigned j=0; j<num; j++) {
863 void scalar_products::clear(void)
868 /** Check whether scalar product pair is defined. */
869 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
871 return spm.find(make_key(v1, v2)) != spm.end();
874 /** Return value of defined scalar product pair. */
875 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
877 return spm.find(make_key(v1, v2))->second;
880 void scalar_products::debugprint(void) const
882 std::cerr << "map size=" << spm.size() << std::endl;
883 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
884 const spmapkey & k = cit->first;
885 std::cerr << "item key=(" << k.first << "," << k.second;
886 std::cerr << "), value=" << cit->second << std::endl;
890 /** Make key from object pair. */
891 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
893 // If indexed, extract base objects
894 ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
895 ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
897 // Enforce canonical order in pair
898 if (s1.compare(s2) > 0)
899 return spmapkey(s2, s1);
901 return spmapkey(s1, s2);