3 * Implementation of GiNaC's simp_lor objects.
4 * No real implementation yet, to be done.
6 * GiNaC Copyright (C) 1999 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
33 // default constructor, destructor, copy constructor assignment operator and helpers
38 simp_lor::simp_lor() : type(invalid)
40 debugmsg("simp_lor default constructor",LOGLEVEL_CONSTRUCT);
41 tinfo_key=TINFO_SIMP_LOR;
46 debugmsg("simp_lor destructor",LOGLEVEL_DESTRUCT);
50 simp_lor::simp_lor(simp_lor const & other)
52 debugmsg("simp_lor copy constructor",LOGLEVEL_CONSTRUCT);
56 simp_lor const & simp_lor::operator=(simp_lor const & other)
58 debugmsg("simp_lor operator=",LOGLEVEL_ASSIGNMENT);
68 void simp_lor::copy(simp_lor const & other)
75 void simp_lor::destroy(bool call_parent)
78 indexed::destroy(call_parent);
88 simp_lor::simp_lor(simp_lor_types const t) : type(t)
90 debugmsg("simp_lor constructor from simp_lor_types",LOGLEVEL_CONSTRUCT);
91 tinfo_key=TINFO_SIMP_LOR;
94 simp_lor::simp_lor(simp_lor_types const t, ex const & i1, ex const & i2) :
95 indexed(i1,i2), type(t)
97 debugmsg("simp_lor constructor from simp_lor_types,ex,ex",LOGLEVEL_CONSTRUCT);
98 tinfo_key=TINFO_SIMP_LOR;
99 ASSERT(all_of_type_lorentzidx());
102 simp_lor::simp_lor(simp_lor_types const t, string const & n, ex const & i1) :
103 indexed(i1), type(t), name(n)
105 debugmsg("simp_lor constructor from simp_lor_types,string,ex",LOGLEVEL_CONSTRUCT);
106 tinfo_key=TINFO_SIMP_LOR;
107 ASSERT(all_of_type_lorentzidx());
110 simp_lor::simp_lor(simp_lor_types const t, string const & n, exvector const & iv) :
111 indexed(iv), type(t), name(n)
113 debugmsg("simp_lor constructor from simp_lor_types,string,exvector",LOGLEVEL_CONSTRUCT);
114 tinfo_key=TINFO_SIMP_LOR;
115 ASSERT(all_of_type_lorentzidx());
118 simp_lor::simp_lor(simp_lor_types const t, string const & n, exvector * ivp) :
119 indexed(ivp), type(t), name(n)
121 debugmsg("simp_lor constructor from simp_lor_types,string,exvector*",LOGLEVEL_CONSTRUCT);
122 tinfo_key=TINFO_SIMP_LOR;
123 ASSERT(all_of_type_lorentzidx());
127 // functions overriding virtual functions from bases classes
132 basic * simp_lor::duplicate() const
134 debugmsg("simp_lor duplicate",LOGLEVEL_DUPLICATE);
135 return new simp_lor(*this);
138 void simp_lor::printraw(ostream & os) const
140 debugmsg("simp_lor printraw",LOGLEVEL_PRINT);
141 os << "simp_lor(type=" << (unsigned)type
142 << ",name=" << name << ",indices=";
144 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
147 void simp_lor::printtree(ostream & os, unsigned indent) const
149 debugmsg("simp_lor printtree",LOGLEVEL_PRINT);
150 os << string(indent,' ') << "simp_lor object: "
151 << "type=" << (unsigned)type
152 << ", name=" << name << ", ";
153 os << seq.size() << " indices" << endl;
154 printtreeindices(os,indent);
155 os << string(indent,' ') << "hash=" << hashvalue
156 << " (0x" << hex << hashvalue << dec << ")"
157 << ", flags=" << flags << endl;
160 void simp_lor::print(ostream & os, unsigned upper_precedence) const
162 debugmsg("simp_lor print",LOGLEVEL_PRINT);
172 os << "INVALID_SIMP_LOR_OBJECT";
178 void simp_lor::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
180 debugmsg("simp_lor print csrc",LOGLEVEL_PRINT);
181 print(os,upper_precedence);
184 bool simp_lor::info(unsigned inf) const
186 return indexed::info(inf);
189 ex simp_lor::eval(int level) const
191 if (type==simp_lor_g) {
192 // canonicalize indices
194 int sig=canonicalize_indices(iv,false); // symmetric
196 // something has changed while sorting indices, more evaluations later
197 if (sig==0) return exZERO();
198 return ex(sig)*simp_lor(type,name,iv);
200 lorentzidx const & idx1=ex_to_lorentzidx(seq[0]);
201 lorentzidx const & idx2=ex_to_lorentzidx(seq[1]);
202 if ((!idx1.is_symbolic())&&(!idx2.is_symbolic())) {
203 // both indices are numeric
204 if ((idx1.get_value()==idx2.get_value())) {
206 if (idx1.get_value()==0) {
210 if (idx1.is_covariant()!=idx2.is_covariant()) {
211 // (_i,~i) or (~i,_i), i=1..3
214 // (_i,_i) or (~i,~i), i=1..3
219 // at least one off-diagonal
222 } else if (idx1.is_symbolic() &&
223 idx1.is_co_contra_pair(idx2)) {
224 return Dim()-idx1.get_dim_parallel_space();
233 int simp_lor::compare_same_type(basic const & other) const
235 ASSERT(other.tinfo() == TINFO_SIMP_LOR);
236 const simp_lor *o = static_cast<const simp_lor *>(&other);
239 return indexed::compare_same_type(other);
241 return name.compare(o->name);
243 return type < o->type ? -1 : 1;
246 bool simp_lor::is_equal_same_type(basic const & other) const
248 ASSERT(other.tinfo() == TINFO_SIMP_LOR);
249 const simp_lor *o = static_cast<const simp_lor *>(&other);
250 if (type!=o->type) return false;
251 if (name!=o->name) return false;
252 return indexed::is_equal_same_type(other);
255 unsigned simp_lor::return_type(void) const
257 return return_types::commutative;
260 unsigned simp_lor::return_type_tinfo(void) const
265 ex simp_lor::thisexprseq(exvector const & v) const
267 return simp_lor(type,name,v);
270 ex simp_lor::thisexprseq(exvector * vp) const
272 return simp_lor(type,name,vp);
276 // virtual functions which can be overridden by derived classes
282 // non-virtual functions in this class
287 bool simp_lor::all_of_type_lorentzidx(void) const
289 // used only inside of ASSERTs
290 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
291 if (!is_ex_of_type(*cit,lorentzidx)) return false;
297 // static member variables
306 const simp_lor some_simp_lor;
307 type_info const & typeid_simp_lor=typeid(some_simp_lor);
313 simp_lor lor_g(ex const & mu, ex const & nu)
315 return simp_lor(simp_lor::simp_lor_g,mu,nu);
318 simp_lor lor_vec(string const & n, ex const & mu)
320 return simp_lor(simp_lor::simp_lor_vec,n,mu);
323 ex simplify_simp_lor_mul(ex const & m, scalar_products const & sp)
325 ASSERT(is_ex_exactly_of_type(m,mul));
326 exvector v_contracted;
328 // collect factors in an exvector, store squares twice
330 v_contracted.reserve(2*n);
331 for (int i=0; i<n; ++i) {
333 if (is_ex_exactly_of_type(f,power)&&f.op(1)==2) {
334 v_contracted.push_back(f.op(0));
335 v_contracted.push_back(f.op(0));
337 v_contracted.push_back(f);
341 unsigned replacements;
342 bool something_changed=false;
344 exvector::iterator it=v_contracted.begin();
345 while (it!=v_contracted.end()) {
346 // process only lor_g objects
347 if (is_ex_exactly_of_type(*it,simp_lor) &&
348 (ex_to_simp_lor(*it).type==simp_lor::simp_lor_g)) {
349 simp_lor const & g=ex_to_simp_lor(*it);
350 ASSERT(g.seq.size()==2);
351 idx const & first_idx=ex_to_lorentzidx(g.seq[0]);
352 idx const & second_idx=ex_to_lorentzidx(g.seq[1]);
353 // g_{mu,mu} should have been contracted in simp_lor::eval()
354 ASSERT(!first_idx.is_equal(second_idx));
355 ex saved_g=*it; // save to restore it later
357 // try to contract first index
359 if (first_idx.is_symbolic()) {
360 replacements = subs_index_in_exvector(v_contracted,
361 first_idx.toggle_covariant(),second_idx);
362 if (replacements==0) {
363 // not contracted, restore g object
366 // a contracted index should occur exactly once
367 ASSERT(replacements==1);
369 something_changed=true;
373 // try second index only if first was not contracted
374 if ((replacements==0)&&(second_idx.is_symbolic())) {
375 // first index not contracted, *it is again the original g object
376 replacements = subs_index_in_exvector(v_contracted,
377 second_idx.toggle_covariant(),first_idx);
378 if (replacements==0) {
379 // not contracted except in itself, restore g object
382 // a contracted index should occur exactly once
383 ASSERT(replacements==1);
385 something_changed=true;
392 // process only lor_vec objects
393 bool jump_to_next=false;
394 exvector::iterator it1=v_contracted.begin();
395 while (it1!=v_contracted.end()-1) {
396 if (is_ex_exactly_of_type(*it1,simp_lor) &&
397 (ex_to_simp_lor(*it1).type==simp_lor::simp_lor_vec)) {
398 exvector::iterator it2=it1+1;
399 while ((it2!=v_contracted.end())&&!jump_to_next) {
400 if (is_ex_exactly_of_type(*it2,simp_lor) &&
401 (ex_to_simp_lor(*it2).type==simp_lor::simp_lor_vec)) {
402 simp_lor const & vec1=ex_to_simp_lor(*it1);
403 simp_lor const & vec2=ex_to_simp_lor(*it2);
404 ASSERT(vec1.seq.size()==1);
405 ASSERT(vec2.seq.size()==1);
406 lorentzidx const & idx1=ex_to_lorentzidx(vec1.seq[0]);
407 lorentzidx const & idx2=ex_to_lorentzidx(vec2.seq[0]);
408 if (idx1.is_symbolic() &&
409 idx1.is_co_contra_pair(idx2) &&
410 sp.is_defined(vec1,vec2)) {
411 *it1=sp.evaluate(vec1,vec2);
413 something_changed=true;
423 if (something_changed) {
424 return mul(v_contracted);
429 ex simplify_simp_lor(ex const & e, scalar_products const & sp)
431 // all simplification is done on expanded objects
432 ex e_expanded=e.expand();
434 // simplification of sum=sum of simplifications
435 if (is_ex_exactly_of_type(e_expanded,add)) {
437 for (int i=0; i<e_expanded.nops(); ++i) {
438 sum += simplify_simp_lor(e_expanded.op(i),sp);
443 // simplification of commutative product=commutative product of simplifications
444 if (is_ex_exactly_of_type(e_expanded,mul)) {
445 return simplify_simp_lor_mul(e,sp);
448 // cannot do anything
454 static symbol * d=new symbol("dim");
462 void scalar_products::reg(simp_lor const & v1, simp_lor const & v2,
465 if (v1.compare_same_type(v2)>0) {
469 spm[make_key(v1,v2)]=sp;
472 bool scalar_products::is_defined(simp_lor const & v1, simp_lor const & v2) const
474 if (v1.compare_same_type(v2)>0) {
475 return is_defined(v2,v1);
477 return spm.find(make_key(v1,v2))!=spm.end();
480 ex scalar_products::evaluate(simp_lor const & v1, simp_lor const & v2) const
482 if (v1.compare_same_type(v2)>0) {
483 return evaluate(v2,v1);
485 return spm.find(make_key(v1,v2))->second;
488 void scalar_products::debugprint(void) const
490 cerr << "map size=" << spm.size() << endl;
491 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
492 spmapkey const & k=(*cit).first;
493 cerr << "item key=((" << k.first.first
494 << "," << k.first.second << "),";
495 k.second.printraw(cerr);
496 cerr << ") value=" << (*cit).second << endl;
500 spmapkey scalar_products::make_key(simp_lor const & v1, simp_lor const & v2)
502 ASSERT(v1.type==simp_lor::simp_lor_vec);
503 ASSERT(v2.type==simp_lor::simp_lor_vec);
504 lorentzidx anon=ex_to_lorentzidx(v1.seq[0]).create_anonymous_representative();
505 ASSERT(anon.is_equal_same_type(ex_to_lorentzidx(v2.seq[0]).create_anonymous_representative()));
506 return spmapkey(strstrpair(v1.name,v2.name),anon);