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
27 #include "relational.h"
31 // default constructor, destructor, copy constructor assignment operator and helpers
36 idx::idx() : basic(TINFO_idx), symbolic(true), covariant(false)
38 debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
40 name="index"+ToString(serial);
45 debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
49 idx::idx(idx const & other)
51 debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
55 idx const & idx::operator=(idx const & other)
57 debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
67 void idx::copy(idx const & other)
71 symbolic=other.symbolic;
74 covariant=other.covariant;
77 void idx::destroy(bool call_parent)
79 if (call_parent) basic::destroy(call_parent);
88 idx::idx(bool cov) : basic(TINFO_idx), symbolic(true), covariant(cov)
90 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
92 name="index"+ToString(serial);
95 idx::idx(string const & n, bool cov) : basic(TINFO_idx),
96 symbolic(true), name(n), covariant(cov)
98 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
102 idx::idx(char const * n, bool cov) : basic(TINFO_idx),
103 symbolic(true), name(n), covariant(cov)
105 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
106 serial=next_serial++;
109 idx::idx(unsigned const v, bool cov) : basic(TINFO_idx),
110 symbolic(false), value(v), covariant(cov)
112 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
118 // functions overriding virtual functions from bases classes
123 basic * idx::duplicate() const
125 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
126 return new idx(*this);
129 void idx::printraw(ostream & os) const
131 debugmsg("idx printraw",LOGLEVEL_PRINT);
136 os << "symbolic,name=" << name;
138 os << "non symbolic,value=" << value;
144 os << ",contravariant";
147 os << ",serial=" << serial;
148 os << ",hash=" << hashvalue << ",flags=" << flags;
152 void idx::printtree(ostream & os, unsigned indent) const
154 debugmsg("idx printtree",LOGLEVEL_PRINT);
156 os << string(indent,' ') << "idx: ";
159 os << "symbolic,name=" << name;
161 os << "non symbolic,value=" << value;
167 os << ",contravariant";
170 os << ", serial=" << serial
171 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
172 << ", flags=" << flags << endl;
175 void idx::print(ostream & os, unsigned upper_precedence) const
177 debugmsg("idx print",LOGLEVEL_PRINT);
191 bool idx::info(unsigned inf) const
193 if (inf==info_flags::idx) return true;
194 return basic::info(inf);
197 ex idx::subs(lst const & ls, lst const & lr) const
199 ASSERT(ls.nops()==lr.nops());
201 for (int i=0; i<ls.nops(); i++) {
202 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
203 is_ex_of_type(ls.op(i),idx));
205 #endif // def DOASSERT
207 for (int i=0; i<ls.nops(); i++) {
208 if (is_equal(*(ls.op(i)).bp)) {
217 int idx::compare_same_type(basic const & other) const
219 ASSERT(is_of_type(other,idx));
220 idx const & o=static_cast<idx const &>
221 (const_cast<basic &>(other));
223 if (covariant!=o.covariant) {
224 // different co/contravariant
225 return covariant ? -1 : 1;
227 if ((!symbolic) && (!o.symbolic)) {
228 // non-symbolic, of equal type: compare values
229 if (value==o.value) {
232 return value<o.value ? -1 : 1;
234 if (symbolic && o.symbolic) {
235 // both symbolic: compare serials
236 if (serial==o.serial) {
239 return serial<o.serial ? -1 : 1;
241 // one symbolic, one value: value is sorted first
242 return o.symbolic ? -1 : 1;
245 bool idx::is_equal_same_type(basic const & other) const
247 ASSERT(is_of_type(other,idx));
248 idx const & o=static_cast<idx const &>
249 (const_cast<basic &>(other));
251 if (covariant!=o.covariant) return false;
252 if (symbolic!=o.symbolic) return false;
253 if (symbolic && o.symbolic) return serial==o.serial;
254 return value==o.value;
257 unsigned idx::calchash(void) const
259 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
260 setflag(status_flags::hash_calculated);
265 // new virtual functions which can be overridden by derived classes
270 bool idx::is_co_contra_pair(basic const & other) const
272 // like is_equal_same_type(), but tests for different covariant status
273 ASSERT(is_of_type(other,idx));
274 idx const & o=static_cast<idx const &>
275 (const_cast<basic &>(other));
277 if (covariant==o.covariant) return false;
278 if (symbolic!=o.symbolic) return false;
279 if (symbolic && o.symbolic) return serial==o.serial;
280 return value==o.value;
283 bool idx::is_symbolic(void) const
288 unsigned idx::get_value(void) const
293 bool idx::is_covariant(void) const
298 ex idx::toggle_covariant(void) const
300 idx * i_copy=static_cast<idx *>(duplicate());
301 i_copy->covariant = !i_copy->covariant;
302 i_copy->clearflag(status_flags::hash_calculated);
303 return i_copy->setflag(status_flags::dynallocated);
307 // non-virtual functions in this class
313 // static member variables
318 unsigned idx::next_serial=0;
325 type_info const & typeid_idx=typeid(some_idx);
331 int canonicalize_indices(exvector & iv, bool antisymmetric)
334 // nothing do to for 0 or 1 indices
338 bool something_changed=false;
340 // simple bubble sort algorithm should be sufficient for the small number of indices needed
341 exvector::const_iterator last_idx=iv.end();
342 exvector::const_iterator next_to_last_idx=iv.end()-1;
343 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
344 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
345 int cmpval=(*it1).compare(*it2);
348 something_changed=true;
349 if (antisymmetric) sig=-sig;
350 } else if ((cmpval==0) && antisymmetric) {
351 something_changed=true;
356 return something_changed ? sig : INT_MAX;
359 exvector idx_intersect(exvector const & iv1, exvector const & iv2)
361 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
362 // assumes (but does not test) that each index occurs at most twice
363 exvector iv_intersect;
364 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
365 ASSERT(is_ex_of_type(*cit1,idx));
366 if (ex_to_idx(*cit1).is_symbolic()) {
367 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
368 ASSERT(is_ex_of_type(*cit2,idx));
369 if ((*cit1).is_equal(*cit2)) {
370 iv_intersect.push_back(*cit1);
379 #define TEST_PERMUTATION(A,B,C,P) \
380 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
381 if (antisymmetric) *sig=P; \
385 ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
386 bool antisymmetric, int * sig)
388 // match (return value,iv2) to iv3 by permuting indices
389 // iv3 is always cyclic
391 ASSERT(iv3.size()==3);
392 ASSERT(iv2.size()==2);
396 TEST_PERMUTATION(0,1,2, 1);
397 TEST_PERMUTATION(0,2,1, -1);
398 TEST_PERMUTATION(1,0,2, -1);
399 TEST_PERMUTATION(1,2,0, 1);
400 TEST_PERMUTATION(2,0,1, 1);
401 TEST_PERMUTATION(2,1,0, -1);
402 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
405 unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
407 exvector::iterator it;
408 unsigned replacements=0;
409 unsigned current_replacements;
411 ASSERT(is_ex_of_type(is,idx));
412 ASSERT(is_ex_of_type(ir,idx));
414 for (it=v.begin(); it!=v.end(); ++it) {
415 current_replacements=count_index(*it,is);
416 if (current_replacements>0) {
417 (*it)=(*it).subs(is==ir);
419 replacements += current_replacements;
424 unsigned count_index(ex const & e, ex const & i)
426 exvector idxv=e.get_indices();
428 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
429 if ((*cit).is_equal(i)) count++;
434 ex subs_indices(ex const & e, exvector const & idxv_subs,
435 exvector const & idxv_repl)
437 ASSERT(idxv_subs.size()==idxv_repl.size());
439 for (unsigned i=0; i<idxv_subs.size(); ++i) {
440 res=res.subs(idxv_subs[i]==idxv_repl[i]);