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 std::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(std::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(std::ostream & os, unsigned indent) const
209 debugmsg("idx printtree",LOGLEVEL_PRINT);
211 os << std::string(indent,' ') << "idx: ";
214 os << "symbolic,name=" << name;
216 os << "non symbolic,value=" << value;
222 os << ",contravariant";
225 os << ", serial=" << serial
226 << ", hash=" << hashvalue
227 << " (0x" << std::hex << hashvalue << std::dec << ")"
228 << ", flags=" << flags << std::endl;
231 void idx::print(std::ostream & os, unsigned upper_precedence) const
233 debugmsg("idx print",LOGLEVEL_PRINT);
247 bool idx::info(unsigned inf) const
249 if (inf==info_flags::idx) return true;
250 return inherited::info(inf);
253 ex idx::subs(const lst & ls, const lst & lr) const
255 GINAC_ASSERT(ls.nops()==lr.nops());
256 #ifdef DO_GINAC_ASSERT
257 for (unsigned i=0; i<ls.nops(); i++) {
258 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
259 is_ex_of_type(ls.op(i),idx));
261 #endif // def DO_GINAC_ASSERT
263 for (unsigned i=0; i<ls.nops(); i++) {
264 if (is_equal(*(ls.op(i)).bp)) {
273 int idx::compare_same_type(const basic & other) const
275 GINAC_ASSERT(is_of_type(other,idx));
276 const idx & o=static_cast<const idx &>
277 (const_cast<basic &>(other));
279 if (covariant!=o.covariant) {
280 // different co/contravariant
281 return covariant ? -1 : 1;
283 if ((!symbolic) && (!o.symbolic)) {
284 // non-symbolic, of equal type: compare values
285 if (value==o.value) {
288 return value<o.value ? -1 : 1;
290 if (symbolic && o.symbolic) {
291 // both symbolic: compare serials
292 if (serial==o.serial) {
295 return serial<o.serial ? -1 : 1;
297 // one symbolic, one value: value is sorted first
298 return o.symbolic ? -1 : 1;
301 bool idx::is_equal_same_type(const basic & other) const
303 GINAC_ASSERT(is_of_type(other,idx));
304 const idx & o=static_cast<const idx &>
305 (const_cast<basic &>(other));
307 if (covariant!=o.covariant) return false;
308 if (symbolic!=o.symbolic) return false;
309 if (symbolic && o.symbolic) return serial==o.serial;
310 return value==o.value;
313 unsigned idx::calchash(void) const
315 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
316 setflag(status_flags::hash_calculated);
321 // new virtual functions which can be overridden by derived classes
326 bool idx::is_co_contra_pair(const basic & other) const
328 // like is_equal_same_type(), but tests for different covariant status
329 GINAC_ASSERT(is_of_type(other,idx));
330 const idx & o=static_cast<const idx &>
331 (const_cast<basic &>(other));
333 if (covariant==o.covariant) return false;
334 if (symbolic!=o.symbolic) return false;
335 if (symbolic && o.symbolic) return serial==o.serial;
336 return value==o.value;
339 bool idx::is_symbolic(void) const
344 unsigned idx::get_value(void) const
349 bool idx::is_covariant(void) const
354 ex idx::toggle_covariant(void) const
356 idx * i_copy=static_cast<idx *>(duplicate());
357 i_copy->covariant = !i_copy->covariant;
358 i_copy->clearflag(status_flags::hash_calculated);
359 return i_copy->setflag(status_flags::dynallocated);
363 // non-virtual functions in this class
369 // static member variables
374 unsigned idx::next_serial=0;
381 const type_info & typeid_idx=typeid(some_idx);
387 int canonicalize_indices(exvector & iv, bool antisymmetric)
390 // nothing do to for 0 or 1 indices
394 bool something_changed=false;
396 // simple bubble sort algorithm should be sufficient for the small number of indices needed
397 exvector::const_iterator last_idx=iv.end();
398 exvector::const_iterator next_to_last_idx=iv.end()-1;
399 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
400 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
401 int cmpval=(*it1).compare(*it2);
404 something_changed=true;
405 if (antisymmetric) sig=-sig;
406 } else if ((cmpval==0) && antisymmetric) {
407 something_changed=true;
412 return something_changed ? sig : INT_MAX;
415 exvector idx_intersect(const exvector & iv1, const exvector & iv2)
417 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
418 // assumes (but does not test) that each index occurs at most twice
419 exvector iv_intersect;
420 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
421 GINAC_ASSERT(is_ex_of_type(*cit1,idx));
422 if (ex_to_idx(*cit1).is_symbolic()) {
423 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
424 GINAC_ASSERT(is_ex_of_type(*cit2,idx));
425 if ((*cit1).is_equal(*cit2)) {
426 iv_intersect.push_back(*cit1);
435 #define TEST_PERMUTATION(A,B,C,P) \
436 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
437 if (antisymmetric) *sig=P; \
441 ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2,
442 bool antisymmetric, int * sig)
444 // match (return value,iv2) to iv3 by permuting indices
445 // iv3 is always cyclic
447 GINAC_ASSERT(iv3.size()==3);
448 GINAC_ASSERT(iv2.size()==2);
452 TEST_PERMUTATION(0,1,2, 1);
453 TEST_PERMUTATION(0,2,1, -1);
454 TEST_PERMUTATION(1,0,2, -1);
455 TEST_PERMUTATION(1,2,0, 1);
456 TEST_PERMUTATION(2,0,1, 1);
457 TEST_PERMUTATION(2,1,0, -1);
458 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
461 unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
463 exvector::iterator it;
464 unsigned replacements=0;
465 unsigned current_replacements;
467 GINAC_ASSERT(is_ex_of_type(is,idx));
468 GINAC_ASSERT(is_ex_of_type(ir,idx));
470 for (it=v.begin(); it!=v.end(); ++it) {
471 current_replacements=count_index(*it,is);
472 if (current_replacements>0) {
473 (*it)=(*it).subs(is==ir);
475 replacements += current_replacements;
480 unsigned count_index(const ex & e, const ex & i)
482 exvector idxv=e.get_indices();
484 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
485 if ((*cit).is_equal(i)) count++;
490 ex subs_indices(const ex & e, const exvector & idxv_subs,
491 const exvector & idxv_repl)
493 GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
495 for (unsigned i=0; i<idxv_subs.size(); ++i) {
496 res=res.subs(idxv_subs[i]==idxv_repl[i]);
501 #ifndef NO_NAMESPACE_GINAC
503 #endif // ndef NO_NAMESPACE_GINAC