]> www.ginac.de Git - ginac.git/blob - ginac/idx.cpp
Hunted down some output bugs. Hope it can be safely piped into Maple now.
[ginac.git] / ginac / idx.cpp
1 /** @file idx.cpp
2  *
3  *  Implementation of GiNaC's indices. */
4
5 #include <stdexcept>
6
7 #include "ginac.h"
8 #include "utils.h"
9
10 //////////
11 // default constructor, destructor, copy constructor assignment operator and helpers
12 //////////
13
14 // public
15
16 idx::idx() : basic(TINFO_IDX), symbolic(true), covariant(false)
17 {
18     debugmsg("idx default constructor",LOGLEVEL_CONSTRUCT);
19     serial=next_serial++;
20     name="index"+ToString(serial);
21 }
22
23 idx::~idx() 
24 {
25     debugmsg("idx destructor",LOGLEVEL_DESTRUCT);
26     destroy(0);
27 }
28
29 idx::idx(idx const & other)
30 {
31     debugmsg("idx copy constructor",LOGLEVEL_CONSTRUCT);
32     copy(other);
33 }
34
35 idx const & idx::operator=(idx const & other)
36 {
37     debugmsg("idx operator=",LOGLEVEL_ASSIGNMENT);
38     if (this != &other) {
39         destroy(1);
40         copy(other);
41     }
42     return *this;
43 }
44
45 // protected
46
47 void idx::copy(idx const & other)
48 {
49     basic::copy(other);
50     serial=other.serial;
51     symbolic=other.symbolic;
52     name=other.name;
53     value=other.value;
54     covariant=other.covariant;
55 }
56
57 void idx::destroy(bool call_parent)
58 {
59     if (call_parent) basic::destroy(call_parent);
60 }
61
62 //////////
63 // other constructors
64 //////////
65
66 // public
67
68 idx::idx(bool cov) : basic(TINFO_IDX), symbolic(true), covariant(cov)
69 {
70     debugmsg("idx constructor from bool",LOGLEVEL_CONSTRUCT);
71     serial=next_serial++;
72     name="index"+ToString(serial);
73 }
74
75 idx::idx(string const & n, bool cov) : basic(TINFO_IDX),  
76     symbolic(true), name(n), covariant(cov)
77 {
78     debugmsg("idx constructor from string,bool",LOGLEVEL_CONSTRUCT);
79     serial=next_serial++;
80 }
81
82 idx::idx(char const * n, bool cov) : basic(TINFO_IDX),  
83     symbolic(true), name(n), covariant(cov)
84 {
85     debugmsg("idx constructor from char*,bool",LOGLEVEL_CONSTRUCT);
86     serial=next_serial++;
87 }
88
89 idx::idx(unsigned const v, bool cov) : basic(TINFO_IDX),
90     symbolic(false), value(v), covariant(cov)
91 {
92     debugmsg("idx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
93     serial=0;
94 }
95
96
97 //////////
98 // functions overriding virtual functions from bases classes
99 //////////
100
101 // public
102
103 basic * idx::duplicate() const
104 {
105     debugmsg("idx duplicate",LOGLEVEL_DUPLICATE);
106     return new idx(*this);
107 }
108
109 void idx::printraw(ostream & os) const
110 {
111     debugmsg("idx printraw",LOGLEVEL_PRINT);
112
113     os << "idx(";
114
115     if (symbolic) {
116         os << "symbolic,name=" << name;
117     } else {
118         os << "non symbolic,value=" << value;
119     }
120
121     if (covariant) {
122         os << ",covariant";
123     } else {
124         os << ",contravariant";
125     }
126
127     os << ",serial=" << serial;
128     os << ",hash=" << hashvalue << ",flags=" << flags;
129     os << ")";
130 }
131
132 void idx::printtree(ostream & os, unsigned indent) const
133 {
134     debugmsg("idx printtree",LOGLEVEL_PRINT);
135
136     os << string(indent,' ') << "idx: ";
137
138     if (symbolic) {
139         os << "symbolic,name=" << name;
140     } else {
141         os << "non symbolic,value=" << value;
142     }
143
144     if (covariant) {
145         os << ",covariant";
146     } else {
147         os << ",contravariant";
148     }
149
150     os << ", serial=" << serial
151        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
152        << ", flags=" << flags << endl;
153 }
154
155 void idx::print(ostream & os, unsigned upper_precedence) const
156 {
157     debugmsg("idx print",LOGLEVEL_PRINT);
158
159     if (covariant) {
160         os << "_";
161     } else {
162         os << "~";
163     }
164     if (symbolic) {
165         os << name;
166     } else {
167         os << value;
168     }
169 }
170
171 bool idx::info(unsigned inf) const
172 {
173     if (inf==info_flags::idx) return true;
174     return basic::info(inf);
175 }
176
177 ex idx::subs(lst const & ls, lst const & lr) const
178 {
179     ASSERT(ls.nops()==lr.nops());
180 #ifdef DOASSERT
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));
184     }
185 #endif // def DOASSERT
186
187     for (int i=0; i<ls.nops(); i++) {
188         if (is_equal(*(ls.op(i)).bp)) {
189             return lr.op(i);
190         }
191     }
192     return *this;
193 }
194
195 // protected
196
197 int idx::compare_same_type(basic const & other) const
198 {
199     ASSERT(is_of_type(other,idx));
200     idx const & o=static_cast<idx const &>
201                              (const_cast<basic &>(other));
202
203     if (covariant!=o.covariant) {
204         // different co/contravariant
205         return covariant ? -1 : 1;
206     }
207     if ((!symbolic) && (!o.symbolic)) {
208         // non-symbolic, of equal type: compare values
209         if (value==o.value) {
210             return 0;
211         }
212         return value<o.value ? -1 : 1;
213     }
214     if (symbolic && o.symbolic) {
215         // both symbolic: compare serials
216         if (serial==o.serial) {
217             return 0;
218         }
219         return serial<o.serial ? -1 : 1;
220     }
221     // one symbolic, one value: value is sorted first
222     return o.symbolic ? -1 : 1;
223 }
224
225 bool idx::is_equal_same_type(basic const & other) const
226 {
227     ASSERT(is_of_type(other,idx));
228     idx const & o=static_cast<idx const &>
229                              (const_cast<basic &>(other));
230
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;
235 }    
236
237 unsigned idx::calchash(void) const
238 {
239     hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
240     setflag(status_flags::hash_calculated);
241     return hashvalue;
242 }
243
244 //////////
245 // new virtual functions which can be overridden by derived classes
246 //////////
247
248 // public
249
250 bool idx::is_co_contra_pair(basic const & other) const
251 {
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));
256
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;
261 }    
262
263 bool idx::is_symbolic(void) const
264 {
265     return symbolic;
266 }
267
268 unsigned idx::get_value(void) const
269 {
270     return value;
271 }
272
273 bool idx::is_covariant(void) const
274 {
275     return covariant;
276 }
277
278 ex idx::toggle_covariant(void) const
279 {
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);
284 }
285
286 //////////
287 // non-virtual functions in this class
288 //////////
289
290 // none
291
292 //////////
293 // static member variables
294 //////////
295
296 // protected
297
298 unsigned idx::next_serial=0;
299
300 //////////
301 // global constants
302 //////////
303
304 const idx some_idx;
305 type_info const & typeid_idx=typeid(some_idx);
306
307 //////////
308 // other functions
309 //////////
310
311 int canonicalize_indices(exvector & iv, bool antisymmetric)
312 {
313     if (iv.size()<2) {
314         // nothing do to for 0 or 1 indices
315         return INT_MAX;
316     }
317
318     bool something_changed=false;
319     int sig=1;
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);
326             if (cmpval==1) {
327                 iter_swap(it1,it2);
328                 something_changed=true;
329                 if (antisymmetric) sig=-sig;
330             } else if ((cmpval==0) && antisymmetric) {
331                 something_changed=true;
332                 sig=0;
333             }
334         }
335     }
336     return something_changed ? sig : INT_MAX;
337 }
338
339 exvector idx_intersect(exvector const & iv1, exvector const & iv2)
340 {
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);
351                     break;
352                 }
353             }
354         }
355     }
356     return iv_intersect;
357 }
358
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; \
362         return iv3[A]; \
363     }
364
365 ex permute_free_index_to_front(exvector const & iv3, exvector const & iv2,
366                                bool antisymmetric, int * sig)
367 {
368     // match (return value,iv2) to iv3 by permuting indices
369     // iv3 is always cyclic
370
371     ASSERT(iv3.size()==3);
372     ASSERT(iv2.size()==2);
373
374     *sig=1;
375     
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"));
383 }
384     
385 unsigned subs_index_in_exvector(exvector & v, ex const & is, ex const & ir)
386 {
387     exvector::iterator it;
388     unsigned replacements=0;
389     unsigned current_replacements;
390
391     ASSERT(is_ex_of_type(is,idx));
392     ASSERT(is_ex_of_type(ir,idx));
393    
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);
398         }
399         replacements += current_replacements;
400     }
401     return replacements;
402 }
403
404 unsigned count_index(ex const & e, ex const & i)
405 {
406     exvector idxv=e.get_indices();
407     unsigned count=0;
408     for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
409         if ((*cit).is_equal(i)) count++;
410     }
411     return count;
412 }
413
414 ex subs_indices(ex const & e, exvector const & idxv_subs,
415                 exvector const & idxv_repl)
416 {
417     ASSERT(idxv_subs.size()==idxv_repl.size());
418     ex res=e;
419     for (unsigned i=0; i<idxv_subs.size(); ++i) {
420         res=res.subs(idxv_subs[i]==idxv_repl[i]);
421     }
422     return res;
423 }
424
425
426
427