3 * Implementation of GiNaC's indices.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 // default constructor, destructor, copy constructor assignment operator and helpers
33 idx::idx() : basic(TINFO_IDX), symbolic(true), covariant(false)
35 debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
37 name="index"+ToString(serial);
42 debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
46 idx::idx(idx const & other)
48 debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
52 idx const & idx::operator=(idx const & other)
54 debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
64 void idx::copy(idx const & other)
68 symbolic=other.symbolic;
71 covariant=other.covariant;
74 void idx::destroy(bool call_parent)
76 if (call_parent) basic::destroy(call_parent);
85 idx::idx(bool cov) : basic(TINFO_IDX), symbolic(true), covariant(cov)
87 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
89 name="index"+ToString(serial);
92 idx::idx(string const & n, bool cov) : basic(TINFO_IDX),
93 symbolic(true), name(n), covariant(cov)
95 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
99 idx::idx(char const * n, bool cov) : basic(TINFO_IDX),
100 symbolic(true), name(n), covariant(cov)
102 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
103 serial=next_serial++;
106 idx::idx(unsigned const v, bool cov) : basic(TINFO_IDX),
107 symbolic(false), value(v), covariant(cov)
109 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
115 // functions overriding virtual functions from bases classes
120 basic * idx::duplicate() const
122 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
123 return new idx(*this);
126 void idx::printraw(ostream & os) const
128 debugmsg("idx printraw",LOGLEVEL_PRINT);
133 os << "symbolic,name=" << name;
135 os << "non symbolic,value=" << value;
141 os << ",contravariant";
144 os << ",serial=" << serial;
145 os << ",hash=" << hashvalue << ",flags=" << flags;
149 void idx::printtree(ostream & os, unsigned indent) const
151 debugmsg("idx printtree",LOGLEVEL_PRINT);
153 os << string(indent,' ') << "idx: ";
156 os << "symbolic,name=" << name;
158 os << "non symbolic,value=" << value;
164 os << ",contravariant";
167 os << ", serial=" << serial
168 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
169 << ", flags=" << flags << endl;
172 void idx::print(ostream & os, unsigned upper_precedence) const
174 debugmsg("idx print",LOGLEVEL_PRINT);
188 bool idx::info(unsigned inf) const
190 if (inf==info_flags::idx) return true;
191 return basic::info(inf);
194 ex idx::subs(lst const & ls, lst const & lr) const
196 ASSERT(ls.nops()==lr.nops());
198 for (int i=0; i<ls.nops(); i++) {
199 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
200 is_ex_of_type(ls.op(i),idx));
202 #endif // def DOASSERT
204 for (int i=0; i<ls.nops(); i++) {
205 if (is_equal(*(ls.op(i)).bp)) {
214 int idx::compare_same_type(basic const & other) const
216 ASSERT(is_of_type(other,idx));
217 idx const & o=static_cast<idx const &>
218 (const_cast<basic &>(other));
220 if (covariant!=o.covariant) {
221 // different co/contravariant
222 return covariant ? -1 : 1;
224 if ((!symbolic) && (!o.symbolic)) {
225 // non-symbolic, of equal type: compare values
226 if (value==o.value) {
229 return value<o.value ? -1 : 1;
231 if (symbolic && o.symbolic) {
232 // both symbolic: compare serials
233 if (serial==o.serial) {
236 return serial<o.serial ? -1 : 1;
238 // one symbolic, one value: value is sorted first
239 return o.symbolic ? -1 : 1;
242 bool idx::is_equal_same_type(basic const & other) const
244 ASSERT(is_of_type(other,idx));
245 idx const & o=static_cast<idx const &>
246 (const_cast<basic &>(other));
248 if (covariant!=o.covariant) return false;
249 if (symbolic!=o.symbolic) return false;
250 if (symbolic && o.symbolic) return serial==o.serial;
251 return value==o.value;
254 unsigned idx::calchash(void) const
256 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
257 setflag(status_flags::hash_calculated);
262 // new virtual functions which can be overridden by derived classes
267 bool idx::is_co_contra_pair(basic const & other) const
269 // like is_equal_same_type(), but tests for different covariant status
270 ASSERT(is_of_type(other,idx));
271 idx const & o=static_cast<idx const &>
272 (const_cast<basic &>(other));
274 if (covariant==o.covariant) return false;
275 if (symbolic!=o.symbolic) return false;
276 if (symbolic && o.symbolic) return serial==o.serial;
277 return value==o.value;
280 bool idx::is_symbolic(void) const
285 unsigned idx::get_value(void) const
290 bool idx::is_covariant(void) const
295 ex idx::toggle_covariant(void) const
297 idx * i_copy=static_cast<idx *>(duplicate());
298 i_copy->covariant = !i_copy->covariant;
299 i_copy->clearflag(status_flags::hash_calculated);
300 return i_copy->setflag(status_flags::dynallocated);
304 // non-virtual functions in this class
310 // static member variables
315 unsigned idx::next_serial=0;
322 type_info const & typeid_idx=typeid(some_idx);
328 int canonicalize_indices(exvector & iv, bool antisymmetric)
331 // nothing do to for 0 or 1 indices
335 bool something_changed=false;
337 // simple bubble sort algorithm should be sufficient for the small number of indices needed
338 exvector::const_iterator last_idx=iv.end();
339 exvector::const_iterator next_to_last_idx=iv.end()-1;
340 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
341 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
342 int cmpval=(*it1).compare(*it2);
345 something_changed=true;
346 if (antisymmetric) sig=-sig;
347 } else if ((cmpval==0) && antisymmetric) {
348 something_changed=true;
353 return something_changed ? sig : INT_MAX;
356 exvector idx_intersect(exvector const & iv1, exvector const & iv2)
358 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
359 // assumes (but does not test) that each index occurs at most twice
360 exvector iv_intersect;
361 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
362 ASSERT(is_ex_of_type(*cit1,idx));
363 if (ex_to_idx(*cit1).is_symbolic()) {
364 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
365 ASSERT(is_ex_of_type(*cit2,idx));
366 if ((*cit1).is_equal(*cit2)) {
367 iv_intersect.push_back(*cit1);
376 #define TEST_PERMUTATION(A,B,C,P) \
377 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
378 if (antisymmetric) *sig=P; \
382 ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
383 bool antisymmetric, int * sig)
385 // match (return value,iv2) to iv3 by permuting indices
386 // iv3 is always cyclic
388 ASSERT(iv3.size()==3);
389 ASSERT(iv2.size()==2);
393 TEST_PERMUTATION(0,1,2, 1);
394 TEST_PERMUTATION(0,2,1, -1);
395 TEST_PERMUTATION(1,0,2, -1);
396 TEST_PERMUTATION(1,2,0, 1);
397 TEST_PERMUTATION(2,0,1, 1);
398 TEST_PERMUTATION(2,1,0, -1);
399 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
402 unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
404 exvector::iterator it;
405 unsigned replacements=0;
406 unsigned current_replacements;
408 ASSERT(is_ex_of_type(is,idx));
409 ASSERT(is_ex_of_type(ir,idx));
411 for (it=v.begin(); it!=v.end(); ++it) {
412 current_replacements=count_index(*it,is);
413 if (current_replacements>0) {
414 (*it)=(*it).subs(is==ir);
416 replacements += current_replacements;
421 unsigned count_index(ex const & e, ex const & i)
423 exvector idxv=e.get_indices();
425 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
426 if ((*cit).is_equal(i)) count++;
431 ex subs_indices(ex const & e, exvector const & idxv_subs,
432 exvector const & idxv_repl)
434 ASSERT(idxv_subs.size()==idxv_repl.size());
436 for (unsigned i=0; i<idxv_subs.size(); ++i) {
437 res=res.subs(idxv_subs[i]==idxv_repl[i]);