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
36 #include "lortensor.h"
37 #include "operators.h"
44 #ifndef NO_NAMESPACE_GINAC
46 #endif // ndef NO_NAMESPACE_GINAC
48 GINAC_IMPLEMENT_REGISTERED_CLASS(lortensor, indexed)
51 // default constructor, destructor, copy constructor assignment operator and helpers
56 lortensor::lortensor() : inherited(TINFO_lortensor), type(invalid)
58 debugmsg("lortensor default constructor",LOGLEVEL_CONSTRUCT);
60 name=autoname_prefix()+ToString(serial);
63 lortensor::~lortensor()
65 debugmsg("lortensor destructor",LOGLEVEL_DESTRUCT);
69 lortensor::lortensor(const lortensor & other)
71 debugmsg("lortensor copy constructor",LOGLEVEL_CONSTRUCT);
75 const lortensor & lortensor::operator=(const lortensor & other)
77 debugmsg("lortensor operator=",LOGLEVEL_ASSIGNMENT);
78 if (this != & other) {
87 void lortensor::copy(const lortensor & other)
89 inherited::copy(other);
95 void lortensor::destroy(bool call_parent)
97 if (call_parent) inherited::destroy(call_parent);
101 // other constructors
106 /** Construct object without any Lorentz index. This constructor is for
107 * internal use only. */
108 lortensor::lortensor(lortensor_types const lt, const std::string & n) : type(lt), name(n)
110 debugmsg("lortensor constructor from lortensor_types,string",LOGLEVEL_CONSTRUCT);
111 if (lt == lortensor_symbolic)
112 serial = next_serial++;
115 tinfo_key = TINFO_lortensor;
118 /** Construct object with one Lorentz index. This constructor is for
119 * internal use only. Use the lortensor_vector() or lortensor_symbolic()
121 * @see lortensor_vector
122 * @see lortensor_symbolic */
123 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu) : inherited(mu), type(lt), name(n)
125 debugmsg("lortensor constructor from lortensor_types,string,ex",LOGLEVEL_CONSTRUCT);
126 GINAC_ASSERT(all_of_type_lorentzidx());
127 if (lt == lortensor_symbolic)
128 serial = next_serial++;
131 tinfo_key=TINFO_lortensor;
134 /** Construct object with two Lorentz indices. This constructor is for
135 * internal use only. Use the lortensor_g(), lortensor_delta() or
136 * lortensor_symbolic() functions instead.
138 * @see lortensor_delta
139 * @see lortensor_symbolic */
140 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu, const ex & nu) : inherited(mu,nu), type(lt), name(n)
142 debugmsg("lortensor constructor from lortensor_types,string,ex,ex",LOGLEVEL_CONSTRUCT);
143 GINAC_ASSERT(all_of_type_lorentzidx());
144 if (lt == lortensor_symbolic)
145 serial = next_serial++;
148 tinfo_key=TINFO_lortensor;
151 /** Construct object with three Lorentz indices. This constructor is for
152 * internal use only. Use the lortensor_symbolic() function instead.
153 * @see lortensor_symbolic */
154 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)
156 debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex",LOGLEVEL_CONSTRUCT);
157 GINAC_ASSERT(all_of_type_lorentzidx());
158 if (lt == lortensor_symbolic)
159 serial = next_serial++;
162 tinfo_key=TINFO_lortensor;
165 /** Construct object with four Lorentz indices. This constructor is for
166 * internal use only. Use the lortensor_epsilon() or lortensor_symbolic()
168 * @see lortensor_epsilon
169 * @see lortensor_symbolic */
170 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)
172 debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
173 GINAC_ASSERT(all_of_type_lorentzidx());
174 if (lt == lortensor_symbolic)
175 serial = next_serial++;
178 tinfo_key=TINFO_lortensor;
181 /** Construct object with arbitrary number of Lorentz indices. This
182 * constructor is for internal use only. Use the lortensor_symbolic()
185 * @see lortensor_symbolic */
186 lortensor::lortensor(lortensor_types const lt, const std::string & n, const exvector & iv) : inherited(iv), type(lt), name(n)
188 debugmsg("lortensor constructor from lortensor_types,string,exvector",LOGLEVEL_CONSTRUCT);
189 GINAC_ASSERT(all_of_type_lorentzidx());
190 if (lt == lortensor_symbolic)
191 serial = next_serial++;
194 tinfo_key=TINFO_lortensor;
197 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, const exvector & iv) : indexed(iv), type(lt), name(n), serial(s)
199 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
200 GINAC_ASSERT(all_of_type_lorentzidx());
201 tinfo_key=TINFO_lortensor;
204 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, exvector *ivp) : indexed(ivp), type(lt), name(n), serial(s)
206 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
207 GINAC_ASSERT(all_of_type_lorentzidx());
208 tinfo_key=TINFO_lortensor;
216 /** Construct object from archive_node. */
217 lortensor::lortensor(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
219 debugmsg("lortensor constructor from archive_node", LOGLEVEL_CONSTRUCT);
221 if (!(n.find_unsigned("type", ty)))
222 throw (std::runtime_error("unknown lortensor type in archive"));
223 type = (lortensor_types)ty;
224 if (type == lortensor_symbolic) {
225 serial = next_serial++;
226 if (!(n.find_string("name", name)))
227 name = autoname_prefix() + ToString(serial);
232 /** Unarchive the object. */
233 ex lortensor::unarchive(const archive_node &n, const lst &sym_lst)
235 ex s = (new lortensor(n, sym_lst))->setflag(status_flags::dynallocated);
237 if (ex_to_lortensor(s).type == lortensor_symbolic) {
238 // If lortensor is in sym_lst, return the existing lortensor
239 for (unsigned i=0; i<sym_lst.nops(); i++) {
240 if (is_ex_of_type(sym_lst.op(i), lortensor) && (ex_to_lortensor(sym_lst.op(i)).name == ex_to_lortensor(s).name))
241 return sym_lst.op(i);
247 /** Archive the object. */
248 void lortensor::archive(archive_node &n) const
250 inherited::archive(n);
251 n.add_unsigned("type", type);
252 if (type == lortensor_symbolic)
253 n.add_string("name", name);
258 // functions overriding virtual functions from bases classes
263 basic * lortensor::duplicate() const
265 debugmsg("lortensor duplicate",LOGLEVEL_DUPLICATE);
266 return new lortensor(*this);
269 void lortensor::printraw(std::ostream & os) const
271 debugmsg("lortensor printraw",LOGLEVEL_PRINT);
272 os << "lortensor(type=" << (unsigned)type
275 os << ",serial=" << serial;
276 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
279 void lortensor::printtree(std::ostream & os, unsigned indent) const
281 debugmsg("lortensor printtree",LOGLEVEL_PRINT);
282 os << std::string(indent,' ') <<"lortensor object: "
283 << "type=" << (unsigned)type << ","
284 << seq.size() << " indices" << std::endl;
285 printtreeindices(os,indent);
286 os << std::string(indent,' ') << "hash=" << hashvalue
287 << " (0x" << std::hex << hashvalue << std::dec << ")"
288 << ", flags=" << flags << std::endl;
291 void lortensor::print(std::ostream & os, unsigned upper_precedence) const
293 debugmsg("lortensor print",LOGLEVEL_PRINT);
298 case lortensor_delta:
301 case lortensor_epsilon:
304 case lortensor_symbolic:
309 os << "INVALID_LORTENSOR_OBJECT";
315 bool lortensor::info(unsigned inf) const
317 return inherited::info(inf);
320 ex lortensor::eval(int level) const
322 if (type==lortensor_g) {
323 // canonicalize indices
325 int sig=canonicalize_indices(iv,false); //symmetric
327 //something has changed while sorting indices, more evaluations later
328 return ex(sig) *lortensor(type,name,iv);
330 const lorentzidx & idx1=ex_to_lorentzidx(seq[0]);
331 const lorentzidx & idx2=ex_to_lorentzidx(seq[1]);
332 if ((!idx1.is_symbolic()) && (!idx2.is_symbolic())) {
333 //both indices are numeric
334 if ((idx1.get_value()==idx2.get_value())) {
336 if (idx1.get_value()==0){
340 if (idx1.is_covariant() != idx2.is_covariant()) {
341 // (_i,~i) or (~i,_i), i = 1...3
344 // (_i,_i) or (~i,~i), i= 1...3
349 // at least one off-diagonal
352 } else if (idx1.is_symbolic() && idx1.is_co_contra_pair(idx2)) {
356 return this -> hold();
361 int lortensor::compare_same_type(const basic & other) const
363 GINAC_ASSERT(is_of_type(other,lortensor));
364 const lortensor &o = static_cast<const lortensor &>(other);
368 return type < o.type ? -1 : 1;
371 if (type == lortensor_symbolic) {
372 // symbolic, compare serials
373 if (serial != o.serial) {
374 return serial < o.serial ? -1 : 1;
378 return inherited::compare_same_type(other);
381 bool lortensor::is_equal_same_type(const basic & other) const
383 GINAC_ASSERT(is_of_type(other,lortensor));
384 const lortensor &o = static_cast<const lortensor &>(other);
386 if (type != o.type) return false;
387 if (type == lortensor_symbolic && serial != o.serial) return false;
388 return inherited::is_equal_same_type(other);
391 unsigned lortensor::return_type(void) const
393 return return_types::commutative;
396 unsigned lortensor::return_type_tinfo(void) const
401 ex lortensor::thisexprseq(const exvector & v) const
403 return lortensor(type,name,serial,v);
406 ex lortensor::thisexprseq(exvector *vp) const
408 return lortensor(type,name,serial,vp);
412 // non-virtual functions in this class
417 /** Check whether all indices are of class lorentzidx or a subclass. This
418 * function is used internally to make sure that all constructed Lorentz
419 * tensors really carry Lorentz indices and not some other classes. */
420 bool lortensor::all_of_type_lorentzidx(void) const
422 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++ cit) {
423 if (!is_ex_of_type(*cit,lorentzidx)) return false;
430 std::string & lortensor::autoname_prefix(void)
432 static std::string * s = new std::string("lortensor");
437 // static member variables
442 unsigned lortensor::next_serial=0;
448 /** Construct an object representing the metric tensor g. The indices must
449 * be of class lorentzidx.
451 * @param mu First index
452 * @param nu Second index
453 * @return newly constructed object */
454 lortensor lortensor_g(const ex & mu, const ex & nu)
456 return lortensor(lortensor::lortensor_g,"",mu,nu);
459 /** Construct an object representing the unity matrix delta. The indices
460 * must be of class lorentzidx.
462 * @param mu First index
463 * @param nu Second index
464 * @return newly constructed object */
465 lortensor lortensor_delta(const ex & mu, const ex & nu)
467 return lortensor(lortensor::lortensor_delta,"",mu,nu);
470 /** Construct an object representing the four-dimensional totally
471 * antisymmetric tensor epsilon. The indices must be of class lorentzidx.
473 * @param mu First index
474 * @param nu Second index
475 * @param rho Third index
476 * @param sigma Fourth index
477 * @return newly constructed object */
478 lortensor lortensor_epsilon(const ex & mu, const ex & nu, const ex & rho, const ex & sigma)
480 return lortensor(lortensor::lortensor_epsilon,"",mu,nu,rho,sigma);
483 /** Construct an object representing a symbolic Lorentz vector. The index
484 * must be of class lorentzidx.
486 * @param n Symbolic name
488 * @return newly constructed object */
489 lortensor lortensor_vector(const std::string & n, const ex & mu)
491 return lortensor(lortensor::lortensor_symbolic,n,mu);
494 /** Construct an object representing a symbolic Lorentz tensor of arbitrary
495 * rank. The indices must be of class lorentzidx.
497 * @param n Symbolic name
498 * @param iv Vector of indices
499 * @return newly constructed object */
500 lortensor lortensor_symbolic(const std::string & n, const exvector & iv)
502 return lortensor(lortensor::lortensor_symbolic,n,iv);
505 ex simplify_lortensor_mul(const ex & m)
507 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
508 exvector v_contracted;
510 // collect factors in an exvector, store squares twice
512 v_contracted.reserve(2*n);
513 for (int i=0; i<n; ++i) {
515 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
516 v_contracted.push_back(f.op(0));
517 v_contracted.push_back(f.op(0));
519 v_contracted.push_back(f);
523 unsigned replacements;
524 bool something_changed=false;
526 exvector::iterator it=v_contracted.begin();
527 while (it!=v_contracted.end()) {
528 // process only lor_g objects
529 if (is_ex_exactly_of_type(*it,lortensor) &&
530 (ex_to_lortensor(*it).type==lortensor::lortensor_g)) {
531 const lortensor & g=ex_to_lortensor(*it);
532 GINAC_ASSERT(g.seq.size()==2);
533 const idx & first_idx=ex_to_lorentzidx(g.seq[0]);
534 const idx & second_idx=ex_to_lorentzidx(g.seq[1]);
535 // g_{mu,mu} should have been contracted in lortensor::eval()
536 GINAC_ASSERT(!first_idx.is_equal(second_idx));
537 ex saved_g=*it; // save to restore it later
539 // try to contract first index
541 if (first_idx.is_symbolic()) {
542 replacements = subs_index_in_exvector(v_contracted,
543 first_idx.toggle_covariant(),second_idx);
544 if (replacements==0) {
545 // not contracted, restore g object
548 // a contracted index should occur exactly once
549 GINAC_ASSERT(replacements==1);
551 something_changed=true;
555 // try second index only if first was not contracted
556 if ((replacements==0)&&(second_idx.is_symbolic())) {
557 // first index not contracted, *it is again the original g object
558 replacements = subs_index_in_exvector(v_contracted,
559 second_idx.toggle_covariant(),first_idx);
560 if (replacements==0) {
561 // not contracted except in itself, restore g object
564 // a contracted index should occur exactly once
565 GINAC_ASSERT(replacements==1);
567 something_changed=true;
573 if (something_changed) {
574 return mul(v_contracted);
579 /** Perform some simplifications on an expression containing Lorentz tensors. */
580 ex simplify_lortensor(const ex & e)
582 // all simplification is done on expanded objects
583 ex e_expanded=e.expand();
585 // simplification of sum=sum of simplifications
586 if (is_ex_exactly_of_type(e_expanded,add)) {
588 for (unsigned i=0; i<e_expanded.nops(); ++i) {
589 sum += simplify_lortensor(e_expanded.op(i));
594 // simplification of (commutative) product
595 if (is_ex_exactly_of_type(e_expanded,mul)) {
596 return simplify_lortensor_mul(e);
599 // cannot do anything
607 const lortensor some_lortensor;
608 const std::type_info & typeid_lortensor = typeid(some_lortensor);
610 #ifndef NO_NAMESPACE_GINAC
612 #endif // ndef NO_NAMESPACE_GINAC