3 * Implementation of GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2001 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
36 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
37 GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx)
38 GINAC_IMPLEMENT_REGISTERED_CLASS(spinidx, varidx)
41 // default constructor, destructor, copy constructor assignment operator and helpers
44 idx::idx() : inherited(TINFO_idx)
46 debugmsg("idx default constructor", LOGLEVEL_CONSTRUCT);
49 varidx::varidx() : covariant(false)
51 debugmsg("varidx default constructor", LOGLEVEL_CONSTRUCT);
52 tinfo_key = TINFO_varidx;
55 spinidx::spinidx() : dotted(false)
57 debugmsg("spinidx default constructor", LOGLEVEL_CONSTRUCT);
58 tinfo_key = TINFO_spinidx;
61 void idx::copy(const idx & other)
63 inherited::copy(other);
68 void varidx::copy(const varidx & other)
70 inherited::copy(other);
71 covariant = other.covariant;
74 void spinidx::copy(const spinidx & other)
76 inherited::copy(other);
77 dotted = other.dotted;
81 DEFAULT_DESTROY(varidx)
82 DEFAULT_DESTROY(spinidx)
88 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
90 debugmsg("idx constructor from ex,ex", LOGLEVEL_CONSTRUCT);
92 if (!dim.info(info_flags::posint))
93 throw(std::invalid_argument("dimension of space must be a positive integer"));
96 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
98 debugmsg("varidx constructor from ex,ex,bool", LOGLEVEL_CONSTRUCT);
99 tinfo_key = TINFO_varidx;
102 spinidx::spinidx(const ex & v, const ex & d, bool cov, bool dot) : inherited(v, d, cov), dotted(dot)
104 debugmsg("spinidx constructor from ex,ex,bool,bool", LOGLEVEL_CONSTRUCT);
105 tinfo_key = TINFO_spinidx;
112 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
114 debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
115 n.find_ex("value", value, sym_lst);
116 n.find_ex("dim", dim, sym_lst);
119 varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
121 debugmsg("varidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
122 n.find_bool("covariant", covariant);
125 spinidx::spinidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
127 debugmsg("spinidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
128 n.find_bool("dotted", dotted);
131 void idx::archive(archive_node &n) const
133 inherited::archive(n);
134 n.add_ex("value", value);
135 n.add_ex("dim", dim);
138 void varidx::archive(archive_node &n) const
140 inherited::archive(n);
141 n.add_bool("covariant", covariant);
144 void spinidx::archive(archive_node &n) const
146 inherited::archive(n);
147 n.add_bool("dotted", dotted);
150 DEFAULT_UNARCHIVE(idx)
151 DEFAULT_UNARCHIVE(varidx)
152 DEFAULT_UNARCHIVE(spinidx)
155 // functions overriding virtual functions from bases classes
158 void idx::print(const print_context & c, unsigned level) const
160 debugmsg("idx print", LOGLEVEL_PRINT);
162 if (is_of_type(c, print_tree)) {
164 c.s << std::string(level, ' ') << class_name()
165 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
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);
173 if (!is_of_type(c, print_latex))
175 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
184 void varidx::print(const print_context & c, unsigned level) const
186 debugmsg("varidx print", LOGLEVEL_PRINT);
188 if (is_of_type(c, print_tree)) {
190 c.s << std::string(level, ' ') << class_name()
191 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
192 << (covariant ? ", covariant" : ", contravariant")
194 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
195 value.print(c, level + delta_indent);
196 dim.print(c, level + delta_indent);
200 if (!is_of_type(c, print_latex)) {
206 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
215 void spinidx::print(const print_context & c, unsigned level) const
217 debugmsg("spinidx print", LOGLEVEL_PRINT);
219 if (is_of_type(c, print_tree)) {
221 c.s << std::string(level, ' ') << class_name()
222 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
223 << (covariant ? ", covariant" : ", contravariant")
224 << (dotted ? ", dotted" : ", undotted")
226 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
227 value.print(c, level + delta_indent);
228 dim.print(c, level + delta_indent);
232 bool is_tex = is_of_type(c, print_latex);
245 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
251 if (is_tex && dotted)
256 bool idx::info(unsigned inf) const
258 if (inf == info_flags::idx)
260 return inherited::info(inf);
263 unsigned idx::nops() const
265 // don't count the dimension as that is not really a sub-expression
269 ex & idx::let_op(int i)
271 GINAC_ASSERT(i == 0);
275 /** Returns order relation between two indices of the same type. The order
276 * must be such that dummy indices lie next to each other. */
277 int idx::compare_same_type(const basic & other) const
279 GINAC_ASSERT(is_of_type(other, idx));
280 const idx &o = static_cast<const idx &>(other);
282 int cmpval = value.compare(o.value);
285 return dim.compare(o.dim);
288 int varidx::compare_same_type(const basic & other) const
290 GINAC_ASSERT(is_of_type(other, varidx));
291 const varidx &o = static_cast<const varidx &>(other);
293 int cmpval = inherited::compare_same_type(other);
297 // Check variance last so dummy indices will end up next to each other
298 if (covariant != o.covariant)
299 return covariant ? -1 : 1;
303 int spinidx::compare_same_type(const basic & other) const
305 GINAC_ASSERT(is_of_type(other, spinidx));
306 const spinidx &o = static_cast<const spinidx &>(other);
308 // Check dottedness first so dummy indices will end up next to each other
309 if (dotted != o.dotted)
310 return dotted ? -1 : 1;
312 int cmpval = inherited::compare_same_type(other);
319 bool idx::match(const ex & pattern, lst & repl_lst) const
321 if (!is_ex_of_type(pattern, idx))
323 const idx &o = ex_to_idx(pattern);
324 if (!dim.is_equal(o.dim))
326 return value.match(o.value, repl_lst);
329 bool varidx::match(const ex & pattern, lst & repl_lst) const
331 if (!is_ex_of_type(pattern, varidx))
333 const varidx &o = ex_to_varidx(pattern);
334 if (covariant != o.covariant)
336 return inherited::match(pattern, repl_lst);
339 bool spinidx::match(const ex & pattern, lst & repl_lst) const
341 if (!is_ex_of_type(pattern, spinidx))
343 const spinidx &o = ex_to_spinidx(pattern);
344 if (dotted != o.dotted)
346 return inherited::match(pattern, repl_lst);
349 ex idx::subs(const lst & ls, const lst & lr, bool no_pattern) const
351 GINAC_ASSERT(ls.nops() == lr.nops());
353 // First look for index substitutions
354 for (unsigned i=0; i<ls.nops(); i++) {
355 if (is_equal(*(ls.op(i)).bp)) {
357 // Substitution index->index
358 if (is_ex_of_type(lr.op(i), idx))
361 // Otherwise substitute value
362 idx *i_copy = static_cast<idx *>(duplicate());
363 i_copy->value = lr.op(i);
364 i_copy->clearflag(status_flags::hash_calculated);
365 return i_copy->setflag(status_flags::dynallocated);
369 // None, substitute objects in value (not in dimension)
370 const ex &subsed_value = value.subs(ls, lr, no_pattern);
371 if (are_ex_trivially_equal(value, subsed_value))
374 idx *i_copy = static_cast<idx *>(duplicate());
375 i_copy->value = subsed_value;
376 i_copy->clearflag(status_flags::hash_calculated);
377 return i_copy->setflag(status_flags::dynallocated);
381 // new virtual functions
384 bool idx::is_dummy_pair_same_type(const basic & other) const
386 const idx &o = static_cast<const idx &>(other);
388 // Only pure symbols form dummy pairs, "2n+1" doesn't
389 if (!is_ex_of_type(value, symbol))
392 // Value must be equal, of course
393 if (!value.is_equal(o.value))
396 // Also the dimension
397 return dim.is_equal(o.dim);
400 bool varidx::is_dummy_pair_same_type(const basic & other) const
402 const varidx &o = static_cast<const varidx &>(other);
404 // Variance must be opposite
405 if (covariant == o.covariant)
408 return inherited::is_dummy_pair_same_type(other);
411 bool spinidx::is_dummy_pair_same_type(const basic & other) const
413 const spinidx &o = static_cast<const spinidx &>(other);
415 // Dottedness must be the same
416 if (dotted != o.dotted)
419 return inherited::is_dummy_pair_same_type(other);
424 // non-virtual functions
427 ex varidx::toggle_variance(void) const
429 varidx *i_copy = static_cast<varidx *>(duplicate());
430 i_copy->covariant = !i_copy->covariant;
431 i_copy->clearflag(status_flags::hash_calculated);
432 return i_copy->setflag(status_flags::dynallocated);
435 ex spinidx::toggle_dot(void) const
437 spinidx *i_copy = static_cast<spinidx *>(duplicate());
438 i_copy->dotted = !i_copy->dotted;
439 i_copy->clearflag(status_flags::hash_calculated);
440 return i_copy->setflag(status_flags::dynallocated);
443 ex spinidx::toggle_variance_dot(void) const
445 spinidx *i_copy = static_cast<spinidx *>(duplicate());
446 i_copy->covariant = !i_copy->covariant;
447 i_copy->dotted = !i_copy->dotted;
448 i_copy->clearflag(status_flags::hash_calculated);
449 return i_copy->setflag(status_flags::dynallocated);
456 bool is_dummy_pair(const idx & i1, const idx & i2)
458 // The indices must be of exactly the same type
459 if (i1.tinfo() != i2.tinfo())
462 // Same type, let the indices decide whether they are paired
463 return i1.is_dummy_pair_same_type(i2);
466 bool is_dummy_pair(const ex & e1, const ex & e2)
468 // The expressions must be indices
469 if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
472 return is_dummy_pair(ex_to_idx(e1), ex_to_idx(e2));
475 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
480 // No indices? Then do nothing
484 // Only one index? Then it is a free one if it's not numeric
485 if (itend - it == 1) {
486 if (ex_to_idx(*it).is_symbolic())
487 out_free.push_back(*it);
491 // Sort index vector. This will cause dummy indices come to lie next
492 // to each other (because the sort order is defined to guarantee this).
493 exvector v(it, itend);
494 shaker_sort(v.begin(), v.end(), ex_is_less());
496 // Find dummy pairs and free indices
497 it = v.begin(); itend = v.end();
498 exvector::const_iterator last = it++;
499 while (it != itend) {
500 if (is_dummy_pair(*it, *last)) {
501 out_dummy.push_back(*last);
506 if (!it->is_equal(*last) && ex_to_idx(*last).is_symbolic())
507 out_free.push_back(*last);
511 if (ex_to_idx(*last).is_symbolic())
512 out_free.push_back(*last);