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