3 * Implementation of GiNaC's simp_lor objects.
4 * No real implementation yet, to be done. */
16 // default constructor, destructor, copy constructor assignment operator and helpers
21 simp_lor::simp_lor() : type(invalid)
23 debugmsg("simp_lor default constructor",LOGLEVEL_CONSTRUCT);
24 tinfo_key=TINFO_SIMP_LOR;
29 debugmsg("simp_lor destructor",LOGLEVEL_DESTRUCT);
33 simp_lor::simp_lor(simp_lor const & other)
35 debugmsg("simp_lor copy constructor",LOGLEVEL_CONSTRUCT);
39 simp_lor const & simp_lor::operator=(simp_lor const & other)
41 debugmsg("simp_lor operator=",LOGLEVEL_ASSIGNMENT);
51 void simp_lor::copy(simp_lor const & other)
58 void simp_lor::destroy(bool call_parent)
61 indexed::destroy(call_parent);
71 simp_lor::simp_lor(simp_lor_types const t) : type(t)
73 debugmsg("simp_lor constructor from simp_lor_types",LOGLEVEL_CONSTRUCT);
74 tinfo_key=TINFO_SIMP_LOR;
77 simp_lor::simp_lor(simp_lor_types const t, ex const & i1, ex const & i2) :
78 indexed(i1,i2), type(t)
80 debugmsg("simp_lor constructor from simp_lor_types,ex,ex",LOGLEVEL_CONSTRUCT);
81 tinfo_key=TINFO_SIMP_LOR;
82 ASSERT(all_of_type_lorentzidx());
85 simp_lor::simp_lor(simp_lor_types const t, string const & n, ex const & i1) :
86 indexed(i1), type(t), name(n)
88 debugmsg("simp_lor constructor from simp_lor_types,string,ex",LOGLEVEL_CONSTRUCT);
89 tinfo_key=TINFO_SIMP_LOR;
90 ASSERT(all_of_type_lorentzidx());
93 simp_lor::simp_lor(simp_lor_types const t, string const & n, exvector const & iv) :
94 indexed(iv), type(t), name(n)
96 debugmsg("simp_lor constructor from simp_lor_types,string,exvector",LOGLEVEL_CONSTRUCT);
97 tinfo_key=TINFO_SIMP_LOR;
98 ASSERT(all_of_type_lorentzidx());
101 simp_lor::simp_lor(simp_lor_types const t, string const & n, exvector * ivp) :
102 indexed(ivp), type(t), name(n)
104 debugmsg("simp_lor constructor from simp_lor_types,string,exvector*",LOGLEVEL_CONSTRUCT);
105 tinfo_key=TINFO_SIMP_LOR;
106 ASSERT(all_of_type_lorentzidx());
110 // functions overriding virtual functions from bases classes
115 basic * simp_lor::duplicate() const
117 debugmsg("simp_lor duplicate",LOGLEVEL_DUPLICATE);
118 return new simp_lor(*this);
121 void simp_lor::printraw(ostream & os) const
123 debugmsg("simp_lor printraw",LOGLEVEL_PRINT);
124 os << "simp_lor(type=" << (unsigned)type
125 << ",name=" << name << ",indices=";
127 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
130 void simp_lor::printtree(ostream & os, unsigned indent) const
132 debugmsg("simp_lor printtree",LOGLEVEL_PRINT);
133 os << string(indent,' ') << "simp_lor object: "
134 << "type=" << (unsigned)type
135 << ", name=" << name << ", ";
136 os << seq.size() << " indices" << endl;
137 printtreeindices(os,indent);
138 os << string(indent,' ') << "hash=" << hashvalue
139 << " (0x" << hex << hashvalue << dec << ")"
140 << ", flags=" << flags << endl;
143 void simp_lor::print(ostream & os, unsigned upper_precedence) const
145 debugmsg("simp_lor print",LOGLEVEL_PRINT);
155 os << "INVALID_SIMP_LOR_OBJECT";
161 void simp_lor::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
163 debugmsg("simp_lor print csrc",LOGLEVEL_PRINT);
164 print(os,upper_precedence);
167 bool simp_lor::info(unsigned inf) const
169 return indexed::info(inf);
172 ex simp_lor::eval(int level) const
174 if (type==simp_lor_g) {
175 // canonicalize indices
177 int sig=canonicalize_indices(iv,false); // symmetric
179 // something has changed while sorting indices, more evaluations later
180 if (sig==0) return exZERO();
181 return ex(sig)*simp_lor(type,name,iv);
183 lorentzidx const & idx1=ex_to_lorentzidx(seq[0]);
184 lorentzidx const & idx2=ex_to_lorentzidx(seq[1]);
185 if ((!idx1.is_symbolic())&&(!idx2.is_symbolic())) {
186 // both indices are numeric
187 if ((idx1.get_value()==idx2.get_value())) {
189 if (idx1.get_value()==0) {
193 if (idx1.is_covariant()!=idx2.is_covariant()) {
194 // (_i,~i) or (~i,_i), i=1..3
197 // (_i,_i) or (~i,~i), i=1..3
202 // at least one off-diagonal
205 } else if (idx1.is_symbolic() &&
206 idx1.is_co_contra_pair(idx2)) {
207 return Dim()-idx1.get_dim_parallel_space();
216 int simp_lor::compare_same_type(basic const & other) const
218 ASSERT(other.tinfo() == TINFO_SIMP_LOR);
219 const simp_lor *o = static_cast<const simp_lor *>(&other);
222 return indexed::compare_same_type(other);
224 return name.compare(o->name);
226 return type < o->type ? -1 : 1;
229 bool simp_lor::is_equal_same_type(basic const & other) const
231 ASSERT(other.tinfo() == TINFO_SIMP_LOR);
232 const simp_lor *o = static_cast<const simp_lor *>(&other);
233 if (type!=o->type) return false;
234 if (name!=o->name) return false;
235 return indexed::is_equal_same_type(other);
238 unsigned simp_lor::return_type(void) const
240 return return_types::commutative;
243 unsigned simp_lor::return_type_tinfo(void) const
248 ex simp_lor::thisexprseq(exvector const & v) const
250 return simp_lor(type,name,v);
253 ex simp_lor::thisexprseq(exvector * vp) const
255 return simp_lor(type,name,vp);
259 // virtual functions which can be overridden by derived classes
265 // non-virtual functions in this class
270 bool simp_lor::all_of_type_lorentzidx(void) const
272 // used only inside of ASSERTs
273 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
274 if (!is_ex_of_type(*cit,lorentzidx)) return false;
280 // static member variables
289 const simp_lor some_simp_lor;
290 type_info const & typeid_simp_lor=typeid(some_simp_lor);
296 simp_lor lor_g(ex const & mu, ex const & nu)
298 return simp_lor(simp_lor::simp_lor_g,mu,nu);
301 simp_lor lor_vec(string const & n, ex const & mu)
303 return simp_lor(simp_lor::simp_lor_vec,n,mu);
306 ex simplify_simp_lor_mul(ex const & m, scalar_products const & sp)
308 ASSERT(is_ex_exactly_of_type(m,mul));
309 exvector v_contracted;
311 // collect factors in an exvector, store squares twice
313 v_contracted.reserve(2*n);
314 for (int i=0; i<n; ++i) {
316 if (is_ex_exactly_of_type(f,power)&&f.op(1)==2) {
317 v_contracted.push_back(f.op(0));
318 v_contracted.push_back(f.op(0));
320 v_contracted.push_back(f);
324 unsigned replacements;
325 bool something_changed=false;
327 exvector::iterator it=v_contracted.begin();
328 while (it!=v_contracted.end()) {
329 // process only lor_g objects
330 if (is_ex_exactly_of_type(*it,simp_lor) &&
331 (ex_to_simp_lor(*it).type==simp_lor::simp_lor_g)) {
332 simp_lor const & g=ex_to_simp_lor(*it);
333 ASSERT(g.seq.size()==2);
334 idx const & first_idx=ex_to_lorentzidx(g.seq[0]);
335 idx const & second_idx=ex_to_lorentzidx(g.seq[1]);
336 // g_{mu,mu} should have been contracted in simp_lor::eval()
337 ASSERT(!first_idx.is_equal(second_idx));
338 ex saved_g=*it; // save to restore it later
340 // try to contract first index
342 if (first_idx.is_symbolic()) {
343 replacements = subs_index_in_exvector(v_contracted,
344 first_idx.toggle_covariant(),second_idx);
345 if (replacements==0) {
346 // not contracted, restore g object
349 // a contracted index should occur exactly once
350 ASSERT(replacements==1);
352 something_changed=true;
356 // try second index only if first was not contracted
357 if ((replacements==0)&&(second_idx.is_symbolic())) {
358 // first index not contracted, *it is again the original g object
359 replacements = subs_index_in_exvector(v_contracted,
360 second_idx.toggle_covariant(),first_idx);
361 if (replacements==0) {
362 // not contracted except in itself, restore g object
365 // a contracted index should occur exactly once
366 ASSERT(replacements==1);
368 something_changed=true;
375 // process only lor_vec objects
376 bool jump_to_next=false;
377 exvector::iterator it1=v_contracted.begin();
378 while (it1!=v_contracted.end()-1) {
379 if (is_ex_exactly_of_type(*it1,simp_lor) &&
380 (ex_to_simp_lor(*it1).type==simp_lor::simp_lor_vec)) {
381 exvector::iterator it2=it1+1;
382 while ((it2!=v_contracted.end())&&!jump_to_next) {
383 if (is_ex_exactly_of_type(*it2,simp_lor) &&
384 (ex_to_simp_lor(*it2).type==simp_lor::simp_lor_vec)) {
385 simp_lor const & vec1=ex_to_simp_lor(*it1);
386 simp_lor const & vec2=ex_to_simp_lor(*it2);
387 ASSERT(vec1.seq.size()==1);
388 ASSERT(vec2.seq.size()==1);
389 lorentzidx const & idx1=ex_to_lorentzidx(vec1.seq[0]);
390 lorentzidx const & idx2=ex_to_lorentzidx(vec2.seq[0]);
391 if (idx1.is_symbolic() &&
392 idx1.is_co_contra_pair(idx2) &&
393 sp.is_defined(vec1,vec2)) {
394 *it1=sp.evaluate(vec1,vec2);
396 something_changed=true;
406 if (something_changed) {
407 return mul(v_contracted);
412 ex simplify_simp_lor(ex const & e, scalar_products const & sp)
414 // all simplification is done on expanded objects
415 ex e_expanded=e.expand();
417 // simplification of sum=sum of simplifications
418 if (is_ex_exactly_of_type(e_expanded,add)) {
420 for (int i=0; i<e_expanded.nops(); ++i) {
421 sum += simplify_simp_lor(e_expanded.op(i),sp);
426 // simplification of commutative product=commutative product of simplifications
427 if (is_ex_exactly_of_type(e_expanded,mul)) {
428 return simplify_simp_lor_mul(e,sp);
431 // cannot do anything
437 static symbol * d=new symbol("dim");
445 void scalar_products::reg(simp_lor const & v1, simp_lor const & v2,
448 if (v1.compare_same_type(v2)>0) {
452 spm[make_key(v1,v2)]=sp;
455 bool scalar_products::is_defined(simp_lor const & v1, simp_lor const & v2) const
457 if (v1.compare_same_type(v2)>0) {
458 return is_defined(v2,v1);
460 return spm.find(make_key(v1,v2))!=spm.end();
463 ex scalar_products::evaluate(simp_lor const & v1, simp_lor const & v2) const
465 if (v1.compare_same_type(v2)>0) {
466 return evaluate(v2,v1);
468 return spm.find(make_key(v1,v2))->second;
471 void scalar_products::debugprint(void) const
473 cerr << "map size=" << spm.size() << endl;
474 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
475 spmapkey const & k=(*cit).first;
476 cerr << "item key=((" << k.first.first
477 << "," << k.first.second << "),";
478 k.second.printraw(cerr);
479 cerr << ") value=" << (*cit).second << endl;
483 spmapkey scalar_products::make_key(simp_lor const & v1, simp_lor const & v2)
485 ASSERT(v1.type==simp_lor::simp_lor_vec);
486 ASSERT(v2.type==simp_lor::simp_lor_vec);
487 lorentzidx anon=ex_to_lorentzidx(v1.seq[0]).create_anonymous_representative();
488 ASSERT(anon.is_equal_same_type(ex_to_lorentzidx(v2.seq[0]).create_anonymous_representative()));
489 return spmapkey(strstrpair(v1.name,v2.name),anon);