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 int cmpval = inherited::compare_same_type(other);
312 // Check variance and dottedness last so dummy indices will end up next to each other
313 if (covariant != o.covariant)
314 return covariant ? -1 : 1;
315 if (dotted != o.dotted)
316 return dotted ? -1 : 1;
321 ex idx::subs(const lst & ls, const lst & lr) const
323 GINAC_ASSERT(ls.nops() == lr.nops());
325 // First look for index substitutions
326 for (unsigned i=0; i<ls.nops(); i++) {
327 if (is_equal(*(ls.op(i)).bp)) {
329 // Substitution index->index
330 if (is_ex_of_type(lr.op(i), idx))
333 // Otherwise substitute value
334 idx *i_copy = static_cast<idx *>(duplicate());
335 i_copy->value = lr.op(i);
336 i_copy->clearflag(status_flags::hash_calculated);
337 return i_copy->setflag(status_flags::dynallocated);
341 // None, substitute objects in value (not in dimension)
342 const ex &subsed_value = value.subs(ls, lr);
343 if (are_ex_trivially_equal(value, subsed_value))
346 idx *i_copy = static_cast<idx *>(duplicate());
347 i_copy->value = subsed_value;
348 i_copy->clearflag(status_flags::hash_calculated);
349 return i_copy->setflag(status_flags::dynallocated);
353 // new virtual functions
356 bool idx::is_dummy_pair_same_type(const basic & other) const
358 const idx &o = static_cast<const idx &>(other);
360 // Only pure symbols form dummy pairs, "2n+1" doesn't
361 if (!is_ex_of_type(value, symbol))
364 // Value must be equal, of course
365 if (!value.is_equal(o.value))
368 // Also the dimension
369 return dim.is_equal(o.dim);
372 bool varidx::is_dummy_pair_same_type(const basic & other) const
374 const varidx &o = static_cast<const varidx &>(other);
376 // Variance must be opposite
377 if (covariant == o.covariant)
380 return inherited::is_dummy_pair_same_type(other);
383 bool spinidx::is_dummy_pair_same_type(const basic & other) const
385 const spinidx &o = static_cast<const spinidx &>(other);
387 // Dottedness must be the same
388 if (dotted != o.dotted)
391 return inherited::is_dummy_pair_same_type(other);
396 // non-virtual functions
399 ex varidx::toggle_variance(void) const
401 varidx *i_copy = static_cast<varidx *>(duplicate());
402 i_copy->covariant = !i_copy->covariant;
403 i_copy->clearflag(status_flags::hash_calculated);
404 return i_copy->setflag(status_flags::dynallocated);
407 ex spinidx::toggle_dot(void) const
409 spinidx *i_copy = static_cast<spinidx *>(duplicate());
410 i_copy->dotted = !i_copy->dotted;
411 i_copy->clearflag(status_flags::hash_calculated);
412 return i_copy->setflag(status_flags::dynallocated);
415 ex spinidx::toggle_variance_dot(void) const
417 spinidx *i_copy = static_cast<spinidx *>(duplicate());
418 i_copy->covariant = !i_copy->covariant;
419 i_copy->dotted = !i_copy->dotted;
420 i_copy->clearflag(status_flags::hash_calculated);
421 return i_copy->setflag(status_flags::dynallocated);
428 bool is_dummy_pair(const idx & i1, const idx & i2)
430 // The indices must be of exactly the same type
431 if (i1.tinfo() != i2.tinfo())
434 // Same type, let the indices decide whether they are paired
435 return i1.is_dummy_pair_same_type(i2);
438 bool is_dummy_pair(const ex & e1, const ex & e2)
440 // The expressions must be indices
441 if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
444 return is_dummy_pair(ex_to_idx(e1), ex_to_idx(e2));
447 // Shaker sort is sufficient for the expected small number of indices
448 template <class It, class Cmp>
449 inline void shaker_sort(It first, It last, Cmp comp)
459 for (i=last; i>first; --i) {
460 if (comp(*i, i[-1])) {
467 for (i=first; i<last; ++i) {
468 if (comp(i[1], *i)) {
474 } while (first <= last);
477 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
482 // No indices? Then do nothing
486 // Only one index? Then it is a free one if it's not numeric
487 if (itend - it == 1) {
488 if (ex_to_idx(*it).is_symbolic())
489 out_free.push_back(*it);
493 // Sort index vector. This will cause dummy indices come to lie next
494 // to each other (because the sort order is defined to guarantee this).
495 exvector v(it, itend);
496 shaker_sort(v.begin(), v.end(), ex_is_less());
498 // Find dummy pairs and free indices
499 it = v.begin(); itend = v.end();
500 exvector::const_iterator last = it++;
501 while (it != itend) {
502 if (is_dummy_pair(*it, *last)) {
503 out_dummy.push_back(*last);
508 if (!it->is_equal(*last) && ex_to_idx(*last).is_symbolic())
509 out_free.push_back(*last);
513 if (ex_to_idx(*last).is_symbolic())
514 out_free.push_back(*last);