1 /** @file lortensor.cpp
3 * Implementation of GiNaCĀ“s lortensor objects.
4 * No real implementation yet, do be done. */
7 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include "lortensor.h"
38 #include "operators.h"
45 #ifndef NO_NAMESPACE_GINAC
47 #endif // ndef NO_NAMESPACE_GINAC
50 // default constructor, destructor, copy constructor assignment operator and helpers
55 lortensor::lortensor()
57 debugmsg("lortensor default constructor",LOGLEVEL_CONSTRUCT);
59 name=autoname_prefix()+ToString(serial);
60 tinfo_key=TINFO_lortensor;
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)
95 void lortensor::destroy(bool call_parent)
98 indexed::destroy(call_parent);
103 // other constructors
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 serial=next_serial++;
112 tinfo_key=TINFO_lortensor;
115 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu) : indexed(mu), type(lt), name(n)
117 debugmsg("lortensor constructor from lortensor_types,string,ex",LOGLEVEL_CONSTRUCT);
118 serial=next_serial++;
119 GINAC_ASSERT(all_of_type_lorentzidx());
120 tinfo_key=TINFO_lortensor;
123 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu, const ex & nu) : indexed(mu,nu), type(lt), name(n)
125 debugmsg("lortensor constructor from lortensor_types,string,ex,ex",LOGLEVEL_CONSTRUCT);
126 serial=next_serial++;
127 GINAC_ASSERT(all_of_type_lorentzidx());
128 tinfo_key=TINFO_lortensor;
131 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu, const ex & nu, const ex & rho) : indexed(mu,nu,rho), type(lt), name(n)
133 debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex",LOGLEVEL_CONSTRUCT);
134 serial=next_serial++;
135 GINAC_ASSERT(all_of_type_lorentzidx());
136 tinfo_key=TINFO_lortensor;
139 lortensor::lortensor(lortensor_types const lt, const std::string & n, const ex & mu, const ex & nu, const ex & rho, const ex & sigma) : indexed(mu,nu,rho,sigma), type(lt), name(n)
141 debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
142 serial=next_serial++;
143 GINAC_ASSERT(all_of_type_lorentzidx());
144 tinfo_key=TINFO_lortensor;
147 lortensor::lortensor(lortensor_types const lt, const std::string & n, const exvector & iv) : indexed(iv), type(lt), name(n)
149 debugmsg("lortensor constructor from lortensor_types,string,exvector",LOGLEVEL_CONSTRUCT);
150 serial=next_serial++;
151 GINAC_ASSERT(all_of_type_lorentzidx());
152 tinfo_key=TINFO_lortensor;
155 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, const exvector & iv) : indexed(iv), type(lt), name(n), serial(s)
157 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
158 GINAC_ASSERT(all_of_type_lorentzidx());
159 tinfo_key=TINFO_lortensor;
162 lortensor::lortensor(lortensor_types const lt, const std::string & n, unsigned s, exvector *ivp) : indexed(ivp), type(lt), name(n), serial(s)
164 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
165 GINAC_ASSERT(all_of_type_lorentzidx());
166 tinfo_key=TINFO_lortensor;
170 // functions overriding virtual functions from bases classes
175 basic * lortensor::duplicate() const
177 debugmsg("lortensor duplicate",LOGLEVEL_DUPLICATE);
178 return new lortensor(*this);
181 void lortensor::printraw(std::ostream & os) const
183 debugmsg("lortensor printraw",LOGLEVEL_PRINT);
184 os << "lortensor(type=" << (unsigned)type
187 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
190 void lortensor::printtree(std::ostream & os, unsigned indent) const
192 debugmsg("lortensor printtree",LOGLEVEL_PRINT);
193 os << std::string(indent,' ') <<"lortensor object: "
194 << "type=" << (unsigned)type << ","
195 << seq.size() << " indices" << std::endl;
196 printtreeindices(os,indent);
197 os << std::string(indent,' ') << "hash=" << hashvalue
198 << " (0x" << std::hex << hashvalue << std::dec << ")"
199 << ", flags=" << flags << std::endl;
202 void lortensor::print(std::ostream & os, unsigned upper_precedence) const
204 debugmsg("lortensor print",LOGLEVEL_PRINT);
209 case lortensor_rankn:
212 case lortensor_rank1:
215 case lortensor_rank2:
218 case lortensor_epsilon:
223 os << "INVALID_LORTENSOR_OBJECT";
229 void lortensor::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
231 debugmsg("lortensor print csrc",LOGLEVEL_PRINT);
232 print(os,upper_precedence);
235 bool lortensor::info(unsigned inf) const
237 return indexed::info(inf);
240 ex lortensor::eval(int level) const
242 if (type==lortensor_g) {
243 // canonicalize indices
245 int sig=canonicalize_indices(iv,false); //symmetric
247 //something has changed while sorting indices, more evaluations later
248 return ex(sig) *lortensor(type,name,iv);
250 const lorentzidx & idx1=ex_to_lorentzidx(seq[0]);
251 const lorentzidx & idx2=ex_to_lorentzidx(seq[1]);
252 if ((!idx1.is_symbolic()) && (!idx2.is_symbolic())) {
253 //both indices are numeric
254 if ((idx1.get_value()==idx2.get_value())) {
256 if (idx1.get_value()==0){
260 if (idx1.is_covariant() != idx2.is_covariant()) {
261 // (_i,~i) or (~i,_i), i = 1...3
264 // (_i,_i) or (~i,~i), i= 1...3
269 // at least one off-diagonal
272 } else if (idx1.is_symbolic() && idx1.is_co_contra_pair(idx2)) {
273 return Dim()-idx1.get_dim_parallel_space();
276 return this -> hold();
281 int lortensor::compare_same_type(const basic & other) const
283 GINAC_ASSERT(is_of_type(other,lortensor));
284 const lortensor *o = static_cast <const lortensor *> (&other);
286 if (type==lortensor_rankn) {
287 if (serial!=o->serial) {
288 return serial < o->serial ? -1 : 1;
291 return indexed::compare_same_type(other);
293 return type < o->type ? -1 : 1;
296 bool lortensor::is_equal_same_type(const basic & other) const
298 GINAC_ASSERT(is_of_type(other,lortensor));
299 const lortensor *o=static_cast<const lortensor *> (&other);
300 if (type!=o->type) return false;
301 if (type==lortensor_rankn) {
302 if (serial!=o->serial) return false;
304 return indexed::is_equal_same_type(other);
307 unsigned lortensor::return_type(void) const
309 return return_types::commutative;
312 unsigned lortensor::return_type_tinfo(void) const
317 ex lortensor::thisexprseq(const exvector & v) const
319 return lortensor(type,name,serial,v);
322 ex lortensor::thisexprseq(exvector *vp) const
324 return lortensor(type,name,serial,vp);
328 // non-virtual functions in this class
333 void lortensor::setname(const std::string & n)
338 bool lortensor::all_of_type_lorentzidx(void) const
340 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++ cit) {
341 if (!is_ex_of_type(*cit,lorentzidx)) {
350 std::string & lortensor::autoname_prefix(void)
352 static std::string * s = new std::string("lortensor");
357 // static member variables
362 unsigned lortensor::next_serial=0;
368 lortensor lortensor_g(const ex & mu, const ex & nu)
370 return lortensor(lortensor::lortensor_g,"",mu,nu);
373 lortensor lortensor_epsilon(const ex & mu, const ex & nu, const ex & rho, const ex & sigma)
375 return lortensor(lortensor::lortensor_epsilon,"",mu,nu,rho,sigma);
378 lortensor lortensor_rank1(const std::string & n, const ex & mu)
380 return lortensor(lortensor::lortensor_rank1,n,mu);
383 lortensor lortensor_rank2(const std::string & n, const ex & mu, const ex & nu)
385 return lortensor(lortensor::lortensor_rank2,n,mu,nu);
388 ex simplify_lortensor_mul(const ex & m)
390 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
391 exvector v_contracted;
393 // collect factors in an exvector, store squares twice
395 v_contracted.reserve(2*n);
396 for (int i=0; i<n; ++i) {
398 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
399 v_contracted.push_back(f.op(0));
400 v_contracted.push_back(f.op(0));
402 v_contracted.push_back(f);
406 unsigned replacements;
407 bool something_changed=false;
409 exvector::iterator it=v_contracted.begin();
410 while (it!=v_contracted.end()) {
411 // process only lor_g objects
412 if (is_ex_exactly_of_type(*it,lortensor) &&
413 (ex_to_lortensor(*it).type==lortensor::lortensor_g)) {
414 const lortensor & g=ex_to_lortensor(*it);
415 GINAC_ASSERT(g.seq.size()==2);
416 const idx & first_idx=ex_to_lorentzidx(g.seq[0]);
417 const idx & second_idx=ex_to_lorentzidx(g.seq[1]);
418 // g_{mu,mu} should have been contracted in lortensor::eval()
419 GINAC_ASSERT(!first_idx.is_equal(second_idx));
420 ex saved_g=*it; // save to restore it later
422 // try to contract first index
424 if (first_idx.is_symbolic()) {
425 replacements = subs_index_in_exvector(v_contracted,
426 first_idx.toggle_covariant(),second_idx);
427 if (replacements==0) {
428 // not contracted, restore g object
431 // a contracted index should occur exactly once
432 GINAC_ASSERT(replacements==1);
434 something_changed=true;
438 // try second index only if first was not contracted
439 if ((replacements==0)&&(second_idx.is_symbolic())) {
440 // first index not contracted, *it is again the original g object
441 replacements = subs_index_in_exvector(v_contracted,
442 second_idx.toggle_covariant(),first_idx);
443 if (replacements==0) {
444 // not contracted except in itself, restore g object
447 // a contracted index should occur exactly once
448 GINAC_ASSERT(replacements==1);
450 something_changed=true;
456 if (something_changed) {
457 return mul(v_contracted);
462 ex simplify_lortensor(const ex & e)
464 // all simplification is done on expanded objects
465 ex e_expanded=e.expand();
467 // simplification of sum=sum of simplifications
468 if (is_ex_exactly_of_type(e_expanded,add)) {
470 for (unsigned i=0; i<e_expanded.nops(); ++i) {
471 sum += simplify_lortensor(e_expanded.op(i));
476 // simplification of commutative product=commutative product of simplifications
477 if (is_ex_exactly_of_type(e_expanded,mul)) {
478 return simplify_lortensor_mul(e);
481 // cannot do anything
487 static symbol * d=new symbol("dim");
495 const lortensor some_lortensor;
496 const std::type_info & typeid_lortensor = typeid(some_lortensor);
498 #ifndef NO_NAMESPACE_GINAC
500 #endif // ndef NO_NAMESPACE_GINAC