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