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