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