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"
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))
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))
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))
54 // default constructor
57 idx::idx() : inherited(TINFO_idx) {}
59 varidx::varidx() : covariant(false)
61 tinfo_key = TINFO_varidx;
64 spinidx::spinidx() : dotted(false)
66 tinfo_key = TINFO_spinidx;
73 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
76 if (!dim.info(info_flags::posint))
77 throw(std::invalid_argument("dimension of space must be a positive integer"));
80 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
82 tinfo_key = TINFO_varidx;
85 spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot)
87 tinfo_key = TINFO_spinidx;
94 idx::idx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
96 n.find_ex("value", value, sym_lst);
97 n.find_ex("dim", dim, sym_lst);
100 varidx::varidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
102 n.find_bool("covariant", covariant);
105 spinidx::spinidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
107 n.find_bool("dotted", dotted);
110 void idx::archive(archive_node &n) const
112 inherited::archive(n);
113 n.add_ex("value", value);
114 n.add_ex("dim", dim);
117 void varidx::archive(archive_node &n) const
119 inherited::archive(n);
120 n.add_bool("covariant", covariant);
123 void spinidx::archive(archive_node &n) const
125 inherited::archive(n);
126 n.add_bool("dotted", dotted);
129 DEFAULT_UNARCHIVE(idx)
130 DEFAULT_UNARCHIVE(varidx)
131 DEFAULT_UNARCHIVE(spinidx)
134 // functions overriding virtual functions from base classes
137 void idx::print_index(const print_context & c, unsigned level) const
139 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
145 if (c.options & print_options::print_index_dimensions) {
152 void idx::do_print(const print_context & c, unsigned level) const
155 print_index(c, level);
158 void idx::do_print_latex(const print_latex & c, unsigned level) const
161 print_index(c, level);
165 void idx::do_print_tree(const print_tree & c, unsigned level) const
167 c.s << std::string(level, ' ') << class_name()
168 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
170 value.print(c, level + c.delta_indent);
171 dim.print(c, level + c.delta_indent);
174 void varidx::do_print(const print_context & c, unsigned level) const
180 print_index(c, level);
183 void varidx::do_print_tree(const print_tree & c, unsigned level) const
185 c.s << std::string(level, ' ') << class_name()
186 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
187 << (covariant ? ", covariant" : ", contravariant")
189 value.print(c, level + c.delta_indent);
190 dim.print(c, level + c.delta_indent);
193 void spinidx::do_print(const print_context & c, unsigned level) const
201 print_index(c, level);
204 void spinidx::do_print_latex(const print_latex & c, unsigned level) const
210 print_index(c, level);
214 void spinidx::do_print_tree(const print_tree & c, unsigned level) const
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")
221 value.print(c, level + c.delta_indent);
222 dim.print(c, level + c.delta_indent);
225 bool idx::info(unsigned inf) const
227 if (inf == info_flags::idx)
229 return inherited::info(inf);
232 size_t idx::nops() const
234 // don't count the dimension as that is not really a sub-expression
238 ex idx::op(size_t i) const
240 GINAC_ASSERT(i == 0);
244 ex idx::map(map_function & f) const
246 idx *copy = duplicate();
247 copy->setflag(status_flags::dynallocated);
248 copy->clearflag(status_flags::hash_calculated);
249 copy->value = f(value);
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
257 GINAC_ASSERT(is_a<idx>(other));
258 const idx &o = static_cast<const idx &>(other);
260 int cmpval = value.compare(o.value);
263 return dim.compare(o.dim);
266 bool idx::match_same_type(const basic & other) const
268 GINAC_ASSERT(is_a<idx>(other));
269 const idx &o = static_cast<const idx &>(other);
271 return dim.is_equal(o.dim);
274 int varidx::compare_same_type(const basic & other) const
276 GINAC_ASSERT(is_a<varidx>(other));
277 const varidx &o = static_cast<const varidx &>(other);
279 int cmpval = inherited::compare_same_type(other);
283 // Check variance last so dummy indices will end up next to each other
284 if (covariant != o.covariant)
285 return covariant ? -1 : 1;
290 bool varidx::match_same_type(const basic & other) const
292 GINAC_ASSERT(is_a<varidx>(other));
293 const varidx &o = static_cast<const varidx &>(other);
295 if (covariant != o.covariant)
298 return inherited::match_same_type(other);
301 int spinidx::compare_same_type(const basic & other) const
303 GINAC_ASSERT(is_a<spinidx>(other));
304 const spinidx &o = static_cast<const spinidx &>(other);
306 // Check dottedness first so dummy indices will end up next to each other
307 if (dotted != o.dotted)
308 return dotted ? -1 : 1;
310 int cmpval = inherited::compare_same_type(other);
317 bool spinidx::match_same_type(const basic & other) const
319 GINAC_ASSERT(is_a<spinidx>(other));
320 const spinidx &o = static_cast<const spinidx &>(other);
322 if (dotted != o.dotted)
324 return inherited::match_same_type(other);
327 unsigned idx::calchash() const
329 unsigned v = golden_ratio_hash(tinfo());
331 v ^= value.gethash();
335 // Store calculated hash value only if object is already evaluated
336 if (flags & status_flags::evaluated) {
337 setflag(status_flags::hash_calculated);
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
351 ex idx::subs(const exmap & m, unsigned options) const
353 // First look for index substitutions
354 exmap::const_iterator it = m.find(*this);
357 // Substitution index->index
358 if (is_a<idx>(it->second))
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);
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))
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);
379 /** Implementation of ex::diff() for an index always returns 0.
382 ex idx::derivative(const symbol & s) const
388 // new virtual functions
391 bool idx::is_dummy_pair_same_type(const basic & other) const
393 const idx &o = static_cast<const idx &>(other);
395 // Only pure symbols form dummy pairs, "2n+1" doesn't
396 if (!is_a<symbol>(value))
399 // Value must be equal, of course
400 if (!value.is_equal(o.value))
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))
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)));
411 bool varidx::is_dummy_pair_same_type(const basic & other) const
413 const varidx &o = static_cast<const varidx &>(other);
415 // Variance must be opposite
416 if (covariant == o.covariant)
419 return inherited::is_dummy_pair_same_type(other);
422 bool spinidx::is_dummy_pair_same_type(const basic & other) const
424 const spinidx &o = static_cast<const spinidx &>(other);
426 // Dottedness must be the same
427 if (dotted != o.dotted)
430 return inherited::is_dummy_pair_same_type(other);
435 // non-virtual functions
438 ex idx::replace_dim(const ex & new_dim) const
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);
446 ex idx::minimal_dim(const idx & other) const
448 return GiNaC::minimal_dim(dim, other.dim);
451 ex varidx::toggle_variance() const
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);
459 ex spinidx::toggle_dot() const
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);
467 ex spinidx::toggle_variance_dot() const
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);
480 bool is_dummy_pair(const idx & i1, const idx & i2)
482 // The indices must be of exactly the same type
483 if (i1.tinfo() != i2.tinfo())
486 // Same type, let the indices decide whether they are paired
487 return i1.is_dummy_pair_same_type(i2);
490 bool is_dummy_pair(const ex & e1, const ex & e2)
492 // The expressions must be indices
493 if (!is_a<idx>(e1) || !is_a<idx>(e2))
496 return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
499 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
504 // No indices? Then do nothing
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);
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());
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);
530 if (!it->is_equal(*last) && ex_to<idx>(*last).is_symbolic())
531 out_free.push_back(*last);
535 if (ex_to<idx>(*last).is_symbolic())
536 out_free.push_back(*last);
539 ex minimal_dim(const ex & dim1, const ex & dim2)
541 if (dim1.is_equal(dim2) || dim1 < dim2 || (is_exactly_a<numeric>(dim1) && is_a<symbol>(dim2)))
543 else if (dim1 > dim2 || (is_a<symbol>(dim1) && is_exactly_a<numeric>(dim2)))
546 std::ostringstream s;
547 s << "minimal_dim(): index dimensions " << dim1 << " and " << dim2 << " cannot be ordered";
548 throw (std::runtime_error(s.str()));