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