]> www.ginac.de Git - ginac.git/blob - ginac/idx.cpp
Use AX_CXX_COMPILE_STDCXX_11 macro to force C++11 compilation.
[ginac.git] / ginac / idx.cpp
1 /** @file idx.cpp
2  *
3  *  Implementation of GiNaC's indices. */
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 "idx.h"
24 #include "symbol.h"
25 #include "lst.h"
26 #include "relational.h"
27 #include "operators.h"
28 #include "archive.h"
29 #include "utils.h"
30 #include "hash_seed.h"
31
32 #include <iostream>
33 #include <sstream>
34 #include <stdexcept>
35
36 namespace GiNaC {
37
38 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(idx, basic,
39   print_func<print_context>(&idx::do_print).
40   print_func<print_latex>(&idx::do_print_latex).
41   print_func<print_csrc>(&idx::do_print_csrc).
42   print_func<print_tree>(&idx::do_print_tree))
43
44 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(varidx, idx,
45   print_func<print_context>(&varidx::do_print).
46   print_func<print_latex>(&varidx::do_print_latex).
47   print_func<print_tree>(&varidx::do_print_tree))
48
49 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(spinidx, varidx,
50   print_func<print_context>(&spinidx::do_print).
51   print_func<print_latex>(&spinidx::do_print_latex).
52   print_func<print_tree>(&spinidx::do_print_tree))
53
54 //////////
55 // default constructor
56 //////////
57
58 idx::idx() {}
59
60 varidx::varidx() : covariant(false)
61 {
62 }
63
64 spinidx::spinidx() : dotted(false)
65 {
66 }
67
68 //////////
69 // other constructors
70 //////////
71
72 idx::idx(const ex & v, const ex & d) :  value(v), dim(d)
73 {
74         if (is_dim_numeric())
75                 if (!dim.info(info_flags::posint))
76                         throw(std::invalid_argument("dimension of space must be a positive integer"));
77 }
78
79 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
80 {
81 }
82
83 spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot)
84 {
85 }
86
87 //////////
88 // archiving
89 //////////
90
91 void idx::read_archive(const archive_node& n, lst& sym_lst) 
92 {
93         inherited::read_archive(n, sym_lst);
94         n.find_ex("value", value, sym_lst);
95         n.find_ex("dim", dim, sym_lst);
96 }
97 GINAC_BIND_UNARCHIVER(idx);
98
99 void varidx::read_archive(const archive_node& n, lst& sym_lst)
100 {
101         inherited::read_archive(n, sym_lst);
102         n.find_bool("covariant", covariant);
103 }
104 GINAC_BIND_UNARCHIVER(varidx);
105
106 void spinidx::read_archive(const archive_node& n, lst& sym_lst)
107 {
108         inherited::read_archive(n, sym_lst);
109         n.find_bool("dotted", dotted);
110 }
111 GINAC_BIND_UNARCHIVER(spinidx);
112
113 void idx::archive(archive_node &n) const
114 {
115         inherited::archive(n);
116         n.add_ex("value", value);
117         n.add_ex("dim", dim);
118 }
119
120 void varidx::archive(archive_node &n) const
121 {
122         inherited::archive(n);
123         n.add_bool("covariant", covariant);
124 }
125
126 void spinidx::archive(archive_node &n) const
127 {
128         inherited::archive(n);
129         n.add_bool("dotted", dotted);
130 }
131
132 //////////
133 // functions overriding virtual functions from base classes
134 //////////
135
136 void idx::print_index(const print_context & c, unsigned level) const
137 {
138         bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
139         if (need_parens)
140                 c.s << "(";
141         value.print(c);
142         if (need_parens)
143                 c.s << ")";
144         if (c.options & print_options::print_index_dimensions) {
145                 c.s << "[";
146                 dim.print(c);
147                 c.s << "]";
148         }
149 }
150
151 void idx::do_print(const print_context & c, unsigned level) const
152 {
153         c.s << ".";
154         print_index(c, level);
155 }
156
157 void idx::do_print_latex(const print_latex & c, unsigned level) const
158 {
159         c.s << "{";
160         print_index(c, level);
161         c.s << "}";
162 }
163
164 void idx::do_print_csrc(const print_csrc & c, unsigned level) const
165 {
166         c.s << "[";
167         if (value.info(info_flags::integer))
168                 c.s << ex_to<numeric>(value).to_int();
169         else
170                 value.print(c);
171         c.s << "]";
172 }
173
174 void idx::do_print_tree(const print_tree & c, unsigned level) const
175 {
176         c.s << std::string(level, ' ') << class_name() << " @" << this
177             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
178             << std::endl;
179         value.print(c, level +  c.delta_indent);
180         dim.print(c, level + c.delta_indent);
181 }
182
183 void varidx::do_print(const print_context & c, unsigned level) const
184 {
185         if (covariant)
186                 c.s << ".";
187         else
188                 c.s << "~";
189         print_index(c, level);
190 }
191
192 void varidx::do_print_tree(const print_tree & c, unsigned level) const
193 {
194         c.s << std::string(level, ' ') << class_name() << " @" << this
195             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
196             << (covariant ? ", covariant" : ", contravariant")
197             << std::endl;
198         value.print(c, level + c.delta_indent);
199         dim.print(c, level + c.delta_indent);
200 }
201
202 void spinidx::do_print(const print_context & c, unsigned level) const
203 {
204         if (covariant)
205                 c.s << ".";
206         else
207                 c.s << "~";
208         if (dotted)
209                 c.s << "*";
210         print_index(c, level);
211 }
212
213 void spinidx::do_print_latex(const print_latex & c, unsigned level) const
214 {
215         if (dotted)
216                 c.s << "\\dot{";
217         else
218                 c.s << "{";
219         print_index(c, level);
220         c.s << "}";
221 }
222
223 void spinidx::do_print_tree(const print_tree & c, unsigned level) const
224 {
225         c.s << std::string(level, ' ') << class_name() << " @" << this
226             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
227             << (covariant ? ", covariant" : ", contravariant")
228             << (dotted ? ", dotted" : ", undotted")
229             << std::endl;
230         value.print(c, level + c.delta_indent);
231         dim.print(c, level + c.delta_indent);
232 }
233
234 bool idx::info(unsigned inf) const
235 {
236         switch(inf) {
237                 case info_flags::idx:
238                 case info_flags::has_indices:
239                         return true;
240         }
241         return inherited::info(inf);
242 }
243
244 size_t idx::nops() const
245 {
246         // don't count the dimension as that is not really a sub-expression
247         return 1;
248 }
249
250 ex idx::op(size_t i) const
251 {
252         GINAC_ASSERT(i == 0);
253         return value;
254 }
255
256 ex idx::map(map_function & f) const
257 {
258         const ex &mapped_value = f(value);
259         if (are_ex_trivially_equal(value, mapped_value))
260                 return *this;
261         else {
262                 idx *copy = duplicate();
263                 copy->setflag(status_flags::dynallocated);
264                 copy->clearflag(status_flags::hash_calculated);
265                 copy->value = mapped_value;
266                 return *copy;
267         }
268 }
269
270 /** Returns order relation between two indices of the same type. The order
271  *  must be such that dummy indices lie next to each other. */
272 int idx::compare_same_type(const basic & other) const
273 {
274         GINAC_ASSERT(is_a<idx>(other));
275         const idx &o = static_cast<const idx &>(other);
276
277         int cmpval = value.compare(o.value);
278         if (cmpval)
279                 return cmpval;
280         return dim.compare(o.dim);
281 }
282
283 bool idx::match_same_type(const basic & other) const
284 {
285         GINAC_ASSERT(is_a<idx>(other));
286         const idx &o = static_cast<const idx &>(other);
287
288         return dim.is_equal(o.dim);
289 }
290
291 int varidx::compare_same_type(const basic & other) const
292 {
293         GINAC_ASSERT(is_a<varidx>(other));
294         const varidx &o = static_cast<const varidx &>(other);
295
296         int cmpval = inherited::compare_same_type(other);
297         if (cmpval)
298                 return cmpval;
299
300         // Check variance last so dummy indices will end up next to each other
301         if (covariant != o.covariant)
302                 return covariant ? -1 : 1;
303
304         return 0;
305 }
306
307 bool varidx::match_same_type(const basic & other) const
308 {
309         GINAC_ASSERT(is_a<varidx>(other));
310         const varidx &o = static_cast<const varidx &>(other);
311
312         if (covariant != o.covariant)
313                 return false;
314
315         return inherited::match_same_type(other);
316 }
317
318 int spinidx::compare_same_type(const basic & other) const
319 {
320         GINAC_ASSERT(is_a<spinidx>(other));
321         const spinidx &o = static_cast<const spinidx &>(other);
322
323         // Check dottedness first so dummy indices will end up next to each other
324         if (dotted != o.dotted)
325                 return dotted ? -1 : 1;
326
327         int cmpval = inherited::compare_same_type(other);
328         if (cmpval)
329                 return cmpval;
330
331         return 0;
332 }
333
334 bool spinidx::match_same_type(const basic & other) const
335 {
336         GINAC_ASSERT(is_a<spinidx>(other));
337         const spinidx &o = static_cast<const spinidx &>(other);
338
339         if (dotted != o.dotted)
340                 return false;
341         return inherited::match_same_type(other);
342 }
343
344 unsigned idx::calchash() const
345 {
346         // NOTE: The code in simplify_indexed() assumes that canonically
347         // ordered sequences of indices have the two members of dummy index
348         // pairs lying next to each other. The hash values for indices must
349         // be devised accordingly. The easiest (only?) way to guarantee the
350         // desired ordering is to make indices with the same value have equal
351         // hash keys. That is, the hash values must not depend on the index
352         // dimensions or other attributes (variance etc.).
353         // The compare_same_type() methods will take care of the rest.
354         unsigned v = make_hash_seed(typeid(*this));
355         v = rotate_left(v);
356         v ^= value.gethash();
357
358         // Store calculated hash value only if object is already evaluated
359         if (flags & status_flags::evaluated) {
360                 setflag(status_flags::hash_calculated);
361                 hashvalue = v;
362         }
363
364         return v;
365 }
366
367 /** By default, basic::evalf would evaluate the index value but we don't want
368  *  a.1 to become a.(1.0). */
369 ex idx::evalf(int level) const
370 {
371         return *this;
372 }
373
374 ex idx::subs(const exmap & m, unsigned options) const
375 {
376         // First look for index substitutions
377         exmap::const_iterator it = m.find(*this);
378         if (it != m.end()) {
379
380                 // Substitution index->index
381                 if (is_a<idx>(it->second) || (options & subs_options::really_subs_idx))
382                         return it->second;
383
384                 // Otherwise substitute value
385                 idx *i_copy = duplicate();
386                 i_copy->value = it->second;
387                 i_copy->clearflag(status_flags::hash_calculated);
388                 return i_copy->setflag(status_flags::dynallocated);
389         }
390
391         // None, substitute objects in value (not in dimension)
392         const ex &subsed_value = value.subs(m, options);
393         if (are_ex_trivially_equal(value, subsed_value))
394                 return *this;
395
396         idx *i_copy = duplicate();
397         i_copy->value = subsed_value;
398         i_copy->clearflag(status_flags::hash_calculated);
399         return i_copy->setflag(status_flags::dynallocated);
400 }
401
402 /** Implementation of ex::diff() for an index always returns 0.
403  *
404  *  @see ex::diff */
405 ex idx::derivative(const symbol & s) const
406 {
407         return _ex0;
408 }
409
410 //////////
411 // new virtual functions
412 //////////
413
414 bool idx::is_dummy_pair_same_type(const basic & other) const
415 {
416         const idx &o = static_cast<const idx &>(other);
417
418         // Only pure symbols form dummy pairs, "2n+1" doesn't
419         if (!is_a<symbol>(value))
420                 return false;
421
422         // Value must be equal, of course
423         if (!value.is_equal(o.value))
424                 return false;
425
426         // Dimensions need not be equal but must be comparable (so we can
427         // determine the minimum dimension of contractions)
428         if (dim.is_equal(o.dim))
429                 return true;
430
431         return is_exactly_a<numeric>(dim) || is_exactly_a<numeric>(o.dim);
432 }
433
434 bool varidx::is_dummy_pair_same_type(const basic & other) const
435 {
436         const varidx &o = static_cast<const varidx &>(other);
437
438         // Variance must be opposite
439         if (covariant == o.covariant)
440                 return false;
441
442         return inherited::is_dummy_pair_same_type(other);
443 }
444
445 bool spinidx::is_dummy_pair_same_type(const basic & other) const
446 {
447         const spinidx &o = static_cast<const spinidx &>(other);
448
449         // Dottedness must be the same
450         if (dotted != o.dotted)
451                 return false;
452
453         return inherited::is_dummy_pair_same_type(other);
454 }
455
456
457 //////////
458 // non-virtual functions
459 //////////
460
461 ex idx::replace_dim(const ex & new_dim) const
462 {
463         idx *i_copy = duplicate();
464         i_copy->dim = new_dim;
465         i_copy->clearflag(status_flags::hash_calculated);
466         return i_copy->setflag(status_flags::dynallocated);
467 }
468
469 ex idx::minimal_dim(const idx & other) const
470 {
471         return GiNaC::minimal_dim(dim, other.dim);
472 }
473
474 ex varidx::toggle_variance() const
475 {
476         varidx *i_copy = duplicate();
477         i_copy->covariant = !i_copy->covariant;
478         i_copy->clearflag(status_flags::hash_calculated);
479         return i_copy->setflag(status_flags::dynallocated);
480 }
481
482 ex spinidx::toggle_dot() const
483 {
484         spinidx *i_copy = duplicate();
485         i_copy->dotted = !i_copy->dotted;
486         i_copy->clearflag(status_flags::hash_calculated);
487         return i_copy->setflag(status_flags::dynallocated);
488 }
489
490 ex spinidx::toggle_variance_dot() const
491 {
492         spinidx *i_copy = duplicate();
493         i_copy->covariant = !i_copy->covariant;
494         i_copy->dotted = !i_copy->dotted;
495         i_copy->clearflag(status_flags::hash_calculated);
496         return i_copy->setflag(status_flags::dynallocated);
497 }
498
499 //////////
500 // global functions
501 //////////
502
503 bool is_dummy_pair(const idx & i1, const idx & i2)
504 {
505         // The indices must be of exactly the same type
506         if (typeid(i1) != typeid(i2))
507                 return false;
508
509         // Same type, let the indices decide whether they are paired
510         return i1.is_dummy_pair_same_type(i2);
511 }
512
513 bool is_dummy_pair(const ex & e1, const ex & e2)
514 {
515         // The expressions must be indices
516         if (!is_a<idx>(e1) || !is_a<idx>(e2))
517                 return false;
518
519         return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
520 }
521
522 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
523 {
524         out_free.clear();
525         out_dummy.clear();
526
527         // No indices? Then do nothing
528         if (it == itend)
529                 return;
530
531         // Only one index? Then it is a free one if it's not numeric
532         if (itend - it == 1) {
533                 if (ex_to<idx>(*it).is_symbolic())
534                         out_free.push_back(*it);
535                 return;
536         }
537
538         // Sort index vector. This will cause dummy indices come to lie next
539         // to each other (because the sort order is defined to guarantee this).
540         exvector v(it, itend);
541         shaker_sort(v.begin(), v.end(), ex_is_less(), ex_swap());
542
543         // Find dummy pairs and free indices
544         it = v.begin(); itend = v.end();
545         exvector::const_iterator last = it++;
546         while (it != itend) {
547                 if (is_dummy_pair(*it, *last)) {
548                         out_dummy.push_back(*last);
549                         it++;
550                         if (it == itend)
551                                 return;
552                 } else {
553                         if (!it->is_equal(*last) && ex_to<idx>(*last).is_symbolic())
554                                 out_free.push_back(*last);
555                 }
556                 last = it++;
557         }
558         if (ex_to<idx>(*last).is_symbolic())
559                 out_free.push_back(*last);
560 }
561
562 ex minimal_dim(const ex & dim1, const ex & dim2)
563 {
564         if (dim1.is_equal(dim2) || dim1 < dim2 || (is_exactly_a<numeric>(dim1) && !is_a<numeric>(dim2)))
565                 return dim1;
566         else if (dim1 > dim2 || (!is_a<numeric>(dim1) && is_exactly_a<numeric>(dim2)))
567                 return dim2;
568         else {
569                 std::ostringstream s;
570                 s << "minimal_dim(): index dimensions " << dim1 << " and " << dim2 << " cannot be ordered";
571                 throw (std::runtime_error(s.str()));
572         }
573 }
574
575 } // namespace GiNaC