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