3 * Implementation of GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
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.
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.
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
30 #include "relational.h"
31 #include "operators.h"
37 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(idx, basic,
38 print_func<print_context>(&idx::do_print).
39 print_func<print_latex>(&idx::do_print_latex).
40 print_func<print_tree>(&idx::do_print_tree))
42 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(varidx, idx,
43 print_func<print_context>(&varidx::do_print).
44 // print_latex inherited from idx
45 print_func<print_tree>(&varidx::do_print_tree))
47 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(spinidx, varidx,
48 print_func<print_context>(&spinidx::do_print).
49 print_func<print_latex>(&spinidx::do_print_latex).
50 print_func<print_tree>(&spinidx::do_print_tree))
53 // default constructor
56 idx::idx() : inherited(TINFO_idx) {}
58 varidx::varidx() : covariant(false)
60 tinfo_key = TINFO_varidx;
63 spinidx::spinidx() : dotted(false)
65 tinfo_key = TINFO_spinidx;
72 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
75 if (!dim.info(info_flags::posint))
76 throw(std::invalid_argument("dimension of space must be a positive integer"));
79 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
81 tinfo_key = TINFO_varidx;
84 spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot)
86 tinfo_key = TINFO_spinidx;
93 idx::idx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
95 n.find_ex("value", value, sym_lst);
96 n.find_ex("dim", dim, sym_lst);
99 varidx::varidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
101 n.find_bool("covariant", covariant);
104 spinidx::spinidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
106 n.find_bool("dotted", dotted);
109 void idx::archive(archive_node &n) const
111 inherited::archive(n);
112 n.add_ex("value", value);
113 n.add_ex("dim", dim);
116 void varidx::archive(archive_node &n) const
118 inherited::archive(n);
119 n.add_bool("covariant", covariant);
122 void spinidx::archive(archive_node &n) const
124 inherited::archive(n);
125 n.add_bool("dotted", dotted);
128 DEFAULT_UNARCHIVE(idx)
129 DEFAULT_UNARCHIVE(varidx)
130 DEFAULT_UNARCHIVE(spinidx)
133 // functions overriding virtual functions from base classes
136 void idx::print_index(const print_context & c, unsigned level) const
138 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
144 if (c.options & print_options::print_index_dimensions) {
151 void idx::do_print(const print_context & c, unsigned level) const
154 print_index(c, level);
157 void idx::do_print_latex(const print_latex & c, unsigned level) const
160 print_index(c, level);
164 void idx::do_print_tree(const print_tree & c, unsigned level) const
166 c.s << std::string(level, ' ') << class_name()
167 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
169 value.print(c, level + c.delta_indent);
170 dim.print(c, level + c.delta_indent);
173 void varidx::do_print(const print_context & c, unsigned level) const
179 print_index(c, level);
182 void varidx::do_print_tree(const print_tree & c, unsigned level) const
184 c.s << std::string(level, ' ') << class_name()
185 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
186 << (covariant ? ", covariant" : ", contravariant")
188 value.print(c, level + c.delta_indent);
189 dim.print(c, level + c.delta_indent);
192 void spinidx::do_print(const print_context & c, unsigned level) const
200 print_index(c, level);
203 void spinidx::do_print_latex(const print_latex & c, unsigned level) const
209 print_index(c, level);
213 void spinidx::do_print_tree(const print_tree & c, unsigned level) const
215 c.s << std::string(level, ' ') << class_name()
216 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
217 << (covariant ? ", covariant" : ", contravariant")
218 << (dotted ? ", dotted" : ", undotted")
220 value.print(c, level + c.delta_indent);
221 dim.print(c, level + c.delta_indent);
224 bool idx::info(unsigned inf) const
226 if (inf == info_flags::idx)
228 return inherited::info(inf);
231 size_t idx::nops() const
233 // don't count the dimension as that is not really a sub-expression
237 ex idx::op(size_t i) const
239 GINAC_ASSERT(i == 0);
243 ex idx::map(map_function & f) const
245 idx *copy = duplicate();
246 copy->setflag(status_flags::dynallocated);
247 copy->clearflag(status_flags::hash_calculated);
248 copy->value = f(value);
252 /** Returns order relation between two indices of the same type. The order
253 * must be such that dummy indices lie next to each other. */
254 int idx::compare_same_type(const basic & other) const
256 GINAC_ASSERT(is_a<idx>(other));
257 const idx &o = static_cast<const idx &>(other);
259 int cmpval = value.compare(o.value);
262 return dim.compare(o.dim);
265 bool idx::match_same_type(const basic & other) const
267 GINAC_ASSERT(is_a<idx>(other));
268 const idx &o = static_cast<const idx &>(other);
270 return dim.is_equal(o.dim);
273 int varidx::compare_same_type(const basic & other) const
275 GINAC_ASSERT(is_a<varidx>(other));
276 const varidx &o = static_cast<const varidx &>(other);
278 int cmpval = inherited::compare_same_type(other);
282 // Check variance last so dummy indices will end up next to each other
283 if (covariant != o.covariant)
284 return covariant ? -1 : 1;
289 bool varidx::match_same_type(const basic & other) const
291 GINAC_ASSERT(is_a<varidx>(other));
292 const varidx &o = static_cast<const varidx &>(other);
294 if (covariant != o.covariant)
297 return inherited::match_same_type(other);
300 int spinidx::compare_same_type(const basic & other) const
302 GINAC_ASSERT(is_a<spinidx>(other));
303 const spinidx &o = static_cast<const spinidx &>(other);
305 // Check dottedness first so dummy indices will end up next to each other
306 if (dotted != o.dotted)
307 return dotted ? -1 : 1;
309 int cmpval = inherited::compare_same_type(other);
316 bool spinidx::match_same_type(const basic & other) const
318 GINAC_ASSERT(is_a<spinidx>(other));
319 const spinidx &o = static_cast<const spinidx &>(other);
321 if (dotted != o.dotted)
323 return inherited::match_same_type(other);
326 unsigned idx::calchash() const
328 unsigned v = golden_ratio_hash(tinfo());
330 v ^= value.gethash();
334 // Store calculated hash value only if object is already evaluated
335 if (flags & status_flags::evaluated) {
336 setflag(status_flags::hash_calculated);
343 /** By default, basic::evalf would evaluate the index value but we don't want
344 * a.1 to become a.(1.0). */
345 ex idx::evalf(int level) const
350 ex idx::subs(const exmap & m, unsigned options) const
352 // First look for index substitutions
353 exmap::const_iterator it = m.find(*this);
356 // Substitution index->index
357 if (is_a<idx>(it->second))
360 // Otherwise substitute value
361 idx *i_copy = duplicate();
362 i_copy->value = it->second;
363 i_copy->clearflag(status_flags::hash_calculated);
364 return i_copy->setflag(status_flags::dynallocated);
367 // None, substitute objects in value (not in dimension)
368 const ex &subsed_value = value.subs(m, options);
369 if (are_ex_trivially_equal(value, subsed_value))
372 idx *i_copy = duplicate();
373 i_copy->value = subsed_value;
374 i_copy->clearflag(status_flags::hash_calculated);
375 return i_copy->setflag(status_flags::dynallocated);
378 /** Implementation of ex::diff() for an index always returns 0.
381 ex idx::derivative(const symbol & s) const
387 // new virtual functions
390 bool idx::is_dummy_pair_same_type(const basic & other) const
392 const idx &o = static_cast<const idx &>(other);
394 // Only pure symbols form dummy pairs, "2n+1" doesn't
395 if (!is_a<symbol>(value))
398 // Value must be equal, of course
399 if (!value.is_equal(o.value))
402 // Dimensions need not be equal but must be comparable (so we can
403 // determine the minimum dimension of contractions)
404 if (dim.is_equal(o.dim))
407 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)));
410 bool varidx::is_dummy_pair_same_type(const basic & other) const
412 const varidx &o = static_cast<const varidx &>(other);
414 // Variance must be opposite
415 if (covariant == o.covariant)
418 return inherited::is_dummy_pair_same_type(other);
421 bool spinidx::is_dummy_pair_same_type(const basic & other) const
423 const spinidx &o = static_cast<const spinidx &>(other);
425 // Dottedness must be the same
426 if (dotted != o.dotted)
429 return inherited::is_dummy_pair_same_type(other);
434 // non-virtual functions
437 ex idx::replace_dim(const ex & new_dim) const
439 idx *i_copy = duplicate();
440 i_copy->dim = new_dim;
441 i_copy->clearflag(status_flags::hash_calculated);
442 return i_copy->setflag(status_flags::dynallocated);
445 ex idx::minimal_dim(const idx & other) const
447 return GiNaC::minimal_dim(dim, other.dim);
450 ex varidx::toggle_variance() const
452 varidx *i_copy = duplicate();
453 i_copy->covariant = !i_copy->covariant;
454 i_copy->clearflag(status_flags::hash_calculated);
455 return i_copy->setflag(status_flags::dynallocated);
458 ex spinidx::toggle_dot() const
460 spinidx *i_copy = duplicate();
461 i_copy->dotted = !i_copy->dotted;
462 i_copy->clearflag(status_flags::hash_calculated);
463 return i_copy->setflag(status_flags::dynallocated);
466 ex spinidx::toggle_variance_dot() const
468 spinidx *i_copy = duplicate();
469 i_copy->covariant = !i_copy->covariant;
470 i_copy->dotted = !i_copy->dotted;
471 i_copy->clearflag(status_flags::hash_calculated);
472 return i_copy->setflag(status_flags::dynallocated);
479 bool is_dummy_pair(const idx & i1, const idx & i2)
481 // The indices must be of exactly the same type
482 if (i1.tinfo() != i2.tinfo())
485 // Same type, let the indices decide whether they are paired
486 return i1.is_dummy_pair_same_type(i2);
489 bool is_dummy_pair(const ex & e1, const ex & e2)
491 // The expressions must be indices
492 if (!is_a<idx>(e1) || !is_a<idx>(e2))
495 return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
498 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
503 // No indices? Then do nothing
507 // Only one index? Then it is a free one if it's not numeric
508 if (itend - it == 1) {
509 if (ex_to<idx>(*it).is_symbolic())
510 out_free.push_back(*it);
514 // Sort index vector. This will cause dummy indices come to lie next
515 // to each other (because the sort order is defined to guarantee this).
516 exvector v(it, itend);
517 shaker_sort(v.begin(), v.end(), ex_is_less(), ex_swap());
519 // Find dummy pairs and free indices
520 it = v.begin(); itend = v.end();
521 exvector::const_iterator last = it++;
522 while (it != itend) {
523 if (is_dummy_pair(*it, *last)) {
524 out_dummy.push_back(*last);
529 if (!it->is_equal(*last) && ex_to<idx>(*last).is_symbolic())
530 out_free.push_back(*last);
534 if (ex_to<idx>(*last).is_symbolic())
535 out_free.push_back(*last);
538 ex minimal_dim(const ex & dim1, const ex & dim2)
540 if (dim1.is_equal(dim2) || dim1 < dim2 || (is_exactly_a<numeric>(dim1) && is_a<symbol>(dim2)))
542 else if (dim1 > dim2 || (is_a<symbol>(dim1) && is_exactly_a<numeric>(dim2)))
545 std::ostringstream s;
546 s << "minimal_dim(): index dimensions " << dim1 << " and " << dim2 << " cannot be ordered";
547 throw (std::runtime_error(s.str()));