]> www.ginac.de Git - ginac.git/blob - ginac/idx.cpp
- fixes to the indentation
[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 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),  
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(std::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(std::ostream & os, unsigned indent) const
208 {
209         debugmsg("idx printtree",LOGLEVEL_PRINT);
210
211         os << std::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
227            << " (0x" << std::hex << hashvalue << std::dec << ")"
228            << ", flags=" << flags << std::endl;
229 }
230
231 void idx::print(std::ostream & os, unsigned upper_precedence) const
232 {
233         debugmsg("idx print",LOGLEVEL_PRINT);
234
235         if (covariant) {
236                 os << "_";
237         } else {
238                 os << "~";
239         }
240         if (symbolic) {
241                 os << name;
242         } else {
243                 os << value;
244         }
245 }
246
247 bool idx::info(unsigned inf) const
248 {
249         if (inf==info_flags::idx) return true;
250         return inherited::info(inf);
251 }
252
253 ex idx::subs(const lst & ls, const lst & lr) const
254 {
255         GINAC_ASSERT(ls.nops()==lr.nops());
256 #ifdef DO_GINAC_ASSERT
257         for (unsigned i=0; i<ls.nops(); i++) {
258                 GINAC_ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
259                            is_ex_of_type(ls.op(i),idx));
260         }
261 #endif // def DO_GINAC_ASSERT
262
263         for (unsigned i=0; i<ls.nops(); i++) {
264                 if (is_equal(*(ls.op(i)).bp)) {
265                         return lr.op(i);
266                 }
267         }
268         return *this;
269 }
270
271 // protected
272
273 int idx::compare_same_type(const basic & other) const
274 {
275         GINAC_ASSERT(is_of_type(other,idx));
276         const idx & o=static_cast<const idx &>
277                                                          (const_cast<basic &>(other));
278
279         if (covariant!=o.covariant) {
280                 // different co/contravariant
281                 return covariant ? -1 : 1;
282         }
283         if ((!symbolic) && (!o.symbolic)) {
284                 // non-symbolic, of equal type: compare values
285                 if (value==o.value) {
286                         return 0;
287                 }
288                 return value<o.value ? -1 : 1;
289         }
290         if (symbolic && o.symbolic) {
291                 // both symbolic: compare serials
292                 if (serial==o.serial) {
293                         return 0;
294                 }
295                 return serial<o.serial ? -1 : 1;
296         }
297         // one symbolic, one value: value is sorted first
298         return o.symbolic ? -1 : 1;
299 }
300
301 bool idx::is_equal_same_type(const basic & other) const
302 {
303         GINAC_ASSERT(is_of_type(other,idx));
304         const idx & o=static_cast<const idx &>
305                                                          (const_cast<basic &>(other));
306
307         if (covariant!=o.covariant) return false;
308         if (symbolic!=o.symbolic) return false;
309         if (symbolic && o.symbolic) return serial==o.serial;
310         return value==o.value;
311 }    
312
313 unsigned idx::calchash(void) const
314 {
315         hashvalue=golden_ratio_hash(golden_ratio_hash(tinfo_key ^ serial));
316         setflag(status_flags::hash_calculated);
317         return hashvalue;
318 }
319
320 //////////
321 // new virtual functions which can be overridden by derived classes
322 //////////
323
324 // public
325
326 bool idx::is_co_contra_pair(const basic & other) const
327 {
328         // like is_equal_same_type(), but tests for different covariant status
329         GINAC_ASSERT(is_of_type(other,idx));
330         const idx & o=static_cast<const idx &>
331                                                          (const_cast<basic &>(other));
332
333         if (covariant==o.covariant) return false;
334         if (symbolic!=o.symbolic) return false;
335         if (symbolic && o.symbolic) return serial==o.serial;
336         return value==o.value;
337 }    
338
339 bool idx::is_symbolic(void) const
340 {
341         return symbolic;
342 }
343
344 unsigned idx::get_value(void) const
345 {
346         return value;
347 }
348
349 bool idx::is_covariant(void) const
350 {
351         return covariant;
352 }
353
354 ex idx::toggle_covariant(void) const
355 {
356         idx * i_copy=static_cast<idx *>(duplicate());
357         i_copy->covariant = !i_copy->covariant;
358         i_copy->clearflag(status_flags::hash_calculated);
359         return i_copy->setflag(status_flags::dynallocated);
360 }
361
362 //////////
363 // non-virtual functions in this class
364 //////////
365
366 // none
367
368 //////////
369 // static member variables
370 //////////
371
372 // protected
373
374 unsigned idx::next_serial=0;
375
376 //////////
377 // global constants
378 //////////
379
380 const idx some_idx;
381 const type_info & typeid_idx=typeid(some_idx);
382
383 //////////
384 // other functions
385 //////////
386
387 int canonicalize_indices(exvector & iv, bool antisymmetric)
388 {
389         if (iv.size()<2) {
390                 // nothing do to for 0 or 1 indices
391                 return INT_MAX;
392         }
393
394         bool something_changed=false;
395         int sig=1;
396         // simple bubble sort algorithm should be sufficient for the small number of indices needed
397         exvector::const_iterator last_idx=iv.end();
398         exvector::const_iterator next_to_last_idx=iv.end()-1;
399         for (exvector::iterator it1=iv.begin(); it1!=next_to_last_idx; ++it1) {
400                 for (exvector::iterator it2=it1+1; it2!=last_idx; ++it2) {
401                         int cmpval=(*it1).compare(*it2);
402                         if (cmpval==1) {
403                                 iter_swap(it1,it2);
404                                 something_changed=true;
405                                 if (antisymmetric) sig=-sig;
406                         } else if ((cmpval==0) && antisymmetric) {
407                                 something_changed=true;
408                                 sig=0;
409                         }
410                 }
411         }
412         return something_changed ? sig : INT_MAX;
413 }
414
415 exvector idx_intersect(const exvector & iv1, const exvector & iv2)
416 {
417         // build a vector of symbolic indices contained in iv1 and iv2 simultaneously
418         // assumes (but does not test) that each index occurs at most twice
419         exvector iv_intersect;
420         for (exvector::const_iterator cit1=iv1.begin(); cit1!=iv1.end(); ++cit1) {
421                 GINAC_ASSERT(is_ex_of_type(*cit1,idx));
422                 if (ex_to_idx(*cit1).is_symbolic()) {
423                         for (exvector::const_iterator cit2=iv2.begin(); cit2!=iv2.end(); ++cit2) {
424                                 GINAC_ASSERT(is_ex_of_type(*cit2,idx));
425                                 if ((*cit1).is_equal(*cit2)) {
426                                         iv_intersect.push_back(*cit1);
427                                         break;
428                                 }
429                         }
430                 }
431         }
432         return iv_intersect;
433 }
434
435 #define TEST_PERMUTATION(A,B,C,P) \
436         if ((iv3[B].is_equal(iv2[0]))&&(iv3[C].is_equal(iv2[1]))) { \
437                 if (antisymmetric) *sig=P; \
438                 return iv3[A]; \
439         }
440
441 ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2,
442                                                            bool antisymmetric, int * sig)
443 {
444         // match (return value,iv2) to iv3 by permuting indices
445         // iv3 is always cyclic
446
447         GINAC_ASSERT(iv3.size()==3);
448         GINAC_ASSERT(iv2.size()==2);
449
450         *sig=1;
451         
452         TEST_PERMUTATION(0,1,2,  1);
453         TEST_PERMUTATION(0,2,1, -1);
454         TEST_PERMUTATION(1,0,2, -1);
455         TEST_PERMUTATION(1,2,0,  1);
456         TEST_PERMUTATION(2,0,1,  1);
457         TEST_PERMUTATION(2,1,0, -1);
458         throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
459 }
460         
461 unsigned subs_index_in_exvector(exvector & v, const ex & is, const ex & ir)
462 {
463         exvector::iterator it;
464         unsigned replacements=0;
465         unsigned current_replacements;
466
467         GINAC_ASSERT(is_ex_of_type(is,idx));
468         GINAC_ASSERT(is_ex_of_type(ir,idx));
469    
470         for (it=v.begin(); it!=v.end(); ++it) {
471                 current_replacements=count_index(*it,is);
472                 if (current_replacements>0) {
473                         (*it)=(*it).subs(is==ir);
474                 }
475                 replacements += current_replacements;
476         }
477         return replacements;
478 }
479
480 unsigned count_index(const ex & e, const ex & i)
481 {
482         exvector idxv=e.get_indices();
483         unsigned count=0;
484         for (exvector::const_iterator cit=idxv.begin(); cit!=idxv.end(); ++cit) {
485                 if ((*cit).is_equal(i)) count++;
486         }
487         return count;
488 }
489
490 ex subs_indices(const ex & e, const exvector & idxv_subs,
491                                 const exvector & idxv_repl)
492 {
493         GINAC_ASSERT(idxv_subs.size()==idxv_repl.size());
494         ex res=e;
495         for (unsigned i=0; i<idxv_subs.size(); ++i) {
496                 res=res.subs(idxv_subs[i]==idxv_repl[i]);
497         }
498         return res;
499 }
500
501 #ifndef NO_NAMESPACE_GINAC
502 } // namespace GiNaC
503 #endif // ndef NO_NAMESPACE_GINAC