1 /** @file lortensor.cpp
3 * Implementation of GiNaCĀ“s lortensor objects.
4 * No real implementation yet, do be done. */
7 * GiNaC Copyright (C) 1999 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"
44 #ifndef NO_GINAC_NAMESPACE
46 #endif // ndef NO_GINAC_NAMESPACE
49 // default constructor, destructor, copy constructor assignment operator and helpers
54 lortensor::lortensor()
56 debugmsg("lortensor default constructor",LOGLEVEL_CONSTRUCT);
58 name=autoname_prefix()+ToString(serial);
59 tinfo_key=TINFO_lortensor;
62 lortensor::~lortensor()
64 debugmsg("lortensor destructor",LOGLEVEL_DESTRUCT);
68 lortensor::lortensor(lortensor const & other)
70 debugmsg("lortensor copy constructor",LOGLEVEL_CONSTRUCT);
74 lortensor const & lortensor::operator=(lortensor const & other)
76 debugmsg("lortensor operator=",LOGLEVEL_ASSIGNMENT);
77 if (this != & other) {
86 void lortensor::copy(lortensor const & other)
94 void lortensor::destroy(bool call_parent)
97 indexed::destroy(call_parent);
102 // other constructors
107 lortensor::lortensor(lortensor_types const lt, string const & n) : type(lt), name(n)
109 debugmsg("lortensor constructor from lortensor_types,string",LOGLEVEL_CONSTRUCT);
110 serial=next_serial++;
111 tinfo_key=TINFO_lortensor;
114 lortensor::lortensor(lortensor_types const lt, string const & n, ex const & mu) : indexed(mu), type(lt), name(n)
116 debugmsg("lortensor constructor from lortensor_types,string,ex",LOGLEVEL_CONSTRUCT);
117 serial=next_serial++;
118 GINAC_ASSERT(all_of_type_lorentzidx());
119 tinfo_key=TINFO_lortensor;
122 lortensor::lortensor(lortensor_types const lt, string const & n, ex const & mu, ex const & nu) : indexed(mu,nu), type(lt), name(n)
124 debugmsg("lortensor constructor from lortensor_types,string,ex,ex",LOGLEVEL_CONSTRUCT);
125 serial=next_serial++;
126 GINAC_ASSERT(all_of_type_lorentzidx());
127 tinfo_key=TINFO_lortensor;
130 lortensor::lortensor(lortensor_types const lt, string const & n, ex const & mu, ex const & nu, ex const & rho) : indexed(mu,nu,rho), type(lt), name(n)
132 debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex",LOGLEVEL_CONSTRUCT);
133 serial=next_serial++;
134 GINAC_ASSERT(all_of_type_lorentzidx());
135 tinfo_key=TINFO_lortensor;
138 lortensor::lortensor(lortensor_types const lt, string const & n, ex const & mu, ex const & nu, ex const & rho, ex const & sigma) : indexed(mu,nu,rho,sigma), type(lt), name(n)
140 debugmsg("lortensor constructor from lortensor_types,string,ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
141 serial=next_serial++;
142 GINAC_ASSERT(all_of_type_lorentzidx());
143 tinfo_key=TINFO_lortensor;
146 lortensor::lortensor(lortensor_types const lt, string const & n, exvector const & iv) : indexed(iv), type(lt), name(n)
148 debugmsg("lortensor constructor from lortensor_types,string,exvector",LOGLEVEL_CONSTRUCT);
149 serial=next_serial++;
150 GINAC_ASSERT(all_of_type_lorentzidx());
151 tinfo_key=TINFO_lortensor;
154 lortensor::lortensor(lortensor_types const lt, string const & n, unsigned s, exvector const & iv) : indexed(iv), type(lt), name(n), serial(s)
156 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
157 GINAC_ASSERT(all_of_type_lorentzidx());
158 tinfo_key=TINFO_lortensor;
161 lortensor::lortensor(lortensor_types const lt, string const & n, unsigned s, exvector *ivp) : indexed(ivp), type(lt), name(n), serial(s)
163 debugmsg("lortensor constructor from lortensor_types,string,unsigned,exvector",LOGLEVEL_CONSTRUCT);
164 GINAC_ASSERT(all_of_type_lorentzidx());
165 tinfo_key=TINFO_lortensor;
169 // functions overriding virtual functions from bases classes
174 basic * lortensor::duplicate() const
176 debugmsg("lortensor duplicate",LOGLEVEL_DUPLICATE);
177 return new lortensor(*this);
180 void lortensor::printraw(ostream & os) const
182 debugmsg("lortensor printraw",LOGLEVEL_PRINT);
183 os << "lortensor(type=" << (unsigned)type
186 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
189 void lortensor::printtree(ostream & os, unsigned indent) const
191 debugmsg("lortensor printtree",LOGLEVEL_PRINT);
192 os << string(indent,' ') <<"lortensor object: "
193 << "type=" << (unsigned)type << ","
194 << seq.size() << " indices" << endl;
195 printtreeindices(os,indent);
196 os << string(indent,' ') << "hash=" << hashvalue
197 << " (0x" << hex << hashvalue << dec << ")"
198 << ", flags=" << flags << endl;
201 void lortensor::print(ostream & os, unsigned upper_precedence) const
203 debugmsg("lortensor print",LOGLEVEL_PRINT);
208 case lortensor_rankn:
211 case lortensor_rank1:
214 case lortensor_rank2:
217 case lortensor_epsilon:
222 os << "INVALID_LORTENSOR_OBJECT";
228 void lortensor::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
230 debugmsg("lortensor print csrc",LOGLEVEL_PRINT);
231 print(os,upper_precedence);
234 bool lortensor::info(unsigned inf) const
236 return indexed::info(inf);
239 ex lortensor::eval(int level) const
241 if (type==lortensor_g) {
242 // canonicalize indices
244 int sig=canonicalize_indices(iv,false); //symmetric
246 //something has changed while sorting indices, more evaluations later
247 return ex(sig) *lortensor(type,name,iv);
249 lorentzidx const & idx1=ex_to_lorentzidx(seq[0]);
250 lorentzidx const & idx2=ex_to_lorentzidx(seq[1]);
251 if ((!idx1.is_symbolic()) && (!idx2.is_symbolic())) {
252 //both indices are numeric
253 if ((idx1.get_value()==idx2.get_value())) {
255 if (idx1.get_value()==0){
259 if (idx1.is_covariant() != idx2.is_covariant()) {
260 // (_i,~i) or (~i,_i), i = 1...3
263 // (_i,_i) or (~i,~i), i= 1...3
268 // at least one off-diagonal
271 } else if (idx1.is_symbolic() && idx1.is_co_contra_pair(idx2)) {
272 return Dim()-idx1.get_dim_parallel_space();
275 return this -> hold();
280 int lortensor::compare_same_type(basic const & other) const
282 GINAC_ASSERT(is_of_type(other,lortensor));
283 const lortensor *o = static_cast <const lortensor *> (&other);
285 if (type==lortensor_rankn) {
286 if (serial!=o->serial) {
287 return serial < o->serial ? -1 : 1;
290 return indexed::compare_same_type(other);
292 return type < o->type ? -1 : 1;
295 bool lortensor::is_equal_same_type(basic const & other) const
297 GINAC_ASSERT(is_of_type(other,lortensor));
298 const lortensor *o=static_cast<const lortensor *> (&other);
299 if (type!=o->type) return false;
300 if (type==lortensor_rankn) {
301 if (serial!=o->serial) return false;
303 return indexed::is_equal_same_type(other);
306 unsigned lortensor::return_type(void) const
308 return return_types::commutative;
310 unsigned lortensor::return_type_tinfo(void) const
314 ex lortensor::thisexprseq(exvector const & v) const
316 return lortensor(type,name,serial,v);
318 ex lortensor::thisexprseq(exvector *vp) const
320 return lortensor(type,name,serial,vp);
324 // non-virtual functions in this class
329 void lortensor::setname(string const & n)
334 bool lortensor::all_of_type_lorentzidx(void) const
336 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++ cit) {
337 if (!is_ex_of_type(*cit,lorentzidx)) {
346 string & lortensor::autoname_prefix(void)
348 static string * s=new string("lortensor");
353 // static member variables
358 unsigned lortensor::next_serial=0;
364 lortensor lortensor_g(ex const & mu, ex const & nu)
366 return lortensor(lortensor::lortensor_g,"",mu,nu);
369 lortensor lortensor_epsilon(ex const & mu, ex const & nu, ex const & rho, ex const & sigma)
371 return lortensor(lortensor::lortensor_epsilon,"",mu,nu,rho,sigma);
374 lortensor lortensor_rank1(string const & n, ex const & mu)
376 return lortensor(lortensor::lortensor_rank1,n,mu);
379 lortensor lortensor_rank2(string const & n, ex const & mu, ex const & nu)
381 return lortensor(lortensor::lortensor_rank2,n,mu,nu);
384 ex simplify_lortensor_mul(ex const & m)
386 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
387 exvector v_contracted;
389 // collect factors in an exvector, store squares twice
391 v_contracted.reserve(2*n);
392 for (int i=0; i<n; ++i) {
394 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
395 v_contracted.push_back(f.op(0));
396 v_contracted.push_back(f.op(0));
398 v_contracted.push_back(f);
402 unsigned replacements;
403 bool something_changed=false;
405 exvector::iterator it=v_contracted.begin();
406 while (it!=v_contracted.end()) {
407 // process only lor_g objects
408 if (is_ex_exactly_of_type(*it,lortensor) &&
409 (ex_to_lortensor(*it).type==lortensor::lortensor_g)) {
410 lortensor const & g=ex_to_lortensor(*it);
411 GINAC_ASSERT(g.seq.size()==2);
412 idx const & first_idx=ex_to_lorentzidx(g.seq[0]);
413 idx const & second_idx=ex_to_lorentzidx(g.seq[1]);
414 // g_{mu,mu} should have been contracted in lortensor::eval()
415 GINAC_ASSERT(!first_idx.is_equal(second_idx));
416 ex saved_g=*it; // save to restore it later
418 // try to contract first index
420 if (first_idx.is_symbolic()) {
421 replacements = subs_index_in_exvector(v_contracted,
422 first_idx.toggle_covariant(),second_idx);
423 if (replacements==0) {
424 // not contracted, restore g object
427 // a contracted index should occur exactly once
428 GINAC_ASSERT(replacements==1);
430 something_changed=true;
434 // try second index only if first was not contracted
435 if ((replacements==0)&&(second_idx.is_symbolic())) {
436 // first index not contracted, *it is again the original g object
437 replacements = subs_index_in_exvector(v_contracted,
438 second_idx.toggle_covariant(),first_idx);
439 if (replacements==0) {
440 // not contracted except in itself, restore g object
443 // a contracted index should occur exactly once
444 GINAC_ASSERT(replacements==1);
446 something_changed=true;
452 if (something_changed) {
453 return mul(v_contracted);
458 ex simplify_lortensor(ex const & e)
460 // all simplification is done on expanded objects
461 ex e_expanded=e.expand();
463 // simplification of sum=sum of simplifications
464 if (is_ex_exactly_of_type(e_expanded,add)) {
466 for (int i=0; i<e_expanded.nops(); ++i) {
467 sum += simplify_lortensor(e_expanded.op(i));
472 // simplification of commutative product=commutative product of simplifications
473 if (is_ex_exactly_of_type(e_expanded,mul)) {
474 return simplify_lortensor_mul(e);
477 // cannot do anything
483 static symbol * d=new symbol("dim");
487 #ifndef NO_GINAC_NAMESPACE
489 #endif // ndef NO_GINAC_NAMESPACE