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