]> www.ginac.de Git - ginac.git/blob - ginac/indexed.cpp
- the tensor returned by metric_tensor() is symmetric
[ginac.git] / ginac / indexed.cpp
1 /** @file indexed.cpp
2  *
3  *  Implementation of GiNaC's indexed expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <stdexcept>
24
25 #include "indexed.h"
26 #include "idx.h"
27 #include "add.h"
28 #include "mul.h"
29 #include "ncmul.h"
30 #include "power.h"
31 #include "archive.h"
32 #include "utils.h"
33 #include "debugmsg.h"
34
35 namespace GiNaC {
36
37 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
38
39 //////////
40 // default constructor, destructor, copy constructor assignment operator and helpers
41 //////////
42
43 indexed::indexed() : symmetry(unknown)
44 {
45         debugmsg("indexed default constructor", LOGLEVEL_CONSTRUCT);
46         tinfo_key = TINFO_indexed;
47 }
48
49 void indexed::copy(const indexed & other)
50 {
51         inherited::copy(other);
52         symmetry = other.symmetry;
53 }
54
55 void indexed::destroy(bool call_parent)
56 {
57         if (call_parent)
58                 inherited::destroy(call_parent);
59 }
60
61 //////////
62 // other constructors
63 //////////
64
65 indexed::indexed(const ex & b) : inherited(b), symmetry(unknown)
66 {
67         debugmsg("indexed constructor from ex", LOGLEVEL_CONSTRUCT);
68         tinfo_key = TINFO_indexed;
69         assert_all_indices_of_type_idx();
70 }
71
72 indexed::indexed(const ex & b, const ex & i1) : inherited(b, i1), symmetry(unknown)
73 {
74         debugmsg("indexed constructor from ex,ex", LOGLEVEL_CONSTRUCT);
75         tinfo_key = TINFO_indexed;
76         assert_all_indices_of_type_idx();
77 }
78
79 indexed::indexed(const ex & b, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(unknown)
80 {
81         debugmsg("indexed constructor from ex,ex,ex", LOGLEVEL_CONSTRUCT);
82         tinfo_key = TINFO_indexed;
83         assert_all_indices_of_type_idx();
84 }
85
86 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(unknown)
87 {
88         debugmsg("indexed constructor from ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
89         tinfo_key = TINFO_indexed;
90         assert_all_indices_of_type_idx();
91 }
92
93 indexed::indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symmetry(unknown)
94 {
95         debugmsg("indexed constructor from ex,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
96         tinfo_key = TINFO_indexed;
97         assert_all_indices_of_type_idx();
98 }
99
100 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2) : inherited(b, i1, i2), symmetry(symm)
101 {
102         debugmsg("indexed constructor from ex,symmetry,ex,ex", LOGLEVEL_CONSTRUCT);
103         tinfo_key = TINFO_indexed;
104         assert_all_indices_of_type_idx();
105 }
106
107 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3) : inherited(b, i1, i2, i3), symmetry(symm)
108 {
109         debugmsg("indexed constructor from ex,symmetry,ex,ex,ex", LOGLEVEL_CONSTRUCT);
110         tinfo_key = TINFO_indexed;
111         assert_all_indices_of_type_idx();
112 }
113
114 indexed::indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4) : inherited(b, i1, i2, i3, i4), symmetry(symm)
115 {
116         debugmsg("indexed constructor from ex,symmetry,ex,ex,ex,ex", LOGLEVEL_CONSTRUCT);
117         tinfo_key = TINFO_indexed;
118         assert_all_indices_of_type_idx();
119 }
120
121 indexed::indexed(const ex & b, const exvector & v) : inherited(b), symmetry(unknown)
122 {
123         debugmsg("indexed constructor from ex,exvector", LOGLEVEL_CONSTRUCT);
124         seq.insert(seq.end(), v.begin(), v.end());
125         tinfo_key = TINFO_indexed;
126         assert_all_indices_of_type_idx();
127 }
128
129 indexed::indexed(const ex & b, symmetry_type symm, const exvector & v) : inherited(b), symmetry(symm)
130 {
131         debugmsg("indexed constructor from ex,symmetry,exvector", LOGLEVEL_CONSTRUCT);
132         seq.insert(seq.end(), v.begin(), v.end());
133         tinfo_key = TINFO_indexed;
134         assert_all_indices_of_type_idx();
135 }
136
137 indexed::indexed(symmetry_type symm, const exprseq & es) : inherited(es), symmetry(symm)
138 {
139         debugmsg("indexed constructor from symmetry,exprseq", LOGLEVEL_CONSTRUCT);
140         tinfo_key = TINFO_indexed;
141         assert_all_indices_of_type_idx();
142 }
143
144 indexed::indexed(symmetry_type symm, const exvector & v, bool discardable) : inherited(v, discardable), symmetry(symm)
145 {
146         debugmsg("indexed constructor from symmetry,exvector", LOGLEVEL_CONSTRUCT);
147         tinfo_key = TINFO_indexed;
148         assert_all_indices_of_type_idx();
149 }
150
151 indexed::indexed(symmetry_type symm, exvector * vp) : inherited(vp), symmetry(symm)
152 {
153         debugmsg("indexed constructor from symmetry,exvector *", LOGLEVEL_CONSTRUCT);
154         tinfo_key = TINFO_indexed;
155         assert_all_indices_of_type_idx();
156 }
157
158 //////////
159 // archiving
160 //////////
161
162 /** Construct object from archive_node. */
163 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
164 {
165         debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
166         unsigned int symm;
167         if (!(n.find_unsigned("symmetry", symm)))
168                 throw (std::runtime_error("unknown indexed symmetry type in archive"));
169 }
170
171 /** Unarchive the object. */
172 ex indexed::unarchive(const archive_node &n, const lst &sym_lst)
173 {
174         return (new indexed(n, sym_lst))->setflag(status_flags::dynallocated);
175 }
176
177 /** Archive the object. */
178 void indexed::archive(archive_node &n) const
179 {
180         inherited::archive(n);
181         n.add_unsigned("symmetry", symmetry);
182 }
183
184 //////////
185 // functions overriding virtual functions from bases classes
186 //////////
187
188 void indexed::printraw(std::ostream & os) const
189 {
190         debugmsg("indexed printraw", LOGLEVEL_PRINT);
191         GINAC_ASSERT(seq.size() > 0);
192
193         os << class_name() << "(";
194         seq[0].printraw(os);
195         os << ",indices=";
196         printrawindices(os);
197         os << ",hash=" << hashvalue << ",flags=" << flags << ")";
198 }
199
200 void indexed::printtree(std::ostream & os, unsigned indent) const
201 {
202         debugmsg("indexed printtree", LOGLEVEL_PRINT);
203         GINAC_ASSERT(seq.size() > 0);
204
205         os << std::string(indent, ' ') << class_name() << ", " << seq.size()-1 << " indices";
206         os << ",hash=" << hashvalue << ",flags=" << flags << std::endl;
207         printtreeindices(os, indent);
208 }
209
210 void indexed::print(std::ostream & os, unsigned upper_precedence) const
211 {
212         debugmsg("indexed print", LOGLEVEL_PRINT);
213         GINAC_ASSERT(seq.size() > 0);
214
215         const ex & base = seq[0];
216         bool need_parens = is_ex_exactly_of_type(base, add) || is_ex_exactly_of_type(base, mul)
217                         || is_ex_exactly_of_type(base, ncmul) || is_ex_exactly_of_type(base, power);
218         if (need_parens)
219                 os << "(";
220         os << base;
221         if (need_parens)
222                 os << ")";
223         printindices(os);
224 }
225
226 bool indexed::info(unsigned inf) const
227 {
228         if (inf == info_flags::indexed) return true;
229         if (inf == info_flags::has_indices) return seq.size() > 1;
230         return inherited::info(inf);
231 }
232
233 bool indexed::all_index_values_are(unsigned inf) const
234 {
235         // No indices? Then no property can be fulfilled
236         if (seq.size() < 2)
237                 return false;
238
239         // Check all indices
240         exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
241         while (it != itend) {
242                 GINAC_ASSERT(is_ex_of_type(*it, idx));
243                 if (!ex_to_idx(*it).get_value().info(inf))
244                         return false;
245                 it++;
246         }
247         return true;
248 }
249
250 int indexed::compare_same_type(const basic & other) const
251 {
252         GINAC_ASSERT(is_of_type(other, indexed));
253         return inherited::compare_same_type(other);
254 }
255
256 // The main difference between sort_index_vector() and canonicalize_indices()
257 // is that the latter takes the symmetry of the object into account. Once we
258 // implement mixed symmetries, canonicalize_indices() will only be able to
259 // reorder index pairs with known symmetry properties, while sort_index_vector()
260 // always sorts the whole vector.
261
262 /** Bring a vector of indices into a canonic order (don't care about the
263  *  symmetry of the objects carrying the indices). Dummy indices will lie
264  *  next to each other after the sorting.
265  *
266  *  @param v Index vector to be sorted */
267 static void sort_index_vector(exvector &v)
268 {
269         // Nothing to sort if less than 2 elements
270         if (v.size() < 2)
271                 return;
272
273         // Simple bubble sort algorithm should be sufficient for the small
274         // number of indices expected
275         exvector::iterator it1 = v.begin(), itend = v.end(), next_to_last_idx = itend - 1;
276         while (it1 != next_to_last_idx) {
277                 exvector::iterator it2 = it1 + 1;
278                 while (it2 != itend) {
279                         if (it1->compare(*it2) > 0)
280                                 it1->swap(*it2);
281                         it2++;
282                 }
283                 it1++;
284         }
285 }
286
287 /** Bring a vector of indices into a canonic order. This operation only makes
288  *  sense if the object carrying these indices is either symmetric or totally
289  *  antisymmetric with respect to the indices.
290  *
291  *  @param itbegin Start of index vector
292  *  @param itend End of index vector
293  *  @param antisymm Whether the object is antisymmetric
294  *  @return the sign introduced by the reordering of the indices if the object
295  *          is antisymmetric (or 0 if two equal indices are encountered). For
296  *          symmetric objects, this is always +1. If the index vector was
297  *          already in a canonic order this function returns INT_MAX. */
298 static int canonicalize_indices(exvector::iterator itbegin, exvector::iterator itend, bool antisymm)
299 {
300         bool something_changed = false;
301         int sig = 1;
302
303         // Simple bubble sort algorithm should be sufficient for the small
304         // number of indices expected
305         exvector::iterator it1 = itbegin, next_to_last_idx = itend - 1;
306         while (it1 != next_to_last_idx) {
307                 exvector::iterator it2 = it1 + 1;
308                 while (it2 != itend) {
309                         int cmpval = it1->compare(*it2);
310                         if (cmpval == 1) {
311                                 it1->swap(*it2);
312                                 something_changed = true;
313                                 if (antisymm)
314                                         sig = -sig;
315                         } else if (cmpval == 0 && antisymm) {
316                                 something_changed = true;
317                                 sig = 0;
318                         }
319                         it2++;
320                 }
321                 it1++;
322         }
323
324         return something_changed ? sig : INT_MAX;
325 }
326
327 ex indexed::eval(int level) const
328 {
329         // First evaluate children, then we will end up here again
330         if (level > 1)
331                 return indexed(symmetry, evalchildren(level));
332
333         const ex &base = seq[0];
334
335         // If the base object is 0, the whole object is 0
336         if (base.is_zero())
337                 return _ex0();
338
339         // If the base object is a product, pull out the numeric factor
340         if (is_ex_exactly_of_type(base, mul) && is_ex_exactly_of_type(base.op(base.nops() - 1), numeric)) {
341                 exvector v = seq;
342                 ex f = ex_to_numeric(base.op(base.nops() - 1));
343                 v[0] = seq[0] / f;
344                 return f * thisexprseq(v);
345         }
346
347         // Canonicalize indices according to the symmetry properties
348         if (seq.size() > 2 && (symmetry != unknown && symmetry != mixed)) {
349                 exvector v = seq;
350                 int sig = canonicalize_indices(v.begin() + 1, v.end(), symmetry == antisymmetric);
351                 if (sig != INT_MAX) {
352                         // Something has changed while sorting indices, more evaluations later
353                         if (sig == 0)
354                                 return _ex0();
355                         return ex(sig) * thisexprseq(v);
356                 }
357         }
358
359         // Let the class of the base object perform additional evaluations
360         return base.bp->eval_indexed(*this);
361 }
362
363 ex indexed::thisexprseq(const exvector & v) const
364 {
365         return indexed(symmetry, v);
366 }
367
368 ex indexed::thisexprseq(exvector * vp) const
369 {
370         return indexed(symmetry, vp);
371 }
372
373 ex indexed::expand(unsigned options) const
374 {
375         GINAC_ASSERT(seq.size() > 0);
376
377         if ((options & expand_options::expand_indexed) && is_ex_exactly_of_type(seq[0], add)) {
378
379                 // expand_indexed expands (a+b).i -> a.i + b.i
380                 const ex & base = seq[0];
381                 ex sum = _ex0();
382                 for (unsigned i=0; i<base.nops(); i++) {
383                         exvector s = seq;
384                         s[0] = base.op(i);
385                         sum += thisexprseq(s).expand();
386                 }
387                 return sum;
388
389         } else
390                 return inherited::expand(options);
391 }
392
393 //////////
394 // virtual functions which can be overridden by derived classes
395 //////////
396
397 // none
398
399 //////////
400 // non-virtual functions in this class
401 //////////
402
403 void indexed::printrawindices(std::ostream & os) const
404 {
405         if (seq.size() > 1) {
406                 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
407                 while (it != itend) {
408                         it->printraw(os);
409                         it++;
410                         if (it != itend)
411                                 os << ",";
412                 }
413         }
414 }
415
416 void indexed::printtreeindices(std::ostream & os, unsigned indent) const
417 {
418         if (seq.size() > 1) {
419                 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
420                 while (it != itend) {
421                         os << std::string(indent + delta_indent, ' ');
422                         it->printraw(os);
423                         os << std::endl;
424                         it++;
425                 }
426         }
427 }
428
429 void indexed::printindices(std::ostream & os) const
430 {
431         if (seq.size() > 1) {
432                 exvector::const_iterator it=seq.begin() + 1, itend = seq.end();
433                 while (it != itend) {
434                         it->print(os);
435                         it++;
436                 }
437         }
438 }
439
440 /** Check whether all indices are of class idx. This function is used
441  *  internally to make sure that all constructed indexed objects really
442  *  carry indices and not some other classes. */
443 void indexed::assert_all_indices_of_type_idx(void) const
444 {
445         GINAC_ASSERT(seq.size() > 0);
446         exvector::const_iterator it = seq.begin() + 1, itend = seq.end();
447         while (it != itend) {
448                 if (!is_ex_of_type(*it, idx))
449                         throw(std::invalid_argument("indices of indexed object must be of type idx"));
450                 it++;
451         }
452 }
453
454 //////////
455 // global functions
456 //////////
457
458 /** Given a vector of indices, split them into two vectors, one containing
459  *  the free indices, the other containing the dummy indices. */
460 static void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
461 {
462         out_free.clear();
463         out_dummy.clear();
464
465         // No indices? Then do nothing
466         if (it == itend)
467                 return;
468
469         // Only one index? Then it is a free one if it's not numeric
470         if (itend - it == 1) {
471                 if (ex_to_idx(*it).is_symbolic())
472                         out_free.push_back(*it);
473                 return;
474         }
475
476         // Sort index vector. This will cause dummy indices come to lie next
477         // to each other (because the sort order is defined to guarantee this).
478         exvector v(it, itend);
479         sort_index_vector(v);
480
481         // Find dummy pairs and free indices
482         it = v.begin(); itend = v.end();
483         exvector::const_iterator last = it++;
484         while (it != itend) {
485                 if (is_dummy_pair(*it, *last)) {
486                         out_dummy.push_back(*last);
487                         it++;
488                         if (it == itend)
489                                 return;
490                 } else {
491                         if (!it->is_equal(*last) && ex_to_idx(*last).is_symbolic())
492                                 out_free.push_back(*last);
493                 }
494                 last = it++;
495         }
496         if (ex_to_idx(*last).is_symbolic())
497                 out_free.push_back(*last);
498 }
499
500 /** Check whether two sorted index vectors are consistent (i.e. equal). */
501 static bool indices_consistent(const exvector & v1, const exvector & v2)
502 {
503         // Number of indices must be the same
504         if (v1.size() != v2.size())
505                 return false;
506
507         // And also the indices themselves
508         exvector::const_iterator ait = v1.begin(), aitend = v1.end(),
509                                  bit = v2.begin(), bitend = v2.end();
510         while (ait != aitend) {
511                 if (!ait->is_equal(*bit))
512                         return false;
513                 ait++; bit++;
514         }
515         return true;
516 }
517
518 exvector indexed::get_dummy_indices(void) const
519 {
520         exvector free_indices, dummy_indices;
521         find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
522         return dummy_indices;
523 }
524
525 exvector indexed::get_free_indices(void) const
526 {
527         exvector free_indices, dummy_indices;
528         find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
529         return free_indices;
530 }
531
532 exvector add::get_free_indices(void) const
533 {
534         exvector free_indices;
535         for (unsigned i=0; i<nops(); i++) {
536                 if (i == 0)
537                         free_indices = op(i).get_free_indices();
538                 else {
539                         exvector free_indices_of_term = op(i).get_free_indices();
540                         if (!indices_consistent(free_indices, free_indices_of_term))
541                                 throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
542                 }
543         }
544         return free_indices;
545 }
546
547 exvector mul::get_free_indices(void) const
548 {
549         // Concatenate free indices of all factors
550         exvector un;
551         for (unsigned i=0; i<nops(); i++) {
552                 exvector free_indices_of_factor = op(i).get_free_indices();
553                 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
554         }
555
556         // And remove the dummy indices
557         exvector free_indices, dummy_indices;
558         find_free_and_dummy(un.begin(), un.end(), free_indices, dummy_indices);
559         return free_indices;
560 }
561
562 exvector ncmul::get_free_indices(void) const
563 {
564         // Concatenate free indices of all factors
565         exvector un;
566         for (unsigned i=0; i<nops(); i++) {
567                 exvector free_indices_of_factor = op(i).get_free_indices();
568                 un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
569         }
570
571         // And remove the dummy indices
572         exvector free_indices, dummy_indices;
573         find_free_and_dummy(un.begin(), un.end(), free_indices, dummy_indices);
574         return free_indices;
575 }
576
577 exvector power::get_free_indices(void) const
578 {
579         // Return free indices of basis
580         return basis.get_free_indices();
581 }
582
583 /** Simplify product of indexed expressions (commutative, noncommutative and
584  *  simple squares), return list of free indices. */
585 ex simplify_indexed_product(const ex & e, exvector & free_indices, const scalar_products & sp)
586 {
587         // Remember whether the product was commutative or noncommutative
588         // (because we chop it into factors and need to reassemble later)
589         bool non_commutative = is_ex_exactly_of_type(e, ncmul);
590
591         // Collect factors in an exvector, store squares twice
592         exvector v;
593         v.reserve(e.nops() * 2);
594
595         if (is_ex_exactly_of_type(e, power)) {
596                 // We only get called for simple squares, split a^2 -> a*a
597                 GINAC_ASSERT(e.op(1).is_equal(_ex2()));
598                 v.push_back(e.op(0));
599                 v.push_back(e.op(0));
600         } else {
601                 for (int i=0; i<e.nops(); i++) {
602                         ex f = e.op(i);
603                         if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2())) {
604                                 v.push_back(f.op(0));
605                     v.push_back(f.op(0));
606                         } else if (is_ex_exactly_of_type(f, ncmul)) {
607                                 // Noncommutative factor found, split it as well
608                                 non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
609                                 for (int j=0; j<f.nops(); i++)
610                                         v.push_back(f.op(j));
611                         } else
612                                 v.push_back(f);
613                 }
614         }
615
616         // Perform contractions
617         bool something_changed = false;
618         GINAC_ASSERT(v.size() > 1);
619         exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
620         for (it1 = v.begin(); it1 != next_to_last; it1++) {
621
622 try_again:
623                 if (!is_ex_of_type(*it1, indexed))
624                         continue;
625
626                 // Indexed factor found, look for contraction candidates
627                 exvector::iterator it2;
628                 for (it2 = it1 + 1; it2 != itend; it2++) {
629
630                         if (!is_ex_of_type(*it2, indexed))
631                                 continue;
632
633                         // Check whether the two factors share dummy indices
634                         exvector un(ex_to_indexed(*it1).seq.begin() + 1, ex_to_indexed(*it1).seq.end());
635                         un.insert(un.end(), ex_to_indexed(*it2).seq.begin() + 1, ex_to_indexed(*it2).seq.end());
636                         exvector free, dummy;
637                         find_free_and_dummy(un.begin(), un.end(), free, dummy);
638                         if (dummy.size() == 0)
639                                 continue;
640
641                         // At least one dummy index, is it a defined scalar product?
642                         if (free.size() == 0) {
643                                 if (sp.is_defined(*it1, *it2)) {
644                                         *it1 = sp.evaluate(*it1, *it2);
645                                         *it2 = _ex1();
646                                         something_changed = true;
647                                         goto try_again;
648                                 }
649                         }
650
651                         // Try to contract the first one with the second one
652                         bool contracted = it1->op(0).bp->contract_with(it1, it2, v);
653                         if (!contracted) {
654
655                                 // That didn't work; maybe the second object knows how to
656                                 // contract itself with the first one
657                                 contracted = it2->op(0).bp->contract_with(it2, it1, v);
658                         }
659                         if (contracted) {
660                                 something_changed = true;
661
662                                 // Both objects may have new indices now or they might
663                                 // even not be indexed objects any more, so we have to
664                                 // start over
665                                 goto try_again;
666                         }
667                 }
668         }
669
670         // Find free indices (concatenate them all and call find_free_and_dummy())
671         exvector un, dummy_indices;
672         it1 = v.begin(); itend = v.end();
673         while (it1 != itend) {
674                 if (is_ex_of_type(*it1, indexed)) {
675                         const indexed & o = ex_to_indexed(*it1);
676                         un.insert(un.end(), o.seq.begin() + 1, o.seq.end());
677                 }
678                 it1++;
679         }
680         find_free_and_dummy(un.begin(), un.end(), free_indices, dummy_indices);
681
682         if (something_changed) {
683                 if (non_commutative)
684                         return ncmul(v);
685                 else
686                         return mul(v);
687         } else
688                 return e;
689 }
690
691 /** Simplify indexed expression, return list of free indices. */
692 ex simplify_indexed(const ex & e, exvector & free_indices, const scalar_products & sp)
693 {
694         // Expand the expression
695         ex e_expanded = e.expand();
696
697         // Simplification of single indexed object: just find the free indices
698         if (is_ex_of_type(e_expanded, indexed)) {
699                 const indexed &i = ex_to_indexed(e_expanded);
700                 exvector dummy_indices;
701                 find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, dummy_indices);
702                 return e_expanded;
703         }
704
705         // Simplification of sum = sum of simplifications, check consistency of
706         // free indices in each term
707         if (is_ex_exactly_of_type(e_expanded, add)) {
708                 ex sum = _ex0();
709
710                 for (unsigned i=0; i<e_expanded.nops(); i++) {
711                         exvector free_indices_of_term;
712                         sum += simplify_indexed(e_expanded.op(i), free_indices_of_term, sp);
713                         if (i == 0)
714                                 free_indices = free_indices_of_term;
715                         else if (!indices_consistent(free_indices, free_indices_of_term))
716                                 throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
717                 }
718
719                 return sum;
720         }
721
722         // Simplification of products
723         if (is_ex_exactly_of_type(e_expanded, mul)
724          || is_ex_exactly_of_type(e_expanded, ncmul)
725          || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2())))
726                 return simplify_indexed_product(e_expanded, free_indices, sp);
727
728         // Cannot do anything
729         free_indices.clear();
730         return e_expanded;
731 }
732
733 ex simplify_indexed(const ex & e)
734 {
735         exvector free_indices;
736         scalar_products sp;
737         return simplify_indexed(e, free_indices, sp);
738 }
739
740 ex simplify_indexed(const ex & e, const scalar_products & sp)
741 {
742         exvector free_indices;
743         return simplify_indexed(e, free_indices, sp);
744 }
745
746 //////////
747 // helper classes
748 //////////
749
750 void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
751 {
752         spm[make_key(v1, v2)] = sp;
753 }
754
755 void scalar_products::clear(void)
756 {
757         spm.clear();
758 }
759
760 /** Check whether scalar product pair is defined. */
761 bool scalar_products::is_defined(const ex & v1, const ex & v2) const
762 {
763         return spm.find(make_key(v1, v2)) != spm.end();
764 }
765
766 /** Return value of defined scalar product pair. */
767 ex scalar_products::evaluate(const ex & v1, const ex & v2) const
768 {
769         return spm.find(make_key(v1, v2))->second;
770 }
771
772 void scalar_products::debugprint(void) const
773 {
774         std::cerr << "map size=" << spm.size() << std::endl;
775         for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
776                 const spmapkey & k = cit->first;
777                 std::cerr << "item key=(" << k.first << "," << k.second;
778                 std::cerr << "), value=" << cit->second << std::endl;
779         }
780 }
781
782 /** Make key from object pair. */
783 spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
784 {
785         // If indexed, extract base objects
786         ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
787         ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
788
789         // Enforce canonical order in pair
790         if (s1.compare(s2) > 0)
791                 return spmapkey(s2, s1);
792         else
793                 return spmapkey(s1, s2);
794 }
795
796 } // namespace GiNaC