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
38 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
41 // default constructor, destructor, copy constructor assignment operator and helpers
44 indexed::indexed() : symmetry(unknown)
46 debugmsg("indexed default constructor", LOGLEVEL_CONSTRUCT);
47 tinfo_key = TINFO_indexed;
50 void indexed::copy(const indexed & other)
52 inherited::copy(other);
53 symmetry = other.symmetry;
56 DEFAULT_DESTROY(indexed)
62 indexed::indexed(const ex & b) : inherited(b), symmetry(unknown)
64 debugmsg("indexed constructor from ex", LOGLEVEL_CONSTRUCT);
65 tinfo_key = TINFO_indexed;
66 assert_all_indices_of_type_idx();
69 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symmetry(unknown)
71 debugmsg("indexed constructor from ex,ex", LOGLEVEL_CONSTRUCT);
72 tinfo_key = TINFO_indexed;
73 assert_all_indices_of_type_idx();
76 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(unknown)
78 debugmsg("indexed constructor from ex,ex,ex", LOGLEVEL_CONSTRUCT);
79 tinfo_key = TINFO_indexed;
80 assert_all_indices_of_type_idx();
83 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(unknown)
85 debugmsg("indexed constructor from ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
86 tinfo_key = TINFO_indexed;
87 assert_all_indices_of_type_idx();
90 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)
92 debugmsg("indexed constructor from ex,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
93 tinfo_key = TINFO_indexed;
94 assert_all_indices_of_type_idx();
97 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(symm)
99 debugmsg("indexed constructor from ex,symmetry,ex,ex", LOGLEVEL_CONSTRUCT);
100 tinfo_key = TINFO_indexed;
101 assert_all_indices_of_type_idx();
104 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(symm)
106 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex", LOGLEVEL_CONSTRUCT);
107 tinfo_key = TINFO_indexed;
108 assert_all_indices_of_type_idx();
111 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)
113 debugmsg("indexed constructor from ex,symmetry,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
114 tinfo_key = TINFO_indexed;
115 assert_all_indices_of_type_idx();
118 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symmetry(unknown)
120 debugmsg("indexed constructor from ex,exvector", LOGLEVEL_CONSTRUCT);
121 seq.insert(seq.end(), v.begin(), v.end());
122 tinfo_key = TINFO_indexed;
123 assert_all_indices_of_type_idx();
126 indexed::indexed(const ex & b, symmetry_type symm, const exvector & v) : inherited(b), symmetry(symm)
128 debugmsg("indexed constructor from ex,symmetry,exvector", LOGLEVEL_CONSTRUCT);
129 seq.insert(seq.end(), v.begin(), v.end());
130 tinfo_key = TINFO_indexed;
131 assert_all_indices_of_type_idx();
134 indexed::indexed(symmetry_type symm, const exprseq & es) : inherited(es), symmetry(symm)
136 debugmsg("indexed constructor from symmetry,exprseq", LOGLEVEL_CONSTRUCT);
137 tinfo_key = TINFO_indexed;
138 assert_all_indices_of_type_idx();
141 indexed::indexed(symmetry_type symm, const exvector & v, bool discardable) : inherited(v, discardable), symmetry(symm)
143 debugmsg("indexed constructor from symmetry,exvector", LOGLEVEL_CONSTRUCT);
144 tinfo_key = TINFO_indexed;
145 assert_all_indices_of_type_idx();
148 indexed::indexed(symmetry_type symm, exvector * vp) : inherited(vp), symmetry(symm)
150 debugmsg("indexed constructor from symmetry,exvector *", LOGLEVEL_CONSTRUCT);
151 tinfo_key = TINFO_indexed;
152 assert_all_indices_of_type_idx();
159 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
161 debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
163 if (!(n.find_unsigned("symmetry", symm)))
164 throw (std::runtime_error("unknown indexed symmetry type in archive"));
167 void indexed::archive(archive_node &n) const
169 inherited::archive(n);
170 n.add_unsigned("symmetry", symmetry);
173 DEFAULT_UNARCHIVE(indexed)
176 // functions overriding virtual functions from bases classes
179 void indexed::printraw(std::ostream & os) const
181 debugmsg("indexed printraw", LOGLEVEL_PRINT);
182 GINAC_ASSERT(seq.size() > 0);
184 os << class_name() << "(";
188 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
191 void indexed::printtree(std::ostream & os, unsigned indent) const
193 debugmsg("indexed printtree", LOGLEVEL_PRINT);
194 GINAC_ASSERT(seq.size() > 0);
196 os << std::string(indent, ' ') << class_name() << ", " << seq.size()-1 << " indices";
197 os << ",hash=" << hashvalue << ",flags=" << flags << std::endl;
198 printtreeindices(os, indent);
201 void indexed::print(std::ostream & os, unsigned upper_precedence) const
203 debugmsg("indexed print", LOGLEVEL_PRINT);
204 GINAC_ASSERT(seq.size() > 0);
206 const ex & base = seq[0];
207 bool need_parens = is_ex_exactly_of_type(base, add) || is_ex_exactly_of_type(base, mul)
208 || is_ex_exactly_of_type(base, ncmul) || is_ex_exactly_of_type(base, power);
217 bool indexed::info(unsigned inf) const
219 if (inf == info_flags::indexed) return true;
220 if (inf == info_flags::has_indices) return seq.size() > 1;
221 return inherited::info(inf);
224 bool indexed::all_index_values_are(unsigned inf) const
226 // No indices? Then no property can be fulfilled
231 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
232 while (it != itend) {
233 GINAC_ASSERT(is_ex_of_type(*it, idx));
234 if (!ex_to_idx(*it).get_value().info(inf))
241 int indexed::compare_same_type(const basic & other) const
243 GINAC_ASSERT(is_of_type(other, indexed));
244 return inherited::compare_same_type(other);
247 // The main difference between sort_index_vector() and canonicalize_indices()
248 // is that the latter takes the symmetry of the object into account. Once we
249 // implement mixed symmetries, canonicalize_indices() will only be able to
250 // reorder index pairs with known symmetry properties, while sort_index_vector()
251 // always sorts the whole vector.
253 /** Bring a vector of indices into a canonic order (don't care about the
254 * symmetry of the objects carrying the indices). Dummy indices will lie
255 * next to each other after the sorting.
257 * @param v Index vector to be sorted */
258 static void sort_index_vector(exvector &v)
260 // Nothing to sort if less than 2 elements
264 // Simple bubble sort algorithm should be sufficient for the small
265 // number of indices expected
266 exvector::iterator it1 = v.begin(), itend = v.end(), next_to_last_idx = itend - 1;
267 while (it1 != next_to_last_idx) {
268 exvector::iterator it2 = it1 + 1;
269 while (it2 != itend) {
270 if (it1->compare(*it2) > 0)
278 /** Bring a vector of indices into a canonic order. This operation only makes
279 * sense if the object carrying these indices is either symmetric or totally
280 * antisymmetric with respect to the indices.
282 * @param itbegin Start of index vector
283 * @param itend End of index vector
284 * @param antisymm Whether the object is antisymmetric
285 * @return the sign introduced by the reordering of the indices if the object
286 * is antisymmetric (or 0 if two equal indices are encountered). For
287 * symmetric objects, this is always +1. If the index vector was
288 * already in a canonic order this function returns INT_MAX. */
289 static int canonicalize_indices(exvector::iterator itbegin, exvector::iterator itend, bool antisymm)
291 bool something_changed = false;
294 // Simple bubble sort algorithm should be sufficient for the small
295 // number of indices expected
296 exvector::iterator it1 = itbegin, next_to_last_idx = itend - 1;
297 while (it1 != next_to_last_idx) {
298 exvector::iterator it2 = it1 + 1;
299 while (it2 != itend) {
300 int cmpval = it1->compare(*it2);
303 something_changed = true;
306 } else if (cmpval == 0 && antisymm) {
307 something_changed = true;
315 return something_changed ? sig : INT_MAX;
318 ex indexed::eval(int level) const
320 // First evaluate children, then we will end up here again
322 return indexed(symmetry, evalchildren(level));
324 const ex &base = seq[0];
326 // If the base object is 0, the whole object is 0
330 // If the base object is a product, pull out the numeric factor
331 if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
333 ex f = ex_to_numeric(base.op(base.nops() - 1));
335 return f * thisexprseq(v);
338 // Canonicalize indices according to the symmetry properties
339 if (seq.size() > 2 && (symmetry != unknown && symmetry != mixed)) {
341 int sig = canonicalize_indices(v.begin() + 1, v.end(), symmetry == antisymmetric);
342 if (sig != INT_MAX) {
343 // Something has changed while sorting indices, more evaluations later
346 return ex(sig) * thisexprseq(v);
350 // Let the class of the base object perform additional evaluations
351 return base.bp->eval_indexed(*this);
354 int indexed::degree(const ex & s) const
356 return is_equal(*s.bp) ? 1 : 0;
359 int indexed::ldegree(const ex & s) const
361 return is_equal(*s.bp) ? 1 : 0;
364 ex indexed::coeff(const ex & s, int n) const
367 return n==1 ? _ex1() : _ex0();
369 return n==0 ? ex(*this) : _ex0();
372 ex indexed::thisexprseq(const exvector & v) const
374 return indexed(symmetry, v);
377 ex indexed::thisexprseq(exvector * vp) const
379 return indexed(symmetry, vp);
382 ex indexed::expand(unsigned options) const
384 GINAC_ASSERT(seq.size() > 0);
386 if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
388 // expand_indexed expands (a+b).i -> a.i + b.i
389 const ex & base = seq[0];
391 for (unsigned i=0; i<base.nops(); i++) {
394 sum += thisexprseq(s).expand();
399 return inherited::expand(options);
403 // virtual functions which can be overridden by derived classes
409 // non-virtual functions in this class
412 void indexed::printrawindices(std::ostream & os) const
414 if (seq.size() > 1) {
415 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
416 while (it != itend) {
425 void indexed::printtreeindices(std::ostream & os, unsigned indent) const
427 if (seq.size() > 1) {
428 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
429 while (it != itend) {
430 os << std::string(indent + delta_indent, ' ');
438 void indexed::printindices(std::ostream & os) const
440 if (seq.size() > 1) {
441 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
442 while (it != itend) {
449 /** Check whether all indices are of class idx. This function is used
450 * internally to make sure that all constructed indexed objects really
451 * carry indices and not some other classes. */
452 void indexed::assert_all_indices_of_type_idx(void) const
454 GINAC_ASSERT(seq.size() > 0);
455 exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
456 while (it != itend) {
457 if (!is_ex_of_type(*it, idx))
458 throw(std::invalid_argument("indices of indexed object must be of type idx"));
467 /** Check whether two sorted index vectors are consistent (i.e. equal). */
468 static bool indices_consistent(const exvector & v1, const exvector & v2)
470 // Number of indices must be the same
471 if (v1.size() != v2.size())
474 // And also the indices themselves
475 exvector::const_iterator ait = v1.begin(), aitend = v1.end(),
476 bit = v2.begin(), bitend = v2.end();
477 while (ait != aitend) {
478 if (!ait->is_equal(*bit))
485 exvector indexed::get_indices(void) const
487 GINAC_ASSERT(seq.size() >= 1);
488 return exvector(seq.begin() + 1, seq.end());
491 exvector indexed::get_dummy_indices(void) const
493 exvector free_indices, dummy_indices;
494 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
495 return dummy_indices;
498 exvector indexed::get_dummy_indices(const indexed & other) const
500 exvector indices = get_free_indices();
501 exvector other_indices = other.get_free_indices();
502 indices.insert(indices.end(), other_indices.begin(), other_indices.end());
503 exvector dummy_indices;
504 find_dummy_indices(indices, dummy_indices);
505 return dummy_indices;
508 exvector indexed::get_free_indices(void) const
510 exvector free_indices, dummy_indices;
511 find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
515 exvector add::get_free_indices(void) const
517 exvector free_indices;
518 for (unsigned i=0; i<nops(); i++) {
520 free_indices = op(i).get_free_indices();
522 exvector free_indices_of_term = op(i).get_free_indices();
523 if (!indices_consistent(free_indices, free_indices_of_term))
524 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
530 exvector mul::get_free_indices(void) const
532 // Concatenate free indices of all factors
534 for (unsigned i=0; i<nops(); i++) {
535 exvector free_indices_of_factor = op(i).get_free_indices();
536 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
539 // And remove the dummy indices
540 exvector free_indices, dummy_indices;
541 find_free_and_dummy(un, free_indices, dummy_indices);
545 exvector ncmul::get_free_indices(void) const
547 // Concatenate free indices of all factors
549 for (unsigned i=0; i<nops(); i++) {
550 exvector free_indices_of_factor = op(i).get_free_indices();
551 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
554 // And remove the dummy indices
555 exvector free_indices, dummy_indices;
556 find_free_and_dummy(un, free_indices, dummy_indices);
560 exvector power::get_free_indices(void) const
562 // Return free indices of basis
563 return basis.get_free_indices();
566 /** Simplify product of indexed expressions (commutative, noncommutative and
567 * simple squares), return list of free indices. */
568 ex simplify_indexed_product(const ex & e, exvector & free_indices, const scalar_products & sp)
570 // Remember whether the product was commutative or noncommutative
571 // (because we chop it into factors and need to reassemble later)
572 bool non_commutative = is_ex_exactly_of_type(e, ncmul);
574 // Collect factors in an exvector, store squares twice
576 v.reserve(e.nops() * 2);
578 if (is_ex_exactly_of_type(e, power)) {
579 // We only get called for simple squares, split a^2 -> a*a
580 GINAC_ASSERT(e.op(1).is_equal(_ex2()));
581 v.push_back(e.op(0));
582 v.push_back(e.op(0));
584 for (int i=0; i<e.nops(); i++) {
586 if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2())) {
587 v.push_back(f.op(0));
588 v.push_back(f.op(0));
589 } else if (is_ex_exactly_of_type(f, ncmul)) {
590 // Noncommutative factor found, split it as well
591 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
592 for (int j=0; j<f.nops(); j++)
593 v.push_back(f.op(j));
599 // Perform contractions
600 bool something_changed = false;
601 GINAC_ASSERT(v.size() > 1);
602 exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
603 for (it1 = v.begin(); it1 != next_to_last; it1++) {
606 if (!is_ex_of_type(*it1, indexed))
609 // Indexed factor found, get free indices and look for contraction
611 exvector free1, dummy1;
612 find_free_and_dummy(ex_to_indexed(*it1).seq.begin() + 1, ex_to_indexed(*it1).seq.end(), free1, dummy1);
614 exvector::iterator it2;
615 for (it2 = it1 + 1; it2 != itend; it2++) {
617 if (!is_ex_of_type(*it2, indexed))
620 // Find free indices of second factor and merge them with free
621 // indices of first factor
623 find_free_and_dummy(ex_to_indexed(*it2).seq.begin() + 1, ex_to_indexed(*it2).seq.end(), un, dummy1);
624 un.insert(un.end(), free1.begin(), free1.end());
626 // Check whether the two factors share dummy indices
627 exvector free, dummy;
628 find_free_and_dummy(un, free, dummy);
629 if (dummy.size() == 0)
632 // At least one dummy index, is it a defined scalar product?
633 bool contracted = false;
634 if (free.size() == 0) {
635 if (sp.is_defined(*it1, *it2)) {
636 *it1 = sp.evaluate(*it1, *it2);
638 goto contraction_done;
642 // Contraction of symmetric with antisymmetric object is zero
643 if ((ex_to_indexed(*it1).symmetry == indexed::symmetric &&
644 ex_to_indexed(*it2).symmetry == indexed::antisymmetric
645 || ex_to_indexed(*it1).symmetry == indexed::antisymmetric &&
646 ex_to_indexed(*it2).symmetry == indexed::symmetric)
647 && dummy.size() > 1) {
648 free_indices.clear();
652 // Try to contract the first one with the second one
653 contracted = it1->op(0).bp->contract_with(it1, it2, v);
656 // That didn't work; maybe the second object knows how to
657 // contract itself with the first one
658 contracted = it2->op(0).bp->contract_with(it2, it1, v);
662 if (is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
663 || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)) {
665 // One of the factors became a sum or product:
666 // re-expand expression and run again
667 ex r = non_commutative ? ex(ncmul(v)) : ex(mul(v));
668 return simplify_indexed(r, free_indices, sp);
671 // Both objects may have new indices now or they might
672 // even not be indexed objects any more, so we have to
674 something_changed = true;
680 // Find free indices (concatenate them all and call find_free_and_dummy())
681 exvector un, dummy_indices;
682 it1 = v.begin(); itend = v.end();
683 while (it1 != itend) {
684 exvector free_indices_of_factor = it1->get_free_indices();
685 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
688 find_free_and_dummy(un, free_indices, dummy_indices);
691 if (something_changed)
692 r = non_commutative ? ex(ncmul(v)) : ex(mul(v));
696 // Product of indexed object with a scalar?
697 if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
698 && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
699 return r.op(0).op(0).bp->scalar_mul_indexed(r.op(0), ex_to_numeric(r.op(1)));
704 /** Simplify indexed expression, return list of free indices. */
705 ex simplify_indexed(const ex & e, exvector & free_indices, const scalar_products & sp)
707 // Expand the expression
708 ex e_expanded = e.expand();
710 // Simplification of single indexed object: just find the free indices
711 if (is_ex_of_type(e_expanded, indexed)) {
712 const indexed &i = ex_to_indexed(e_expanded);
713 exvector dummy_indices;
714 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, dummy_indices);
718 // Simplification of sum = sum of simplifications, check consistency of
719 // free indices in each term
720 if (is_ex_exactly_of_type(e_expanded, add)) {
723 free_indices.clear();
725 for (unsigned i=0; i<e_expanded.nops(); i++) {
726 exvector free_indices_of_term;
727 ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, sp);
728 if (!term.is_zero()) {
730 free_indices = free_indices_of_term;
734 if (!indices_consistent(free_indices, free_indices_of_term))
735 throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
736 if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
737 sum = sum.op(0).bp->add_indexed(sum, term);
747 // Simplification of products
748 if (is_ex_exactly_of_type(e_expanded, mul)
749 || is_ex_exactly_of_type(e_expanded, ncmul)
750 || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2())))
751 return simplify_indexed_product(e_expanded, free_indices, sp);
753 // Cannot do anything
754 free_indices.clear();
758 ex simplify_indexed(const ex & e)
760 exvector free_indices;
762 return simplify_indexed(e, free_indices, sp);
765 ex simplify_indexed(const ex & e, const scalar_products & sp)
767 exvector free_indices;
768 return simplify_indexed(e, free_indices, sp);
775 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
777 spm[make_key(v1, v2)] = sp;
780 void scalar_products::clear(void)
785 /** Check whether scalar product pair is defined. */
786 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
788 return spm.find(make_key(v1, v2)) != spm.end();
791 /** Return value of defined scalar product pair. */
792 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
794 return spm.find(make_key(v1, v2))->second;
797 void scalar_products::debugprint(void) const
799 std::cerr << "map size=" << spm.size() << std::endl;
800 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
801 const spmapkey & k = cit->first;
802 std::cerr << "item key=(" << k.first << "," << k.second;
803 std::cerr << "), value=" << cit->second << std::endl;
807 /** Make key from object pair. */
808 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
810 // If indexed, extract base objects
811 ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
812 ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
814 // Enforce canonical order in pair
815 if (s1.compare(s2) > 0)
816 return spmapkey(s2, s1);
818 return spmapkey(s1, s2);