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;
65 void idx::destroy(bool call_parent)
68 inherited::destroy(call_parent);
71 void varidx::destroy(bool call_parent)
74 inherited::destroy(call_parent);
81 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
83 debugmsg("idx constructor from ex,ex", LOGLEVEL_CONSTRUCT);
85 if (!dim.info(info_flags::posint))
86 throw(std::invalid_argument("dimension of space must be a positive integer"));
89 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
91 debugmsg("varidx constructor from ex,ex,bool", LOGLEVEL_CONSTRUCT);
92 tinfo_key = TINFO_varidx;
99 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
101 debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
102 n.find_ex("value", value, sym_lst);
103 n.find_ex("dim", dim, sym_lst);
106 varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
108 debugmsg("varidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
109 n.find_bool("covariant", covariant);
112 ex idx::unarchive(const archive_node &n, const lst &sym_lst)
114 return (new idx(n, sym_lst))->setflag(status_flags::dynallocated);
117 ex varidx::unarchive(const archive_node &n, const lst &sym_lst)
119 return (new varidx(n, sym_lst))->setflag(status_flags::dynallocated);
122 void idx::archive(archive_node &n) const
124 inherited::archive(n);
125 n.add_ex("value", value);
126 n.add_ex("dim", dim);
129 void varidx::archive(archive_node &n) const
131 inherited::archive(n);
132 n.add_bool("covariant", covariant);
136 // functions overriding virtual functions from bases classes
139 void idx::printraw(std::ostream & os) const
141 debugmsg("idx printraw", LOGLEVEL_PRINT);
143 os << class_name() << "(";
147 os << ",hash=" << hashvalue << ",flags=" << flags;
151 void idx::printtree(std::ostream & os, unsigned indent) const
153 debugmsg("idx printtree",LOGLEVEL_PRINT);
155 os << std::string(indent, ' ') << "type=" << class_name();
156 value.printtree(os, indent + delta_indent);
157 os << std::string(indent, ' ');
158 os << ", hash=" << hashvalue
159 << " (0x" << std::hex << hashvalue << std::dec << ")"
160 << ", flags=" << flags << std::endl;
163 void idx::print(std::ostream & os, unsigned upper_precedence) const
165 debugmsg("idx print", LOGLEVEL_PRINT);
169 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
177 void varidx::print(std::ostream & os, unsigned upper_precedence) const
179 debugmsg("varidx print", LOGLEVEL_PRINT);
186 bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
194 bool idx::info(unsigned inf) const
196 if (inf == info_flags::idx)
198 return inherited::info(inf);
201 /** Returns order relation between two indices of the same type. The order
202 * must be such that dummy indices lie next to each other. */
203 int idx::compare_same_type(const basic & other) const
205 GINAC_ASSERT(is_of_type(other, idx));
206 const idx &o = static_cast<const idx &>(other);
208 int cmpval = value.compare(o.value);
211 return dim.compare(o.dim);
214 int varidx::compare_same_type(const basic & other) const
216 GINAC_ASSERT(is_of_type(other, varidx));
217 const varidx &o = static_cast<const varidx &>(other);
219 int cmpval = inherited::compare_same_type(other);
223 // Check variance last so dummy indices will end up next to each other
224 if (covariant != o.covariant)
225 return covariant ? -1 : 1;
229 ex idx::subs(const lst & ls, const lst & lr) const
231 GINAC_ASSERT(ls.nops() == lr.nops());
233 // First look for index substitutions
234 for (unsigned i=0; i<ls.nops(); i++) {
235 if (is_equal(*(ls.op(i)).bp)) {
237 // Substitution index->index
238 if (is_ex_of_type(lr.op(i), idx))
241 // Otherwise substitute value
242 idx *i_copy = static_cast<idx *>(duplicate());
243 i_copy->value = lr.op(i);
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 return i_copy->setflag(status_flags::dynallocated);
259 // new virtual functions
262 bool idx::is_dummy_pair_same_type(const basic & other) const
264 const idx &o = static_cast<const idx &>(other);
266 // Only pure symbols form dummy pairs, "2n+1" doesn't
267 if (!is_ex_of_type(value, symbol))
270 // Value must be equal, of course
271 if (!value.is_equal(o.value))
274 // Also the dimension
275 return dim.is_equal(o.dim);
278 bool varidx::is_dummy_pair_same_type(const basic & other) const
280 const varidx &o = static_cast<const varidx &>(other);
282 // Variance must be opposite
283 if (covariant == o.covariant)
286 return inherited::is_dummy_pair_same_type(other);
290 // non-virtual functions
293 ex varidx::toggle_variance(void) const
295 varidx *i_copy = static_cast<varidx *>(duplicate());
296 i_copy->covariant = !i_copy->covariant;
297 i_copy->clearflag(status_flags::hash_calculated);
298 return i_copy->setflag(status_flags::dynallocated);
305 bool is_dummy_pair(const idx & i1, const idx & i2)
307 // The indices must be of exactly the same type
308 if (i1.tinfo() != i2.tinfo())
311 // Same type, let the indices decide whether they are paired
312 return i1.is_dummy_pair_same_type(i2);
315 bool is_dummy_pair(const ex & e1, const ex & e2)
317 // The expressions must be indices
318 if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
321 return is_dummy_pair(ex_to_idx(e1), ex_to_idx(e2));