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 constructor
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;
62 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
65 if (!dim.info(info_flags::posint))
66 throw(std::invalid_argument("dimension of space must be a positive integer"));
69 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
71 tinfo_key = TINFO_varidx;
74 spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot)
76 tinfo_key = TINFO_spinidx;
83 idx::idx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
85 n.find_ex("value", value, sym_lst);
86 n.find_ex("dim", dim, sym_lst);
89 varidx::varidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
91 n.find_bool("covariant", covariant);
94 spinidx::spinidx(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
96 n.find_bool("dotted", dotted);
99 void idx::archive(archive_node &n) const
101 inherited::archive(n);
102 n.add_ex("value", value);
103 n.add_ex("dim", dim);
106 void varidx::archive(archive_node &n) const
108 inherited::archive(n);
109 n.add_bool("covariant", covariant);
112 void spinidx::archive(archive_node &n) const
114 inherited::archive(n);
115 n.add_bool("dotted", dotted);
118 DEFAULT_UNARCHIVE(idx)
119 DEFAULT_UNARCHIVE(varidx)
120 DEFAULT_UNARCHIVE(spinidx)
123 // functions overriding virtual functions from base classes
126 void idx::print(const print_context & c, unsigned level) const
128 if (is_a<print_tree>(c)) {
130 c.s << std::string(level, ' ') << class_name()
131 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
133 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
134 value.print(c, level + delta_indent);
135 dim.print(c, level + delta_indent);
139 if (is_a<print_latex>(c))
143 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
149 if (c.options & print_options::print_index_dimensions) {
154 if (is_a<print_latex>(c))
159 void varidx::print(const print_context & c, unsigned level) const
161 if (is_a<print_tree>(c)) {
163 c.s << std::string(level, ' ') << class_name()
164 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
165 << (covariant ? ", covariant" : ", contravariant")
167 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
168 value.print(c, level + delta_indent);
169 dim.print(c, level + delta_indent);
172 if (is_a<print_latex>(c))
180 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
186 if (c.options & print_options::print_index_dimensions) {
191 if (is_a<print_latex>(c))
196 void spinidx::print(const print_context & c, unsigned level) const
198 if (is_a<print_tree>(c)) {
200 c.s << std::string(level, ' ') << class_name()
201 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
202 << (covariant ? ", covariant" : ", contravariant")
203 << (dotted ? ", dotted" : ", undotted")
205 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
206 value.print(c, level + delta_indent);
207 dim.print(c, level + delta_indent);
211 bool is_tex = is_a<print_latex>(c);
229 bool need_parens = !(is_exactly_a<numeric>(value) || is_a<symbol>(value));
235 if (is_tex && dotted)
242 bool idx::info(unsigned inf) const
244 if (inf == info_flags::idx)
246 return inherited::info(inf);
249 size_t idx::nops() const
251 // don't count the dimension as that is not really a sub-expression
255 ex idx::op(size_t i) const
257 GINAC_ASSERT(i == 0);
261 ex idx::map(map_function & f) const
263 idx *copy = duplicate();
264 copy->setflag(status_flags::dynallocated);
265 copy->clearflag(status_flags::hash_calculated);
266 copy->value = f(value);
270 /** Returns order relation between two indices of the same type. The order
271 * must be such that dummy indices lie next to each other. */
272 int idx::compare_same_type(const basic & other) const
274 GINAC_ASSERT(is_a<idx>(other));
275 const idx &o = static_cast<const idx &>(other);
277 int cmpval = value.compare(o.value);
280 return dim.compare(o.dim);
283 bool idx::match_same_type(const basic & other) const
285 GINAC_ASSERT(is_a<idx>(other));
286 const idx &o = static_cast<const idx &>(other);
288 return dim.is_equal(o.dim);
291 int varidx::compare_same_type(const basic & other) const
293 GINAC_ASSERT(is_a<varidx>(other));
294 const varidx &o = static_cast<const varidx &>(other);
296 int cmpval = inherited::compare_same_type(other);
300 // Check variance last so dummy indices will end up next to each other
301 if (covariant != o.covariant)
302 return covariant ? -1 : 1;
307 bool varidx::match_same_type(const basic & other) const
309 GINAC_ASSERT(is_a<varidx>(other));
310 const varidx &o = static_cast<const varidx &>(other);
312 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, unsigned options) const
353 GINAC_ASSERT(ls.nops() == lr.nops());
355 // First look for index substitutions
356 lst::const_iterator its, itr;
357 for (its = ls.begin(), itr = lr.begin(); its != ls.end(); ++its, ++itr) {
358 if (is_equal(ex_to<basic>(*its))) {
360 // Substitution index->index
364 // Otherwise substitute value
365 idx *i_copy = duplicate();
366 i_copy->value = *itr;
367 i_copy->clearflag(status_flags::hash_calculated);
368 return i_copy->setflag(status_flags::dynallocated);
372 // None, substitute objects in value (not in dimension)
373 const ex &subsed_value = value.subs(ls, lr, options);
374 if (are_ex_trivially_equal(value, subsed_value))
377 idx *i_copy = duplicate();
378 i_copy->value = subsed_value;
379 i_copy->clearflag(status_flags::hash_calculated);
380 return i_copy->setflag(status_flags::dynallocated);
383 /** Implementation of ex::diff() for an index always returns 0.
386 ex idx::derivative(const symbol & s) const
392 // new virtual functions
395 bool idx::is_dummy_pair_same_type(const basic & other) const
397 const idx &o = static_cast<const idx &>(other);
399 // Only pure symbols form dummy pairs, "2n+1" doesn't
400 if (!is_a<symbol>(value))
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_exactly_a<numeric>(dim) && is_a<symbol>(o.dim)) || (is_a<symbol>(dim) && is_exactly_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 = 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 return GiNaC::minimal_dim(dim, other.dim);
455 ex varidx::toggle_variance() const
457 varidx *i_copy = duplicate();
458 i_copy->covariant = !i_copy->covariant;
459 i_copy->clearflag(status_flags::hash_calculated);
460 return i_copy->setflag(status_flags::dynallocated);
463 ex spinidx::toggle_dot() const
465 spinidx *i_copy = duplicate();
466 i_copy->dotted = !i_copy->dotted;
467 i_copy->clearflag(status_flags::hash_calculated);
468 return i_copy->setflag(status_flags::dynallocated);
471 ex spinidx::toggle_variance_dot() const
473 spinidx *i_copy = duplicate();
474 i_copy->covariant = !i_copy->covariant;
475 i_copy->dotted = !i_copy->dotted;
476 i_copy->clearflag(status_flags::hash_calculated);
477 return i_copy->setflag(status_flags::dynallocated);
484 bool is_dummy_pair(const idx & i1, const idx & i2)
486 // The indices must be of exactly the same type
487 if (i1.tinfo() != i2.tinfo())
490 // Same type, let the indices decide whether they are paired
491 return i1.is_dummy_pair_same_type(i2);
494 bool is_dummy_pair(const ex & e1, const ex & e2)
496 // The expressions must be indices
497 if (!is_a<idx>(e1) || !is_a<idx>(e2))
500 return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
503 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
508 // No indices? Then do nothing
512 // Only one index? Then it is a free one if it's not numeric
513 if (itend - it == 1) {
514 if (ex_to<idx>(*it).is_symbolic())
515 out_free.push_back(*it);
519 // Sort index vector. This will cause dummy indices come to lie next
520 // to each other (because the sort order is defined to guarantee this).
521 exvector v(it, itend);
522 shaker_sort(v.begin(), v.end(), ex_is_less(), ex_swap());
524 // Find dummy pairs and free indices
525 it = v.begin(); itend = v.end();
526 exvector::const_iterator last = it++;
527 while (it != itend) {
528 if (is_dummy_pair(*it, *last)) {
529 out_dummy.push_back(*last);
534 if (!it->is_equal(*last) && ex_to<idx>(*last).is_symbolic())
535 out_free.push_back(*last);
539 if (ex_to<idx>(*last).is_symbolic())
540 out_free.push_back(*last);
543 ex minimal_dim(const ex & dim1, const ex & dim2)
545 if (dim1.is_equal(dim2) || dim1 < dim2 || (is_exactly_a<numeric>(dim1) && is_a<symbol>(dim2)))
547 else if (dim1 > dim2 || (is_a<symbol>(dim1) && is_exactly_a<numeric>(dim2)))
550 std::ostringstream s;
551 s << "minimal_dim(): index dimensions " << dim1 << " and " << dim2 << " cannot be ordered";
552 throw (std::runtime_error(s.str()));