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 base 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 bool idx::match_same_type(const basic & other) const
290 GINAC_ASSERT(is_of_type(other, idx));
291 const idx &o = static_cast<const idx &>(other);
293 return dim.is_equal(o.dim);
296 int varidx::compare_same_type(const basic & other) const
298 GINAC_ASSERT(is_of_type(other, varidx));
299 const varidx &o = static_cast<const varidx &>(other);
301 int cmpval = inherited::compare_same_type(other);
305 // Check variance last so dummy indices will end up next to each other
306 if (covariant != o.covariant)
307 return covariant ? -1 : 1;
311 bool varidx::match_same_type(const basic & other) const
313 GINAC_ASSERT(is_of_type(other, varidx));
314 const varidx &o = static_cast<const varidx &>(other);
316 if (covariant != o.covariant)
318 return inherited::match_same_type(other);
321 int spinidx::compare_same_type(const basic & other) const
323 GINAC_ASSERT(is_of_type(other, spinidx));
324 const spinidx &o = static_cast<const spinidx &>(other);
326 // Check dottedness first so dummy indices will end up next to each other
327 if (dotted != o.dotted)
328 return dotted ? -1 : 1;
330 int cmpval = inherited::compare_same_type(other);
337 bool spinidx::match_same_type(const basic & other) const
339 GINAC_ASSERT(is_of_type(other, spinidx));
340 const spinidx &o = static_cast<const spinidx &>(other);
342 if (dotted != o.dotted)
344 return inherited::match_same_type(other);
347 /** By default, basic::evalf would evaluate the index value but we don't want
348 * a.1 to become a.(1.0). */
349 ex idx::evalf(int level) const
354 ex idx::subs(const lst & ls, const lst & lr, bool no_pattern) const
356 GINAC_ASSERT(ls.nops() == lr.nops());
358 // First look for index substitutions
359 for (unsigned i=0; i<ls.nops(); i++) {
360 if (is_equal(*(ls.op(i)).bp)) {
362 // Substitution index->index
363 if (is_ex_of_type(lr.op(i), idx))
366 // Otherwise substitute value
367 idx *i_copy = static_cast<idx *>(duplicate());
368 i_copy->value = lr.op(i);
369 i_copy->clearflag(status_flags::hash_calculated);
370 return i_copy->setflag(status_flags::dynallocated);
374 // None, substitute objects in value (not in dimension)
375 const ex &subsed_value = value.subs(ls, lr, no_pattern);
376 if (are_ex_trivially_equal(value, subsed_value))
379 idx *i_copy = static_cast<idx *>(duplicate());
380 i_copy->value = subsed_value;
381 i_copy->clearflag(status_flags::hash_calculated);
382 return i_copy->setflag(status_flags::dynallocated);
385 /** Implementation of ex::diff() for an index always returns 0.
388 ex idx::derivative(const symbol & s) const
394 // new virtual functions
397 bool idx::is_dummy_pair_same_type(const basic & other) const
399 const idx &o = static_cast<const idx &>(other);
401 // Only pure symbols form dummy pairs, "2n+1" doesn't
402 if (!is_ex_of_type(value, symbol))
405 // Value must be equal, of course
406 if (!value.is_equal(o.value))
409 // Also the dimension
410 return dim.is_equal(o.dim);
413 bool varidx::is_dummy_pair_same_type(const basic & other) const
415 const varidx &o = static_cast<const varidx &>(other);
417 // Variance must be opposite
418 if (covariant == o.covariant)
421 return inherited::is_dummy_pair_same_type(other);
424 bool spinidx::is_dummy_pair_same_type(const basic & other) const
426 const spinidx &o = static_cast<const spinidx &>(other);
428 // Dottedness must be the same
429 if (dotted != o.dotted)
432 return inherited::is_dummy_pair_same_type(other);
437 // non-virtual functions
440 ex varidx::toggle_variance(void) const
442 varidx *i_copy = static_cast<varidx *>(duplicate());
443 i_copy->covariant = !i_copy->covariant;
444 i_copy->clearflag(status_flags::hash_calculated);
445 return i_copy->setflag(status_flags::dynallocated);
448 ex spinidx::toggle_dot(void) const
450 spinidx *i_copy = static_cast<spinidx *>(duplicate());
451 i_copy->dotted = !i_copy->dotted;
452 i_copy->clearflag(status_flags::hash_calculated);
453 return i_copy->setflag(status_flags::dynallocated);
456 ex spinidx::toggle_variance_dot(void) const
458 spinidx *i_copy = static_cast<spinidx *>(duplicate());
459 i_copy->covariant = !i_copy->covariant;
460 i_copy->dotted = !i_copy->dotted;
461 i_copy->clearflag(status_flags::hash_calculated);
462 return i_copy->setflag(status_flags::dynallocated);
469 bool is_dummy_pair(const idx & i1, const idx & i2)
471 // The indices must be of exactly the same type
472 if (i1.tinfo() != i2.tinfo())
475 // Same type, let the indices decide whether they are paired
476 return i1.is_dummy_pair_same_type(i2);
479 bool is_dummy_pair(const ex & e1, const ex & e2)
481 // The expressions must be indices
482 if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
485 return is_dummy_pair(ex_to<idx>(e1), ex_to<idx>(e2));
488 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
493 // No indices? Then do nothing
497 // Only one index? Then it is a free one if it's not numeric
498 if (itend - it == 1) {
499 if (ex_to<idx>(*it).is_symbolic())
500 out_free.push_back(*it);
504 // Sort index vector. This will cause dummy indices come to lie next
505 // to each other (because the sort order is defined to guarantee this).
506 exvector v(it, itend);
507 shaker_sort(v.begin(), v.end(), ex_is_less(), ex_swap());
509 // Find dummy pairs and free indices
510 it = v.begin(); itend = v.end();
511 exvector::const_iterator last = it++;
512 while (it != itend) {
513 if (is_dummy_pair(*it, *last)) {
514 out_dummy.push_back(*last);
519 if (!it->is_equal(*last) && ex_to<idx>(*last).is_symbolic())
520 out_free.push_back(*last);
524 if (ex_to<idx>(*last).is_symbolic())
525 out_free.push_back(*last);