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)) {
324 if (idx1.is_orthogonal_only())
325 return Dim() - idx1.get_dim_parallel_space();
330 return this -> hold();
335 int lortensor::compare_same_type(const basic & other) const
337 GINAC_ASSERT(is_of_type(other,lortensor));
338 const lortensor &o = static_cast<const lortensor &>(other);
342 return type < o.type ? -1 : 1;
345 if (type == lortensor_symbolic) {
346 // symbolic, compare serials
347 if (serial != o.serial) {
348 return serial < o.serial ? -1 : 1;
352 return inherited::compare_same_type(other);
355 bool lortensor::is_equal_same_type(const basic & other) const
357 GINAC_ASSERT(is_of_type(other,lortensor));
358 const lortensor &o = static_cast<const lortensor &>(other);
360 if (type != o.type) return false;
361 if (type == lortensor_symbolic && serial != o.serial) return false;
362 return inherited::is_equal_same_type(other);
365 unsigned lortensor::return_type(void) const
367 return return_types::commutative;
370 unsigned lortensor::return_type_tinfo(void) const
375 ex lortensor::thisexprseq(const exvector & v) const
377 return lortensor(type,name,serial,v);
380 ex lortensor::thisexprseq(exvector *vp) const
382 return lortensor(type,name,serial,vp);
386 // non-virtual functions in this class
391 /** Check whether all indices are of class lorentzidx or a subclass. This
392 * function is used internally to make sure that all constructed Lorentz
393 * tensors really carry Lorentz indices and not some other classes. */
394 bool lortensor::all_of_type_lorentzidx(void) const
396 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++ cit) {
397 if (!is_ex_of_type(*cit,lorentzidx)) return false;
404 std::string & lortensor::autoname_prefix(void)
406 static std::string * s = new std::string("lortensor");
411 // static member variables
416 unsigned lortensor::next_serial=0;
422 /** Construct an object representing the metric tensor g. The indices must
423 * be of class lorentzidx.
425 * @param mu First index
426 * @param nu Second index
427 * @return newly constructed object */
428 lortensor lortensor_g(const ex & mu, const ex & nu)
430 return lortensor(lortensor::lortensor_g,"",mu,nu);
433 /** Construct an object representing the unity matrix delta. The indices
434 * must be of class lorentzidx.
436 * @param mu First index
437 * @param nu Second index
438 * @return newly constructed object */
439 lortensor lortensor_delta(const ex & mu, const ex & nu)
441 return lortensor(lortensor::lortensor_delta,"",mu,nu);
444 /** Construct an object representing the four-dimensional totally
445 * antisymmetric tensor epsilon. The indices must be of class lorentzidx.
447 * @param mu First index
448 * @param nu Second index
449 * @param rho Third index
450 * @param sigma Fourth index
451 * @return newly constructed object */
452 lortensor lortensor_epsilon(const ex & mu, const ex & nu, const ex & rho, const ex & sigma)
454 return lortensor(lortensor::lortensor_epsilon,"",mu,nu,rho,sigma);
457 /** Construct an object representing a symbolic Lorentz vector. The index
458 * must be of class lorentzidx.
460 * @param n Symbolic name
462 * @return newly constructed object */
463 lortensor lortensor_vector(const std::string & n, const ex & mu)
465 return lortensor(lortensor::lortensor_symbolic,n,mu);
468 /** Construct an object representing a symbolic Lorentz tensor of arbitrary
469 * rank. The indices must be of class lorentzidx.
471 * @param n Symbolic name
472 * @param iv Vector of indices
473 * @return newly constructed object */
474 lortensor lortensor_symbolic(const std::string & n, const exvector & iv)
476 return lortensor(lortensor::lortensor_symbolic,n,iv);
479 ex simplify_lortensor_mul(const ex & m)
481 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
482 exvector v_contracted;
484 // collect factors in an exvector, store squares twice
486 v_contracted.reserve(2*n);
487 for (int i=0; i<n; ++i) {
489 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
490 v_contracted.push_back(f.op(0));
491 v_contracted.push_back(f.op(0));
493 v_contracted.push_back(f);
497 unsigned replacements;
498 bool something_changed=false;
500 exvector::iterator it=v_contracted.begin();
501 while (it!=v_contracted.end()) {
502 // process only lor_g objects
503 if (is_ex_exactly_of_type(*it,lortensor) &&
504 (ex_to_lortensor(*it).type==lortensor::lortensor_g)) {
505 const lortensor & g=ex_to_lortensor(*it);
506 GINAC_ASSERT(g.seq.size()==2);
507 const idx & first_idx=ex_to_lorentzidx(g.seq[0]);
508 const idx & second_idx=ex_to_lorentzidx(g.seq[1]);
509 // g_{mu,mu} should have been contracted in lortensor::eval()
510 GINAC_ASSERT(!first_idx.is_equal(second_idx));
511 ex saved_g=*it; // save to restore it later
513 // try to contract first index
515 if (first_idx.is_symbolic()) {
516 replacements = subs_index_in_exvector(v_contracted,
517 first_idx.toggle_covariant(),second_idx);
518 if (replacements==0) {
519 // not contracted, restore g object
522 // a contracted index should occur exactly once
523 GINAC_ASSERT(replacements==1);
525 something_changed=true;
529 // try second index only if first was not contracted
530 if ((replacements==0)&&(second_idx.is_symbolic())) {
531 // first index not contracted, *it is again the original g object
532 replacements = subs_index_in_exvector(v_contracted,
533 second_idx.toggle_covariant(),first_idx);
534 if (replacements==0) {
535 // not contracted except in itself, restore g object
538 // a contracted index should occur exactly once
539 GINAC_ASSERT(replacements==1);
541 something_changed=true;
547 if (something_changed) {
548 return mul(v_contracted);
553 /** Perform some simplifications on an expression containing Lorentz tensors. */
554 ex simplify_lortensor(const ex & e)
556 // all simplification is done on expanded objects
557 ex e_expanded=e.expand();
559 // simplification of sum=sum of simplifications
560 if (is_ex_exactly_of_type(e_expanded,add)) {
562 for (unsigned i=0; i<e_expanded.nops(); ++i) {
563 sum += simplify_lortensor(e_expanded.op(i));
568 // simplification of (commutative) product
569 if (is_ex_exactly_of_type(e_expanded,mul)) {
570 return simplify_lortensor_mul(e);
573 // cannot do anything
577 #ifndef NO_NAMESPACE_GINAC
579 #endif // ndef NO_NAMESPACE_GINAC