3 * Implementation of GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2002 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
29 #include "relational.h"
36 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
37 GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx)
38 GINAC_IMPLEMENT_REGISTERED_CLASS(spinidx, varidx)
41 // default ctor, dtor, copy ctor, assignment operator and helpers
44 idx::idx() : inherited(TINFO_idx) {}
46 varidx::varidx() : covariant(false)
48 tinfo_key = TINFO_varidx;
51 spinidx::spinidx() : dotted(false)
53 tinfo_key = TINFO_spinidx;
56 void idx::copy(const idx & other)
58 inherited::copy(other);
63 void varidx::copy(const varidx & other)
65 inherited::copy(other);
66 covariant = other.covariant;
69 void spinidx::copy(const spinidx & other)
71 inherited::copy(other);
72 dotted = other.dotted;
76 DEFAULT_DESTROY(varidx)
77 DEFAULT_DESTROY(spinidx)
83 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
86 if (!dim.info(info_flags::posint))
87 throw(std::invalid_argument("dimension of space must be a positive integer"));
90 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
92 tinfo_key = TINFO_varidx;
95 spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot)
97 tinfo_key = TINFO_spinidx;
104 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
106 n.find_ex("value", value, sym_lst);
107 n.find_ex("dim", dim, sym_lst);
110 varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
112 n.find_bool("covariant", covariant);
115 spinidx::spinidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
117 n.find_bool("dotted", dotted);
120 void idx::archive(archive_node &n) const
122 inherited::archive(n);
123 n.add_ex("value", value);
124 n.add_ex("dim", dim);
127 void varidx::archive(archive_node &n) const
129 inherited::archive(n);
130 n.add_bool("covariant", covariant);
133 void spinidx::archive(archive_node &n) const
135 inherited::archive(n);
136 n.add_bool("dotted", dotted);
139 DEFAULT_UNARCHIVE(idx)
140 DEFAULT_UNARCHIVE(varidx)
141 DEFAULT_UNARCHIVE(spinidx)
144 // functions overriding virtual functions from base classes
147 void idx::print(const print_context & c, unsigned level) const
149 if (is_of_type(c, print_tree)) {
151 c.s << std::string(level, ' ') << class_name()
152 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
154 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
155 value.print(c, level + delta_indent);
156 dim.print(c, level + delta_indent);
160 if (is_a<print_latex>(c))
164 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
170 if (is_a<print_latex>(c))
175 void varidx::print(const print_context & c, unsigned level) const
177 if (is_of_type(c, print_tree)) {
179 c.s << std::string(level, ' ') << class_name()
180 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
181 << (covariant ? ", covariant" : ", contravariant")
183 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
184 value.print(c, level + delta_indent);
185 dim.print(c, level + delta_indent);
188 if (is_a<print_latex>(c))
196 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
202 if (is_a<print_latex>(c))
207 void spinidx::print(const print_context & c, unsigned level) const
209 if (is_of_type(c, print_tree)) {
211 c.s << std::string(level, ' ') << class_name()
212 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
213 << (covariant ? ", covariant" : ", contravariant")
214 << (dotted ? ", dotted" : ", undotted")
216 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
217 value.print(c, level + delta_indent);
218 dim.print(c, level + delta_indent);
222 bool is_tex = is_of_type(c, print_latex);
240 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
246 if (is_tex && dotted)
253 bool idx::info(unsigned inf) const
255 if (inf == info_flags::idx)
257 return inherited::info(inf);
260 unsigned idx::nops() const
262 // don't count the dimension as that is not really a sub-expression
266 ex & idx::let_op(int i)
268 GINAC_ASSERT(i == 0);
272 /** Returns order relation between two indices of the same type. The order
273 * must be such that dummy indices lie next to each other. */
274 int idx::compare_same_type(const basic & other) const
276 GINAC_ASSERT(is_a<idx>(other));
277 const idx &o = static_cast<const idx &>(other);
279 int cmpval = value.compare(o.value);
282 return dim.compare(o.dim);
285 bool idx::match_same_type(const basic & other) const
287 GINAC_ASSERT(is_a<idx>(other));
288 const idx &o = static_cast<const idx &>(other);
290 return dim.is_equal(o.dim);
293 int varidx::compare_same_type(const basic & other) const
295 GINAC_ASSERT(is_a<varidx>(other));
296 const varidx &o = static_cast<const varidx &>(other);
298 int cmpval = inherited::compare_same_type(other);
302 // Check variance last so dummy indices will end up next to each other
303 if (covariant != o.covariant)
304 return covariant ? -1 : 1;
308 bool varidx::match_same_type(const basic & other) const
310 GINAC_ASSERT(is_a<varidx>(other));
311 const varidx &o = static_cast<const varidx &>(other);
313 if (covariant != o.covariant)
315 return inherited::match_same_type(other);
318 int spinidx::compare_same_type(const basic & other) const
320 GINAC_ASSERT(is_a<spinidx>(other));
321 const spinidx &o = static_cast<const spinidx &>(other);
323 // Check dottedness first so dummy indices will end up next to each other
324 if (dotted != o.dotted)
325 return dotted ? -1 : 1;
327 int cmpval = inherited::compare_same_type(other);
334 bool spinidx::match_same_type(const basic & other) const
336 GINAC_ASSERT(is_a<spinidx>(other));
337 const spinidx &o = static_cast<const spinidx &>(other);
339 if (dotted != o.dotted)
341 return inherited::match_same_type(other);
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 lst & ls, const lst & lr, bool no_pattern) const
353 GINAC_ASSERT(ls.nops() == lr.nops());
355 // First look for index substitutions
356 for (unsigned i=0; i<ls.nops(); i++) {
357 if (is_equal(ex_to<basic>(ls.op(i)))) {
359 // Substitution index->index
360 if (is_ex_of_type(lr.op(i), idx))
363 // Otherwise substitute value
364 idx *i_copy = static_cast<idx *>(duplicate());
365 i_copy->value = lr.op(i);
366 i_copy->clearflag(status_flags::hash_calculated);
367 return i_copy->setflag(status_flags::dynallocated);
371 // None, substitute objects in value (not in dimension)
372 const ex &subsed_value = value.subs(ls, lr, no_pattern);
373 if (are_ex_trivially_equal(value, subsed_value))
376 idx *i_copy = static_cast<idx *>(duplicate());
377 i_copy->value = subsed_value;
378 i_copy->clearflag(status_flags::hash_calculated);
379 return i_copy->setflag(status_flags::dynallocated);
382 /** Implementation of ex::diff() for an index always returns 0.
385 ex idx::derivative(const symbol & s) const
391 // new virtual functions
394 bool idx::is_dummy_pair_same_type(const basic & other) const
396 const idx &o = static_cast<const idx &>(other);
398 // Only pure symbols form dummy pairs, numeric indices and expressions
400 if (!is_ex_of_type(value, symbol))
403 // Value must be equal, of course
404 if (!value.is_equal(o.value))
407 // Dimensions need not be equal but must be comparable (so we can
408 // determine the minimum dimension of contractions)
409 if (dim.is_equal(o.dim))
412 return (dim < o.dim || dim > o.dim || (is_a<numeric>(dim) && is_a<symbol>(o.dim)) || (is_a<symbol>(dim) && is_a<numeric>(o.dim)));
415 bool varidx::is_dummy_pair_same_type(const basic & other) const
417 const varidx &o = static_cast<const varidx &>(other);
419 // Variance must be opposite
420 if (covariant == o.covariant)
423 return inherited::is_dummy_pair_same_type(other);
426 bool spinidx::is_dummy_pair_same_type(const basic & other) const
428 const spinidx &o = static_cast<const spinidx &>(other);
430 // Dottedness must be the same
431 if (dotted != o.dotted)
434 return inherited::is_dummy_pair_same_type(other);
439 // non-virtual functions
442 ex idx::replace_dim(const ex & new_dim) const
444 idx *i_copy = static_cast<idx *>(duplicate());
445 i_copy->dim = new_dim;
446 i_copy->clearflag(status_flags::hash_calculated);
447 return i_copy->setflag(status_flags::dynallocated);
450 ex idx::minimal_dim(const idx & other) const
452 if (dim.is_equal(other.dim) || dim < other.dim || (is_a<numeric>(dim) && is_a<symbol>(other.dim)))
454 else if (dim > other.dim || (is_a<symbol>(dim) && is_a<numeric>(other.dim)))
457 throw (std::runtime_error("idx::minimal_dim: index dimensions cannot be ordered"));
460 ex varidx::toggle_variance(void) const
462 varidx *i_copy = static_cast<varidx *>(duplicate());
463 i_copy->covariant = !i_copy->covariant;
464 i_copy->clearflag(status_flags::hash_calculated);
465 return i_copy->setflag(status_flags::dynallocated);
468 ex spinidx::toggle_dot(void) const
470 spinidx *i_copy = static_cast<spinidx *>(duplicate());
471 i_copy->dotted = !i_copy->dotted;
472 i_copy->clearflag(status_flags::hash_calculated);
473 return i_copy->setflag(status_flags::dynallocated);
476 ex spinidx::toggle_variance_dot(void) const
478 spinidx *i_copy = static_cast<spinidx *>(duplicate());
479 i_copy->covariant = !i_copy->covariant;
480 i_copy->dotted = !i_copy->dotted;
481 i_copy->clearflag(status_flags::hash_calculated);
482 return i_copy->setflag(status_flags::dynallocated);
489 bool is_dummy_pair(const idx & i1, const idx & i2)
491 // The indices must be of exactly the same type
492 if (i1.tinfo() != i2.tinfo())
495 // Same type, let the indices decide whether they are paired
496 return i1.is_dummy_pair_same_type(i2);
499 bool is_dummy_pair(const ex & e1, const ex & e2)
501 // The expressions must be indices
502 if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
505 return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
508 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
513 // No indices? Then do nothing
517 // Only one index? Then it is a free one if it's not numeric
518 if (itend - it == 1) {
519 if (ex_to<idx>(*it).is_symbolic())
520 out_free.push_back(*it);
524 // Sort index vector. This will cause dummy indices come to lie next
525 // to each other (because the sort order is defined to guarantee this).
526 exvector v(it, itend);
527 shaker_sort(v.begin(), v.end(), ex_is_less(), ex_swap());
529 // Find dummy pairs and free indices
530 it = v.begin(); itend = v.end();
531 exvector::const_iterator last = it++;
532 while (it != itend) {
533 if (is_dummy_pair(*it, *last)) {
534 out_dummy.push_back(*last);
539 if (!it->is_equal(*last) && ex_to<idx>(*last).is_symbolic())
540 out_free.push_back(*last);
544 if (ex_to<idx>(*last).is_symbolic())
545 out_free.push_back(*last);