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
35 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
36 GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx)
39 // default constructor, destructor, copy constructor assignment operator and helpers
42 idx::idx() : inherited(TINFO_idx)
44 debugmsg("idx default constructor", LOGLEVEL_CONSTRUCT);
47 varidx::varidx() : covariant(false)
49 debugmsg("varidx default constructor", LOGLEVEL_CONSTRUCT);
50 tinfo_key = TINFO_varidx;
53 void idx::copy(const idx & other)
55 inherited::copy(other);
60 void varidx::copy(const varidx & other)
62 inherited::copy(other);
63 covariant = other.covariant;
67 DEFAULT_DESTROY(varidx)
73 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
75 debugmsg("idx constructor from ex,ex", LOGLEVEL_CONSTRUCT);
77 if (!dim.info(info_flags::posint))
78 throw(std::invalid_argument("dimension of space must be a positive integer"));
81 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
83 debugmsg("varidx constructor from ex,ex,bool", LOGLEVEL_CONSTRUCT);
84 tinfo_key = TINFO_varidx;
91 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
93 debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
94 n.find_ex("value", value, sym_lst);
95 n.find_ex("dim", dim, sym_lst);
98 varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
100 debugmsg("varidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
101 n.find_bool("covariant", covariant);
104 void idx::archive(archive_node &n) const
106 inherited::archive(n);
107 n.add_ex("value", value);
108 n.add_ex("dim", dim);
111 void varidx::archive(archive_node &n) const
113 inherited::archive(n);
114 n.add_bool("covariant", covariant);
117 DEFAULT_UNARCHIVE(idx)
118 DEFAULT_UNARCHIVE(varidx)
121 // functions overriding virtual functions from bases classes
124 void idx::print(const print_context & c, unsigned level) const
126 debugmsg("idx print", LOGLEVEL_PRINT);
128 if (is_of_type(c, print_tree)) {
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);
140 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
149 void varidx::print(const print_context & c, unsigned level) const
151 debugmsg("varidx print", LOGLEVEL_PRINT);
153 if (is_of_type(c, print_tree)) {
155 c.s << std::string(level, ' ') << class_name()
156 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
157 << (covariant ? ", covariant" : ", contravariant")
159 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
160 value.print(c, level + delta_indent);
161 dim.print(c, level + delta_indent);
169 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
178 bool idx::info(unsigned inf) const
180 if (inf == info_flags::idx)
182 return inherited::info(inf);
185 unsigned idx::nops() const
187 // don't count the dimension as that is not really a sub-expression
191 ex & idx::let_op(int i)
193 GINAC_ASSERT(i == 0);
197 /** Returns order relation between two indices of the same type. The order
198 * must be such that dummy indices lie next to each other. */
199 int idx::compare_same_type(const basic & other) const
201 GINAC_ASSERT(is_of_type(other, idx));
202 const idx &o = static_cast<const idx &>(other);
204 int cmpval = value.compare(o.value);
207 return dim.compare(o.dim);
210 int varidx::compare_same_type(const basic & other) const
212 GINAC_ASSERT(is_of_type(other, varidx));
213 const varidx &o = static_cast<const varidx &>(other);
215 int cmpval = inherited::compare_same_type(other);
219 // Check variance last so dummy indices will end up next to each other
220 if (covariant != o.covariant)
221 return covariant ? -1 : 1;
225 ex idx::subs(const lst & ls, const lst & lr) const
227 GINAC_ASSERT(ls.nops() == lr.nops());
229 // First look for index substitutions
230 for (unsigned i=0; i<ls.nops(); i++) {
231 if (is_equal(*(ls.op(i)).bp)) {
233 // Substitution index->index
234 if (is_ex_of_type(lr.op(i), idx))
237 // Otherwise substitute value
238 idx *i_copy = static_cast<idx *>(duplicate());
239 i_copy->value = lr.op(i);
240 i_copy->clearflag(status_flags::hash_calculated);
241 return i_copy->setflag(status_flags::dynallocated);
245 // None, substitute objects in value (not in dimension)
246 const ex &subsed_value = value.subs(ls, lr);
247 if (are_ex_trivially_equal(value, subsed_value))
250 idx *i_copy = static_cast<idx *>(duplicate());
251 i_copy->value = subsed_value;
252 i_copy->clearflag(status_flags::hash_calculated);
253 return i_copy->setflag(status_flags::dynallocated);
257 // new virtual functions
260 bool idx::is_dummy_pair_same_type(const basic & other) const
262 const idx &o = static_cast<const idx &>(other);
264 // Only pure symbols form dummy pairs, "2n+1" doesn't
265 if (!is_ex_of_type(value, symbol))
268 // Value must be equal, of course
269 if (!value.is_equal(o.value))
272 // Also the dimension
273 return dim.is_equal(o.dim);
276 bool varidx::is_dummy_pair_same_type(const basic & other) const
278 const varidx &o = static_cast<const varidx &>(other);
280 // Variance must be opposite
281 if (covariant == o.covariant)
284 return inherited::is_dummy_pair_same_type(other);
288 // non-virtual functions
291 ex varidx::toggle_variance(void) const
293 varidx *i_copy = static_cast<varidx *>(duplicate());
294 i_copy->covariant = !i_copy->covariant;
295 i_copy->clearflag(status_flags::hash_calculated);
296 return i_copy->setflag(status_flags::dynallocated);
303 bool is_dummy_pair(const idx & i1, const idx & i2)
305 // The indices must be of exactly the same type
306 if (i1.tinfo() != i2.tinfo())
309 // Same type, let the indices decide whether they are paired
310 return i1.is_dummy_pair_same_type(i2);
313 bool is_dummy_pair(const ex & e1, const ex & e2)
315 // The expressions must be indices
316 if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
319 return is_dummy_pair(ex_to_idx(e1), ex_to_idx(e2));
322 /** Bring a vector of indices into a canonic order. Dummy indices will lie
323 * next to each other after the sorting. */
324 static void sort_index_vector(exvector &v)
326 // Nothing to sort if less than 2 elements
330 // Simple bubble sort algorithm should be sufficient for the small
331 // number of indices expected
332 exvector::iterator it1 = v.begin(), itend = v.end(), next_to_last_idx = itend - 1;
333 while (it1 != next_to_last_idx) {
334 exvector::iterator it2 = it1 + 1;
335 while (it2 != itend) {
336 if (it1->compare(*it2) > 0)
345 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
350 // No indices? Then do nothing
354 // Only one index? Then it is a free one if it's not numeric
355 if (itend - it == 1) {
356 if (ex_to_idx(*it).is_symbolic())
357 out_free.push_back(*it);
361 // Sort index vector. This will cause dummy indices come to lie next
362 // to each other (because the sort order is defined to guarantee this).
363 exvector v(it, itend);
364 sort_index_vector(v);
366 // Find dummy pairs and free indices
367 it = v.begin(); itend = v.end();
368 exvector::const_iterator last = it++;
369 while (it != itend) {
370 if (is_dummy_pair(*it, *last)) {
371 out_dummy.push_back(*last);
376 if (!it->is_equal(*last) && ex_to_idx(*last).is_symbolic())
377 out_free.push_back(*last);
381 if (ex_to_idx(*last).is_symbolic())
382 out_free.push_back(*last);
385 exvector index_set_difference(const exvector & set1, const exvector & set2)
389 exvector::const_iterator ait = set1.begin(), aitend = set1.end();
390 while (ait != aitend) {
391 exvector::const_iterator bit = set2.begin(), bitend = set2.end();
393 while (bit != bitend) {
394 if (ait->is_equal(*bit)) {