3 * Implementation of GiNaC's indices. */
11 // default constructor, destructor, copy constructor assignment operator and helpers
16 idx::idx() : basic(TINFO_IDX), symbolic(true), covariant(false)
18 debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
20 name="index"+ToString(serial);
25 debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
29 idx::idx(idx const & other)
31 debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
35 idx const & idx::operator=(idx const & other)
37 debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
47 void idx::copy(idx const & other)
51 symbolic=other.symbolic;
54 covariant=other.covariant;
57 void idx::destroy(bool call_parent)
59 if (call_parent) basic::destroy(call_parent);
68 idx::idx(bool cov) : basic(TINFO_IDX), symbolic(true), covariant(cov)
70 debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
72 name="index"+ToString(serial);
75 idx::idx(string const & n, bool cov) : basic(TINFO_IDX),
76 symbolic(true), name(n), covariant(cov)
78 debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
82 idx::idx(char const * n, bool cov) : basic(TINFO_IDX),
83 symbolic(true), name(n), covariant(cov)
85 debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
89 idx::idx(unsigned const v, bool cov) : basic(TINFO_IDX),
90 symbolic(false), value(v), covariant(cov)
92 debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
98 // functions overriding virtual functions from bases classes
103 basic * idx::duplicate() const
105 debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
106 return new idx(*this);
109 void idx::printraw(ostream & os) const
111 debugmsg("idx printraw",LOGLEVEL_PRINT);
116 os << "symbolic,name=" << name;
118 os << "non symbolic,value=" << value;
124 os << ",contravariant";
127 os << ",serial=" << serial;
128 os << ",hash=" << hashvalue << ",flags=" << flags;
132 void idx::printtree(ostream & os, unsigned indent) const
134 debugmsg("idx printtree",LOGLEVEL_PRINT);
136 os << string(indent,' ') << "idx: ";
139 os << "symbolic,name=" << name;
141 os << "non symbolic,value=" << value;
147 os << ",contravariant";
150 os << ", serial=" << serial
151 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
152 << ", flags=" << flags << endl;
155 void idx::print(ostream & os, unsigned upper_precedence) const
157 debugmsg("idx print",LOGLEVEL_PRINT);
171 bool idx::info(unsigned inf) const
173 if (inf==info_flags::idx) return true;
174 return basic::info(inf);
177 ex idx::subs(lst const & ls, lst const & lr) const
179 ASSERT(ls.nops()==lr.nops());
181 for (int i=0; i<ls.nops(); i++) {
182 ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
183 is_ex_of_type(ls.op(i),idx));
185 #endif // def DOASSERT
187 for (int i=0; i<ls.nops(); i++) {
188 if (is_equal(*(ls.op(i)).bp)) {
197 int idx::compare_same_type(basic const & other) const
199 ASSERT(is_of_type(other,idx));
200 idx const & o=static_cast<idx const &>
201 (const_cast<basic &>(other));
203 if (covariant!=o.covariant) {
204 // different co/contravariant
205 return covariant ? -1 : 1;
207 if ((!symbolic) && (!o.symbolic)) {
208 // non-symbolic, of equal type: compare values
209 if (value==o.value) {
212 return value<o.value ? -1 : 1;
214 if (symbolic && o.symbolic) {
215 // both symbolic: compare serials
216 if (serial==o.serial) {
219 return serial<o.serial ? -1 : 1;
221 // one symbolic, one value: value is sorted first
222 return o.symbolic ? -1 : 1;
225 bool idx::is_equal_same_type(basic const & other) const
227 ASSERT(is_of_type(other,idx));
228 idx const & o=static_cast<idx const &>
229 (const_cast<basic &>(other));
231 if (covariant!=o.covariant) return false;
232 if (symbolic!=o.symbolic) return false;
233 if (symbolic && o.symbolic) return serial==o.serial;
234 return value==o.value;
237 unsigned idx::calchash(void) const
239 hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
240 setflag(status_flags::hash_calculated);
245 // new virtual functions which can be overridden by derived classes
250 bool idx::is_co_contra_pair(basic const & other) const
252 // like is_equal_same_type(), but tests for different covariant status
253 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 bool idx::is_symbolic(void) const
268 unsigned idx::get_value(void) const
273 bool idx::is_covariant(void) const
278 ex idx::toggle_covariant(void) const
280 idx * i_copy=static_cast<idx *>(duplicate());
281 i_copy->covariant = !i_copy->covariant;
282 i_copy->clearflag(status_flags::hash_calculated);
283 return i_copy->setflag(status_flags::dynallocated);
287 // non-virtual functions in this class
293 // static member variables
298 unsigned idx::next_serial=0;
305 type_info const & typeid_idx=typeid(some_idx);
311 int canonicalize_indices(exvector & iv, bool antisymmetric)
314 // nothing do to for 0 or 1 indices
318 bool something_changed=false;
320 // simple bubble sort algorithm should be sufficient for the small number of indices needed
321 exvector::const_iterator last_idx=iv.end();
322 exvector::const_iterator next_to_last_idx=iv.end()-1;
323 for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
324 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
325 int cmpval=(*it1).compare(*it2);
328 something_changed=true;
329 if (antisymmetric) sig=-sig;
330 } else if ((cmpval==0) && antisymmetric) {
331 something_changed=true;
336 return something_changed ? sig : INT_MAX;
339 exvector idx_intersect(exvector const & iv1, exvector const & iv2)
341 // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
342 // assumes (but does not test) that each index occurs at most twice
343 exvector iv_intersect;
344 for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
345 ASSERT(is_ex_of_type(*cit1,idx));
346 if (ex_to_idx(*cit1).is_symbolic()) {
347 for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
348 ASSERT(is_ex_of_type(*cit2,idx));
349 if ((*cit1).is_equal(*cit2)) {
350 iv_intersect.push_back(*cit1);
359 #define TEST_PERMUTATION(A,B,C,P) \
360 if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
361 if (antisymmetric) *sig=P; \
365 ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
366 bool antisymmetric, int * sig)
368 // match (return value,iv2) to iv3 by permuting indices
369 // iv3 is always cyclic
371 ASSERT(iv3.size()==3);
372 ASSERT(iv2.size()==2);
376 TEST_PERMUTATION(0,1,2, 1);
377 TEST_PERMUTATION(0,2,1, -1);
378 TEST_PERMUTATION(1,0,2, -1);
379 TEST_PERMUTATION(1,2,0, 1);
380 TEST_PERMUTATION(2,0,1, 1);
381 TEST_PERMUTATION(2,1,0, -1);
382 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
385 unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
387 exvector::iterator it;
388 unsigned replacements=0;
389 unsigned current_replacements;
391 ASSERT(is_ex_of_type(is,idx));
392 ASSERT(is_ex_of_type(ir,idx));
394 for (it=v.begin(); it!=v.end(); ++it) {
395 current_replacements=count_index(*it,is);
396 if (current_replacements>0) {
397 (*it)=(*it).subs(is==ir);
399 replacements += current_replacements;
404 unsigned count_index(ex const & e, ex const & i)
406 exvector idxv=e.get_indices();
408 for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
409 if ((*cit).is_equal(i)) count++;
414 ex subs_indices(ex const & e, exvector const & idxv_subs,
415 exvector const & idxv_repl)
417 ASSERT(idxv_subs.size()==idxv_repl.size());
419 for (unsigned i=0; i<idxv_subs.size(); ++i) {
420 res=res.subs(idxv_subs[i]==idxv_repl[i]);