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