3 * Implementation of GiNaC's simp_lor objects.
4 * No real implementation yet, to 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
40 // default constructor, destructor, copy constructor assignment operator and helpers
45 simp_lor::simp_lor() : type(invalid)
47 debugmsg("simp_lor default constructor",LOGLEVEL_CONSTRUCT);
48 tinfo_key=TINFO_simp_lor;
53 debugmsg("simp_lor destructor",LOGLEVEL_DESTRUCT);
57 simp_lor::simp_lor(simp_lor const & other)
59 debugmsg("simp_lor copy constructor",LOGLEVEL_CONSTRUCT);
63 simp_lor const & simp_lor::operator=(simp_lor const & other)
65 debugmsg("simp_lor operator=",LOGLEVEL_ASSIGNMENT);
75 void simp_lor::copy(simp_lor const & other)
82 void simp_lor::destroy(bool call_parent)
85 indexed::destroy(call_parent);
95 simp_lor::simp_lor(simp_lor_types const t) : type(t)
97 debugmsg("simp_lor constructor from simp_lor_types",LOGLEVEL_CONSTRUCT);
98 tinfo_key=TINFO_simp_lor;
101 simp_lor::simp_lor(simp_lor_types const t, ex const & i1, ex const & i2) :
102 indexed(i1,i2), type(t)
104 debugmsg("simp_lor constructor from simp_lor_types,ex,ex",LOGLEVEL_CONSTRUCT);
105 tinfo_key=TINFO_simp_lor;
106 GINAC_ASSERT(all_of_type_lorentzidx());
109 simp_lor::simp_lor(simp_lor_types const t, string const & n, ex const & i1) :
110 indexed(i1), type(t), name(n)
112 debugmsg("simp_lor constructor from simp_lor_types,string,ex",LOGLEVEL_CONSTRUCT);
113 tinfo_key=TINFO_simp_lor;
114 GINAC_ASSERT(all_of_type_lorentzidx());
117 simp_lor::simp_lor(simp_lor_types const t, string const & n, exvector const & iv) :
118 indexed(iv), type(t), name(n)
120 debugmsg("simp_lor constructor from simp_lor_types,string,exvector",LOGLEVEL_CONSTRUCT);
121 tinfo_key=TINFO_simp_lor;
122 GINAC_ASSERT(all_of_type_lorentzidx());
125 simp_lor::simp_lor(simp_lor_types const t, string const & n, exvector * ivp) :
126 indexed(ivp), type(t), name(n)
128 debugmsg("simp_lor constructor from simp_lor_types,string,exvector*",LOGLEVEL_CONSTRUCT);
129 tinfo_key=TINFO_simp_lor;
130 GINAC_ASSERT(all_of_type_lorentzidx());
134 // functions overriding virtual functions from bases classes
139 basic * simp_lor::duplicate() const
141 debugmsg("simp_lor duplicate",LOGLEVEL_DUPLICATE);
142 return new simp_lor(*this);
145 void simp_lor::printraw(ostream & os) const
147 debugmsg("simp_lor printraw",LOGLEVEL_PRINT);
148 os << "simp_lor(type=" << (unsigned)type
149 << ",name=" << name << ",indices=";
151 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
154 void simp_lor::printtree(ostream & os, unsigned indent) const
156 debugmsg("simp_lor printtree",LOGLEVEL_PRINT);
157 os << string(indent,' ') << "simp_lor object: "
158 << "type=" << (unsigned)type
159 << ", name=" << name << ", ";
160 os << seq.size() << " indices" << endl;
161 printtreeindices(os,indent);
162 os << string(indent,' ') << "hash=" << hashvalue
163 << " (0x" << hex << hashvalue << dec << ")"
164 << ", flags=" << flags << endl;
167 void simp_lor::print(ostream & os, unsigned upper_precedence) const
169 debugmsg("simp_lor print",LOGLEVEL_PRINT);
179 os << "INVALID_SIMP_LOR_OBJECT";
185 void simp_lor::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
187 debugmsg("simp_lor print csrc",LOGLEVEL_PRINT);
188 print(os,upper_precedence);
191 bool simp_lor::info(unsigned inf) const
193 return indexed::info(inf);
196 ex simp_lor::eval(int level) const
198 if (type==simp_lor_g) {
199 // canonicalize indices
201 int sig=canonicalize_indices(iv,false); // symmetric
203 // something has changed while sorting indices, more evaluations later
204 if (sig==0) return exZERO();
205 return ex(sig)*simp_lor(type,name,iv);
207 lorentzidx const & idx1=ex_to_lorentzidx(seq[0]);
208 lorentzidx const & idx2=ex_to_lorentzidx(seq[1]);
209 if ((!idx1.is_symbolic())&&(!idx2.is_symbolic())) {
210 // both indices are numeric
211 if ((idx1.get_value()==idx2.get_value())) {
213 if (idx1.get_value()==0) {
217 if (idx1.is_covariant()!=idx2.is_covariant()) {
218 // (_i,~i) or (~i,_i), i=1..3
221 // (_i,_i) or (~i,~i), i=1..3
226 // at least one off-diagonal
229 } else if (idx1.is_symbolic() &&
230 idx1.is_co_contra_pair(idx2)) {
231 return Dim()-idx1.get_dim_parallel_space();
240 int simp_lor::compare_same_type(basic const & other) const
242 GINAC_ASSERT(other.tinfo() == TINFO_simp_lor);
243 const simp_lor *o = static_cast<const simp_lor *>(&other);
246 return indexed::compare_same_type(other);
248 return name.compare(o->name);
250 return type < o->type ? -1 : 1;
253 bool simp_lor::is_equal_same_type(basic const & other) const
255 GINAC_ASSERT(other.tinfo() == TINFO_simp_lor);
256 const simp_lor *o = static_cast<const simp_lor *>(&other);
257 if (type!=o->type) return false;
258 if (name!=o->name) return false;
259 return indexed::is_equal_same_type(other);
262 unsigned simp_lor::return_type(void) const
264 return return_types::commutative;
267 unsigned simp_lor::return_type_tinfo(void) const
272 ex simp_lor::thisexprseq(exvector const & v) const
274 return simp_lor(type,name,v);
277 ex simp_lor::thisexprseq(exvector * vp) const
279 return simp_lor(type,name,vp);
283 // virtual functions which can be overridden by derived classes
289 // non-virtual functions in this class
294 bool simp_lor::all_of_type_lorentzidx(void) const
296 // used only inside of ASSERTs
297 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
298 if (!is_ex_of_type(*cit,lorentzidx)) return false;
304 // static member variables
313 const simp_lor some_simp_lor;
314 type_info const & typeid_simp_lor=typeid(some_simp_lor);
320 simp_lor lor_g(ex const & mu, ex const & nu)
322 return simp_lor(simp_lor::simp_lor_g,mu,nu);
325 simp_lor lor_vec(string const & n, ex const & mu)
327 return simp_lor(simp_lor::simp_lor_vec,n,mu);
330 ex simplify_simp_lor_mul(ex const & m, scalar_products const & sp)
332 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
333 exvector v_contracted;
335 // collect factors in an exvector, store squares twice
337 v_contracted.reserve(2*n);
338 for (int i=0; i<n; ++i) {
340 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(exTWO())) {
341 v_contracted.push_back(f.op(0));
342 v_contracted.push_back(f.op(0));
344 v_contracted.push_back(f);
348 unsigned replacements;
349 bool something_changed=false;
351 exvector::iterator it=v_contracted.begin();
352 while (it!=v_contracted.end()) {
353 // process only lor_g objects
354 if (is_ex_exactly_of_type(*it,simp_lor) &&
355 (ex_to_simp_lor(*it).type==simp_lor::simp_lor_g)) {
356 simp_lor const & g=ex_to_simp_lor(*it);
357 GINAC_ASSERT(g.seq.size()==2);
358 idx const & first_idx=ex_to_lorentzidx(g.seq[0]);
359 idx const & second_idx=ex_to_lorentzidx(g.seq[1]);
360 // g_{mu,mu} should have been contracted in simp_lor::eval()
361 GINAC_ASSERT(!first_idx.is_equal(second_idx));
362 ex saved_g=*it; // save to restore it later
364 // try to contract first index
366 if (first_idx.is_symbolic()) {
367 replacements = subs_index_in_exvector(v_contracted,
368 first_idx.toggle_covariant(),second_idx);
369 if (replacements==0) {
370 // not contracted, restore g object
373 // a contracted index should occur exactly once
374 GINAC_ASSERT(replacements==1);
376 something_changed=true;
380 // try second index only if first was not contracted
381 if ((replacements==0)&&(second_idx.is_symbolic())) {
382 // first index not contracted, *it is again the original g object
383 replacements = subs_index_in_exvector(v_contracted,
384 second_idx.toggle_covariant(),first_idx);
385 if (replacements==0) {
386 // not contracted except in itself, restore g object
389 // a contracted index should occur exactly once
390 GINAC_ASSERT(replacements==1);
392 something_changed=true;
399 // process only lor_vec objects
400 bool jump_to_next=false;
401 exvector::iterator it1=v_contracted.begin();
402 while (it1!=v_contracted.end()-1) {
403 if (is_ex_exactly_of_type(*it1,simp_lor) &&
404 (ex_to_simp_lor(*it1).type==simp_lor::simp_lor_vec)) {
405 exvector::iterator it2=it1+1;
406 while ((it2!=v_contracted.end())&&!jump_to_next) {
407 if (is_ex_exactly_of_type(*it2,simp_lor) &&
408 (ex_to_simp_lor(*it2).type==simp_lor::simp_lor_vec)) {
409 simp_lor const & vec1=ex_to_simp_lor(*it1);
410 simp_lor const & vec2=ex_to_simp_lor(*it2);
411 GINAC_ASSERT(vec1.seq.size()==1);
412 GINAC_ASSERT(vec2.seq.size()==1);
413 lorentzidx const & idx1=ex_to_lorentzidx(vec1.seq[0]);
414 lorentzidx const & idx2=ex_to_lorentzidx(vec2.seq[0]);
415 if (idx1.is_symbolic() &&
416 idx1.is_co_contra_pair(idx2) &&
417 sp.is_defined(vec1,vec2)) {
418 *it1=sp.evaluate(vec1,vec2);
420 something_changed=true;
430 if (something_changed) {
431 return mul(v_contracted);
436 ex simplify_simp_lor(ex const & e, scalar_products const & sp)
438 // all simplification is done on expanded objects
439 ex e_expanded=e.expand();
441 // simplification of sum=sum of simplifications
442 if (is_ex_exactly_of_type(e_expanded,add)) {
444 for (int i=0; i<e_expanded.nops(); ++i) {
445 sum += simplify_simp_lor(e_expanded.op(i),sp);
450 // simplification of commutative product=commutative product of simplifications
451 if (is_ex_exactly_of_type(e_expanded,mul)) {
452 return simplify_simp_lor_mul(e,sp);
455 // cannot do anything
461 static symbol * d=new symbol("dim");
469 void scalar_products::reg(simp_lor const & v1, simp_lor const & v2,
472 if (v1.compare_same_type(v2)>0) {
476 spm[make_key(v1,v2)]=sp;
479 bool scalar_products::is_defined(simp_lor const & v1, simp_lor const & v2) const
481 if (v1.compare_same_type(v2)>0) {
482 return is_defined(v2,v1);
484 return spm.find(make_key(v1,v2))!=spm.end();
487 ex scalar_products::evaluate(simp_lor const & v1, simp_lor const & v2) const
489 if (v1.compare_same_type(v2)>0) {
490 return evaluate(v2,v1);
492 return spm.find(make_key(v1,v2))->second;
495 void scalar_products::debugprint(void) const
497 cerr << "map size=" << spm.size() << endl;
498 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
499 spmapkey const & k=(*cit).first;
500 cerr << "item key=((" << k.first.first
501 << "," << k.first.second << "),";
502 k.second.printraw(cerr);
503 cerr << ") value=" << (*cit).second << endl;
507 spmapkey scalar_products::make_key(simp_lor const & v1, simp_lor const & v2)
509 GINAC_ASSERT(v1.type==simp_lor::simp_lor_vec);
510 GINAC_ASSERT(v2.type==simp_lor::simp_lor_vec);
511 lorentzidx anon=ex_to_lorentzidx(v1.seq[0]).create_anonymous_representative();
512 GINAC_ASSERT(anon.is_equal_same_type(ex_to_lorentzidx(v2.seq[0]).create_anonymous_representative()));
513 return spmapkey(strstrpair(v1.name,v2.name),anon);