]> www.ginac.de Git - ginac.git/blob - ginac/indexed.cpp
18f44571fad8b7ae9c74cc879440ef561feac273
[ginac.git] / ginac / indexed.cpp
1 /** @file indexed.cpp
2  *
3  *  Implementation of GiNaC's indexed expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <iostream>
24 #include <sstream>
25 #include <stdexcept>
26
27 #include "indexed.h"
28 #include "idx.h"
29 #include "add.h"
30 #include "mul.h"
31 #include "ncmul.h"
32 #include "power.h"
33 #include "relational.h"
34 #include "symmetry.h"
35 #include "lst.h"
36 #include "print.h"
37 #include "archive.h"
38 #include "utils.h"
39
40 namespace GiNaC {
41
42 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
43
44 //////////
45 // default ctor, dtor, copy ctor, assignment operator and helpers
46 //////////
47
48 indexed::indexed() : symtree(sy_none())
49 {
50         tinfo_key = TINFO_indexed;
51 }
52
53 void indexed::copy(const indexed & other)
54 {
55         inherited::copy(other);
56         symtree = other.symtree;
57 }
58
59 DEFAULT_DESTROY(indexed)
60
61 //////////
62 // other constructors
63 //////////
64
65 indexed::indexed(const ex & b) : inherited(b), symtree(sy_none())
66 {
67         tinfo_key = TINFO_indexed;
68         validate();
69 }
70
71 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symtree(sy_none())
72 {
73         tinfo_key = TINFO_indexed;
74         validate();
75 }
76
77 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(sy_none())
78 {
79         tinfo_key = TINFO_indexed;
80         validate();
81 }
82
83 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(sy_none())
84 {
85         tinfo_key = TINFO_indexed;
86         validate();
87 }
88
89 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())
90 {
91         tinfo_key = TINFO_indexed;
92         validate();
93 }
94
95 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symtree(symm)
96 {
97         tinfo_key = TINFO_indexed;
98         validate();
99 }
100
101 indexed::indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symtree(symm)
102 {
103         tinfo_key = TINFO_indexed;
104         validate();
105 }
106
107 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)
108 {
109         tinfo_key = TINFO_indexed;
110         validate();
111 }
112
113 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symtree(sy_none())
114 {
115         seq.insert(seq.end(), v.begin(), v.end());
116         tinfo_key = TINFO_indexed;
117         validate();
118 }
119
120 indexed::indexed(const ex & b, const symmetry & symm, const exvector & v) : inherited(b), symtree(symm)
121 {
122         seq.insert(seq.end(), v.begin(), v.end());
123         tinfo_key = TINFO_indexed;
124         validate();
125 }
126
127 indexed::indexed(const symmetry & symm, const exprseq & es) : inherited(es), symtree(symm)
128 {
129         tinfo_key = TINFO_indexed;
130 }
131
132 indexed::indexed(const symmetry & symm, const exvector & v, bool discardable) : inherited(v, discardable), symtree(symm)
133 {
134         tinfo_key = TINFO_indexed;
135 }
136
137 indexed::indexed(const symmetry & symm, exvector * vp) : inherited(vp), symtree(symm)
138 {
139         tinfo_key = TINFO_indexed;
140 }
141
142 //////////
143 // archiving
144 //////////
145
146 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
147 {
148         if (!n.find_ex("symmetry", symtree, sym_lst)) {
149                 // GiNaC versions <= 0.9.0 had an unsigned "symmetry" property
150                 unsigned symm = 0;
151                 n.find_unsigned("symmetry", symm);
152                 switch (symm) {
153                         case 1:
154                                 symtree = sy_symm();
155                                 break;
156                         case 2:
157                                 symtree = sy_anti();
158                                 break;
159                         default:
160                                 symtree = sy_none();
161                                 break;
162                 }
163                 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
164         }
165 }
166
167 void indexed::archive(archive_node &n) const
168 {
169         inherited::archive(n);
170         n.add_ex("symmetry", symtree);
171 }
172
173 DEFAULT_UNARCHIVE(indexed)
174
175 //////////
176 // functions overriding virtual functions from base classes
177 //////////
178
179 void indexed::print(const print_context & c, unsigned level) const
180 {
181         GINAC_ASSERT(seq.size() > 0);
182
183         if (is_a<print_tree>(c)) {
184
185                 c.s << std::string(level, ' ') << class_name()
186                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
187                     << ", " << seq.size()-1 << " indices"
188                     << ", symmetry=" << symtree << std::endl;
189                 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
190                 seq[0].print(c, level + delta_indent);
191                 printindices(c, level + delta_indent);
192
193         } else {
194
195                 bool is_tex = is_a<print_latex>(c);
196                 const ex & base = seq[0];
197
198                 if (precedence() <= level)
199                         c.s << (is_tex ? "{(" : "(");
200                 if (is_tex)
201                         c.s << "{";
202                 base.print(c, precedence());
203                 if (is_tex)
204                         c.s << "}";
205                 printindices(c, level);
206                 if (precedence() <= level)
207                         c.s << (is_tex ? ")}" : ")");
208         }
209 }
210
211 bool indexed::info(unsigned inf) const
212 {
213         if (inf == info_flags::indexed) return true;
214         if (inf == info_flags::has_indices) return seq.size() > 1;
215         return inherited::info(inf);
216 }
217
218 struct idx_is_not : public std::binary_function<ex, unsigned, bool> {
219         bool operator() (const ex & e, unsigned inf) const {
220                 return !(ex_to<idx>(e).get_value().info(inf));
221         }
222 };
223
224 bool indexed::all_index_values_are(unsigned inf) const
225 {
226         // No indices? Then no property can be fulfilled
227         if (seq.size() < 2)
228                 return false;
229
230         // Check all indices
231         return find_if(seq.begin() + 1, seq.end(), bind2nd(idx_is_not(), inf)) == seq.end();
232 }
233
234 int indexed::compare_same_type(const basic & other) const
235 {
236         GINAC_ASSERT(is_a<indexed>(other));
237         return inherited::compare_same_type(other);
238 }
239
240 ex indexed::eval(int level) const
241 {
242         // First evaluate children, then we will end up here again
243         if (level > 1)
244                 return indexed(ex_to<symmetry>(symtree), evalchildren(level));
245
246         const ex &base = seq[0];
247
248         // If the base object is 0, the whole object is 0
249         if (base.is_zero())
250                 return _ex0;
251
252         // If the base object is a product, pull out the numeric factor
253         if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
254                 exvector v(seq);
255                 ex f = ex_to<numeric>(base.op(base.nops() - 1));
256                 v[0] = seq[0] / f;
257                 return f * thisexprseq(v);
258         }
259
260         // Canonicalize indices according to the symmetry properties
261         if (seq.size() > 2) {
262                 exvector v = seq;
263                 GINAC_ASSERT(is_exactly_a<symmetry>(symtree));
264                 int sig = canonicalize(v.begin() + 1, ex_to<symmetry>(symtree));
265                 if (sig != INT_MAX) {
266                         // Something has changed while sorting indices, more evaluations later
267                         if (sig == 0)
268                                 return _ex0;
269                         return ex(sig) * thisexprseq(v);
270                 }
271         }
272
273         // Let the class of the base object perform additional evaluations
274         return ex_to<basic>(base).eval_indexed(*this);
275 }
276
277 ex indexed::thisexprseq(const exvector & v) const
278 {
279         return indexed(ex_to<symmetry>(symtree), v);
280 }
281
282 ex indexed::thisexprseq(exvector * vp) const
283 {
284         return indexed(ex_to<symmetry>(symtree), vp);
285 }
286
287 ex indexed::expand(unsigned options) const
288 {
289         GINAC_ASSERT(seq.size() > 0);
290
291         if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
292
293                 // expand_indexed expands (a+b).i -> a.i + b.i
294                 const ex & base = seq[0];
295                 ex sum = _ex0;
296                 for (unsigned i=0; i<base.nops(); i++) {
297                         exvector s = seq;
298                         s[0] = base.op(i);
299                         sum += thisexprseq(s).expand();
300                 }
301                 return sum;
302
303         } else
304                 return inherited::expand(options);
305 }
306
307 //////////
308 // virtual functions which can be overridden by derived classes
309 //////////
310
311 // none
312
313 //////////
314 // non-virtual functions in this class
315 //////////
316
317 void indexed::printindices(const print_context & c, unsigned level) const
318 {
319         if (seq.size() > 1) {
320
321                 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
322
323                 if (is_a<print_latex>(c)) {
324
325                         // TeX output: group by variance
326                         bool first = true;
327                         bool covariant = true;
328
329                         while (it != itend) {
330                                 bool cur_covariant = (is_ex_of_type(*it, varidx) ? ex_to<varidx>(*it).is_covariant() : true);
331                                 if (first || cur_covariant != covariant) { // Variance changed
332                                         // The empty {} prevents indices from ending up on top of each other
333                                         if (!first)
334                                                 c.s << "}{}";
335                                         covariant = cur_covariant;
336                                         if (covariant)
337                                                 c.s << "_{";
338                                         else
339                                                 c.s << "^{";
340                                 }
341                                 it->print(c, level);
342                                 c.s << " ";
343                                 first = false;
344                                 it++;
345                         }
346                         c.s << "}";
347
348                 } else {
349
350                         // Ordinary output
351                         while (it != itend) {
352                                 it->print(c, level);
353                                 it++;
354                         }
355                 }
356         }
357 }
358
359 /** Check whether all indices are of class idx and validate the symmetry
360  *  tree. This function is used internally to make sure that all constructed
361  *  indexed objects really carry indices and not some other classes. */
362 void indexed::validate(void) const
363 {
364         GINAC_ASSERT(seq.size() > 0);
365         exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
366         while (it != itend) {
367                 if (!is_ex_of_type(*it, idx))
368                         throw(std::invalid_argument("indices of indexed object must be of type idx"));
369                 it++;
370         }
371
372         if (!symtree.is_zero()) {
373                 if (!is_ex_exactly_of_type(symtree, symmetry))
374                         throw(std::invalid_argument("symmetry of indexed object must be of type symmetry"));
375                 const_cast<symmetry &>(ex_to<symmetry>(symtree)).validate(seq.size() - 1);
376         }
377 }
378
379 /** Implementation of ex::diff() for an indexed object always returns 0.
380  *
381  *  @see ex::diff */
382 ex indexed::derivative(const symbol & s) const
383 {
384         return _ex0;
385 }
386
387 //////////
388 // global functions
389 //////////
390
391 struct idx_is_equal_ignore_dim : public std::binary_function<ex, ex, bool> {
392         bool operator() (const ex &lh, const ex &rh) const
393         {
394                 if (lh.is_equal(rh))
395                         return true;
396                 else
397                         try {
398                                 // Replacing the dimension might cause an error (e.g. with
399                                 // index classes that only work in a fixed number of dimensions)
400                                 return lh.is_equal(ex_to<idx>(rh).replace_dim(ex_to<idx>(lh).get_dim()));
401                         } catch (...) {
402                                 return false;
403                         }
404         }
405 };
406
407 /** Check whether two sorted index vectors are consistent (i.e. equal). */
408 static bool indices_consistent(const exvector & v1, const exvector & v2)
409 {
410         // Number of indices must be the same
411         if (v1.size() != v2.size())
412                 return false;
413
414         return equal(v1.begin(), v1.end(), v2.begin(), idx_is_equal_ignore_dim());
415 }
416
417 exvector indexed::get_indices(void) const
418 {
419         GINAC_ASSERT(seq.size() >= 1);
420         return exvector(seq.begin() + 1, seq.end());
421 }
422
423 exvector indexed::get_dummy_indices(void) const
424 {
425         exvector free_indices, dummy_indices;
426         find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
427         return dummy_indices;
428 }
429
430 exvector indexed::get_dummy_indices(const indexed & other) const
431 {
432         exvector indices = get_free_indices();
433         exvector other_indices = other.get_free_indices();
434         indices.insert(indices.end(), other_indices.begin(), other_indices.end());
435         exvector dummy_indices;
436         find_dummy_indices(indices, dummy_indices);
437         return dummy_indices;
438 }
439
440 bool indexed::has_dummy_index_for(const ex & i) const
441 {
442         exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
443         while (it != itend) {
444                 if (is_dummy_pair(*it, i))
445                         return true;
446                 it++;
447         }
448         return false;
449 }
450
451 exvector indexed::get_free_indices(void) const
452 {
453         exvector free_indices, dummy_indices;
454         find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
455         return free_indices;
456 }
457
458 exvector add::get_free_indices(void) const
459 {
460         exvector free_indices;
461         for (unsigned i=0; i<nops(); i++) {
462                 if (i == 0)
463                         free_indices = op(i).get_free_indices();
464                 else {
465                         exvector free_indices_of_term = op(i).get_free_indices();
466                         if (!indices_consistent(free_indices, free_indices_of_term))
467                                 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
468                 }
469         }
470         return free_indices;
471 }
472
473 exvector mul::get_free_indices(void) const
474 {
475         // Concatenate free indices of all factors
476         exvector un;
477         for (unsigned i=0; i<nops(); i++) {
478                 exvector free_indices_of_factor = op(i).get_free_indices();
479                 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
480         }
481
482         // And remove the dummy indices
483         exvector free_indices, dummy_indices;
484         find_free_and_dummy(un, free_indices, dummy_indices);
485         return free_indices;
486 }
487
488 exvector ncmul::get_free_indices(void) const
489 {
490         // Concatenate free indices of all factors
491         exvector un;
492         for (unsigned i=0; i<nops(); i++) {
493                 exvector free_indices_of_factor = op(i).get_free_indices();
494                 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
495         }
496
497         // And remove the dummy indices
498         exvector free_indices, dummy_indices;
499         find_free_and_dummy(un, free_indices, dummy_indices);
500         return free_indices;
501 }
502
503 exvector power::get_free_indices(void) const
504 {
505         // Return free indices of basis
506         return basis.get_free_indices();
507 }
508
509 /** Rename dummy indices in an expression.
510  *
511  *  @param e Expression to work on
512  *  @param local_dummy_indices The set of dummy indices that appear in the
513  *    expression "e"
514  *  @param global_dummy_indices The set of dummy indices that have appeared
515  *    before and which we would like to use in "e", too. This gets updated
516  *    by the function */
517 static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
518 {
519         unsigned global_size = global_dummy_indices.size(),
520                  local_size = local_dummy_indices.size();
521
522         // Any local dummy indices at all?
523         if (local_size == 0)
524                 return e;
525
526         if (global_size < local_size) {
527
528                 // More local indices than we encountered before, add the new ones
529                 // to the global set
530                 int old_global_size = global_size;
531                 int remaining = local_size - global_size;
532                 exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
533                 while (it != itend && remaining > 0) {
534                         if (find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(ex_is_equal(), *it)) == global_dummy_indices.end()) {
535                                 global_dummy_indices.push_back(*it);
536                                 global_size++;
537                                 remaining--;
538                         }
539                         it++;
540                 }
541
542                 // If this is the first set of local indices, do nothing
543                 if (old_global_size == 0)
544                         return e;
545         }
546         GINAC_ASSERT(local_size <= global_size);
547
548         // Construct lists of index symbols
549         exlist local_syms, global_syms;
550         for (unsigned i=0; i<local_size; i++)
551                 local_syms.push_back(local_dummy_indices[i].op(0));
552         shaker_sort(local_syms.begin(), local_syms.end(), ex_is_less(), ex_swap());
553         for (unsigned i=0; i<global_size; i++)
554                 global_syms.push_back(global_dummy_indices[i].op(0));
555         shaker_sort(global_syms.begin(), global_syms.end(), ex_is_less(), ex_swap());
556
557         // Remove common indices
558         exlist local_uniq, global_uniq;
559         set_difference(local_syms.begin(), local_syms.end(), global_syms.begin(), global_syms.end(), std::back_insert_iterator<exlist>(local_uniq), ex_is_less());
560         set_difference(global_syms.begin(), global_syms.end(), local_syms.begin(), local_syms.end(), std::back_insert_iterator<exlist>(global_uniq), ex_is_less());
561
562         // Replace remaining non-common local index symbols by global ones
563         if (local_uniq.empty())
564                 return e;
565         else {
566                 while (global_uniq.size() > local_uniq.size())
567                         global_uniq.pop_back();
568                 return e.subs(lst(local_uniq), lst(global_uniq));
569         }
570 }
571
572 /** Given a set of indices, extract those of class varidx. */
573 static void find_variant_indices(const exvector & v, exvector & variant_indices)
574 {
575         exvector::const_iterator it1, itend;
576         for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
577                 if (is_exactly_a<varidx>(*it1))
578                         variant_indices.push_back(*it1);
579         }
580 }
581
582 /** Raise/lower dummy indices in a single indexed objects to canonicalize their
583  *  variance.
584  *
585  *  @param e Object to work on
586  *  @param variant_dummy_indices The set of indices that might need repositioning (will be changed by this function)
587  *  @param moved_indices The set of indices that have been repositioned (will be changed by this function)
588  *  @return true if 'e' was changed */
589 bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices)
590 {
591         bool something_changed = false;
592
593         // If a dummy index is encountered for the first time in the
594         // product, pull it up, otherwise, pull it down
595         exvector::const_iterator it2, it2start, it2end;
596         for (it2start = ex_to<indexed>(e).seq.begin(), it2end = ex_to<indexed>(e).seq.end(), it2 = it2start + 1; it2 != it2end; ++it2) {
597                 if (!is_exactly_a<varidx>(*it2))
598                         continue;
599
600                 exvector::iterator vit, vitend;
601                 for (vit = variant_dummy_indices.begin(), vitend = variant_dummy_indices.end(); vit != vitend; ++vit) {
602                         if (it2->op(0).is_equal(vit->op(0))) {
603                                 if (ex_to<varidx>(*it2).is_covariant()) {
604                                         e = e.subs(lst(
605                                                 *it2 == ex_to<varidx>(*it2).toggle_variance(),
606                                                 ex_to<varidx>(*it2).toggle_variance() == *it2
607                                         ));
608                                         something_changed = true;
609                                         it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
610                                         it2start = ex_to<indexed>(e).seq.begin();
611                                         it2end = ex_to<indexed>(e).seq.end();
612                                 }
613                                 moved_indices.push_back(*vit);
614                                 variant_dummy_indices.erase(vit);
615                                 goto next_index;
616                         }
617                 }
618
619                 for (vit = moved_indices.begin(), vitend = moved_indices.end(); vit != vitend; ++vit) {
620                         if (it2->op(0).is_equal(vit->op(0))) {
621                                 if (ex_to<varidx>(*it2).is_contravariant()) {
622                                         e = e.subs(*it2 == ex_to<varidx>(*it2).toggle_variance());
623                                         something_changed = true;
624                                         it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
625                                         it2start = ex_to<indexed>(e).seq.begin();
626                                         it2end = ex_to<indexed>(e).seq.end();
627                                 }
628                                 goto next_index;
629                         }
630                 }
631
632 next_index: ;
633         }
634
635         return something_changed;
636 }
637
638 /* Ordering that only compares the base expressions of indexed objects. */
639 struct ex_base_is_less : public std::binary_function<ex, ex, bool> {
640         bool operator() (const ex &lh, const ex &rh) const
641         {
642                 return (is_a<indexed>(lh) ? lh.op(0) : lh).compare(is_a<indexed>(rh) ? rh.op(0) : rh) < 0;
643         }
644 };
645
646 /** Simplify product of indexed expressions (commutative, noncommutative and
647  *  simple squares), return list of free indices. */
648 ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
649 {
650         // Remember whether the product was commutative or noncommutative
651         // (because we chop it into factors and need to reassemble later)
652         bool non_commutative = is_ex_exactly_of_type(e, ncmul);
653
654         // Collect factors in an exvector, store squares twice
655         exvector v;
656         v.reserve(e.nops() * 2);
657
658         if (is_ex_exactly_of_type(e, power)) {
659                 // We only get called for simple squares, split a^2 -> a*a
660                 GINAC_ASSERT(e.op(1).is_equal(_ex2));
661                 v.push_back(e.op(0));
662                 v.push_back(e.op(0));
663         } else {
664                 for (unsigned i=0; i<e.nops(); i++) {
665                         ex f = e.op(i);
666                         if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2)) {
667                                 v.push_back(f.op(0));
668                     v.push_back(f.op(0));
669                         } else if (is_ex_exactly_of_type(f, ncmul)) {
670                                 // Noncommutative factor found, split it as well
671                                 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
672                                 for (unsigned j=0; j<f.nops(); j++)
673                                         v.push_back(f.op(j));
674                         } else
675                                 v.push_back(f);
676                 }
677         }
678
679         // Perform contractions
680         bool something_changed = false;
681         GINAC_ASSERT(v.size() > 1);
682         exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
683         for (it1 = v.begin(); it1 != next_to_last; it1++) {
684
685 try_again:
686                 if (!is_ex_of_type(*it1, indexed))
687                         continue;
688
689                 bool first_noncommutative = (it1->return_type() != return_types::commutative);
690
691                 // Indexed factor found, get free indices and look for contraction
692                 // candidates
693                 exvector free1, dummy1;
694                 find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
695
696                 exvector::iterator it2;
697                 for (it2 = it1 + 1; it2 != itend; it2++) {
698
699                         if (!is_ex_of_type(*it2, indexed))
700                                 continue;
701
702                         bool second_noncommutative = (it2->return_type() != return_types::commutative);
703
704                         // Find free indices of second factor and merge them with free
705                         // indices of first factor
706                         exvector un;
707                         find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
708                         un.insert(un.end(), free1.begin(), free1.end());
709
710                         // Check whether the two factors share dummy indices
711                         exvector free, dummy;
712                         find_free_and_dummy(un, free, dummy);
713                         unsigned num_dummies = dummy.size();
714                         if (num_dummies == 0)
715                                 continue;
716
717                         // At least one dummy index, is it a defined scalar product?
718                         bool contracted = false;
719                         if (free.empty()) {
720                                 if (sp.is_defined(*it1, *it2)) {
721                                         *it1 = sp.evaluate(*it1, *it2);
722                                         *it2 = _ex1;
723                                         goto contraction_done;
724                                 }
725                         }
726
727                         // Try to contract the first one with the second one
728                         contracted = ex_to<basic>(it1->op(0)).contract_with(it1, it2, v);
729                         if (!contracted) {
730
731                                 // That didn't work; maybe the second object knows how to
732                                 // contract itself with the first one
733                                 contracted = ex_to<basic>(it2->op(0)).contract_with(it2, it1, v);
734                         }
735                         if (contracted) {
736 contraction_done:
737                                 if (first_noncommutative || second_noncommutative
738                                  || is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
739                                  || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)
740                                  || is_ex_exactly_of_type(*it1, ncmul) || is_ex_exactly_of_type(*it2, ncmul)) {
741
742                                         // One of the factors became a sum or product:
743                                         // re-expand expression and run again
744                                         // Non-commutative products are always re-expanded to give
745                                         // simplify_ncmul() the chance to re-order and canonicalize
746                                         // the product
747                                         ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
748                                         return simplify_indexed(r, free_indices, dummy_indices, sp);
749                                 }
750
751                                 // Both objects may have new indices now or they might
752                                 // even not be indexed objects any more, so we have to
753                                 // start over
754                                 something_changed = true;
755                                 goto try_again;
756                         }
757                 }
758         }
759
760         // Find free indices (concatenate them all and call find_free_and_dummy())
761         // and all dummy indices that appear
762         exvector un, individual_dummy_indices;
763         for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
764                 exvector free_indices_of_factor;
765                 if (is_ex_of_type(*it1, indexed)) {
766                         exvector dummy_indices_of_factor;
767                         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);
768                         individual_dummy_indices.insert(individual_dummy_indices.end(), dummy_indices_of_factor.begin(), dummy_indices_of_factor.end());
769                 } else
770                         free_indices_of_factor = it1->get_free_indices();
771                 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
772         }
773         exvector local_dummy_indices;
774         find_free_and_dummy(un, free_indices, local_dummy_indices);
775         local_dummy_indices.insert(local_dummy_indices.end(), individual_dummy_indices.begin(), individual_dummy_indices.end());
776
777         // Filter out the dummy indices with variance
778         exvector variant_dummy_indices;
779         find_variant_indices(local_dummy_indices, variant_dummy_indices);
780
781         // Any indices with variance present at all?
782         if (!variant_dummy_indices.empty()) {
783
784                 // Yes, bring the product into a canonical order that only depends on
785                 // the base expressions of indexed objects
786                 if (!non_commutative)
787                         std::sort(v.begin(), v.end(), ex_base_is_less());
788
789                 exvector moved_indices;
790
791                 // Iterate over all indexed objects in the product
792                 for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
793                         if (!is_ex_of_type(*it1, indexed))
794                                 continue;
795
796                         if (reposition_dummy_indices(*it1, variant_dummy_indices, moved_indices))
797                                 something_changed = true;
798                 }
799         }
800
801         ex r;
802         if (something_changed)
803                 r = non_commutative ? ex(ncmul(v, true)) : ex(mul(v));
804         else
805                 r = e;
806
807         // The result should be symmetric with respect to exchange of dummy
808         // indices, so if the symmetrization vanishes, the whole expression is
809         // zero. This detects things like eps.i.j.k * p.j * p.k = 0.
810         if (local_dummy_indices.size() >= 2) {
811                 lst dummy_syms;
812                 for (int i=0; i<local_dummy_indices.size(); i++)
813                         dummy_syms.append(local_dummy_indices[i].op(0));
814                 if (r.symmetrize(dummy_syms).is_zero()) {
815                         free_indices.clear();
816                         return _ex0;
817                 }
818         }
819
820         // Dummy index renaming
821         r = rename_dummy_indices(r, dummy_indices, local_dummy_indices);
822
823         // Product of indexed object with a scalar?
824         if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
825          && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
826                 return ex_to<basic>(r.op(0).op(0)).scalar_mul_indexed(r.op(0), ex_to<numeric>(r.op(1)));
827         else
828                 return r;
829 }
830
831 /** This structure stores the original and symmetrized versions of terms
832  *  obtained during the simplification of sums. */
833 class terminfo {
834 public:
835         terminfo(const ex & orig_, const ex & symm_) : orig(orig_), symm(symm_) {}
836
837         ex orig; /**< original term */
838         ex symm; /**< symmtrized term */
839 };
840
841 class terminfo_is_less {
842 public:
843         bool operator() (const terminfo & ti1, const terminfo & ti2) const
844         {
845                 return (ti1.symm.compare(ti2.symm) < 0);
846         }
847 };
848
849 /** This structure stores the individual symmetrized terms obtained during
850  *  the simplification of sums. */
851 class symminfo {
852 public:
853         symminfo() : num(0) {}
854
855         symminfo(const ex & symmterm_, const ex & orig_, unsigned num_) : orig(orig_), num(num_)
856         {
857                 if (is_exactly_a<mul>(symmterm_) && is_exactly_a<numeric>(symmterm_.op(symmterm_.nops()-1))) {
858                         coeff = symmterm_.op(symmterm_.nops()-1);
859                         symmterm = symmterm_ / coeff;
860                 } else {
861                         coeff = 1;
862                         symmterm = symmterm_;
863                 }
864         }
865
866         ex symmterm;  /**< symmetrized term */
867         ex coeff;     /**< coefficient of symmetrized term */
868         ex orig;      /**< original term */
869         unsigned num; /**< how many symmetrized terms resulted from the original term */
870 };
871
872 class symminfo_is_less_by_symmterm {
873 public:
874         bool operator() (const symminfo & si1, const symminfo & si2) const
875         {
876                 return (si1.symmterm.compare(si2.symmterm) < 0);
877         }
878 };
879
880 class symminfo_is_less_by_orig {
881 public:
882         bool operator() (const symminfo & si1, const symminfo & si2) const
883         {
884                 return (si1.orig.compare(si2.orig) < 0);
885         }
886 };
887
888 /** Simplify indexed expression, return list of free indices. */
889 ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
890 {
891         // Expand the expression
892         ex e_expanded = e.expand();
893
894         // Simplification of single indexed object: just find the free indices
895         // and perform dummy index renaming/repositioning
896         if (is_ex_of_type(e_expanded, indexed)) {
897
898                 // Find the dummy indices
899                 const indexed &i = ex_to<indexed>(e_expanded);
900                 exvector local_dummy_indices;
901                 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, local_dummy_indices);
902
903                 // Filter out the dummy indices with variance
904                 exvector variant_dummy_indices;
905                 find_variant_indices(local_dummy_indices, variant_dummy_indices);
906
907                 // Any indices with variance present at all?
908                 if (!variant_dummy_indices.empty()) {
909
910                         // Yes, reposition them
911                         exvector moved_indices;
912                         reposition_dummy_indices(e_expanded, variant_dummy_indices, moved_indices);
913                 }
914
915                 // Rename the dummy indices
916                 return rename_dummy_indices(e_expanded, dummy_indices, local_dummy_indices);
917         }
918
919         // Simplification of sum = sum of simplifications, check consistency of
920         // free indices in each term
921         if (is_ex_exactly_of_type(e_expanded, add)) {
922                 bool first = true;
923                 ex sum;
924                 free_indices.clear();
925
926                 for (unsigned i=0; i<e_expanded.nops(); i++) {
927                         exvector free_indices_of_term;
928                         ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, dummy_indices, sp);
929                         if (!term.is_zero()) {
930                                 if (first) {
931                                         free_indices = free_indices_of_term;
932                                         sum = term;
933                                         first = false;
934                                 } else {
935                                         if (!indices_consistent(free_indices, free_indices_of_term)) {
936                                                 std::ostringstream s;
937                                                 s << "simplify_indexed: inconsistent indices in sum: ";
938                                                 s << exprseq(free_indices) << " vs. " << exprseq(free_indices_of_term);
939                                                 throw (std::runtime_error(s.str()));
940                                         }
941                                         if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
942                                                 sum = ex_to<basic>(sum.op(0)).add_indexed(sum, term);
943                                         else
944                                                 sum += term;
945                                 }
946                         }
947                 }
948
949                 // If the sum turns out to be zero, we are finished
950                 if (sum.is_zero()) {
951                         free_indices.clear();
952                         return sum;
953                 }
954
955                 // More than one term and more than one dummy index?
956                 int num_terms_orig = (is_exactly_a<add>(sum) ? sum.nops() : 1);
957                 if (num_terms_orig < 2 || dummy_indices.size() < 2)
958                         return sum;
959
960                 // Yes, construct list of all dummy index symbols
961                 lst dummy_syms;
962                 for (int i=0; i<dummy_indices.size(); i++)
963                         dummy_syms.append(dummy_indices[i].op(0));
964
965                 // Chop the sum into terms and symmetrize each one over the dummy
966                 // indices
967                 std::vector<terminfo> terms;
968                 for (unsigned i=0; i<sum.nops(); i++) {
969                         const ex & term = sum.op(i);
970                         ex term_symm = term.symmetrize(dummy_syms);
971                         if (term_symm.is_zero())
972                                 continue;
973                         terms.push_back(terminfo(term, term_symm));
974                 }
975
976                 // Sort by symmetrized terms
977                 std::sort(terms.begin(), terms.end(), terminfo_is_less());
978
979                 // Combine equal symmetrized terms
980                 std::vector<terminfo> terms_pass2;
981                 for (std::vector<terminfo>::const_iterator i=terms.begin(); i!=terms.end(); ) {
982                         unsigned num = 1;
983                         std::vector<terminfo>::const_iterator j = i + 1;
984                         while (j != terms.end() && j->symm == i->symm) {
985                                 num++;
986                                 j++;
987                         }
988                         terms_pass2.push_back(terminfo(i->orig * num, i->symm * num));
989                         i = j;
990                 }
991
992                 // If there is only one term left, we are finished
993                 if (terms_pass2.size() == 1)
994                         return terms_pass2[0].orig;
995
996                 // Chop the symmetrized terms into subterms
997                 std::vector<symminfo> sy;
998                 for (std::vector<terminfo>::const_iterator i=terms_pass2.begin(); i!=terms_pass2.end(); ++i) {
999                         if (is_exactly_a<add>(i->symm)) {
1000                                 unsigned num = i->symm.nops();
1001                                 for (unsigned j=0; j<num; j++)
1002                                         sy.push_back(symminfo(i->symm.op(j), i->orig, num));
1003                         } else
1004                                 sy.push_back(symminfo(i->symm, i->orig, 1));
1005                 }
1006
1007                 // Sort by symmetrized subterms
1008                 std::sort(sy.begin(), sy.end(), symminfo_is_less_by_symmterm());
1009
1010                 // Combine equal symmetrized subterms
1011                 std::vector<symminfo> sy_pass2;
1012                 exvector result;
1013                 for (std::vector<symminfo>::const_iterator i=sy.begin(); i!=sy.end(); ) {
1014
1015                         // Combine equal terms
1016                         std::vector<symminfo>::const_iterator j = i + 1;
1017                         if (j != sy.end() && j->symmterm == i->symmterm) {
1018
1019                                 // More than one term, collect the coefficients
1020                                 ex coeff = i->coeff;
1021                                 while (j != sy.end() && j->symmterm == i->symmterm) {
1022                                         coeff += j->coeff;
1023                                         j++;
1024                                 }
1025
1026                                 // Add combined term to result
1027                                 if (!coeff.is_zero())
1028                                         result.push_back(coeff * i->symmterm);
1029
1030                         } else {
1031
1032                                 // Single term, store for second pass
1033                                 sy_pass2.push_back(*i);
1034                         }
1035
1036                         i = j;
1037                 }
1038
1039                 // Were there any remaining terms that didn't get combined?
1040                 if (sy_pass2.size() > 0) {
1041
1042                         // Yes, sort by their original terms
1043                         std::sort(sy_pass2.begin(), sy_pass2.end(), symminfo_is_less_by_orig());
1044
1045                         for (std::vector<symminfo>::const_iterator i=sy_pass2.begin(); i!=sy_pass2.end(); ) {
1046
1047                                 // How many symmetrized terms of this original term are left?
1048                                 unsigned num = 1;
1049                                 std::vector<symminfo>::const_iterator j = i + 1;
1050                                 while (j != sy_pass2.end() && j->orig == i->orig) {
1051                                         num++;
1052                                         j++;
1053                                 }
1054
1055                                 if (num == i->num) {
1056
1057                                         // All terms left, then add the original term to the result
1058                                         result.push_back(i->orig);
1059
1060                                 } else {
1061
1062                                         // Some terms were combined with others, add up the remaining symmetrized terms
1063                                         std::vector<symminfo>::const_iterator k;
1064                                         for (k=i; k!=j; k++)
1065                                                 result.push_back(k->coeff * k->symmterm);
1066                                 }
1067
1068                                 i = j;
1069                         }
1070                 }
1071
1072                 // Add all resulting terms
1073                 ex sum_symm = (new add(result))->setflag(status_flags::dynallocated);
1074                 if (sum_symm.is_zero())
1075                         free_indices.clear();
1076                 return sum_symm;
1077         }
1078
1079         // Simplification of products
1080         if (is_ex_exactly_of_type(e_expanded, mul)
1081          || is_ex_exactly_of_type(e_expanded, ncmul)
1082          || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2)))
1083                 return simplify_indexed_product(e_expanded, free_indices, dummy_indices, sp);
1084
1085         // Cannot do anything
1086         free_indices.clear();
1087         return e_expanded;
1088 }
1089
1090 /** Simplify/canonicalize expression containing indexed objects. This
1091  *  performs contraction of dummy indices where possible and checks whether
1092  *  the free indices in sums are consistent.
1093  *
1094  *  @return simplified expression */
1095 ex ex::simplify_indexed(void) const
1096 {
1097         exvector free_indices, dummy_indices;
1098         scalar_products sp;
1099         return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1100 }
1101
1102 /** Simplify/canonicalize expression containing indexed objects. This
1103  *  performs contraction of dummy indices where possible, checks whether
1104  *  the free indices in sums are consistent, and automatically replaces
1105  *  scalar products by known values if desired.
1106  *
1107  *  @param sp Scalar products to be replaced automatically
1108  *  @return simplified expression */
1109 ex ex::simplify_indexed(const scalar_products & sp) const
1110 {
1111         exvector free_indices, dummy_indices;
1112         return GiNaC::simplify_indexed(*this, free_indices, dummy_indices, sp);
1113 }
1114
1115 /** Symmetrize expression over its free indices. */
1116 ex ex::symmetrize(void) const
1117 {
1118         return GiNaC::symmetrize(*this, get_free_indices());
1119 }
1120
1121 /** Antisymmetrize expression over its free indices. */
1122 ex ex::antisymmetrize(void) const
1123 {
1124         return GiNaC::antisymmetrize(*this, get_free_indices());
1125 }
1126
1127 /** Symmetrize expression by cyclic permutation over its free indices. */
1128 ex ex::symmetrize_cyclic(void) const
1129 {
1130         return GiNaC::symmetrize_cyclic(*this, get_free_indices());
1131 }
1132
1133 //////////
1134 // helper classes
1135 //////////
1136
1137 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
1138 {
1139         spm[make_key(v1, v2)] = sp;
1140 }
1141
1142 void scalar_products::add_vectors(const lst & l)
1143 {
1144         // Add all possible pairs of products
1145         unsigned num = l.nops();
1146         for (unsigned i=0; i<num; i++) {
1147                 ex a = l.op(i);
1148                 for (unsigned j=0; j<num; j++) {
1149                         ex b = l.op(j);
1150                         add(a, b, a*b);
1151                 }
1152         }
1153 }
1154
1155 void scalar_products::clear(void)
1156 {
1157         spm.clear();
1158 }
1159
1160 /** Check whether scalar product pair is defined. */
1161 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
1162 {
1163         return spm.find(make_key(v1, v2)) != spm.end();
1164 }
1165
1166 /** Return value of defined scalar product pair. */
1167 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
1168 {
1169         return spm.find(make_key(v1, v2))->second;
1170 }
1171
1172 void scalar_products::debugprint(void) const
1173 {
1174         std::cerr << "map size=" << spm.size() << std::endl;
1175         spmap::const_iterator i = spm.begin(), end = spm.end();
1176         while (i != end) {
1177                 const spmapkey & k = i->first;
1178                 std::cerr << "item key=(" << k.first << "," << k.second;
1179                 std::cerr << "), value=" << i->second << std::endl;
1180                 ++i;
1181         }
1182 }
1183
1184 /** Make key from object pair. */
1185 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
1186 {
1187         // If indexed, extract base objects
1188         ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
1189         ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
1190
1191         // Enforce canonical order in pair
1192         if (s1.compare(s2) > 0)
1193                 return spmapkey(s2, s1);
1194         else
1195                 return spmapkey(s1, s2);
1196 }
1197
1198 } // namespace GiNaC