1 /** @file lortensor.cpp
3 * Implementation of GiNaC's Lorentz tensors. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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
35 #include "lortensor.h"
36 #include "operators.h"
43 #ifndef NO_NAMESPACE_GINAC
45 #endif // ndef NO_NAMESPACE_GINAC
47 GINAC_IMPLEMENT_REGISTERED_CLASS(lortensor, indexed)
50 // default constructor, destructor, copy constructor assignment operator and helpers
55 lortensor::lortensor() : inherited(TINFO_lortensor), type(invalid)
57 debugmsg("lortensor default constructor",LOGLEVEL_CONSTRUCT);
59 name=autoname_prefix()+ToString(serial);
64 void lortensor::copy(const lortensor & other)
66 inherited::copy(other);
72 void lortensor::destroy(bool call_parent)
74 if (call_parent) inherited::destroy(call_parent);
83 /** Construct object without any Lorentz index. This constructor is for
84 * internal use only. */
85 lortensor::lortensor(lortensor_types const lt, const std::string & n) : type(lt), name(n)
87 debugmsg("lortensor constructor from lortensor_types,string",LOGLEVEL_CONSTRUCT);
88 if (lt == lortensor_symbolic)
89 serial = next_serial++;
92 tinfo_key = TINFO_lortensor;
95 /** Construct object with one Lorentz index. This constructor is for
96 * internal use only. Use the lortensor_vector() or lortensor_symbolic()
98 * @see lortensor_vector
99 * @see lortensor_symbolic */
100 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu) : inherited(mu), type(lt), name(n)
102 debugmsg("lortensor constructor from lortensor_types,string,ex",LOGLEVEL_CONSTRUCT);
103 GINAC_ASSERT(all_of_type_lorentzidx());
104 if (lt == lortensor_symbolic)
105 serial = next_serial++;
108 tinfo_key=TINFO_lortensor;
111 /** Construct object with two Lorentz indices. This constructor is for
112 * internal use only. Use the lortensor_g(), lortensor_delta() or
113 * lortensor_symbolic() functions instead.
115 * @see lortensor_delta
116 * @see lortensor_symbolic */
117 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu, const ex & nu) : inherited(mu,nu), type(lt), name(n)
119 debugmsg("lortensor constructor from lortensor_types,string,ex,ex",LOGLEVEL_CONSTRUCT);
120 GINAC_ASSERT(all_of_type_lorentzidx());
121 if (lt == lortensor_symbolic)
122 serial = next_serial++;
125 tinfo_key=TINFO_lortensor;
128 /** Construct object with three Lorentz indices. This constructor is for
129 * internal use only. Use the lortensor_symbolic() function instead.
130 * @see lortensor_symbolic */
131 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 debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex",LOGLEVEL_CONSTRUCT);
134 GINAC_ASSERT(all_of_type_lorentzidx());
135 if (lt == lortensor_symbolic)
136 serial = next_serial++;
139 tinfo_key=TINFO_lortensor;
142 /** Construct object with four Lorentz indices. This constructor is for
143 * internal use only. Use the lortensor_epsilon() or lortensor_symbolic()
145 * @see lortensor_epsilon
146 * @see lortensor_symbolic */
147 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 debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
150 GINAC_ASSERT(all_of_type_lorentzidx());
151 if (lt == lortensor_symbolic)
152 serial = next_serial++;
155 tinfo_key=TINFO_lortensor;
158 /** Construct object with arbitrary number of Lorentz indices. This
159 * constructor is for internal use only. Use the lortensor_symbolic()
162 * @see lortensor_symbolic */
163 lortensor::lortensor(lortensor_types const lt, const std::string & n, const exvector & iv) : inherited(iv), type(lt), name(n)
165 debugmsg("lortensor constructor from lortensor_types,string,exvector",LOGLEVEL_CONSTRUCT);
166 GINAC_ASSERT(all_of_type_lorentzidx());
167 if (lt == lortensor_symbolic)
168 serial = next_serial++;
171 tinfo_key=TINFO_lortensor;
174 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, const exvector & iv) : indexed(iv), type(lt), name(n), serial(s)
176 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
177 GINAC_ASSERT(all_of_type_lorentzidx());
178 tinfo_key=TINFO_lortensor;
181 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, exvector *ivp) : indexed(ivp), type(lt), name(n), serial(s)
183 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
184 GINAC_ASSERT(all_of_type_lorentzidx());
185 tinfo_key=TINFO_lortensor;
193 /** Construct object from archive_node. */
194 lortensor::lortensor(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
196 debugmsg("lortensor constructor from archive_node", LOGLEVEL_CONSTRUCT);
198 if (!(n.find_unsigned("type", ty)))
199 throw (std::runtime_error("unknown lortensor type in archive"));
200 type = (lortensor_types)ty;
201 if (type == lortensor_symbolic) {
202 serial = next_serial++;
203 if (!(n.find_string("name", name)))
204 name = autoname_prefix() + ToString(serial);
209 /** Unarchive the object. */
210 ex lortensor::unarchive(const archive_node &n, const lst &sym_lst)
212 ex s = (new lortensor(n, sym_lst))->setflag(status_flags::dynallocated);
214 if (ex_to_lortensor(s).type == lortensor_symbolic) {
215 // If lortensor is in sym_lst, return the existing lortensor
216 for (unsigned i=0; i<sym_lst.nops(); i++) {
217 if (is_ex_of_type(sym_lst.op(i), lortensor) && (ex_to_lortensor(sym_lst.op(i)).name == ex_to_lortensor(s).name))
218 return sym_lst.op(i);
224 /** Archive the object. */
225 void lortensor::archive(archive_node &n) const
227 inherited::archive(n);
228 n.add_unsigned("type", type);
229 if (type == lortensor_symbolic)
230 n.add_string("name", name);
235 // functions overriding virtual functions from bases classes
240 void lortensor::printraw(std::ostream & os) const
242 debugmsg("lortensor printraw",LOGLEVEL_PRINT);
243 os << "lortensor(type=" << (unsigned)type
246 os << ",serial=" << serial;
247 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
250 void lortensor::printtree(std::ostream & os, unsigned indent) const
252 debugmsg("lortensor printtree",LOGLEVEL_PRINT);
253 os << std::string(indent,' ') <<"lortensor object: "
254 << "type=" << (unsigned)type << ","
255 << seq.size() << " indices" << std::endl;
256 printtreeindices(os,indent);
257 os << std::string(indent,' ') << "hash=" << hashvalue
258 << " (0x" << std::hex << hashvalue << std::dec << ")"
259 << ", flags=" << flags << std::endl;
262 void lortensor::print(std::ostream & os, unsigned upper_precedence) const
264 debugmsg("lortensor print",LOGLEVEL_PRINT);
269 case lortensor_delta:
272 case lortensor_epsilon:
275 case lortensor_symbolic:
280 os << "INVALID_LORTENSOR_OBJECT";
286 bool lortensor::info(unsigned inf) const
288 return inherited::info(inf);
291 ex lortensor::eval(int level) const
293 if (type==lortensor_g) {
294 // canonicalize indices
296 int sig=canonicalize_indices(iv,false); //symmetric
298 //something has changed while sorting indices, more evaluations later
299 return ex(sig) *lortensor(type,name,iv);
301 const lorentzidx & idx1=ex_to_lorentzidx(seq[0]);
302 const lorentzidx & idx2=ex_to_lorentzidx(seq[1]);
303 if ((!idx1.is_symbolic()) && (!idx2.is_symbolic())) {
304 //both indices are numeric
305 if ((idx1.get_value()==idx2.get_value())) {
307 if (idx1.get_value()==0){
311 if (idx1.is_covariant() != idx2.is_covariant()) {
312 // (_i,~i) or (~i,_i), i = 1...3
315 // (_i,_i) or (~i,~i), i= 1...3
320 // at least one off-diagonal
323 } else if (idx1.is_symbolic() && idx1.is_co_contra_pair(idx2)) {
327 return this -> hold();
332 int lortensor::compare_same_type(const basic & other) const
334 GINAC_ASSERT(is_of_type(other,lortensor));
335 const lortensor &o = static_cast<const lortensor &>(other);
339 return type < o.type ? -1 : 1;
342 if (type == lortensor_symbolic) {
343 // symbolic, compare serials
344 if (serial != o.serial) {
345 return serial < o.serial ? -1 : 1;
349 return inherited::compare_same_type(other);
352 bool lortensor::is_equal_same_type(const basic & other) const
354 GINAC_ASSERT(is_of_type(other,lortensor));
355 const lortensor &o = static_cast<const lortensor &>(other);
357 if (type != o.type) return false;
358 if (type == lortensor_symbolic && serial != o.serial) return false;
359 return inherited::is_equal_same_type(other);
362 unsigned lortensor::return_type(void) const
364 return return_types::commutative;
367 unsigned lortensor::return_type_tinfo(void) const
372 ex lortensor::thisexprseq(const exvector & v) const
374 return lortensor(type,name,serial,v);
377 ex lortensor::thisexprseq(exvector *vp) const
379 return lortensor(type,name,serial,vp);
383 // non-virtual functions in this class
388 /** Check whether all indices are of class lorentzidx or a subclass. This
389 * function is used internally to make sure that all constructed Lorentz
390 * tensors really carry Lorentz indices and not some other classes. */
391 bool lortensor::all_of_type_lorentzidx(void) const
393 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++ cit) {
394 if (!is_ex_of_type(*cit,lorentzidx)) return false;
401 std::string & lortensor::autoname_prefix(void)
403 static std::string * s = new std::string("lortensor");
408 // static member variables
413 unsigned lortensor::next_serial=0;
419 /** Construct an object representing the metric tensor g. The indices must
420 * be of class lorentzidx.
422 * @param mu First index
423 * @param nu Second index
424 * @return newly constructed object */
425 lortensor lortensor_g(const ex & mu, const ex & nu)
427 return lortensor(lortensor::lortensor_g,"",mu,nu);
430 /** Construct an object representing the unity matrix delta. The indices
431 * must be of class lorentzidx.
433 * @param mu First index
434 * @param nu Second index
435 * @return newly constructed object */
436 lortensor lortensor_delta(const ex & mu, const ex & nu)
438 return lortensor(lortensor::lortensor_delta,"",mu,nu);
441 /** Construct an object representing the four-dimensional totally
442 * antisymmetric tensor epsilon. The indices must be of class lorentzidx.
444 * @param mu First index
445 * @param nu Second index
446 * @param rho Third index
447 * @param sigma Fourth index
448 * @return newly constructed object */
449 lortensor lortensor_epsilon(const ex & mu, const ex & nu, const ex & rho, const ex & sigma)
451 return lortensor(lortensor::lortensor_epsilon,"",mu,nu,rho,sigma);
454 /** Construct an object representing a symbolic Lorentz vector. The index
455 * must be of class lorentzidx.
457 * @param n Symbolic name
459 * @return newly constructed object */
460 lortensor lortensor_vector(const std::string & n, const ex & mu)
462 return lortensor(lortensor::lortensor_symbolic,n,mu);
465 /** Construct an object representing a symbolic Lorentz tensor of arbitrary
466 * rank. The indices must be of class lorentzidx.
468 * @param n Symbolic name
469 * @param iv Vector of indices
470 * @return newly constructed object */
471 lortensor lortensor_symbolic(const std::string & n, const exvector & iv)
473 return lortensor(lortensor::lortensor_symbolic,n,iv);
476 ex simplify_lortensor_mul(const ex & m)
478 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
479 exvector v_contracted;
481 // collect factors in an exvector, store squares twice
483 v_contracted.reserve(2*n);
484 for (int i=0; i<n; ++i) {
486 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
487 v_contracted.push_back(f.op(0));
488 v_contracted.push_back(f.op(0));
490 v_contracted.push_back(f);
494 unsigned replacements;
495 bool something_changed=false;
497 exvector::iterator it=v_contracted.begin();
498 while (it!=v_contracted.end()) {
499 // process only lor_g objects
500 if (is_ex_exactly_of_type(*it,lortensor) &&
501 (ex_to_lortensor(*it).type==lortensor::lortensor_g)) {
502 const lortensor & g=ex_to_lortensor(*it);
503 GINAC_ASSERT(g.seq.size()==2);
504 const idx & first_idx=ex_to_lorentzidx(g.seq[0]);
505 const idx & second_idx=ex_to_lorentzidx(g.seq[1]);
506 // g_{mu,mu} should have been contracted in lortensor::eval()
507 GINAC_ASSERT(!first_idx.is_equal(second_idx));
508 ex saved_g=*it; // save to restore it later
510 // try to contract first index
512 if (first_idx.is_symbolic()) {
513 replacements = subs_index_in_exvector(v_contracted,
514 first_idx.toggle_covariant(),second_idx);
515 if (replacements==0) {
516 // not contracted, restore g object
519 // a contracted index should occur exactly once
520 GINAC_ASSERT(replacements==1);
522 something_changed=true;
526 // try second index only if first was not contracted
527 if ((replacements==0)&&(second_idx.is_symbolic())) {
528 // first index not contracted, *it is again the original g object
529 replacements = subs_index_in_exvector(v_contracted,
530 second_idx.toggle_covariant(),first_idx);
531 if (replacements==0) {
532 // not contracted except in itself, restore g object
535 // a contracted index should occur exactly once
536 GINAC_ASSERT(replacements==1);
538 something_changed=true;
544 if (something_changed) {
545 return mul(v_contracted);
550 /** Perform some simplifications on an expression containing Lorentz tensors. */
551 ex simplify_lortensor(const ex & e)
553 // all simplification is done on expanded objects
554 ex e_expanded=e.expand();
556 // simplification of sum=sum of simplifications
557 if (is_ex_exactly_of_type(e_expanded,add)) {
559 for (unsigned i=0; i<e_expanded.nops(); ++i) {
560 sum += simplify_lortensor(e_expanded.op(i));
565 // simplification of (commutative) product
566 if (is_ex_exactly_of_type(e_expanded,mul)) {
567 return simplify_lortensor_mul(e);
570 // cannot do anything
574 #ifndef NO_NAMESPACE_GINAC
576 #endif // ndef NO_NAMESPACE_GINAC