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