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