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);
65 void lortensor::copy(const lortensor & other)
67 inherited::copy(other);
73 void lortensor::destroy(bool call_parent)
75 if (call_parent) inherited::destroy(call_parent);
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)
88 debugmsg("lortensor constructor from lortensor_types,string",LOGLEVEL_CONSTRUCT);
89 if (lt == lortensor_symbolic)
90 serial = next_serial++;
93 tinfo_key = TINFO_lortensor;
96 /** Construct object with one Lorentz index. This constructor is for
97 * internal use only. Use the lortensor_vector() or lortensor_symbolic()
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)
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++;
109 tinfo_key=TINFO_lortensor;
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.
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)
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++;
126 tinfo_key=TINFO_lortensor;
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)
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++;
140 tinfo_key=TINFO_lortensor;
143 /** Construct object with four Lorentz indices. This constructor is for
144 * internal use only. Use the lortensor_epsilon() or lortensor_symbolic()
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)
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++;
156 tinfo_key=TINFO_lortensor;
159 /** Construct object with arbitrary number of Lorentz indices. This
160 * constructor is for internal use only. Use the lortensor_symbolic()
163 * @see lortensor_symbolic */
164 lortensor::lortensor(lortensor_types const lt, const std::string & n, const exvector & iv) : inherited(iv), type(lt), name(n)
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++;
172 tinfo_key=TINFO_lortensor;
175 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, const exvector & iv) : indexed(iv), type(lt), name(n), serial(s)
177 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
178 GINAC_ASSERT(all_of_type_lorentzidx());
179 tinfo_key=TINFO_lortensor;
182 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, exvector *ivp) : indexed(ivp), type(lt), name(n), serial(s)
184 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
185 GINAC_ASSERT(all_of_type_lorentzidx());
186 tinfo_key=TINFO_lortensor;
194 /** Construct object from archive_node. */
195 lortensor::lortensor(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
197 debugmsg("lortensor constructor from archive_node", LOGLEVEL_CONSTRUCT);
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);
210 /** Unarchive the object. */
211 ex lortensor::unarchive(const archive_node &n, const lst &sym_lst)
213 ex s = (new lortensor(n, sym_lst))->setflag(status_flags::dynallocated);
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);
225 /** Archive the object. */
226 void lortensor::archive(archive_node &n) const
228 inherited::archive(n);
229 n.add_unsigned("type", type);
230 if (type == lortensor_symbolic)
231 n.add_string("name", name);
236 // functions overriding virtual functions from bases classes
241 void lortensor::printraw(std::ostream & os) const
243 debugmsg("lortensor printraw",LOGLEVEL_PRINT);
244 os << "lortensor(type=" << (unsigned)type
247 os << ",serial=" << serial;
248 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
251 void lortensor::printtree(std::ostream & os, unsigned indent) const
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;
263 void lortensor::print(std::ostream & os, unsigned upper_precedence) const
265 debugmsg("lortensor print",LOGLEVEL_PRINT);
270 case lortensor_delta:
273 case lortensor_epsilon:
276 case lortensor_symbolic:
281 os << "INVALID_LORTENSOR_OBJECT";
287 bool lortensor::info(unsigned inf) const
289 return inherited::info(inf);
292 ex lortensor::eval(int level) const
294 if (type==lortensor_g) {
295 // canonicalize indices
297 int sig=canonicalize_indices(iv,false); //symmetric
299 //something has changed while sorting indices, more evaluations later
300 return ex(sig) *lortensor(type,name,iv);
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())) {
308 if (idx1.get_value()==0){
312 if (idx1.is_covariant() != idx2.is_covariant()) {
313 // (_i,~i) or (~i,_i), i = 1...3
316 // (_i,_i) or (~i,~i), i= 1...3
321 // at least one off-diagonal
324 } else if (idx1.is_symbolic() && idx1.is_co_contra_pair(idx2)) {
328 return this -> hold();
333 int lortensor::compare_same_type(const basic & other) const
335 GINAC_ASSERT(is_of_type(other,lortensor));
336 const lortensor &o = static_cast<const lortensor &>(other);
340 return type < o.type ? -1 : 1;
343 if (type == lortensor_symbolic) {
344 // symbolic, compare serials
345 if (serial != o.serial) {
346 return serial < o.serial ? -1 : 1;
350 return inherited::compare_same_type(other);
353 bool lortensor::is_equal_same_type(const basic & other) const
355 GINAC_ASSERT(is_of_type(other,lortensor));
356 const lortensor &o = static_cast<const lortensor &>(other);
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);
363 unsigned lortensor::return_type(void) const
365 return return_types::commutative;
368 unsigned lortensor::return_type_tinfo(void) const
373 ex lortensor::thisexprseq(const exvector & v) const
375 return lortensor(type,name,serial,v);
378 ex lortensor::thisexprseq(exvector *vp) const
380 return lortensor(type,name,serial,vp);
384 // non-virtual functions in this class
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
394 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++ cit) {
395 if (!is_ex_of_type(*cit,lorentzidx)) return false;
402 std::string & lortensor::autoname_prefix(void)
404 static std::string * s = new std::string("lortensor");
409 // static member variables
414 unsigned lortensor::next_serial=0;
420 /** Construct an object representing the metric tensor g. The indices must
421 * be of class lorentzidx.
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)
428 return lortensor(lortensor::lortensor_g,"",mu,nu);
431 /** Construct an object representing the unity matrix delta. The indices
432 * must be of class lorentzidx.
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)
439 return lortensor(lortensor::lortensor_delta,"",mu,nu);
442 /** Construct an object representing the four-dimensional totally
443 * antisymmetric tensor epsilon. The indices must be of class lorentzidx.
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)
452 return lortensor(lortensor::lortensor_epsilon,"",mu,nu,rho,sigma);
455 /** Construct an object representing a symbolic Lorentz vector. The index
456 * must be of class lorentzidx.
458 * @param n Symbolic name
460 * @return newly constructed object */
461 lortensor lortensor_vector(const std::string & n, const ex & mu)
463 return lortensor(lortensor::lortensor_symbolic,n,mu);
466 /** Construct an object representing a symbolic Lorentz tensor of arbitrary
467 * rank. The indices must be of class lorentzidx.
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)
474 return lortensor(lortensor::lortensor_symbolic,n,iv);
477 ex simplify_lortensor_mul(const ex & m)
479 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
480 exvector v_contracted;
482 // collect factors in an exvector, store squares twice
484 v_contracted.reserve(2*n);
485 for (int i=0; i<n; ++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));
491 v_contracted.push_back(f);
495 unsigned replacements;
496 bool something_changed=false;
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
511 // try to contract first index
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
520 // a contracted index should occur exactly once
521 GINAC_ASSERT(replacements==1);
523 something_changed=true;
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
536 // a contracted index should occur exactly once
537 GINAC_ASSERT(replacements==1);
539 something_changed=true;
545 if (something_changed) {
546 return mul(v_contracted);
551 /** Perform some simplifications on an expression containing Lorentz tensors. */
552 ex simplify_lortensor(const ex & e)
554 // all simplification is done on expanded objects
555 ex e_expanded=e.expand();
557 // simplification of sum=sum of simplifications
558 if (is_ex_exactly_of_type(e_expanded,add)) {
560 for (unsigned i=0; i<e_expanded.nops(); ++i) {
561 sum += simplify_lortensor(e_expanded.op(i));
566 // simplification of (commutative) product
567 if (is_ex_exactly_of_type(e_expanded,mul)) {
568 return simplify_lortensor_mul(e);
571 // cannot do anything
575 #ifndef NO_NAMESPACE_GINAC
577 #endif // ndef NO_NAMESPACE_GINAC