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