3 * Implementation of GiNaC's indices. */
6 * GiNaC Copyright (C) 1999 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
28 #include "relational.h"
32 // default constructor, destructor, copy constructor assignment operator and helpers
37 idx::idx() : basic(TINFO_idx), symbolic(true), covariant(false)
39 debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
41 name="index"+ToString(serial);
46 debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
50 idx::idx(idx const & other)
52 debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
56 idx const & idx::operator=(idx const & other)
58 debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
68 void idx::copy(idx const & other)
72 symbolic=other.symbolic;
75 covariant=other.covariant;
78 void idx::destroy(bool call_parent)
80 if (call_parent) basic::destroy(call_parent);
89 idx::idx(bool cov) : basic(TINFO_idx), symbolic(true), covariant(cov)
91 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
93 name="index"+ToString(serial);
96 idx::idx(string const & n, bool cov) : basic(TINFO_idx),
97 symbolic(true), name(n), covariant(cov)
99 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
100 serial=next_serial++;
103 idx::idx(char const * n, bool cov) : basic(TINFO_idx),
104 symbolic(true), name(n), covariant(cov)
106 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
107 serial=next_serial++;
110 idx::idx(unsigned const v, bool cov) : basic(TINFO_idx),
111 symbolic(false), value(v), covariant(cov)
113 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
119 // functions overriding virtual functions from bases classes
124 basic * idx::duplicate() const
126 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
127 return new idx(*this);
130 void idx::printraw(ostream & os) const
132 debugmsg("idx printraw",LOGLEVEL_PRINT);
137 os << "symbolic,name=" << name;
139 os << "non symbolic,value=" << value;
145 os << ",contravariant";
148 os << ",serial=" << serial;
149 os << ",hash=" << hashvalue << ",flags=" << flags;
153 void idx::printtree(ostream & os, unsigned indent) const
155 debugmsg("idx printtree",LOGLEVEL_PRINT);
157 os << string(indent,' ') << "idx: ";
160 os << "symbolic,name=" << name;
162 os << "non symbolic,value=" << value;
168 os << ",contravariant";
171 os << ", serial=" << serial
172 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
173 << ", flags=" << flags << endl;
176 void idx::print(ostream & os, unsigned upper_precedence) const
178 debugmsg("idx print",LOGLEVEL_PRINT);
192 bool idx::info(unsigned inf) const
194 if (inf==info_flags::idx) return true;
195 return basic::info(inf);
198 ex idx::subs(lst const & ls, lst const & lr) const
200 ASSERT(ls.nops()==lr.nops());
202 for (int i=0; i<ls.nops(); i++) {
203 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
204 is_ex_of_type(ls.op(i),idx));
206 #endif // def DOASSERT
208 for (int i=0; i<ls.nops(); i++) {
209 if (is_equal(*(ls.op(i)).bp)) {
218 int idx::compare_same_type(basic const & other) const
220 ASSERT(is_of_type(other,idx));
221 idx const & o=static_cast<idx const &>
222 (const_cast<basic &>(other));
224 if (covariant!=o.covariant) {
225 // different co/contravariant
226 return covariant ? -1 : 1;
228 if ((!symbolic) && (!o.symbolic)) {
229 // non-symbolic, of equal type: compare values
230 if (value==o.value) {
233 return value<o.value ? -1 : 1;
235 if (symbolic && o.symbolic) {
236 // both symbolic: compare serials
237 if (serial==o.serial) {
240 return serial<o.serial ? -1 : 1;
242 // one symbolic, one value: value is sorted first
243 return o.symbolic ? -1 : 1;
246 bool idx::is_equal_same_type(basic const & other) const
248 ASSERT(is_of_type(other,idx));
249 idx const & o=static_cast<idx const &>
250 (const_cast<basic &>(other));
252 if (covariant!=o.covariant) return false;
253 if (symbolic!=o.symbolic) return false;
254 if (symbolic && o.symbolic) return serial==o.serial;
255 return value==o.value;
258 unsigned idx::calchash(void) const
260 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
261 setflag(status_flags::hash_calculated);
266 // new virtual functions which can be overridden by derived classes
271 bool idx::is_co_contra_pair(basic const & other) const
273 // like is_equal_same_type(), but tests for different covariant status
274 ASSERT(is_of_type(other,idx));
275 idx const & o=static_cast<idx const &>
276 (const_cast<basic &>(other));
278 if (covariant==o.covariant) return false;
279 if (symbolic!=o.symbolic) return false;
280 if (symbolic && o.symbolic) return serial==o.serial;
281 return value==o.value;
284 bool idx::is_symbolic(void) const
289 unsigned idx::get_value(void) const
294 bool idx::is_covariant(void) const
299 ex idx::toggle_covariant(void) const
301 idx * i_copy=static_cast<idx *>(duplicate());
302 i_copy->covariant = !i_copy->covariant;
303 i_copy->clearflag(status_flags::hash_calculated);
304 return i_copy->setflag(status_flags::dynallocated);
308 // non-virtual functions in this class
314 // static member variables
319 unsigned idx::next_serial=0;
326 type_info const & typeid_idx=typeid(some_idx);
332 int canonicalize_indices(exvector & iv, bool antisymmetric)
335 // nothing do to for 0 or 1 indices
339 bool something_changed=false;
341 // simple bubble sort algorithm should be sufficient for the small number of indices needed
342 exvector::const_iterator last_idx=iv.end();
343 exvector::const_iterator next_to_last_idx=iv.end()-1;
344 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
345 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
346 int cmpval=(*it1).compare(*it2);
349 something_changed=true;
350 if (antisymmetric) sig=-sig;
351 } else if ((cmpval==0) && antisymmetric) {
352 something_changed=true;
357 return something_changed ? sig : INT_MAX;
360 exvector idx_intersect(exvector const & iv1, exvector const & iv2)
362 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
363 // assumes (but does not test) that each index occurs at most twice
364 exvector iv_intersect;
365 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
366 ASSERT(is_ex_of_type(*cit1,idx));
367 if (ex_to_idx(*cit1).is_symbolic()) {
368 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
369 ASSERT(is_ex_of_type(*cit2,idx));
370 if ((*cit1).is_equal(*cit2)) {
371 iv_intersect.push_back(*cit1);
380 #define TEST_PERMUTATION(A,B,C,P) \
381 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
382 if (antisymmetric) *sig=P; \
386 ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
387 bool antisymmetric, int * sig)
389 // match (return value,iv2) to iv3 by permuting indices
390 // iv3 is always cyclic
392 ASSERT(iv3.size()==3);
393 ASSERT(iv2.size()==2);
397 TEST_PERMUTATION(0,1,2, 1);
398 TEST_PERMUTATION(0,2,1, -1);
399 TEST_PERMUTATION(1,0,2, -1);
400 TEST_PERMUTATION(1,2,0, 1);
401 TEST_PERMUTATION(2,0,1, 1);
402 TEST_PERMUTATION(2,1,0, -1);
403 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
406 unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
408 exvector::iterator it;
409 unsigned replacements=0;
410 unsigned current_replacements;
412 ASSERT(is_ex_of_type(is,idx));
413 ASSERT(is_ex_of_type(ir,idx));
415 for (it=v.begin(); it!=v.end(); ++it) {
416 current_replacements=count_index(*it,is);
417 if (current_replacements>0) {
418 (*it)=(*it).subs(is==ir);
420 replacements += current_replacements;
425 unsigned count_index(ex const & e, ex const & i)
427 exvector idxv=e.get_indices();
429 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
430 if ((*cit).is_equal(i)) count++;
435 ex subs_indices(ex const & e, exvector const & idxv_subs,
436 exvector const & idxv_repl)
438 ASSERT(idxv_subs.size()==idxv_repl.size());
440 for (unsigned i=0; i<idxv_subs.size(); ++i) {
441 res=res.subs(idxv_subs[i]==idxv_repl[i]);