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 ex_to_nonconst_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 bases 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;
205 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
206 seq[0].print(c, level + delta_indent);
207 printindices(c, level + delta_indent);
211 bool is_tex = is_of_type(c, print_latex);
212 const ex & base = seq[0];
213 bool need_parens = is_ex_exactly_of_type(base, add) || is_ex_exactly_of_type(base, mul)
214 || is_ex_exactly_of_type(base, ncmul) || is_ex_exactly_of_type(base, power)
215 || is_ex_of_type(base, indexed);
225 printindices(c, level);
229 bool indexed::info(unsigned inf) const
231 if (inf == info_flags::indexed) return true;
232 if (inf == info_flags::has_indices) return seq.size() > 1;
233 return inherited::info(inf);
236 struct idx_is_not : public std::binary_function<ex, unsigned, bool> {
237 bool operator() (const ex & e, unsigned inf) const {
238 return !(ex_to<idx>(e).get_value().info(inf));
242 bool indexed::all_index_values_are(unsigned inf) const
244 // No indices? Then no property can be fulfilled
249 return find_if(seq.begin() + 1, seq.end(), bind2nd(idx_is_not(), inf)) == seq.end();
252 int indexed::compare_same_type(const basic & other) const
254 GINAC_ASSERT(is_of_type(other, indexed));
255 return inherited::compare_same_type(other);
258 ex indexed::eval(int level) const
260 // First evaluate children, then we will end up here again
262 return indexed(ex_to<symmetry>(symtree), evalchildren(level));
264 const ex &base = seq[0];
266 // If the base object is 0, the whole object is 0
270 // If the base object is a product, pull out the numeric factor
271 if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
273 ex f = ex_to<numeric>(base.op(base.nops() - 1));
275 return f * thisexprseq(v);
278 // Canonicalize indices according to the symmetry properties
279 if (seq.size() > 2) {
281 GINAC_ASSERT(is_ex_exactly_of_type(symtree, symmetry));
282 int sig = canonicalize(v.begin() + 1, ex_to<symmetry>(symtree));
283 if (sig != INT_MAX) {
284 // Something has changed while sorting indices, more evaluations later
287 return ex(sig) * thisexprseq(v);
291 // Let the class of the base object perform additional evaluations
292 return base.bp->eval_indexed(*this);
295 int indexed::degree(const ex & s) const
297 return is_equal(*s.bp) ? 1 : 0;
300 int indexed::ldegree(const ex & s) const
302 return is_equal(*s.bp) ? 1 : 0;
305 ex indexed::coeff(const ex & s, int n) const
308 return n==1 ? _ex1() : _ex0();
310 return n==0 ? ex(*this) : _ex0();
313 ex indexed::thisexprseq(const exvector & v) const
315 return indexed(ex_to<symmetry>(symtree), v);
318 ex indexed::thisexprseq(exvector * vp) const
320 return indexed(ex_to<symmetry>(symtree), vp);
323 ex indexed::expand(unsigned options) const
325 GINAC_ASSERT(seq.size() > 0);
327 if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
329 // expand_indexed expands (a+b).i -> a.i + b.i
330 const ex & base = seq[0];
332 for (unsigned i=0; i<base.nops(); i++) {
335 sum += thisexprseq(s).expand();
340 return inherited::expand(options);
344 // virtual functions which can be overridden by derived classes
350 // non-virtual functions in this class
353 void indexed::printindices(const print_context & c, unsigned level) const
355 if (seq.size() > 1) {
357 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
359 if (is_of_type(c, print_latex)) {
361 // TeX output: group by variance
363 bool covariant = true;
365 while (it != itend) {
366 bool cur_covariant = (is_ex_of_type(*it, varidx) ? ex_to<varidx>(*it).is_covariant() : true);
367 if (first || cur_covariant != covariant) {
370 covariant = cur_covariant;
386 while (it != itend) {
394 /** Check whether all indices are of class idx and validate the symmetry
395 * tree. This function is used internally to make sure that all constructed
396 * indexed objects really carry indices and not some other classes. */
397 void indexed::validate(void) const
399 GINAC_ASSERT(seq.size() > 0);
400 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
401 while (it != itend) {
402 if (!is_ex_of_type(*it, idx))
403 throw(std::invalid_argument("indices of indexed object must be of type idx"));
407 if (!symtree.is_zero()) {
408 if (!is_ex_exactly_of_type(symtree, symmetry))
409 throw(std::invalid_argument("symmetry of indexed object must be of type symmetry"));
410 ex_to_nonconst_symmetry(symtree).validate(seq.size() - 1);
418 /** Check whether two sorted index vectors are consistent (i.e. equal). */
419 static bool indices_consistent(const exvector & v1, const exvector & v2)
421 // Number of indices must be the same
422 if (v1.size() != v2.size())
425 return equal(v1.begin(), v1.end(), v2.begin(), ex_is_equal());
428 exvector indexed::get_indices(void) const
430 GINAC_ASSERT(seq.size() >= 1);
431 return exvector(seq.begin() + 1, seq.end());
434 exvector indexed::get_dummy_indices(void) const
436 exvector free_indices, dummy_indices;
437 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
438 return dummy_indices;
441 exvector indexed::get_dummy_indices(const indexed & other) const
443 exvector indices = get_free_indices();
444 exvector other_indices = other.get_free_indices();
445 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
446 exvector dummy_indices;
447 find_dummy_indices(indices, dummy_indices);
448 return dummy_indices;
451 bool indexed::has_dummy_index_for(const ex & i) const
453 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
454 while (it != itend) {
455 if (is_dummy_pair(*it, i))
462 exvector indexed::get_free_indices(void) const
464 exvector free_indices, dummy_indices;
465 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
469 exvector add::get_free_indices(void) const
471 exvector free_indices;
472 for (unsigned i=0; i<nops(); i++) {
474 free_indices = op(i).get_free_indices();
476 exvector free_indices_of_term = op(i).get_free_indices();
477 if (!indices_consistent(free_indices, free_indices_of_term))
478 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
484 exvector mul::get_free_indices(void) const
486 // Concatenate free indices of all factors
488 for (unsigned i=0; i<nops(); i++) {
489 exvector free_indices_of_factor = op(i).get_free_indices();
490 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
493 // And remove the dummy indices
494 exvector free_indices, dummy_indices;
495 find_free_and_dummy(un, free_indices, dummy_indices);
499 exvector ncmul::get_free_indices(void) const
501 // Concatenate free indices of all factors
503 for (unsigned i=0; i<nops(); i++) {
504 exvector free_indices_of_factor = op(i).get_free_indices();
505 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
508 // And remove the dummy indices
509 exvector free_indices, dummy_indices;
510 find_free_and_dummy(un, free_indices, dummy_indices);
514 exvector power::get_free_indices(void) const
516 // Return free indices of basis
517 return basis.get_free_indices();
520 /** Rename dummy indices in an expression.
522 * @param e Expression to be worked on
523 * @param local_dummy_indices The set of dummy indices that appear in the
525 * @param global_dummy_indices The set of dummy indices that have appeared
526 * before and which we would like to use in "e", too. This gets updated
528 static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
530 int global_size = global_dummy_indices.size(),
531 local_size = local_dummy_indices.size();
533 // Any local dummy indices at all?
537 if (global_size < local_size) {
539 // More local indices than we encountered before, add the new ones
541 int remaining = local_size - global_size;
542 exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
543 while (it != itend && remaining > 0) {
544 if (find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(ex_is_equal(), *it)) == global_dummy_indices.end()) {
545 global_dummy_indices.push_back(*it);
553 // Replace index symbols in expression
554 GINAC_ASSERT(local_size <= global_size);
555 bool all_equal = true;
556 lst local_syms, global_syms;
557 for (unsigned i=0; i<local_size; i++) {
558 ex loc_sym = local_dummy_indices[i].op(0);
559 ex glob_sym = global_dummy_indices[i].op(0);
560 if (!loc_sym.is_equal(glob_sym)) {
562 local_syms.append(loc_sym);
563 global_syms.append(glob_sym);
569 return e.subs(local_syms, global_syms);
572 /** Simplify product of indexed expressions (commutative, noncommutative and
573 * simple squares), return list of free indices. */
574 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
576 // Remember whether the product was commutative or noncommutative
577 // (because we chop it into factors and need to reassemble later)
578 bool non_commutative = is_ex_exactly_of_type(e, ncmul);
580 // Collect factors in an exvector, store squares twice
582 v.reserve(e.nops() * 2);
584 if (is_ex_exactly_of_type(e, power)) {
585 // We only get called for simple squares, split a^2 -> a*a
586 GINAC_ASSERT(e.op(1).is_equal(_ex2()));
587 v.push_back(e.op(0));
588 v.push_back(e.op(0));
590 for (int i=0; i<e.nops(); i++) {
592 if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2())) {
593 v.push_back(f.op(0));
594 v.push_back(f.op(0));
595 } else if (is_ex_exactly_of_type(f, ncmul)) {
596 // Noncommutative factor found, split it as well
597 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
598 for (int j=0; j<f.nops(); j++)
599 v.push_back(f.op(j));
605 // Perform contractions
606 bool something_changed = false;
607 GINAC_ASSERT(v.size() > 1);
608 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
609 for (it1 = v.begin(); it1 != next_to_last; it1++) {
612 if (!is_ex_of_type(*it1, indexed))
615 bool first_noncommutative = (it1->return_type() != return_types::commutative);
617 // Indexed factor found, get free indices and look for contraction
619 exvector free1, dummy1;
620 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
622 exvector::iterator it2;
623 for (it2 = it1 + 1; it2 != itend; it2++) {
625 if (!is_ex_of_type(*it2, indexed))
628 bool second_noncommutative = (it2->return_type() != return_types::commutative);
630 // Find free indices of second factor and merge them with free
631 // indices of first factor
633 find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
634 un.insert(un.end(), free1.begin(), free1.end());
636 // Check whether the two factors share dummy indices
637 exvector free, dummy;
638 find_free_and_dummy(un, free, dummy);
639 if (dummy.size() == 0)
642 // At least one dummy index, is it a defined scalar product?
643 bool contracted = false;
644 if (free.size() == 0) {
645 if (sp.is_defined(*it1, *it2)) {
646 *it1 = sp.evaluate(*it1, *it2);
648 goto contraction_done;
652 // Contraction of symmetric with antisymmetric object is zero
654 && ex_to<symmetry>(ex_to<indexed>(*it1).symtree).has_symmetry()
655 && ex_to<symmetry>(ex_to<indexed>(*it2).symtree).has_symmetry()) {
657 // Check all pairs of dummy indices
658 for (unsigned idx1=0; idx1<dummy.size()-1; idx1++) {
659 for (unsigned idx2=idx1+1; idx2<dummy.size(); idx2++) {
661 // Try and swap the index pair and check whether the
662 // relative sign changed
663 lst subs_lst(dummy[idx1].op(0), dummy[idx2].op(0)), repl_lst(dummy[idx2].op(0), dummy[idx1].op(0));
664 ex swapped1 = it1->subs(subs_lst, repl_lst);
665 ex swapped2 = it2->subs(subs_lst, repl_lst);
666 if (it1->is_equal(swapped1) && it2->is_equal(-swapped2)
667 || it1->is_equal(-swapped1) && it2->is_equal(swapped2)) {
668 free_indices.clear();
675 // Try to contract the first one with the second one
676 contracted = it1->op(0).bp->contract_with(it1, it2, v);
679 // That didn't work; maybe the second object knows how to
680 // contract itself with the first one
681 contracted = it2->op(0).bp->contract_with(it2, it1, v);
685 if (first_noncommutative || second_noncommutative
686 || is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
687 || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)
688 || is_ex_exactly_of_type(*it1, ncmul) || is_ex_exactly_of_type(*it2, ncmul)) {
690 // One of the factors became a sum or product:
691 // re-expand expression and run again
692 // Non-commutative products are always re-expanded to give
693 // simplify_ncmul() the chance to re-order and canonicalize
695 ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
696 return simplify_indexed(r, free_indices, dummy_indices, sp);
699 // Both objects may have new indices now or they might
700 // even not be indexed objects any more, so we have to
702 something_changed = true;
708 // Find free indices (concatenate them all and call find_free_and_dummy())
709 // and all dummy indices that appear
710 exvector un, individual_dummy_indices;
711 it1 = v.begin(); itend = v.end();
712 while (it1 != itend) {
713 exvector free_indices_of_factor;
714 if (is_ex_of_type(*it1, indexed)) {
715 exvector dummy_indices_of_factor;
716 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);
717 individual_dummy_indices.insert(individual_dummy_indices.end(), dummy_indices_of_factor.begin(), dummy_indices_of_factor.end());
719 free_indices_of_factor = it1->get_free_indices();
720 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
723 exvector local_dummy_indices;
724 find_free_and_dummy(un, free_indices, local_dummy_indices);
725 local_dummy_indices.insert(local_dummy_indices.end(), individual_dummy_indices.begin(), individual_dummy_indices.end());
728 if (something_changed)
729 r = non_commutative ? ex(ncmul(v, true)) : ex(mul(v));
733 // Dummy index renaming
734 r = rename_dummy_indices(r, dummy_indices, local_dummy_indices);
736 // Product of indexed object with a scalar?
737 if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
738 && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
739 return r.op(0).op(0).bp->scalar_mul_indexed(r.op(0), ex_to<numeric>(r.op(1)));
744 /** Simplify indexed expression, return list of free indices. */
745 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
747 // Expand the expression
748 ex e_expanded = e.expand();
750 // Simplification of single indexed object: just find the free indices
751 // and perform dummy index renaming
752 if (is_ex_of_type(e_expanded, indexed)) {
753 const indexed &i = ex_to<indexed>(e_expanded);
754 exvector local_dummy_indices;
755 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
756 return rename_dummy_indices(e_expanded, dummy_indices, local_dummy_indices);
759 // Simplification of sum = sum of simplifications, check consistency of
760 // free indices in each term
761 if (is_ex_exactly_of_type(e_expanded, add)) {
764 free_indices.clear();
766 for (unsigned i=0; i<e_expanded.nops(); i++) {
767 exvector free_indices_of_term;
768 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
769 if (!term.is_zero()) {
771 free_indices = free_indices_of_term;
775 if (!indices_consistent(free_indices, free_indices_of_term))
776 throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
777 if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
778 sum = sum.op(0).bp->add_indexed(sum, term);
788 // Simplification of products
789 if (is_ex_exactly_of_type(e_expanded, mul)
790 || is_ex_exactly_of_type(e_expanded, ncmul)
791 || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2())))
792 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
794 // Cannot do anything
795 free_indices.clear();
799 /** Simplify/canonicalize expression containing indexed objects. This
800 * performs contraction of dummy indices where possible and checks whether
801 * the free indices in sums are consistent.
803 * @return simplified expression */
804 ex ex::simplify_indexed(void) const
806 exvector free_indices, dummy_indices;
808 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
811 /** Simplify/canonicalize expression containing indexed objects. This
812 * performs contraction of dummy indices where possible, checks whether
813 * the free indices in sums are consistent, and automatically replaces
814 * scalar products by known values if desired.
816 * @param sp Scalar products to be replaced automatically
817 * @return simplified expression */
818 ex ex::simplify_indexed(const scalar_products & sp) const
820 exvector free_indices, dummy_indices;
821 return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
824 /** Symmetrize expression over its free indices. */
825 ex ex::symmetrize(void) const
827 return GiNaC::symmetrize(*this, get_free_indices());
830 /** Antisymmetrize expression over its free indices. */
831 ex ex::antisymmetrize(void) const
833 return GiNaC::antisymmetrize(*this, get_free_indices());
836 /** Symmetrize expression by cyclic permutation over its free indices. */
837 ex ex::symmetrize_cyclic(void) const
839 return GiNaC::symmetrize_cyclic(*this, get_free_indices());
846 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
848 spm[make_key(v1, v2)] = sp;
851 void scalar_products::add_vectors(const lst & l)
853 // Add all possible pairs of products
854 unsigned num = l.nops();
855 for (unsigned i=0; i<num; i++) {
857 for (unsigned j=0; j<num; j++) {
864 void scalar_products::clear(void)
869 /** Check whether scalar product pair is defined. */
870 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
872 return spm.find(make_key(v1, v2)) != spm.end();
875 /** Return value of defined scalar product pair. */
876 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
878 return spm.find(make_key(v1, v2))->second;
881 void scalar_products::debugprint(void) const
883 std::cerr << "map size=" << spm.size() << std::endl;
884 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
885 const spmapkey & k = cit->first;
886 std::cerr << "item key=(" << k.first << "," << k.second;
887 std::cerr << "), value=" << cit->second << std::endl;
891 /** Make key from object pair. */
892 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
894 // If indexed, extract base objects
895 ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
896 ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
898 // Enforce canonical order in pair
899 if (s1.compare(s2) > 0)
900 return spmapkey(s2, s1);
902 return spmapkey(s1, s2);