]> www.ginac.de Git - ginac.git/blob - ginac/lortensor.cpp
6c326e2b01af3dfe4fe1d4b168352802b700814d
[ginac.git] / ginac / lortensor.cpp
1 /** @file lortensor.cpp
2  *
3  *  Implementation of GiNaC's Lorentz tensors. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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 <string>
24 #include <list>
25 #include <algorithm>
26 #include <iostream>
27 #include <stdexcept>
28 #include <map>
29
30 #include "basic.h"
31 #include "add.h"
32 #include "mul.h"
33 #include "debugmsg.h"
34 #include "flags.h"
35 #include "lst.h"
36 #include "lortensor.h"
37 #include "operators.h"
38 #include "tinfos.h"
39 #include "power.h"
40 #include "archive.h"
41 #include "utils.h"
42 #include "config.h"
43
44 #ifndef NO_NAMESPACE_GINAC
45 namespace GiNaC {
46 #endif // ndef NO_NAMESPACE_GINAC
47
48 GINAC_IMPLEMENT_REGISTERED_CLASS(lortensor, indexed)
49
50 //////////
51 // default constructor, destructor, copy constructor assignment operator and helpers
52 //////////
53
54 // public
55
56 lortensor::lortensor() : inherited(TINFO_lortensor), type(invalid)
57 {
58         debugmsg("lortensor default constructor",LOGLEVEL_CONSTRUCT);
59         serial=next_serial++;
60         name=autoname_prefix()+ToString(serial);
61 }
62
63 //protected
64
65 void lortensor::copy(const lortensor & other)
66 {
67         inherited::copy(other);
68         type=other.type;
69         name=other.name;
70         serial=other.serial;
71 }
72
73 void lortensor::destroy(bool call_parent)
74 {
75         if (call_parent) inherited::destroy(call_parent);
76 }
77
78 //////////
79 // other constructors
80 //////////
81
82 // protected
83
84 /** Construct object without any Lorentz index. This constructor is for
85  *  internal use only. */
86 lortensor::lortensor(lortensor_types const lt, const std::string & n) : type(lt), name(n)
87 {
88         debugmsg("lortensor constructor from lortensor_types,string",LOGLEVEL_CONSTRUCT);
89         if (lt == lortensor_symbolic)
90                 serial = next_serial++;
91         else
92                 serial = 0;
93         tinfo_key = TINFO_lortensor;
94 }
95
96 /** Construct object with one Lorentz index. This constructor is for
97  *  internal use only. Use the lortensor_vector() or lortensor_symbolic()
98  *  functions instead.
99  *  @see lortensor_vector
100  *  @see lortensor_symbolic */
101 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu) : inherited(mu), type(lt), name(n)
102 {
103         debugmsg("lortensor constructor from lortensor_types,string,ex",LOGLEVEL_CONSTRUCT);
104         GINAC_ASSERT(all_of_type_lorentzidx());
105         if (lt == lortensor_symbolic)
106                 serial = next_serial++;
107         else
108                 serial = 0;
109         tinfo_key=TINFO_lortensor;
110 }
111
112 /** Construct object with two Lorentz indices. This constructor is for
113  *  internal use only. Use the lortensor_g(), lortensor_delta() or
114  *  lortensor_symbolic() functions instead.
115  *  @see lortensor_g
116  *  @see lortensor_delta
117  *  @see lortensor_symbolic */
118 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu, const ex & nu) : inherited(mu,nu), type(lt), name(n)
119 {
120         debugmsg("lortensor constructor from lortensor_types,string,ex,ex",LOGLEVEL_CONSTRUCT);
121         GINAC_ASSERT(all_of_type_lorentzidx());
122         if (lt == lortensor_symbolic)
123                 serial = next_serial++;
124         else
125                 serial = 0;
126         tinfo_key=TINFO_lortensor;
127 }
128
129 /** Construct object with three Lorentz indices. This constructor is for
130  *  internal use only. Use the lortensor_symbolic() function instead.
131  *  @see lortensor_symbolic */
132 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu, const ex & nu, const ex & rho) : inherited(mu,nu,rho), type(lt), name(n)
133 {
134         debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex",LOGLEVEL_CONSTRUCT);
135         GINAC_ASSERT(all_of_type_lorentzidx());
136         if (lt == lortensor_symbolic)
137                 serial = next_serial++;
138         else
139                 serial = 0;
140         tinfo_key=TINFO_lortensor;
141 }
142
143 /** Construct object with four Lorentz indices. This constructor is for
144  *  internal use only. Use the lortensor_epsilon() or lortensor_symbolic()
145  *  functions instead.
146  *  @see lortensor_epsilon
147  *  @see lortensor_symbolic */
148 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu, const ex & nu, const ex & rho, const ex & sigma) : inherited(mu,nu,rho,sigma), type(lt), name(n)
149 {
150         debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
151         GINAC_ASSERT(all_of_type_lorentzidx());
152         if (lt == lortensor_symbolic)
153                 serial = next_serial++;
154         else
155                 serial = 0;
156         tinfo_key=TINFO_lortensor;
157 }
158
159 /** Construct object with arbitrary number of Lorentz indices. This
160  *  constructor is for internal use only. Use the lortensor_symbolic()
161  *  function instead.
162  *
163  *  @see lortensor_symbolic */
164 lortensor::lortensor(lortensor_types const lt, const std::string & n, const exvector & iv) : inherited(iv), type(lt), name(n)
165 {
166         debugmsg("lortensor constructor from lortensor_types,string,exvector",LOGLEVEL_CONSTRUCT);
167         GINAC_ASSERT(all_of_type_lorentzidx());
168         if (lt == lortensor_symbolic)
169                 serial = next_serial++;
170         else
171                 serial = 0;
172         tinfo_key=TINFO_lortensor;
173 }
174
175 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, const exvector & iv) : indexed(iv), type(lt), name(n), serial(s)
176 {
177         debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
178         GINAC_ASSERT(all_of_type_lorentzidx());
179         tinfo_key=TINFO_lortensor;
180 }
181
182 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, exvector *ivp) : indexed(ivp), type(lt), name(n), serial(s)
183 {
184         debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
185         GINAC_ASSERT(all_of_type_lorentzidx());
186         tinfo_key=TINFO_lortensor;
187 }
188
189
190 //////////
191 // archiving
192 //////////
193
194 /** Construct object from archive_node. */
195 lortensor::lortensor(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
196 {
197         debugmsg("lortensor constructor from archive_node", LOGLEVEL_CONSTRUCT);
198         unsigned int ty;
199         if (!(n.find_unsigned("type", ty)))
200                 throw (std::runtime_error("unknown lortensor type in archive"));
201         type = (lortensor_types)ty;
202         if (type == lortensor_symbolic) {
203                 serial = next_serial++;
204                 if (!(n.find_string("name", name)))
205                         name = autoname_prefix() + ToString(serial);
206         } else
207                 serial = 0;
208 }
209
210 /** Unarchive the object. */
211 ex lortensor::unarchive(const archive_node &n, const lst &sym_lst)
212 {
213         ex s = (new lortensor(n, sym_lst))->setflag(status_flags::dynallocated);
214
215         if (ex_to_lortensor(s).type == lortensor_symbolic) {
216                 // If lortensor is in sym_lst, return the existing lortensor
217                 for (unsigned i=0; i<sym_lst.nops(); i++) {
218                         if (is_ex_of_type(sym_lst.op(i), lortensor) && (ex_to_lortensor(sym_lst.op(i)).name == ex_to_lortensor(s).name))
219                                 return sym_lst.op(i);
220                 }
221         }
222         return s;
223 }
224
225 /** Archive the object. */
226 void lortensor::archive(archive_node &n) const
227 {
228         inherited::archive(n);
229         n.add_unsigned("type", type);
230         if (type == lortensor_symbolic)
231                 n.add_string("name", name);
232 }
233
234
235 //////////
236 // functions overriding virtual functions from bases classes
237 //////////
238
239 //public
240
241 void lortensor::printraw(std::ostream & os) const
242 {
243         debugmsg("lortensor printraw",LOGLEVEL_PRINT);
244         os << "lortensor(type=" << (unsigned)type
245            << ",indices=";
246         printrawindices(os);
247         os << ",serial=" << serial;
248         os << ",hash=" << hashvalue << ",flags=" << flags << ")";
249 }
250
251 void lortensor::printtree(std::ostream & os, unsigned indent) const
252 {
253         debugmsg("lortensor printtree",LOGLEVEL_PRINT);
254         os << std::string(indent,' ') <<"lortensor object: "
255            << "type=" << (unsigned)type << ","
256            << seq.size() << " indices" << std::endl;
257         printtreeindices(os,indent);
258         os << std::string(indent,' ') << "hash=" << hashvalue
259            << " (0x" << std::hex << hashvalue << std::dec << ")"
260            << ", flags=" << flags << std::endl;
261 }
262
263 void lortensor::print(std::ostream & os, unsigned upper_precedence) const
264 {
265         debugmsg("lortensor print",LOGLEVEL_PRINT);
266         switch (type) {
267         case lortensor_g:
268                 os << "g";
269                 break;
270         case lortensor_delta:
271                 os << "delta";
272                 break;
273         case lortensor_epsilon:
274                 os << "epsilon";
275                 break;
276         case lortensor_symbolic:
277                 os << name;
278                 break;
279         case invalid:
280         default:
281                 os << "INVALID_LORTENSOR_OBJECT";
282                 break;
283         }
284         printindices(os);
285 }
286
287 bool lortensor::info(unsigned inf) const
288 {
289         return inherited::info(inf);
290 }
291
292 ex lortensor::eval(int level) const
293 {
294         if (type==lortensor_g) {
295                 // canonicalize indices
296                 exvector iv=seq;
297                 int sig=canonicalize_indices(iv,false); //symmetric
298                 if (sig!=INT_MAX) {
299                         //something has changed while sorting indices, more evaluations later
300                         return ex(sig) *lortensor(type,name,iv);
301                 }
302                 const lorentzidx & idx1=ex_to_lorentzidx(seq[0]);
303                 const lorentzidx & idx2=ex_to_lorentzidx(seq[1]);
304                 if ((!idx1.is_symbolic()) && (!idx2.is_symbolic())) {
305                         //both indices are numeric
306                         if ((idx1.get_value()==idx2.get_value())) {
307                                 //both on diagonal
308                                 if (idx1.get_value()==0){
309                                         // (0,0)
310                                         return _ex1();
311                                 } else {
312                                         if (idx1.is_covariant() != idx2.is_covariant()) {
313                                                 // (_i,~i) or (~i,_i), i = 1...3
314                                                 return _ex1();
315                                         } else {
316                                                 // (_i,_i) or (~i,~i), i= 1...3
317                                                 return _ex_1();
318                                         }
319                                 }
320                         } else {
321                                 // at least one off-diagonal
322                                 return _ex0();
323                         }
324                 } else if (idx1.is_symbolic() && idx1.is_co_contra_pair(idx2)) {
325                         return Dim();
326                 }
327         }
328         return this -> hold();
329 }
330
331 //protected
332
333 int lortensor::compare_same_type(const basic & other) const
334 {
335         GINAC_ASSERT(is_of_type(other,lortensor));
336         const lortensor &o = static_cast<const lortensor &>(other);
337
338         if (type!=o.type) {
339                 // different type
340                 return type < o.type ? -1 : 1;
341         }
342
343         if (type == lortensor_symbolic) {
344                 // symbolic, compare serials
345                 if (serial != o.serial) {
346                         return serial < o.serial ? -1 : 1;
347                 }
348         }
349
350         return inherited::compare_same_type(other);
351 }
352
353 bool lortensor::is_equal_same_type(const basic & other) const
354 {
355         GINAC_ASSERT(is_of_type(other,lortensor));
356         const lortensor &o = static_cast<const lortensor &>(other);
357
358         if (type != o.type) return false;
359         if (type == lortensor_symbolic && serial != o.serial) return false;
360         return inherited::is_equal_same_type(other);            
361 }
362
363 unsigned lortensor::return_type(void) const
364 {
365         return return_types::commutative;
366 }
367
368 unsigned lortensor::return_type_tinfo(void) const
369 {
370         return tinfo_key;
371 }
372
373 ex lortensor::thisexprseq(const exvector & v) const
374 {
375         return lortensor(type,name,serial,v);
376 }
377
378 ex lortensor::thisexprseq(exvector *vp) const
379 {
380         return lortensor(type,name,serial,vp);
381 }
382         
383 //////////
384 // non-virtual functions in this class
385 //////////
386
387 // protected
388
389 /** Check whether all indices are of class lorentzidx or a subclass. This
390  *  function is used internally to make sure that all constructed Lorentz
391  *  tensors really carry Lorentz indices and not some other classes. */
392 bool lortensor::all_of_type_lorentzidx(void) const
393 {
394         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++ cit) {
395                 if (!is_ex_of_type(*cit,lorentzidx)) return false;
396         }
397         return true;
398 }
399
400 // private
401
402 std::string & lortensor::autoname_prefix(void)
403 {
404         static std::string * s = new std::string("lortensor");
405         return *s;
406 }
407
408 //////////
409 // static member variables
410 //////////
411
412 // private
413
414 unsigned lortensor::next_serial=0;
415
416 //////////
417 // friend functions
418 //////////
419
420 /** Construct an object representing the metric tensor g. The indices must
421  *  be of class lorentzidx.
422  *
423  *  @param mu First index
424  *  @param nu Second index
425  *  @return newly constructed object */
426 lortensor lortensor_g(const ex & mu, const ex & nu)
427 {
428         return lortensor(lortensor::lortensor_g,"",mu,nu);
429 }
430
431 /** Construct an object representing the unity matrix delta. The indices
432  *  must be of class lorentzidx.
433  *
434  *  @param mu First index
435  *  @param nu Second index
436  *  @return newly constructed object */
437 lortensor lortensor_delta(const ex & mu, const ex & nu)
438 {
439         return lortensor(lortensor::lortensor_delta,"",mu,nu);
440 }
441
442 /** Construct an object representing the four-dimensional totally
443  *  antisymmetric tensor epsilon. The indices must be of class lorentzidx.
444  *
445  *  @param mu First index
446  *  @param nu Second index
447  *  @param rho Third index
448  *  @param sigma Fourth index
449  *  @return newly constructed object */
450 lortensor lortensor_epsilon(const ex & mu, const ex & nu, const ex & rho, const ex & sigma)
451 {
452         return lortensor(lortensor::lortensor_epsilon,"",mu,nu,rho,sigma);
453 }
454
455 /** Construct an object representing a symbolic Lorentz vector. The index
456  *  must be of class lorentzidx.
457  *
458  *  @param n Symbolic name
459  *  @param mu Index
460  *  @return newly constructed object */
461 lortensor lortensor_vector(const std::string & n, const ex & mu)
462 {
463         return lortensor(lortensor::lortensor_symbolic,n,mu);
464 }
465
466 /** Construct an object representing a symbolic Lorentz tensor of arbitrary
467  *  rank. The indices must be of class lorentzidx.
468  *
469  *  @param n Symbolic name
470  *  @param iv Vector of indices
471  *  @return newly constructed object */
472 lortensor lortensor_symbolic(const std::string & n, const exvector & iv)
473 {
474         return lortensor(lortensor::lortensor_symbolic,n,iv);
475 }
476
477 ex simplify_lortensor_mul(const ex & m)
478 {
479         GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
480         exvector v_contracted;
481
482         // collect factors in an exvector, store squares twice
483         int n=m.nops();
484         v_contracted.reserve(2*n);
485         for (int i=0; i<n; ++i) {
486                 ex f=m.op(i);
487                 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
488                         v_contracted.push_back(f.op(0));
489                         v_contracted.push_back(f.op(0));
490                 } else {
491                         v_contracted.push_back(f);
492                 }
493         }
494
495         unsigned replacements;
496         bool something_changed=false;
497
498         exvector::iterator it=v_contracted.begin();
499         while (it!=v_contracted.end()) {
500                 // process only lor_g objects
501                 if (is_ex_exactly_of_type(*it,lortensor) &&
502                         (ex_to_lortensor(*it).type==lortensor::lortensor_g)) {            
503                         const lortensor & g=ex_to_lortensor(*it);
504                         GINAC_ASSERT(g.seq.size()==2);
505                         const idx & first_idx=ex_to_lorentzidx(g.seq[0]);
506                         const idx & second_idx=ex_to_lorentzidx(g.seq[1]);
507                         // g_{mu,mu} should have been contracted in lortensor::eval()
508                         GINAC_ASSERT(!first_idx.is_equal(second_idx));
509                         ex saved_g=*it; // save to restore it later
510
511                         // try to contract first index
512                         replacements=0;
513                         if (first_idx.is_symbolic()) {
514                                 replacements = subs_index_in_exvector(v_contracted,
515                                                                       first_idx.toggle_covariant(),second_idx);
516                                 if (replacements==0) {
517                                         // not contracted, restore g object
518                                         *it=saved_g;
519                                 } else {
520                                         // a contracted index should occur exactly once
521                                         GINAC_ASSERT(replacements==1);
522                                         *it=_ex1();
523                                         something_changed=true;
524                                 }
525                         }
526
527                         // try second index only if first was not contracted
528                         if ((replacements==0)&&(second_idx.is_symbolic())) {
529                                 // first index not contracted, *it is again the original g object
530                                 replacements = subs_index_in_exvector(v_contracted,
531                                                                       second_idx.toggle_covariant(),first_idx);
532                                 if (replacements==0) {
533                                         // not contracted except in itself, restore g object
534                                         *it=saved_g;
535                                 } else {
536                                         // a contracted index should occur exactly once
537                                         GINAC_ASSERT(replacements==1);
538                                         *it=_ex1();
539                                         something_changed=true;
540                                 }
541                         }
542                 }
543                 ++it;
544         }
545         if (something_changed) {
546                 return mul(v_contracted);
547         }
548         return m;
549 }
550
551 /** Perform some simplifications on an expression containing Lorentz tensors. */
552 ex simplify_lortensor(const ex & e)
553 {
554         // all simplification is done on expanded objects
555         ex e_expanded=e.expand();
556
557         // simplification of sum=sum of simplifications
558         if (is_ex_exactly_of_type(e_expanded,add)) {
559                 ex sum=_ex0();
560                 for (unsigned i=0; i<e_expanded.nops(); ++i) {
561                         sum += simplify_lortensor(e_expanded.op(i));
562                 }
563                 return sum;
564         }
565
566         // simplification of (commutative) product
567         if (is_ex_exactly_of_type(e_expanded,mul)) {
568                 return simplify_lortensor_mul(e);
569         }
570
571         // cannot do anything
572         return e_expanded;
573 }
574
575 #ifndef NO_NAMESPACE_GINAC
576 } // namespace GiNaC
577 #endif // ndef NO_NAMESPACE_GINAC