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