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"
32 #ifndef NO_GINAC_NAMESPACE
34 #endif // ndef NO_GINAC_NAMESPACE
37 // default constructor, destructor, copy constructor assignment operator and helpers
42 idx::idx() : basic(TINFO_idx), symbolic(true), covariant(false)
44 debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
46 name="index"+ToString(serial);
51 debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
55 idx::idx(idx const & other)
57 debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
61 idx const & idx::operator=(idx const & other)
63 debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
73 void idx::copy(idx const & other)
77 symbolic=other.symbolic;
80 covariant=other.covariant;
83 void idx::destroy(bool call_parent)
85 if (call_parent) basic::destroy(call_parent);
94 idx::idx(bool cov) : basic(TINFO_idx), symbolic(true), covariant(cov)
96 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
98 name="index"+ToString(serial);
101 idx::idx(string const & n, bool cov) : basic(TINFO_idx),
102 symbolic(true), name(n), covariant(cov)
104 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
105 serial=next_serial++;
108 idx::idx(char const * n, bool cov) : basic(TINFO_idx),
109 symbolic(true), name(n), covariant(cov)
111 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
112 serial=next_serial++;
115 idx::idx(unsigned const v, bool cov) : basic(TINFO_idx),
116 symbolic(false), value(v), covariant(cov)
118 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
124 // functions overriding virtual functions from bases classes
129 basic * idx::duplicate() const
131 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
132 return new idx(*this);
135 void idx::printraw(ostream & os) const
137 debugmsg("idx printraw",LOGLEVEL_PRINT);
142 os << "symbolic,name=" << name;
144 os << "non symbolic,value=" << value;
150 os << ",contravariant";
153 os << ",serial=" << serial;
154 os << ",hash=" << hashvalue << ",flags=" << flags;
158 void idx::printtree(ostream & os, unsigned indent) const
160 debugmsg("idx printtree",LOGLEVEL_PRINT);
162 os << string(indent,' ') << "idx: ";
165 os << "symbolic,name=" << name;
167 os << "non symbolic,value=" << value;
173 os << ",contravariant";
176 os << ", serial=" << serial
177 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
178 << ", flags=" << flags << endl;
181 void idx::print(ostream & os, unsigned upper_precedence) const
183 debugmsg("idx print",LOGLEVEL_PRINT);
197 bool idx::info(unsigned inf) const
199 if (inf==info_flags::idx) return true;
200 return basic::info(inf);
203 ex idx::subs(lst const & ls, lst const & lr) const
205 GINAC_ASSERT(ls.nops()==lr.nops());
206 #ifdef DO_GINAC_ASSERT
207 for (unsigned i=0; i<ls.nops(); i++) {
208 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
209 is_ex_of_type(ls.op(i),idx));
211 #endif // def DO_GINAC_ASSERT
213 for (unsigned i=0; i<ls.nops(); i++) {
214 if (is_equal(*(ls.op(i)).bp)) {
223 int idx::compare_same_type(basic const & other) const
225 GINAC_ASSERT(is_of_type(other,idx));
226 idx const & o=static_cast<idx const &>
227 (const_cast<basic &>(other));
229 if (covariant!=o.covariant) {
230 // different co/contravariant
231 return covariant ? -1 : 1;
233 if ((!symbolic) && (!o.symbolic)) {
234 // non-symbolic, of equal type: compare values
235 if (value==o.value) {
238 return value<o.value ? -1 : 1;
240 if (symbolic && o.symbolic) {
241 // both symbolic: compare serials
242 if (serial==o.serial) {
245 return serial<o.serial ? -1 : 1;
247 // one symbolic, one value: value is sorted first
248 return o.symbolic ? -1 : 1;
251 bool idx::is_equal_same_type(basic const & other) const
253 GINAC_ASSERT(is_of_type(other,idx));
254 idx const & o=static_cast<idx const &>
255 (const_cast<basic &>(other));
257 if (covariant!=o.covariant) return false;
258 if (symbolic!=o.symbolic) return false;
259 if (symbolic && o.symbolic) return serial==o.serial;
260 return value==o.value;
263 unsigned idx::calchash(void) const
265 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
266 setflag(status_flags::hash_calculated);
271 // new virtual functions which can be overridden by derived classes
276 bool idx::is_co_contra_pair(basic const & other) const
278 // like is_equal_same_type(), but tests for different covariant status
279 GINAC_ASSERT(is_of_type(other,idx));
280 idx const & o=static_cast<idx const &>
281 (const_cast<basic &>(other));
283 if (covariant==o.covariant) return false;
284 if (symbolic!=o.symbolic) return false;
285 if (symbolic && o.symbolic) return serial==o.serial;
286 return value==o.value;
289 bool idx::is_symbolic(void) const
294 unsigned idx::get_value(void) const
299 bool idx::is_covariant(void) const
304 ex idx::toggle_covariant(void) const
306 idx * i_copy=static_cast<idx *>(duplicate());
307 i_copy->covariant = !i_copy->covariant;
308 i_copy->clearflag(status_flags::hash_calculated);
309 return i_copy->setflag(status_flags::dynallocated);
313 // non-virtual functions in this class
319 // static member variables
324 unsigned idx::next_serial=0;
331 type_info const & typeid_idx=typeid(some_idx);
337 int canonicalize_indices(exvector & iv, bool antisymmetric)
340 // nothing do to for 0 or 1 indices
344 bool something_changed=false;
346 // simple bubble sort algorithm should be sufficient for the small number of indices needed
347 exvector::const_iterator last_idx=iv.end();
348 exvector::const_iterator next_to_last_idx=iv.end()-1;
349 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
350 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
351 int cmpval=(*it1).compare(*it2);
354 something_changed=true;
355 if (antisymmetric) sig=-sig;
356 } else if ((cmpval==0) && antisymmetric) {
357 something_changed=true;
362 return something_changed ? sig : INT_MAX;
365 exvector idx_intersect(exvector const & iv1, exvector const & iv2)
367 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
368 // assumes (but does not test) that each index occurs at most twice
369 exvector iv_intersect;
370 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
371 GINAC_ASSERT(is_ex_of_type(*cit1,idx));
372 if (ex_to_idx(*cit1).is_symbolic()) {
373 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
374 GINAC_ASSERT(is_ex_of_type(*cit2,idx));
375 if ((*cit1).is_equal(*cit2)) {
376 iv_intersect.push_back(*cit1);
385 #define TEST_PERMUTATION(A,B,C,P) \
386 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
387 if (antisymmetric) *sig=P; \
391 ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
392 bool antisymmetric, int * sig)
394 // match (return value,iv2) to iv3 by permuting indices
395 // iv3 is always cyclic
397 GINAC_ASSERT(iv3.size()==3);
398 GINAC_ASSERT(iv2.size()==2);
402 TEST_PERMUTATION(0,1,2, 1);
403 TEST_PERMUTATION(0,2,1, -1);
404 TEST_PERMUTATION(1,0,2, -1);
405 TEST_PERMUTATION(1,2,0, 1);
406 TEST_PERMUTATION(2,0,1, 1);
407 TEST_PERMUTATION(2,1,0, -1);
408 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
411 unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
413 exvector::iterator it;
414 unsigned replacements=0;
415 unsigned current_replacements;
417 GINAC_ASSERT(is_ex_of_type(is,idx));
418 GINAC_ASSERT(is_ex_of_type(ir,idx));
420 for (it=v.begin(); it!=v.end(); ++it) {
421 current_replacements=count_index(*it,is);
422 if (current_replacements>0) {
423 (*it)=(*it).subs(is==ir);
425 replacements += current_replacements;
430 unsigned count_index(ex const & e, ex const & i)
432 exvector idxv=e.get_indices();
434 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
435 if ((*cit).is_equal(i)) count++;
440 ex subs_indices(ex const & e, exvector const & idxv_subs,
441 exvector const & idxv_repl)
443 GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
445 for (unsigned i=0; i<idxv_subs.size(); ++i) {
446 res=res.subs(idxv_subs[i]==idxv_repl[i]);
451 #ifndef NO_GINAC_NAMESPACE
453 #endif // ndef NO_GINAC_NAMESPACE