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);
139 if (!is_of_type(c, print_latex))
141 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
150 void varidx::print(const print_context & c, unsigned level) const
152 debugmsg("varidx print", LOGLEVEL_PRINT);
154 if (is_of_type(c, print_tree)) {
156 c.s << std::string(level, ' ') << class_name()
157 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
158 << (covariant ? ", covariant" : ", contravariant")
160 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
161 value.print(c, level + delta_indent);
162 dim.print(c, level + delta_indent);
166 if (!is_of_type(c, print_latex)) {
172 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
181 bool idx::info(unsigned inf) const
183 if (inf == info_flags::idx)
185 return inherited::info(inf);
188 unsigned idx::nops() const
190 // don't count the dimension as that is not really a sub-expression
194 ex & idx::let_op(int i)
196 GINAC_ASSERT(i == 0);
200 /** Returns order relation between two indices of the same type. The order
201 * must be such that dummy indices lie next to each other. */
202 int idx::compare_same_type(const basic & other) const
204 GINAC_ASSERT(is_of_type(other, idx));
205 const idx &o = static_cast<const idx &>(other);
207 int cmpval = value.compare(o.value);
210 return dim.compare(o.dim);
213 int varidx::compare_same_type(const basic & other) const
215 GINAC_ASSERT(is_of_type(other, varidx));
216 const varidx &o = static_cast<const varidx &>(other);
218 int cmpval = inherited::compare_same_type(other);
222 // Check variance last so dummy indices will end up next to each other
223 if (covariant != o.covariant)
224 return covariant ? -1 : 1;
228 ex idx::subs(const lst & ls, const lst & lr) const
230 GINAC_ASSERT(ls.nops() == lr.nops());
232 // First look for index substitutions
233 for (unsigned i=0; i<ls.nops(); i++) {
234 if (is_equal(*(ls.op(i)).bp)) {
236 // Substitution index->index
237 if (is_ex_of_type(lr.op(i), idx))
240 // Otherwise substitute value
241 idx *i_copy = static_cast<idx *>(duplicate());
242 i_copy->value = lr.op(i);
243 i_copy->clearflag(status_flags::hash_calculated);
244 return i_copy->setflag(status_flags::dynallocated);
248 // None, substitute objects in value (not in dimension)
249 const ex &subsed_value = value.subs(ls, lr);
250 if (are_ex_trivially_equal(value, subsed_value))
253 idx *i_copy = static_cast<idx *>(duplicate());
254 i_copy->value = subsed_value;
255 i_copy->clearflag(status_flags::hash_calculated);
256 return i_copy->setflag(status_flags::dynallocated);
260 // new virtual functions
263 bool idx::is_dummy_pair_same_type(const basic & other) const
265 const idx &o = static_cast<const idx &>(other);
267 // Only pure symbols form dummy pairs, "2n+1" doesn't
268 if (!is_ex_of_type(value, symbol))
271 // Value must be equal, of course
272 if (!value.is_equal(o.value))
275 // Also the dimension
276 return dim.is_equal(o.dim);
279 bool varidx::is_dummy_pair_same_type(const basic & other) const
281 const varidx &o = static_cast<const varidx &>(other);
283 // Variance must be opposite
284 if (covariant == o.covariant)
287 return inherited::is_dummy_pair_same_type(other);
291 // non-virtual functions
294 ex varidx::toggle_variance(void) const
296 varidx *i_copy = static_cast<varidx *>(duplicate());
297 i_copy->covariant = !i_copy->covariant;
298 i_copy->clearflag(status_flags::hash_calculated);
299 return i_copy->setflag(status_flags::dynallocated);
306 bool is_dummy_pair(const idx & i1, const idx & i2)
308 // The indices must be of exactly the same type
309 if (i1.tinfo() != i2.tinfo())
312 // Same type, let the indices decide whether they are paired
313 return i1.is_dummy_pair_same_type(i2);
316 bool is_dummy_pair(const ex & e1, const ex & e2)
318 // The expressions must be indices
319 if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
322 return is_dummy_pair(ex_to_idx(e1), ex_to_idx(e2));
325 /** Bring a vector of indices into a canonic order. Dummy indices will lie
326 * next to each other after the sorting. */
327 static void sort_index_vector(exvector &v)
329 // Nothing to sort if less than 2 elements
333 // Simple bubble sort algorithm should be sufficient for the small
334 // number of indices expected
335 exvector::iterator it1 = v.begin(), itend = v.end(), next_to_last_idx = itend - 1;
336 while (it1 != next_to_last_idx) {
337 exvector::iterator it2 = it1 + 1;
338 while (it2 != itend) {
339 if (it1->compare(*it2) > 0)
348 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
353 // No indices? Then do nothing
357 // Only one index? Then it is a free one if it's not numeric
358 if (itend - it == 1) {
359 if (ex_to_idx(*it).is_symbolic())
360 out_free.push_back(*it);
364 // Sort index vector. This will cause dummy indices come to lie next
365 // to each other (because the sort order is defined to guarantee this).
366 exvector v(it, itend);
367 sort_index_vector(v);
369 // Find dummy pairs and free indices
370 it = v.begin(); itend = v.end();
371 exvector::const_iterator last = it++;
372 while (it != itend) {
373 if (is_dummy_pair(*it, *last)) {
374 out_dummy.push_back(*last);
379 if (!it->is_equal(*last) && ex_to_idx(*last).is_symbolic())
380 out_free.push_back(*last);
384 if (ex_to_idx(*last).is_symbolic())
385 out_free.push_back(*last);
388 exvector index_set_difference(const exvector & set1, const exvector & set2)
392 exvector::const_iterator ait = set1.begin(), aitend = set1.end();
393 while (ait != aitend) {
394 exvector::const_iterator bit = set2.begin(), bitend = set2.end();
396 while (bit != bitend) {
397 if (ait->is_equal(*bit)) {