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