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() : symtree(sy_none())
49 debugmsg("indexed default constructor", LOGLEVEL_CONSTRUCT);
50 tinfo_key = TINFO_indexed;
53 void indexed::copy(const indexed & other)
55 inherited::copy(other);
56 symtree = other.symtree;
59 DEFAULT_DESTROY(indexed)
65 indexed::indexed(const ex & b) : inherited(b), symtree(sy_none())
67 debugmsg("indexed constructor from ex", LOGLEVEL_CONSTRUCT);
68 tinfo_key = TINFO_indexed;
72 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(sy_none())
74 debugmsg("indexed constructor from ex,ex", LOGLEVEL_CONSTRUCT);
75 tinfo_key = TINFO_indexed;
79 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(sy_none())
81 debugmsg("indexed constructor from ex,ex,ex", LOGLEVEL_CONSTRUCT);
82 tinfo_key = TINFO_indexed;
86 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(sy_none())
88 debugmsg("indexed constructor from ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
89 tinfo_key = TINFO_indexed;
93 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symtree(sy_none())
95 debugmsg("indexed constructor from ex,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
96 tinfo_key = TINFO_indexed;
100 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(symm)
102 debugmsg("indexed constructor from ex,symmetry,ex,ex", LOGLEVEL_CONSTRUCT);
103 tinfo_key = TINFO_indexed;
107 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(symm)
109 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex", LOGLEVEL_CONSTRUCT);
110 tinfo_key = TINFO_indexed;
114 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)
116 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
117 tinfo_key = TINFO_indexed;
121 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(sy_none())
123 debugmsg("indexed constructor from ex,exvector", LOGLEVEL_CONSTRUCT);
124 seq.insert(seq.end(), v.begin(), v.end());
125 tinfo_key = TINFO_indexed;
129 indexed::indexed(const ex & b, const symmetry & symm, const exvector & v) : inherited(b), symtree(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;
137 indexed::indexed(const symmetry & symm, const exprseq & es) : inherited(es), symtree(symm)
139 debugmsg("indexed constructor from symmetry,exprseq", LOGLEVEL_CONSTRUCT);
140 tinfo_key = TINFO_indexed;
143 indexed::indexed(const symmetry & symm, const exvector & v, bool discardable) : inherited(v, discardable), symtree(symm)
145 debugmsg("indexed constructor from symmetry,exvector", LOGLEVEL_CONSTRUCT);
146 tinfo_key = TINFO_indexed;
149 indexed::indexed(const symmetry & symm, exvector * vp) : inherited(vp), symtree(symm)
151 debugmsg("indexed constructor from symmetry,exvector *", LOGLEVEL_CONSTRUCT);
152 tinfo_key = TINFO_indexed;
159 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
161 debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
162 if (!n.find_ex("symmetry", symtree, sym_lst)) {
163 // GiNaC versions <= 0.9.0 had an unsigned "symmetry" property
165 n.find_unsigned("symmetry", symm);
177 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
181 void indexed::archive(archive_node &n) const
183 inherited::archive(n);
184 n.add_ex("symmetry", symtree);
187 DEFAULT_UNARCHIVE(indexed)
190 // functions overriding virtual functions from base classes
193 void indexed::print(const print_context & c, unsigned level) const
195 debugmsg("indexed print", LOGLEVEL_PRINT);
196 GINAC_ASSERT(seq.size() > 0);
198 if (is_of_type(c, print_tree)) {
200 c.s << std::string(level, ' ') << class_name()
201 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
202 << ", " << seq.size()-1 << " indices"
203 << ", symmetry=" << symtree << std::endl;
204 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
205 seq[0].print(c, level + delta_indent);
206 printindices(c, level + delta_indent);
210 bool is_tex = is_of_type(c, print_latex);
211 const ex & base = seq[0];
212 bool need_parens = is_ex_exactly_of_type(base, add) || is_ex_exactly_of_type(base, mul)
213 || is_ex_exactly_of_type(base, ncmul) || is_ex_exactly_of_type(base, power)
214 || is_ex_of_type(base, indexed);
224 printindices(c, level);
228 bool indexed::info(unsigned inf) const
230 if (inf == info_flags::indexed) return true;
231 if (inf == info_flags::has_indices) return seq.size() > 1;
232 return inherited::info(inf);
235 struct idx_is_not : public std::binary_function<ex, unsigned, bool> {
236 bool operator() (const ex & e, unsigned inf) const {
237 return !(ex_to<idx>(e).get_value().info(inf));
241 bool indexed::all_index_values_are(unsigned inf) const
243 // No indices? Then no property can be fulfilled
248 return find_if(seq.begin() + 1, seq.end(), bind2nd(idx_is_not(), inf)) == seq.end();
251 int indexed::compare_same_type(const basic & other) const
253 GINAC_ASSERT(is_a<indexed>(other));
254 return inherited::compare_same_type(other);
257 ex indexed::eval(int level) const
259 // First evaluate children, then we will end up here again
261 return indexed(ex_to<symmetry>(symtree), evalchildren(level));
263 const ex &base = seq[0];
265 // If the base object is 0, the whole object is 0
269 // If the base object is a product, pull out the numeric factor
270 if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
272 ex f = ex_to<numeric>(base.op(base.nops() - 1));
274 return f * thisexprseq(v);
277 // Canonicalize indices according to the symmetry properties
278 if (seq.size() > 2) {
280 GINAC_ASSERT(is_exactly_a<symmetry>(symtree));
281 int sig = canonicalize(v.begin() + 1, ex_to<symmetry>(symtree));
282 if (sig != INT_MAX) {
283 // Something has changed while sorting indices, more evaluations later
286 return ex(sig) * thisexprseq(v);
290 // Let the class of the base object perform additional evaluations
291 return ex_to<basic>(base).eval_indexed(*this);
294 int indexed::degree(const ex & s) const
296 return is_equal(ex_to<basic>(s)) ? 1 : 0;
299 int indexed::ldegree(const ex & s) const
301 return is_equal(ex_to<basic>(s)) ? 1 : 0;
304 ex indexed::coeff(const ex & s, int n) const
306 if (is_equal(ex_to<basic>(s)))
307 return n==1 ? _ex1() : _ex0();
309 return n==0 ? ex(*this) : _ex0();
312 ex indexed::thisexprseq(const exvector & v) const
314 return indexed(ex_to<symmetry>(symtree), v);
317 ex indexed::thisexprseq(exvector * vp) const
319 return indexed(ex_to<symmetry>(symtree), vp);
322 ex indexed::expand(unsigned options) const
324 GINAC_ASSERT(seq.size() > 0);
326 if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
328 // expand_indexed expands (a+b).i -> a.i + b.i
329 const ex & base = seq[0];
331 for (unsigned i=0; i<base.nops(); i++) {
334 sum += thisexprseq(s).expand();
339 return inherited::expand(options);
343 // virtual functions which can be overridden by derived classes
349 // non-virtual functions in this class
352 void indexed::printindices(const print_context & c, unsigned level) const
354 if (seq.size() > 1) {
356 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
358 if (is_of_type(c, print_latex)) {
360 // TeX output: group by variance
362 bool covariant = true;
364 while (it != itend) {
365 bool cur_covariant = (is_ex_of_type(*it, varidx) ? ex_to<varidx>(*it).is_covariant() : true);
366 if (first || cur_covariant != covariant) {
369 covariant = cur_covariant;
385 while (it != itend) {
393 /** Check whether all indices are of class idx and validate the symmetry
394 * tree. This function is used internally to make sure that all constructed
395 * indexed objects really carry indices and not some other classes. */
396 void indexed::validate(void) const
398 GINAC_ASSERT(seq.size() > 0);
399 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
400 while (it != itend) {
401 if (!is_ex_of_type(*it, idx))
402 throw(std::invalid_argument("indices of indexed object must be of type idx"));
406 if (!symtree.is_zero()) {
407 if (!is_ex_exactly_of_type(symtree, symmetry))
408 throw(std::invalid_argument("symmetry of indexed object must be of type symmetry"));
409 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
413 /** Implementation of ex::diff() for an indexed object always returns 0.
416 ex indexed::derivative(const symbol & s) const
425 /** Check whether two sorted index vectors are consistent (i.e. equal). */
426 static bool indices_consistent(const exvector & v1, const exvector & v2)
428 // Number of indices must be the same
429 if (v1.size() != v2.size())
432 return equal(v1.begin(), v1.end(), v2.begin(), ex_is_equal());
435 exvector indexed::get_indices(void) const
437 GINAC_ASSERT(seq.size() >= 1);
438 return exvector(seq.begin() + 1, seq.end());
441 exvector indexed::get_dummy_indices(void) const
443 exvector free_indices, dummy_indices;
444 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
445 return dummy_indices;
448 exvector indexed::get_dummy_indices(const indexed & other) const
450 exvector indices = get_free_indices();
451 exvector other_indices = other.get_free_indices();
452 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
453 exvector dummy_indices;
454 find_dummy_indices(indices, dummy_indices);
455 return dummy_indices;
458 bool indexed::has_dummy_index_for(const ex & i) const
460 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
461 while (it != itend) {
462 if (is_dummy_pair(*it, i))
469 exvector indexed::get_free_indices(void) const
471 exvector free_indices, dummy_indices;
472 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
476 exvector add::get_free_indices(void) const
478 exvector free_indices;
479 for (unsigned i=0; i<nops(); i++) {
481 free_indices = op(i).get_free_indices();
483 exvector free_indices_of_term = op(i).get_free_indices();
484 if (!indices_consistent(free_indices, free_indices_of_term))
485 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
491 exvector mul::get_free_indices(void) const
493 // Concatenate free indices of all factors
495 for (unsigned i=0; i<nops(); i++) {
496 exvector free_indices_of_factor = op(i).get_free_indices();
497 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
500 // And remove the dummy indices
501 exvector free_indices, dummy_indices;
502 find_free_and_dummy(un, free_indices, dummy_indices);
506 exvector ncmul::get_free_indices(void) const
508 // Concatenate free indices of all factors
510 for (unsigned i=0; i<nops(); i++) {
511 exvector free_indices_of_factor = op(i).get_free_indices();
512 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
515 // And remove the dummy indices
516 exvector free_indices, dummy_indices;
517 find_free_and_dummy(un, free_indices, dummy_indices);
521 exvector power::get_free_indices(void) const
523 // Return free indices of basis
524 return basis.get_free_indices();
527 /** Rename dummy indices in an expression.
529 * @param e Expression to be worked on
530 * @param local_dummy_indices The set of dummy indices that appear in the
532 * @param global_dummy_indices The set of dummy indices that have appeared
533 * before and which we would like to use in "e", too. This gets updated
535 static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
537 unsigned global_size = global_dummy_indices.size(),
538 local_size = local_dummy_indices.size();
540 // Any local dummy indices at all?
544 if (global_size < local_size) {
546 // More local indices than we encountered before, add the new ones
548 int old_global_size = global_size;
549 int remaining = local_size - global_size;
550 exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
551 while (it != itend && remaining > 0) {
552 if (find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(ex_is_equal(), *it)) == global_dummy_indices.end()) {
553 global_dummy_indices.push_back(*it);
559 shaker_sort(global_dummy_indices.begin(), global_dummy_indices.end(), ex_is_less(), ex_swap());
561 // If this is the first set of local indices, do nothing
562 if (old_global_size == 0)
565 GINAC_ASSERT(local_size <= global_size);
567 // Construct lists of index symbols
568 exlist local_syms, global_syms;
569 for (unsigned i=0; i<local_size; i++)
570 local_syms.push_back(local_dummy_indices[i].op(0));
571 shaker_sort(local_syms.begin(), local_syms.end(), ex_is_less(), ex_swap());
572 for (unsigned i=0; i<global_size; i++)
573 global_syms.push_back(global_dummy_indices[i].op(0));
575 // Remove common indices
576 exlist local_uniq, global_uniq;
577 set_difference(local_syms.begin(), local_syms.end(), global_syms.begin(), global_syms.end(), std::back_insert_iterator<exlist>(local_uniq), ex_is_less());
578 set_difference(global_syms.begin(), global_syms.end(), local_syms.begin(), local_syms.end(), std::back_insert_iterator<exlist>(global_uniq), ex_is_less());
580 // Replace remaining non-common local index symbols by global ones
581 if (local_uniq.empty())
584 while (global_uniq.size() > local_uniq.size())
585 global_uniq.pop_back();
586 return e.subs(lst(local_uniq), lst(global_uniq));
590 /** Simplify product of indexed expressions (commutative, noncommutative and
591 * simple squares), return list of free indices. */
592 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
594 // Remember whether the product was commutative or noncommutative
595 // (because we chop it into factors and need to reassemble later)
596 bool non_commutative = is_ex_exactly_of_type(e, ncmul);
598 // Collect factors in an exvector, store squares twice
600 v.reserve(e.nops() * 2);
602 if (is_ex_exactly_of_type(e, power)) {
603 // We only get called for simple squares, split a^2 -> a*a
604 GINAC_ASSERT(e.op(1).is_equal(_ex2()));
605 v.push_back(e.op(0));
606 v.push_back(e.op(0));
608 for (unsigned i=0; i<e.nops(); i++) {
610 if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2())) {
611 v.push_back(f.op(0));
612 v.push_back(f.op(0));
613 } else if (is_ex_exactly_of_type(f, ncmul)) {
614 // Noncommutative factor found, split it as well
615 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
616 for (unsigned j=0; j<f.nops(); j++)
617 v.push_back(f.op(j));
623 // Perform contractions
624 bool something_changed = false;
625 GINAC_ASSERT(v.size() > 1);
626 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
627 for (it1 = v.begin(); it1 != next_to_last; it1++) {
630 if (!is_ex_of_type(*it1, indexed))
633 bool first_noncommutative = (it1->return_type() != return_types::commutative);
635 // Indexed factor found, get free indices and look for contraction
637 exvector free1, dummy1;
638 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
640 exvector::iterator it2;
641 for (it2 = it1 + 1; it2 != itend; it2++) {
643 if (!is_ex_of_type(*it2, indexed))
646 bool second_noncommutative = (it2->return_type() != return_types::commutative);
648 // Find free indices of second factor and merge them with free
649 // indices of first factor
651 find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
652 un.insert(un.end(), free1.begin(), free1.end());
654 // Check whether the two factors share dummy indices
655 exvector free, dummy;
656 find_free_and_dummy(un, free, dummy);
657 unsigned num_dummies = dummy.size();
658 if (num_dummies == 0)
661 // At least one dummy index, is it a defined scalar product?
662 bool contracted = false;
664 if (sp.is_defined(*it1, *it2)) {
665 *it1 = sp.evaluate(*it1, *it2);
667 goto contraction_done;
671 // Try to contract the first one with the second one
672 contracted = ex_to<basic>(it1->op(0)).contract_with(it1, it2, v);
675 // That didn't work; maybe the second object knows how to
676 // contract itself with the first one
677 contracted = ex_to<basic>(it2->op(0)).contract_with(it2, it1, v);
681 if (first_noncommutative || second_noncommutative
682 || is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
683 || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)
684 || is_ex_exactly_of_type(*it1, ncmul) || is_ex_exactly_of_type(*it2, ncmul)) {
686 // One of the factors became a sum or product:
687 // re-expand expression and run again
688 // Non-commutative products are always re-expanded to give
689 // simplify_ncmul() the chance to re-order and canonicalize
691 ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
692 return simplify_indexed(r, free_indices, dummy_indices, sp);
695 // Both objects may have new indices now or they might
696 // even not be indexed objects any more, so we have to
698 something_changed = true;
704 // Find free indices (concatenate them all and call find_free_and_dummy())
705 // and all dummy indices that appear
706 exvector un, individual_dummy_indices;
707 it1 = v.begin(); itend = v.end();
708 while (it1 != itend) {
709 exvector free_indices_of_factor;
710 if (is_ex_of_type(*it1, indexed)) {
711 exvector dummy_indices_of_factor;
712 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);
713 individual_dummy_indices.insert(individual_dummy_indices.end(), dummy_indices_of_factor.begin(), dummy_indices_of_factor.end());
715 free_indices_of_factor = it1->get_free_indices();
716 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
719 exvector local_dummy_indices;
720 find_free_and_dummy(un, free_indices, local_dummy_indices);
721 local_dummy_indices.insert(local_dummy_indices.end(), individual_dummy_indices.begin(), individual_dummy_indices.end());
724 if (something_changed)
725 r = non_commutative ? ex(ncmul(v, true)) : ex(mul(v));
729 // The result should be symmetric with respect to exchange of dummy
730 // indices, so if the symmetrization vanishes, the whole expression is
731 // zero. This detects things like eps.i.j.k * p.j * p.k = 0.
732 if (local_dummy_indices.size() >= 2) {
734 for (int i=0; i<local_dummy_indices.size(); i++)
735 dummy_syms.append(local_dummy_indices[i].op(0));
736 if (r.symmetrize(dummy_syms).is_zero()) {
737 free_indices.clear();
742 // Dummy index renaming
743 r = rename_dummy_indices(r, dummy_indices, local_dummy_indices);
745 // Product of indexed object with a scalar?
746 if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
747 && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
748 return ex_to<basic>(r.op(0).op(0)).scalar_mul_indexed(r.op(0), ex_to<numeric>(r.op(1)));
753 /** Simplify indexed expression, return list of free indices. */
754 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
756 // Expand the expression
757 ex e_expanded = e.expand();
759 // Simplification of single indexed object: just find the free indices
760 // and perform dummy index renaming
761 if (is_ex_of_type(e_expanded, indexed)) {
762 const indexed &i = ex_to<indexed>(e_expanded);
763 exvector local_dummy_indices;
764 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
765 return rename_dummy_indices(e_expanded, dummy_indices, local_dummy_indices);
768 // Simplification of sum = sum of simplifications, check consistency of
769 // free indices in each term
770 if (is_ex_exactly_of_type(e_expanded, add)) {
773 free_indices.clear();
775 for (unsigned i=0; i<e_expanded.nops(); i++) {
776 exvector free_indices_of_term;
777 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
778 if (!term.is_zero()) {
780 free_indices = free_indices_of_term;
784 if (!indices_consistent(free_indices, free_indices_of_term))
785 throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
786 if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
787 sum = ex_to<basic>(sum.op(0)).add_indexed(sum, term);
797 // Simplification of products
798 if (is_ex_exactly_of_type(e_expanded, mul)
799 || is_ex_exactly_of_type(e_expanded, ncmul)
800 || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2())))
801 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
803 // Cannot do anything
804 free_indices.clear();
808 /** Simplify/canonicalize expression containing indexed objects. This
809 * performs contraction of dummy indices where possible and checks whether
810 * the free indices in sums are consistent.
812 * @return simplified expression */
813 ex ex::simplify_indexed(void) const
815 exvector free_indices, dummy_indices;
817 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
820 /** Simplify/canonicalize expression containing indexed objects. This
821 * performs contraction of dummy indices where possible, checks whether
822 * the free indices in sums are consistent, and automatically replaces
823 * scalar products by known values if desired.
825 * @param sp Scalar products to be replaced automatically
826 * @return simplified expression */
827 ex ex::simplify_indexed(const scalar_products & sp) const
829 exvector free_indices, dummy_indices;
830 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
833 /** Symmetrize expression over its free indices. */
834 ex ex::symmetrize(void) const
836 return GiNaC::symmetrize(*this, get_free_indices());
839 /** Antisymmetrize expression over its free indices. */
840 ex ex::antisymmetrize(void) const
842 return GiNaC::antisymmetrize(*this, get_free_indices());
845 /** Symmetrize expression by cyclic permutation over its free indices. */
846 ex ex::symmetrize_cyclic(void) const
848 return GiNaC::symmetrize_cyclic(*this, get_free_indices());
855 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
857 spm[make_key(v1, v2)] = sp;
860 void scalar_products::add_vectors(const lst & l)
862 // Add all possible pairs of products
863 unsigned num = l.nops();
864 for (unsigned i=0; i<num; i++) {
866 for (unsigned j=0; j<num; j++) {
873 void scalar_products::clear(void)
878 /** Check whether scalar product pair is defined. */
879 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
881 return spm.find(make_key(v1, v2)) != spm.end();
884 /** Return value of defined scalar product pair. */
885 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
887 return spm.find(make_key(v1, v2))->second;
890 void scalar_products::debugprint(void) const
892 std::cerr << "map size=" << spm.size() << std::endl;
893 spmap::const_iterator i = spm.begin(), end = spm.end();
895 const spmapkey & k = i->first;
896 std::cerr << "item key=(" << k.first << "," << k.second;
897 std::cerr << "), value=" << i->second << std::endl;
902 /** Make key from object pair. */
903 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
905 // If indexed, extract base objects
906 ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
907 ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
909 // Enforce canonical order in pair
910 if (s1.compare(s2) > 0)
911 return spmapkey(s2, s1);
913 return spmapkey(s1, s2);