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), symbolic(true), name(n), covariant(cov)
113 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
114 serial = next_serial++;
117 idx::idx(unsigned v, bool cov) : inherited(TINFO_idx), symbolic(false), value(v), covariant(cov)
119 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
127 /** Construct object from archive_node. */
128 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
130 debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
131 n.find_bool("symbolic", symbolic);
132 n.find_bool("covariant", covariant);
134 serial = next_serial++;
135 if (!(n.find_string("name", name)))
136 name = "index" + ToString(serial);
139 n.find_unsigned("value", value);
143 /** Unarchive the object. */
144 ex idx::unarchive(const archive_node &n, const lst &sym_lst)
146 ex s = (new idx(n, sym_lst))->setflag(status_flags::dynallocated);
148 if (ex_to_idx(s).symbolic) {
149 // If idx is in sym_lst, return the existing idx
150 for (unsigned i=0; i<sym_lst.nops(); i++) {
151 if (is_ex_of_type(sym_lst.op(i), idx) && (ex_to_idx(sym_lst.op(i)).name == ex_to_idx(s).name))
152 return sym_lst.op(i);
158 /** Archive the object. */
159 void idx::archive(archive_node &n) const
161 inherited::archive(n);
162 n.add_bool("symbolic", symbolic);
163 n.add_bool("covariant", covariant);
165 n.add_string("name", name);
167 n.add_unsigned("value", value);
171 // functions overriding virtual functions from bases classes
176 basic * idx::duplicate() const
178 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
179 return new idx(*this);
182 void idx::printraw(std::ostream & os) const
184 debugmsg("idx printraw",LOGLEVEL_PRINT);
189 os << "symbolic,name=" << name;
191 os << "non symbolic,value=" << value;
197 os << ",contravariant";
200 os << ",serial=" << serial;
201 os << ",hash=" << hashvalue << ",flags=" << flags;
205 void idx::printtree(std::ostream & os, unsigned indent) const
207 debugmsg("idx printtree",LOGLEVEL_PRINT);
209 os << std::string(indent,' ') << "idx: ";
212 os << "symbolic,name=" << name;
214 os << "non symbolic,value=" << value;
220 os << ",contravariant";
223 os << ", serial=" << serial
224 << ", hash=" << hashvalue
225 << " (0x" << std::hex << hashvalue << std::dec << ")"
226 << ", flags=" << flags << std::endl;
229 void idx::print(std::ostream & os, unsigned upper_precedence) const
231 debugmsg("idx print",LOGLEVEL_PRINT);
245 bool idx::info(unsigned inf) const
247 if (inf==info_flags::idx) return true;
248 return inherited::info(inf);
251 ex idx::subs(const lst & ls, const lst & lr) const
253 GINAC_ASSERT(ls.nops()==lr.nops());
254 #ifdef DO_GINAC_ASSERT
255 for (unsigned i=0; i<ls.nops(); i++) {
256 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
257 is_ex_of_type(ls.op(i),idx));
259 #endif // def DO_GINAC_ASSERT
261 for (unsigned i=0; i<ls.nops(); i++) {
262 if (is_equal(*(ls.op(i)).bp)) {
271 int idx::compare_same_type(const basic & other) const
273 GINAC_ASSERT(is_of_type(other,idx));
274 const idx & o=static_cast<const idx &>(const_cast<basic &>(other));
276 if (covariant!=o.covariant) {
277 // different co/contravariant
278 return covariant ? -1 : 1;
280 if ((!symbolic) && (!o.symbolic)) {
281 // non-symbolic, of equal type: compare values
282 if (value==o.value) {
285 return value<o.value ? -1 : 1;
287 if (symbolic && o.symbolic) {
288 // both symbolic: compare serials
289 if (serial==o.serial) {
292 return serial<o.serial ? -1 : 1;
294 // one symbolic, one value: value is sorted first
295 return o.symbolic ? -1 : 1;
298 bool idx::is_equal_same_type(const basic & other) const
300 GINAC_ASSERT(is_of_type(other,idx));
301 const idx & o=static_cast<const idx &>(const_cast<basic &>(other));
303 if (covariant!=o.covariant) return false;
304 if (symbolic!=o.symbolic) return false;
305 if (symbolic && o.symbolic) return serial==o.serial;
306 return value==o.value;
309 unsigned idx::calchash(void) const
311 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
312 setflag(status_flags::hash_calculated);
317 // new virtual functions which can be overridden by derived classes
322 bool idx::is_co_contra_pair(const basic & other) const
324 // like is_equal_same_type(), but tests for different covariant status
325 GINAC_ASSERT(is_of_type(other,idx));
326 const idx & o=static_cast<const idx &>(const_cast<basic &>(other));
328 if (covariant==o.covariant) return false;
329 if (symbolic!=o.symbolic) return false;
330 if (symbolic && o.symbolic) return serial==o.serial;
331 return value==o.value;
334 bool idx::is_symbolic(void) const
339 unsigned idx::get_value(void) const
344 bool idx::is_covariant(void) const
349 ex idx::toggle_covariant(void) const
351 idx * i_copy=static_cast<idx *>(duplicate());
352 i_copy->covariant = !i_copy->covariant;
353 i_copy->clearflag(status_flags::hash_calculated);
354 return i_copy->setflag(status_flags::dynallocated);
358 // non-virtual functions in this class
364 // static member variables
369 unsigned idx::next_serial=0;
376 const std::type_info & typeid_idx = typeid(some_idx);
382 int canonicalize_indices(exvector & iv, bool antisymmetric)
385 // nothing do to for 0 or 1 indices
389 bool something_changed=false;
391 // simple bubble sort algorithm should be sufficient for the small number of indices needed
392 exvector::const_iterator last_idx=iv.end();
393 exvector::const_iterator next_to_last_idx=iv.end()-1;
394 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
395 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
396 int cmpval=(*it1).compare(*it2);
399 something_changed=true;
400 if (antisymmetric) sig=-sig;
401 } else if ((cmpval==0) && antisymmetric) {
402 something_changed=true;
407 return something_changed ? sig : INT_MAX;
410 exvector idx_intersect(const exvector & iv1, const exvector & iv2)
412 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
413 // assumes (but does not test) that each index occurs at most twice
414 exvector iv_intersect;
415 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
416 GINAC_ASSERT(is_ex_of_type(*cit1,idx));
417 if (ex_to_idx(*cit1).is_symbolic()) {
418 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
419 GINAC_ASSERT(is_ex_of_type(*cit2,idx));
420 if ((*cit1).is_equal(*cit2)) {
421 iv_intersect.push_back(*cit1);
430 #define TEST_PERMUTATION(A,B,C,P) \
431 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
432 if (antisymmetric) *sig=P; \
436 ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2,
437 bool antisymmetric, int * sig)
439 // match (return value,iv2) to iv3 by permuting indices
440 // iv3 is always cyclic
442 GINAC_ASSERT(iv3.size()==3);
443 GINAC_ASSERT(iv2.size()==2);
447 TEST_PERMUTATION(0,1,2, 1);
448 TEST_PERMUTATION(0,2,1, -1);
449 TEST_PERMUTATION(1,0,2, -1);
450 TEST_PERMUTATION(1,2,0, 1);
451 TEST_PERMUTATION(2,0,1, 1);
452 TEST_PERMUTATION(2,1,0, -1);
453 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
456 unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
458 exvector::iterator it;
459 unsigned replacements=0;
460 unsigned current_replacements;
462 GINAC_ASSERT(is_ex_of_type(is,idx));
463 GINAC_ASSERT(is_ex_of_type(ir,idx));
465 for (it=v.begin(); it!=v.end(); ++it) {
466 current_replacements=count_index(*it,is);
467 if (current_replacements>0) {
468 (*it)=(*it).subs(is==ir);
470 replacements += current_replacements;
475 unsigned count_index(const ex & e, const ex & i)
477 exvector idxv=e.get_indices();
479 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
480 if ((*cit).is_equal(i)) count++;
485 ex subs_indices(const ex & e, const exvector & idxv_subs,
486 const exvector & idxv_repl)
488 GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
490 for (unsigned i=0; i<idxv_subs.size(); ++i) {
491 res=res.subs(idxv_subs[i]==idxv_repl[i]);
496 #ifndef NO_NAMESPACE_GINAC
498 #endif // ndef NO_NAMESPACE_GINAC