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
34 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
35 GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx)
38 // default constructor, destructor, copy constructor assignment operator and helpers
41 idx::idx() : inherited(TINFO_idx)
43 debugmsg("idx default constructor", LOGLEVEL_CONSTRUCT);
46 varidx::varidx() : covariant(false)
48 debugmsg("varidx default constructor", LOGLEVEL_CONSTRUCT);
49 tinfo_key = TINFO_varidx;
52 void idx::copy(const idx & other)
54 inherited::copy(other);
59 void varidx::copy(const varidx & other)
61 inherited::copy(other);
62 covariant = other.covariant;
66 DEFAULT_DESTROY(varidx)
72 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
74 debugmsg("idx constructor from ex,ex", LOGLEVEL_CONSTRUCT);
76 if (!dim.info(info_flags::posint))
77 throw(std::invalid_argument("dimension of space must be a positive integer"));
80 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
82 debugmsg("varidx constructor from ex,ex,bool", LOGLEVEL_CONSTRUCT);
83 tinfo_key = TINFO_varidx;
90 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
92 debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
93 n.find_ex("value", value, sym_lst);
94 n.find_ex("dim", dim, sym_lst);
97 varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
99 debugmsg("varidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
100 n.find_bool("covariant", covariant);
103 void idx::archive(archive_node &n) const
105 inherited::archive(n);
106 n.add_ex("value", value);
107 n.add_ex("dim", dim);
110 void varidx::archive(archive_node &n) const
112 inherited::archive(n);
113 n.add_bool("covariant", covariant);
116 DEFAULT_UNARCHIVE(idx)
117 DEFAULT_UNARCHIVE(varidx)
120 // functions overriding virtual functions from bases classes
123 void idx::printraw(std::ostream & os) const
125 debugmsg("idx printraw", LOGLEVEL_PRINT);
127 os << class_name() << "(";
131 os << ",hash=" << hashvalue << ",flags=" << flags;
135 void idx::printtree(std::ostream & os, unsigned indent) const
137 debugmsg("idx printtree",LOGLEVEL_PRINT);
139 os << std::string(indent, ' ') << "type=" << class_name();
140 value.printtree(os, indent + delta_indent);
141 os << std::string(indent, ' ');
142 os << ", hash=" << hashvalue
143 << " (0x" << std::hex << hashvalue << std::dec << ")"
144 << ", flags=" << flags << std::endl;
147 void idx::print(std::ostream & os, unsigned upper_precedence) const
149 debugmsg("idx print", LOGLEVEL_PRINT);
153 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
161 void varidx::print(std::ostream & os, unsigned upper_precedence) const
163 debugmsg("varidx print", LOGLEVEL_PRINT);
170 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 /** Returns order relation between two indices of the same type. The order
186 * must be such that dummy indices lie next to each other. */
187 int idx::compare_same_type(const basic & other) const
189 GINAC_ASSERT(is_of_type(other, idx));
190 const idx &o = static_cast<const idx &>(other);
192 int cmpval = value.compare(o.value);
195 return dim.compare(o.dim);
198 int varidx::compare_same_type(const basic & other) const
200 GINAC_ASSERT(is_of_type(other, varidx));
201 const varidx &o = static_cast<const varidx &>(other);
203 int cmpval = inherited::compare_same_type(other);
207 // Check variance last so dummy indices will end up next to each other
208 if (covariant != o.covariant)
209 return covariant ? -1 : 1;
213 ex idx::subs(const lst & ls, const lst & lr) const
215 GINAC_ASSERT(ls.nops() == lr.nops());
217 // First look for index substitutions
218 for (unsigned i=0; i<ls.nops(); i++) {
219 if (is_equal(*(ls.op(i)).bp)) {
221 // Substitution index->index
222 if (is_ex_of_type(lr.op(i), idx))
225 // Otherwise substitute value
226 idx *i_copy = static_cast<idx *>(duplicate());
227 i_copy->value = lr.op(i);
228 return i_copy->setflag(status_flags::dynallocated);
232 // None, substitute objects in value (not in dimension)
233 const ex &subsed_value = value.subs(ls, lr);
234 if (are_ex_trivially_equal(value, subsed_value))
237 idx *i_copy = static_cast<idx *>(duplicate());
238 i_copy->value = subsed_value;
239 return i_copy->setflag(status_flags::dynallocated);
243 // new virtual functions
246 bool idx::is_dummy_pair_same_type(const basic & other) const
248 const idx &o = static_cast<const idx &>(other);
250 // Only pure symbols form dummy pairs, "2n+1" doesn't
251 if (!is_ex_of_type(value, symbol))
254 // Value must be equal, of course
255 if (!value.is_equal(o.value))
258 // Also the dimension
259 return dim.is_equal(o.dim);
262 bool varidx::is_dummy_pair_same_type(const basic & other) const
264 const varidx &o = static_cast<const varidx &>(other);
266 // Variance must be opposite
267 if (covariant == o.covariant)
270 return inherited::is_dummy_pair_same_type(other);
274 // non-virtual functions
277 ex varidx::toggle_variance(void) const
279 varidx *i_copy = static_cast<varidx *>(duplicate());
280 i_copy->covariant = !i_copy->covariant;
281 i_copy->clearflag(status_flags::hash_calculated);
282 return i_copy->setflag(status_flags::dynallocated);
289 bool is_dummy_pair(const idx & i1, const idx & i2)
291 // The indices must be of exactly the same type
292 if (i1.tinfo() != i2.tinfo())
295 // Same type, let the indices decide whether they are paired
296 return i1.is_dummy_pair_same_type(i2);
299 bool is_dummy_pair(const ex & e1, const ex & e2)
301 // The expressions must be indices
302 if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
305 return is_dummy_pair(ex_to_idx(e1), ex_to_idx(e2));
308 /** Bring a vector of indices into a canonic order. Dummy indices will lie
309 * next to each other after the sorting. */
310 static void sort_index_vector(exvector &v)
312 // Nothing to sort if less than 2 elements
316 // Simple bubble sort algorithm should be sufficient for the small
317 // number of indices expected
318 exvector::iterator it1 = v.begin(), itend = v.end(), next_to_last_idx = itend - 1;
319 while (it1 != next_to_last_idx) {
320 exvector::iterator it2 = it1 + 1;
321 while (it2 != itend) {
322 if (it1->compare(*it2) > 0)
331 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
336 // No indices? Then do nothing
340 // Only one index? Then it is a free one if it's not numeric
341 if (itend - it == 1) {
342 if (ex_to_idx(*it).is_symbolic())
343 out_free.push_back(*it);
347 // Sort index vector. This will cause dummy indices come to lie next
348 // to each other (because the sort order is defined to guarantee this).
349 exvector v(it, itend);
350 sort_index_vector(v);
352 // Find dummy pairs and free indices
353 it = v.begin(); itend = v.end();
354 exvector::const_iterator last = it++;
355 while (it != itend) {
356 if (is_dummy_pair(*it, *last)) {
357 out_dummy.push_back(*last);
362 if (!it->is_equal(*last) && ex_to_idx(*last).is_symbolic())
363 out_free.push_back(*last);
367 if (ex_to_idx(*last).is_symbolic())
368 out_free.push_back(*last);
371 exvector index_set_difference(const exvector & set1, const exvector & set2)
375 exvector::const_iterator ait = set1.begin(), aitend = set1.end();
376 while (ait != aitend) {
377 exvector::const_iterator bit = set2.begin(), bitend = set2.end();
379 while (bit != bitend) {
380 if (ait->is_equal(*bit)) {