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"
35 // default constructor, destructor, copy constructor assignment operator and helpers
40 idx::idx() : basic(TINFO_idx), symbolic(true), covariant(false)
42 debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
44 name="index"+ToString(serial);
49 debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
53 idx::idx(idx const & other)
55 debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
59 idx const & idx::operator=(idx const & other)
61 debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
71 void idx::copy(idx const & other)
75 symbolic=other.symbolic;
78 covariant=other.covariant;
81 void idx::destroy(bool call_parent)
83 if (call_parent) basic::destroy(call_parent);
92 idx::idx(bool cov) : basic(TINFO_idx), symbolic(true), covariant(cov)
94 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
96 name="index"+ToString(serial);
99 idx::idx(string const & n, bool cov) : basic(TINFO_idx),
100 symbolic(true), name(n), covariant(cov)
102 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
103 serial=next_serial++;
106 idx::idx(char const * n, bool cov) : basic(TINFO_idx),
107 symbolic(true), name(n), covariant(cov)
109 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
110 serial=next_serial++;
113 idx::idx(unsigned const v, bool cov) : basic(TINFO_idx),
114 symbolic(false), value(v), covariant(cov)
116 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
122 // functions overriding virtual functions from bases classes
127 basic * idx::duplicate() const
129 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
130 return new idx(*this);
133 void idx::printraw(ostream & os) const
135 debugmsg("idx printraw",LOGLEVEL_PRINT);
140 os << "symbolic,name=" << name;
142 os << "non symbolic,value=" << value;
148 os << ",contravariant";
151 os << ",serial=" << serial;
152 os << ",hash=" << hashvalue << ",flags=" << flags;
156 void idx::printtree(ostream & os, unsigned indent) const
158 debugmsg("idx printtree",LOGLEVEL_PRINT);
160 os << string(indent,' ') << "idx: ";
163 os << "symbolic,name=" << name;
165 os << "non symbolic,value=" << value;
171 os << ",contravariant";
174 os << ", serial=" << serial
175 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
176 << ", flags=" << flags << endl;
179 void idx::print(ostream & os, unsigned upper_precedence) const
181 debugmsg("idx print",LOGLEVEL_PRINT);
195 bool idx::info(unsigned inf) const
197 if (inf==info_flags::idx) return true;
198 return basic::info(inf);
201 ex idx::subs(lst const & ls, lst const & lr) const
203 GINAC_ASSERT(ls.nops()==lr.nops());
204 #ifdef DO_GINAC_ASSERT
205 for (int i=0; i<ls.nops(); i++) {
206 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
207 is_ex_of_type(ls.op(i),idx));
209 #endif // def DO_GINAC_ASSERT
211 for (int i=0; i<ls.nops(); i++) {
212 if (is_equal(*(ls.op(i)).bp)) {
221 int idx::compare_same_type(basic const & other) const
223 GINAC_ASSERT(is_of_type(other,idx));
224 idx const & o=static_cast<idx const &>
225 (const_cast<basic &>(other));
227 if (covariant!=o.covariant) {
228 // different co/contravariant
229 return covariant ? -1 : 1;
231 if ((!symbolic) && (!o.symbolic)) {
232 // non-symbolic, of equal type: compare values
233 if (value==o.value) {
236 return value<o.value ? -1 : 1;
238 if (symbolic && o.symbolic) {
239 // both symbolic: compare serials
240 if (serial==o.serial) {
243 return serial<o.serial ? -1 : 1;
245 // one symbolic, one value: value is sorted first
246 return o.symbolic ? -1 : 1;
249 bool idx::is_equal_same_type(basic const & other) const
251 GINAC_ASSERT(is_of_type(other,idx));
252 idx const & o=static_cast<idx const &>
253 (const_cast<basic &>(other));
255 if (covariant!=o.covariant) return false;
256 if (symbolic!=o.symbolic) return false;
257 if (symbolic && o.symbolic) return serial==o.serial;
258 return value==o.value;
261 unsigned idx::calchash(void) const
263 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
264 setflag(status_flags::hash_calculated);
269 // new virtual functions which can be overridden by derived classes
274 bool idx::is_co_contra_pair(basic const & other) const
276 // like is_equal_same_type(), but tests for different covariant status
277 GINAC_ASSERT(is_of_type(other,idx));
278 idx const & o=static_cast<idx const &>
279 (const_cast<basic &>(other));
281 if (covariant==o.covariant) return false;
282 if (symbolic!=o.symbolic) return false;
283 if (symbolic && o.symbolic) return serial==o.serial;
284 return value==o.value;
287 bool idx::is_symbolic(void) const
292 unsigned idx::get_value(void) const
297 bool idx::is_covariant(void) const
302 ex idx::toggle_covariant(void) const
304 idx * i_copy=static_cast<idx *>(duplicate());
305 i_copy->covariant = !i_copy->covariant;
306 i_copy->clearflag(status_flags::hash_calculated);
307 return i_copy->setflag(status_flags::dynallocated);
311 // non-virtual functions in this class
317 // static member variables
322 unsigned idx::next_serial=0;
329 type_info const & typeid_idx=typeid(some_idx);
335 int canonicalize_indices(exvector & iv, bool antisymmetric)
338 // nothing do to for 0 or 1 indices
342 bool something_changed=false;
344 // simple bubble sort algorithm should be sufficient for the small number of indices needed
345 exvector::const_iterator last_idx=iv.end();
346 exvector::const_iterator next_to_last_idx=iv.end()-1;
347 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
348 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
349 int cmpval=(*it1).compare(*it2);
352 something_changed=true;
353 if (antisymmetric) sig=-sig;
354 } else if ((cmpval==0) && antisymmetric) {
355 something_changed=true;
360 return something_changed ? sig : INT_MAX;
363 exvector idx_intersect(exvector const & iv1, exvector const & iv2)
365 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
366 // assumes (but does not test) that each index occurs at most twice
367 exvector iv_intersect;
368 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
369 GINAC_ASSERT(is_ex_of_type(*cit1,idx));
370 if (ex_to_idx(*cit1).is_symbolic()) {
371 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
372 GINAC_ASSERT(is_ex_of_type(*cit2,idx));
373 if ((*cit1).is_equal(*cit2)) {
374 iv_intersect.push_back(*cit1);
383 #define TEST_PERMUTATION(A,B,C,P) \
384 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
385 if (antisymmetric) *sig=P; \
389 ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
390 bool antisymmetric, int * sig)
392 // match (return value,iv2) to iv3 by permuting indices
393 // iv3 is always cyclic
395 GINAC_ASSERT(iv3.size()==3);
396 GINAC_ASSERT(iv2.size()==2);
400 TEST_PERMUTATION(0,1,2, 1);
401 TEST_PERMUTATION(0,2,1, -1);
402 TEST_PERMUTATION(1,0,2, -1);
403 TEST_PERMUTATION(1,2,0, 1);
404 TEST_PERMUTATION(2,0,1, 1);
405 TEST_PERMUTATION(2,1,0, -1);
406 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
409 unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
411 exvector::iterator it;
412 unsigned replacements=0;
413 unsigned current_replacements;
415 GINAC_ASSERT(is_ex_of_type(is,idx));
416 GINAC_ASSERT(is_ex_of_type(ir,idx));
418 for (it=v.begin(); it!=v.end(); ++it) {
419 current_replacements=count_index(*it,is);
420 if (current_replacements>0) {
421 (*it)=(*it).subs(is==ir);
423 replacements += current_replacements;
428 unsigned count_index(ex const & e, ex const & i)
430 exvector idxv=e.get_indices();
432 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
433 if ((*cit).is_equal(i)) count++;
438 ex subs_indices(ex const & e, exvector const & idxv_subs,
439 exvector const & idxv_repl)
441 GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
443 for (unsigned i=0; i<idxv_subs.size(); ++i) {
444 res=res.subs(idxv_subs[i]==idxv_repl[i]);