3 * Implementation of GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2000 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"
33 #ifndef NO_NAMESPACE_GINAC
35 #endif // ndef NO_NAMESPACE_GINAC
37 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
40 // default constructor, destructor, copy constructor assignment operator and helpers
45 idx::idx() : inherited(TINFO_idx), symbolic(true), covariant(false)
47 debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
49 name="index"+ToString(serial);
54 debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
58 idx::idx(const idx & other)
60 debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
64 const idx & idx::operator=(const idx & other)
66 debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
76 void idx::copy(const idx & other)
78 inherited::copy(other);
80 symbolic=other.symbolic;
83 covariant=other.covariant;
86 void idx::destroy(bool call_parent)
88 if (call_parent) inherited::destroy(call_parent);
97 idx::idx(bool cov) : inherited(TINFO_idx), symbolic(true), covariant(cov)
99 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
100 serial=next_serial++;
101 name="index"+ToString(serial);
104 idx::idx(const string & n, bool cov) : inherited(TINFO_idx),
105 symbolic(true), name(n), covariant(cov)
107 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
108 serial=next_serial++;
111 idx::idx(const char * n, bool cov) : inherited(TINFO_idx),
112 symbolic(true), name(n), covariant(cov)
114 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
115 serial=next_serial++;
118 idx::idx(unsigned v, bool cov) : inherited(TINFO_idx),
119 symbolic(false), value(v), covariant(cov)
121 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
129 /** Construct object from archive_node. */
130 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
132 debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
133 n.find_bool("symbolic", symbolic);
134 n.find_bool("covariant", covariant);
136 serial = next_serial++;
137 if (!(n.find_string("name", name)))
138 name = "index" + ToString(serial);
141 n.find_unsigned("value", value);
145 /** Unarchive the object. */
146 ex idx::unarchive(const archive_node &n, const lst &sym_lst)
148 ex s = (new idx(n, sym_lst))->setflag(status_flags::dynallocated);
150 if (ex_to_idx(s).symbolic) {
151 // If idx is in sym_lst, return the existing idx
152 for (unsigned i=0; i<sym_lst.nops(); i++) {
153 if (is_ex_of_type(sym_lst.op(i), idx) && (ex_to_idx(sym_lst.op(i)).name == ex_to_idx(s).name))
154 return sym_lst.op(i);
160 /** Archive the object. */
161 void idx::archive(archive_node &n) const
163 inherited::archive(n);
164 n.add_bool("symbolic", symbolic);
165 n.add_bool("covariant", covariant);
167 n.add_string("name", name);
169 n.add_unsigned("value", value);
173 // functions overriding virtual functions from bases classes
178 basic * idx::duplicate() const
180 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
181 return new idx(*this);
184 void idx::printraw(ostream & os) const
186 debugmsg("idx printraw",LOGLEVEL_PRINT);
191 os << "symbolic,name=" << name;
193 os << "non symbolic,value=" << value;
199 os << ",contravariant";
202 os << ",serial=" << serial;
203 os << ",hash=" << hashvalue << ",flags=" << flags;
207 void idx::printtree(ostream & os, unsigned indent) const
209 debugmsg("idx printtree",LOGLEVEL_PRINT);
211 os << string(indent,' ') << "idx: ";
214 os << "symbolic,name=" << name;
216 os << "non symbolic,value=" << value;
222 os << ",contravariant";
225 os << ", serial=" << serial
226 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
227 << ", flags=" << flags << endl;
230 void idx::print(ostream & os, unsigned upper_precedence) const
232 debugmsg("idx print",LOGLEVEL_PRINT);
246 bool idx::info(unsigned inf) const
248 if (inf==info_flags::idx) return true;
249 return inherited::info(inf);
252 ex idx::subs(const lst & ls, const lst & lr) const
254 GINAC_ASSERT(ls.nops()==lr.nops());
255 #ifdef DO_GINAC_ASSERT
256 for (unsigned i=0; i<ls.nops(); i++) {
257 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
258 is_ex_of_type(ls.op(i),idx));
260 #endif // def DO_GINAC_ASSERT
262 for (unsigned i=0; i<ls.nops(); i++) {
263 if (is_equal(*(ls.op(i)).bp)) {
272 int idx::compare_same_type(const basic & other) const
274 GINAC_ASSERT(is_of_type(other,idx));
275 const idx & o=static_cast<const idx &>
276 (const_cast<basic &>(other));
278 if (covariant!=o.covariant) {
279 // different co/contravariant
280 return covariant ? -1 : 1;
282 if ((!symbolic) && (!o.symbolic)) {
283 // non-symbolic, of equal type: compare values
284 if (value==o.value) {
287 return value<o.value ? -1 : 1;
289 if (symbolic && o.symbolic) {
290 // both symbolic: compare serials
291 if (serial==o.serial) {
294 return serial<o.serial ? -1 : 1;
296 // one symbolic, one value: value is sorted first
297 return o.symbolic ? -1 : 1;
300 bool idx::is_equal_same_type(const basic & other) const
302 GINAC_ASSERT(is_of_type(other,idx));
303 const idx & o=static_cast<const idx &>
304 (const_cast<basic &>(other));
306 if (covariant!=o.covariant) return false;
307 if (symbolic!=o.symbolic) return false;
308 if (symbolic && o.symbolic) return serial==o.serial;
309 return value==o.value;
312 unsigned idx::calchash(void) const
314 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
315 setflag(status_flags::hash_calculated);
320 // new virtual functions which can be overridden by derived classes
325 bool idx::is_co_contra_pair(const basic & other) const
327 // like is_equal_same_type(), but tests for different covariant status
328 GINAC_ASSERT(is_of_type(other,idx));
329 const idx & o=static_cast<const idx &>
330 (const_cast<basic &>(other));
332 if (covariant==o.covariant) return false;
333 if (symbolic!=o.symbolic) return false;
334 if (symbolic && o.symbolic) return serial==o.serial;
335 return value==o.value;
338 bool idx::is_symbolic(void) const
343 unsigned idx::get_value(void) const
348 bool idx::is_covariant(void) const
353 ex idx::toggle_covariant(void) const
355 idx * i_copy=static_cast<idx *>(duplicate());
356 i_copy->covariant = !i_copy->covariant;
357 i_copy->clearflag(status_flags::hash_calculated);
358 return i_copy->setflag(status_flags::dynallocated);
362 // non-virtual functions in this class
368 // static member variables
373 unsigned idx::next_serial=0;
380 const type_info & typeid_idx=typeid(some_idx);
386 int canonicalize_indices(exvector & iv, bool antisymmetric)
389 // nothing do to for 0 or 1 indices
393 bool something_changed=false;
395 // simple bubble sort algorithm should be sufficient for the small number of indices needed
396 exvector::const_iterator last_idx=iv.end();
397 exvector::const_iterator next_to_last_idx=iv.end()-1;
398 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
399 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
400 int cmpval=(*it1).compare(*it2);
403 something_changed=true;
404 if (antisymmetric) sig=-sig;
405 } else if ((cmpval==0) && antisymmetric) {
406 something_changed=true;
411 return something_changed ? sig : INT_MAX;
414 exvector idx_intersect(const exvector & iv1, const exvector & iv2)
416 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
417 // assumes (but does not test) that each index occurs at most twice
418 exvector iv_intersect;
419 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
420 GINAC_ASSERT(is_ex_of_type(*cit1,idx));
421 if (ex_to_idx(*cit1).is_symbolic()) {
422 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
423 GINAC_ASSERT(is_ex_of_type(*cit2,idx));
424 if ((*cit1).is_equal(*cit2)) {
425 iv_intersect.push_back(*cit1);
434 #define TEST_PERMUTATION(A,B,C,P) \
435 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
436 if (antisymmetric) *sig=P; \
440 ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2,
441 bool antisymmetric, int * sig)
443 // match (return value,iv2) to iv3 by permuting indices
444 // iv3 is always cyclic
446 GINAC_ASSERT(iv3.size()==3);
447 GINAC_ASSERT(iv2.size()==2);
451 TEST_PERMUTATION(0,1,2, 1);
452 TEST_PERMUTATION(0,2,1, -1);
453 TEST_PERMUTATION(1,0,2, -1);
454 TEST_PERMUTATION(1,2,0, 1);
455 TEST_PERMUTATION(2,0,1, 1);
456 TEST_PERMUTATION(2,1,0, -1);
457 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
460 unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
462 exvector::iterator it;
463 unsigned replacements=0;
464 unsigned current_replacements;
466 GINAC_ASSERT(is_ex_of_type(is,idx));
467 GINAC_ASSERT(is_ex_of_type(ir,idx));
469 for (it=v.begin(); it!=v.end(); ++it) {
470 current_replacements=count_index(*it,is);
471 if (current_replacements>0) {
472 (*it)=(*it).subs(is==ir);
474 replacements += current_replacements;
479 unsigned count_index(const ex & e, const ex & i)
481 exvector idxv=e.get_indices();
483 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
484 if ((*cit).is_equal(i)) count++;
489 ex subs_indices(const ex & e, const exvector & idxv_subs,
490 const exvector & idxv_repl)
492 GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
494 for (unsigned i=0; i<idxv_subs.size(); ++i) {
495 res=res.subs(idxv_subs[i]==idxv_repl[i]);
500 #ifndef NO_NAMESPACE_GINAC
502 #endif // ndef NO_NAMESPACE_GINAC