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(idx, basic)
39 GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx)
40 GINAC_IMPLEMENT_REGISTERED_CLASS(spinidx, varidx)
43 // default ctor, dtor, copy ctor, assignment operator and helpers
46 idx::idx() : inherited(TINFO_idx) {}
48 varidx::varidx() : covariant(false)
50 tinfo_key = TINFO_varidx;
53 spinidx::spinidx() : dotted(false)
55 tinfo_key = TINFO_spinidx;
58 void idx::copy(const idx & other)
60 inherited::copy(other);
65 void varidx::copy(const varidx & other)
67 inherited::copy(other);
68 covariant = other.covariant;
71 void spinidx::copy(const spinidx & other)
73 inherited::copy(other);
74 dotted = other.dotted;
78 DEFAULT_DESTROY(varidx)
79 DEFAULT_DESTROY(spinidx)
85 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
88 if (!dim.info(info_flags::posint))
89 throw(std::invalid_argument("dimension of space must be a positive integer"));
92 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
94 tinfo_key = TINFO_varidx;
97 spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot)
99 tinfo_key = TINFO_spinidx;
106 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
108 n.find_ex("value", value, sym_lst);
109 n.find_ex("dim", dim, sym_lst);
112 varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
114 n.find_bool("covariant", covariant);
117 spinidx::spinidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
119 n.find_bool("dotted", dotted);
122 void idx::archive(archive_node &n) const
124 inherited::archive(n);
125 n.add_ex("value", value);
126 n.add_ex("dim", dim);
129 void varidx::archive(archive_node &n) const
131 inherited::archive(n);
132 n.add_bool("covariant", covariant);
135 void spinidx::archive(archive_node &n) const
137 inherited::archive(n);
138 n.add_bool("dotted", dotted);
141 DEFAULT_UNARCHIVE(idx)
142 DEFAULT_UNARCHIVE(varidx)
143 DEFAULT_UNARCHIVE(spinidx)
146 // functions overriding virtual functions from base classes
149 void idx::print(const print_context & c, unsigned level) const
151 if (is_a<print_tree>(c)) {
153 c.s << std::string(level, ' ') << class_name()
154 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
156 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
157 value.print(c, level + delta_indent);
158 dim.print(c, level + delta_indent);
162 if (is_a<print_latex>(c))
166 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
172 if (c.options & print_options::print_index_dimensions) {
177 if (is_a<print_latex>(c))
182 void varidx::print(const print_context & c, unsigned level) const
184 if (is_a<print_tree>(c)) {
186 c.s << std::string(level, ' ') << class_name()
187 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
188 << (covariant ? ", covariant" : ", contravariant")
190 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
191 value.print(c, level + delta_indent);
192 dim.print(c, level + delta_indent);
195 if (is_a<print_latex>(c))
203 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
209 if (c.options & print_options::print_index_dimensions) {
214 if (is_a<print_latex>(c))
219 void spinidx::print(const print_context & c, unsigned level) const
221 if (is_a<print_tree>(c)) {
223 c.s << std::string(level, ' ') << class_name()
224 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
225 << (covariant ? ", covariant" : ", contravariant")
226 << (dotted ? ", dotted" : ", undotted")
228 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
229 value.print(c, level + delta_indent);
230 dim.print(c, level + delta_indent);
234 bool is_tex = is_a<print_latex>(c);
252 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
258 if (is_tex && dotted)
265 bool idx::info(unsigned inf) const
267 if (inf == info_flags::idx)
269 return inherited::info(inf);
272 unsigned idx::nops() const
274 // don't count the dimension as that is not really a sub-expression
278 ex & idx::let_op(int i)
280 GINAC_ASSERT(i == 0);
284 /** Returns order relation between two indices of the same type. The order
285 * must be such that dummy indices lie next to each other. */
286 int idx::compare_same_type(const basic & other) const
288 GINAC_ASSERT(is_a<idx>(other));
289 const idx &o = static_cast<const idx &>(other);
291 int cmpval = value.compare(o.value);
294 return dim.compare(o.dim);
297 bool idx::match_same_type(const basic & other) const
299 GINAC_ASSERT(is_a<idx>(other));
300 const idx &o = static_cast<const idx &>(other);
302 return dim.is_equal(o.dim);
305 int varidx::compare_same_type(const basic & other) const
307 GINAC_ASSERT(is_a<varidx>(other));
308 const varidx &o = static_cast<const varidx &>(other);
310 int cmpval = inherited::compare_same_type(other);
314 // Check variance last so dummy indices will end up next to each other
315 if (covariant != o.covariant)
316 return covariant ? -1 : 1;
321 bool varidx::match_same_type(const basic & other) const
323 GINAC_ASSERT(is_a<varidx>(other));
324 const varidx &o = static_cast<const varidx &>(other);
326 if (covariant != o.covariant)
329 return inherited::match_same_type(other);
332 int spinidx::compare_same_type(const basic & other) const
334 GINAC_ASSERT(is_a<spinidx>(other));
335 const spinidx &o = static_cast<const spinidx &>(other);
337 // Check dottedness first so dummy indices will end up next to each other
338 if (dotted != o.dotted)
339 return dotted ? -1 : 1;
341 int cmpval = inherited::compare_same_type(other);
348 bool spinidx::match_same_type(const basic & other) const
350 GINAC_ASSERT(is_a<spinidx>(other));
351 const spinidx &o = static_cast<const spinidx &>(other);
353 if (dotted != o.dotted)
355 return inherited::match_same_type(other);
358 /** By default, basic::evalf would evaluate the index value but we don't want
359 * a.1 to become a.(1.0). */
360 ex idx::evalf(int level) const
365 ex idx::subs(const lst & ls, const lst & lr, unsigned options) const
367 GINAC_ASSERT(ls.nops() == lr.nops());
369 // First look for index substitutions
370 for (unsigned i=0; i<ls.nops(); i++) {
371 if (is_equal(ex_to<basic>(ls.op(i)))) {
373 // Substitution index->index
374 if (is_a<idx>(lr.op(i)))
377 // Otherwise substitute value
378 idx *i_copy = static_cast<idx *>(duplicate());
379 i_copy->value = lr.op(i);
380 i_copy->clearflag(status_flags::hash_calculated);
381 return i_copy->setflag(status_flags::dynallocated);
385 // None, substitute objects in value (not in dimension)
386 const ex &subsed_value = value.subs(ls, lr, options);
387 if (are_ex_trivially_equal(value, subsed_value))
390 idx *i_copy = static_cast<idx *>(duplicate());
391 i_copy->value = subsed_value;
392 i_copy->clearflag(status_flags::hash_calculated);
393 return i_copy->setflag(status_flags::dynallocated);
396 /** Implementation of ex::diff() for an index always returns 0.
399 ex idx::derivative(const symbol & s) const
405 // new virtual functions
408 bool idx::is_dummy_pair_same_type(const basic & other) const
410 const idx &o = static_cast<const idx &>(other);
412 // Only pure symbols form dummy pairs, "2n+1" doesn't
413 if (!is_a<symbol>(value))
416 // Value must be equal, of course
417 if (!value.is_equal(o.value))
420 // Dimensions need not be equal but must be comparable (so we can
421 // determine the minimum dimension of contractions)
422 if (dim.is_equal(o.dim))
425 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)));
428 bool varidx::is_dummy_pair_same_type(const basic & other) const
430 const varidx &o = static_cast<const varidx &>(other);
432 // Variance must be opposite
433 if (covariant == o.covariant)
436 return inherited::is_dummy_pair_same_type(other);
439 bool spinidx::is_dummy_pair_same_type(const basic & other) const
441 const spinidx &o = static_cast<const spinidx &>(other);
443 // Dottedness must be the same
444 if (dotted != o.dotted)
447 return inherited::is_dummy_pair_same_type(other);
452 // non-virtual functions
455 ex idx::replace_dim(const ex & new_dim) const
457 idx *i_copy = static_cast<idx *>(duplicate());
458 i_copy->dim = new_dim;
459 i_copy->clearflag(status_flags::hash_calculated);
460 return i_copy->setflag(status_flags::dynallocated);
463 ex idx::minimal_dim(const idx & other) const
465 return GiNaC::minimal_dim(dim, other.dim);
468 ex varidx::toggle_variance(void) const
470 varidx *i_copy = static_cast<varidx *>(duplicate());
471 i_copy->covariant = !i_copy->covariant;
472 i_copy->clearflag(status_flags::hash_calculated);
473 return i_copy->setflag(status_flags::dynallocated);
476 ex spinidx::toggle_dot(void) const
478 spinidx *i_copy = static_cast<spinidx *>(duplicate());
479 i_copy->dotted = !i_copy->dotted;
480 i_copy->clearflag(status_flags::hash_calculated);
481 return i_copy->setflag(status_flags::dynallocated);
484 ex spinidx::toggle_variance_dot(void) const
486 spinidx *i_copy = static_cast<spinidx *>(duplicate());
487 i_copy->covariant = !i_copy->covariant;
488 i_copy->dotted = !i_copy->dotted;
489 i_copy->clearflag(status_flags::hash_calculated);
490 return i_copy->setflag(status_flags::dynallocated);
497 bool is_dummy_pair(const idx & i1, const idx & i2)
499 // The indices must be of exactly the same type
500 if (i1.tinfo() != i2.tinfo())
503 // Same type, let the indices decide whether they are paired
504 return i1.is_dummy_pair_same_type(i2);
507 bool is_dummy_pair(const ex & e1, const ex & e2)
509 // The expressions must be indices
510 if (!is_a<idx>(e1) || !is_a<idx>(e2))
513 return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
516 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
521 // No indices? Then do nothing
525 // Only one index? Then it is a free one if it's not numeric
526 if (itend - it == 1) {
527 if (ex_to<idx>(*it).is_symbolic())
528 out_free.push_back(*it);
532 // Sort index vector. This will cause dummy indices come to lie next
533 // to each other (because the sort order is defined to guarantee this).
534 exvector v(it, itend);
535 shaker_sort(v.begin(), v.end(), ex_is_less(), ex_swap());
537 // Find dummy pairs and free indices
538 it = v.begin(); itend = v.end();
539 exvector::const_iterator last = it++;
540 while (it != itend) {
541 if (is_dummy_pair(*it, *last)) {
542 out_dummy.push_back(*last);
547 if (!it->is_equal(*last) && ex_to<idx>(*last).is_symbolic())
548 out_free.push_back(*last);
552 if (ex_to<idx>(*last).is_symbolic())
553 out_free.push_back(*last);
556 ex minimal_dim(const ex & dim1, const ex & dim2)
558 if (dim1.is_equal(dim2) || dim1 < dim2 || (is_exactly_a<numeric>(dim1) && is_a<symbol>(dim2)))
560 else if (dim1 > dim2 || (is_a<symbol>(dim1) && is_exactly_a<numeric>(dim2)))
563 std::ostringstream s;
564 s << "minimal_dim(): index dimensions " << dim1 << " and " << dim2 << " cannot be ordered";
565 throw (std::runtime_error(s.str()));