1 /** @file lortensor.cpp
3 * Implementation of GiNaCĀ“s lortensor objects.
4 * No real implementation yet, do be done. */
7 * GiNaC Copyright (C) 1999-2000 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;
311 unsigned lortensor::return_type_tinfo(void) const
315 ex lortensor::thisexprseq(const exvector & v) const
317 return lortensor(type,name,serial,v);
319 ex lortensor::thisexprseq(exvector *vp) const
321 return lortensor(type,name,serial,vp);
325 // non-virtual functions in this class
330 void lortensor::setname(const std::string & n)
335 bool lortensor::all_of_type_lorentzidx(void) const
337 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++ cit) {
338 if (!is_ex_of_type(*cit,lorentzidx)) {
347 std::string & lortensor::autoname_prefix(void)
349 static std::string * s = new std::string("lortensor");
354 // static member variables
359 unsigned lortensor::next_serial=0;
365 lortensor lortensor_g(const ex & mu, const ex & nu)
367 return lortensor(lortensor::lortensor_g,"",mu,nu);
370 lortensor lortensor_epsilon(const ex & mu, const ex & nu, const ex & rho, const ex & sigma)
372 return lortensor(lortensor::lortensor_epsilon,"",mu,nu,rho,sigma);
375 lortensor lortensor_rank1(const std::string & n, const ex & mu)
377 return lortensor(lortensor::lortensor_rank1,n,mu);
380 lortensor lortensor_rank2(const std::string & n, const ex & mu, const ex & nu)
382 return lortensor(lortensor::lortensor_rank2,n,mu,nu);
385 ex simplify_lortensor_mul(const ex & m)
387 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
388 exvector v_contracted;
390 // collect factors in an exvector, store squares twice
392 v_contracted.reserve(2*n);
393 for (int i=0; i<n; ++i) {
395 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
396 v_contracted.push_back(f.op(0));
397 v_contracted.push_back(f.op(0));
399 v_contracted.push_back(f);
403 unsigned replacements;
404 bool something_changed=false;
406 exvector::iterator it=v_contracted.begin();
407 while (it!=v_contracted.end()) {
408 // process only lor_g objects
409 if (is_ex_exactly_of_type(*it,lortensor) &&
410 (ex_to_lortensor(*it).type==lortensor::lortensor_g)) {
411 const lortensor & g=ex_to_lortensor(*it);
412 GINAC_ASSERT(g.seq.size()==2);
413 const idx & first_idx=ex_to_lorentzidx(g.seq[0]);
414 const idx & second_idx=ex_to_lorentzidx(g.seq[1]);
415 // g_{mu,mu} should have been contracted in lortensor::eval()
416 GINAC_ASSERT(!first_idx.is_equal(second_idx));
417 ex saved_g=*it; // save to restore it later
419 // try to contract first index
421 if (first_idx.is_symbolic()) {
422 replacements = subs_index_in_exvector(v_contracted,
423 first_idx.toggle_covariant(),second_idx);
424 if (replacements==0) {
425 // not contracted, restore g object
428 // a contracted index should occur exactly once
429 GINAC_ASSERT(replacements==1);
431 something_changed=true;
435 // try second index only if first was not contracted
436 if ((replacements==0)&&(second_idx.is_symbolic())) {
437 // first index not contracted, *it is again the original g object
438 replacements = subs_index_in_exvector(v_contracted,
439 second_idx.toggle_covariant(),first_idx);
440 if (replacements==0) {
441 // not contracted except in itself, restore g object
444 // a contracted index should occur exactly once
445 GINAC_ASSERT(replacements==1);
447 something_changed=true;
453 if (something_changed) {
454 return mul(v_contracted);
459 ex simplify_lortensor(const ex & e)
461 // all simplification is done on expanded objects
462 ex e_expanded=e.expand();
464 // simplification of sum=sum of simplifications
465 if (is_ex_exactly_of_type(e_expanded,add)) {
467 for (unsigned i=0; i<e_expanded.nops(); ++i) {
468 sum += simplify_lortensor(e_expanded.op(i));
473 // simplification of commutative product=commutative product of simplifications
474 if (is_ex_exactly_of_type(e_expanded,mul)) {
475 return simplify_lortensor_mul(e);
478 // cannot do anything
484 static symbol * d=new symbol("dim");
492 const lortensor some_lortensor;
493 const type_info & typeid_lortensor=typeid(some_lortensor);
495 #ifndef NO_NAMESPACE_GINAC
497 #endif // ndef NO_NAMESPACE_GINAC