3 * Implementation of GiNaC's simp_lor objects.
4 * No real implementation yet, to be done. */
7 * GiNaC Copyright (C) 1999-2001 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 #ifndef NO_NAMESPACE_GINAC
39 #endif // ndef NO_NAMESPACE_GINAC
42 // default constructor, destructor, copy constructor assignment operator and helpers
47 simp_lor::simp_lor() : type(invalid)
49 debugmsg("simp_lor default constructor",LOGLEVEL_CONSTRUCT);
50 tinfo_key=TINFO_simp_lor;
55 debugmsg("simp_lor destructor",LOGLEVEL_DESTRUCT);
59 simp_lor::simp_lor(const simp_lor & other)
61 debugmsg("simp_lor copy constructor",LOGLEVEL_CONSTRUCT);
65 const simp_lor & simp_lor::operator=(const simp_lor & other)
67 debugmsg("simp_lor operator=",LOGLEVEL_ASSIGNMENT);
77 void simp_lor::copy(const simp_lor & other)
84 void simp_lor::destroy(bool call_parent)
87 indexed::destroy(call_parent);
97 simp_lor::simp_lor(simp_lor_types const t) : type(t)
99 debugmsg("simp_lor constructor from simp_lor_types",LOGLEVEL_CONSTRUCT);
100 tinfo_key=TINFO_simp_lor;
103 simp_lor::simp_lor(simp_lor_types const t, const ex & i1, const ex & i2)
104 : indexed(i1,i2), type(t)
106 debugmsg("simp_lor constructor from simp_lor_types,ex,ex",LOGLEVEL_CONSTRUCT);
107 tinfo_key=TINFO_simp_lor;
108 GINAC_ASSERT(all_of_type_lorentzidx());
111 simp_lor::simp_lor(simp_lor_types const t, const std::string & n, const ex & i1)
112 : indexed(i1), type(t), name(n)
114 debugmsg("simp_lor constructor from simp_lor_types,string,ex",LOGLEVEL_CONSTRUCT);
115 tinfo_key=TINFO_simp_lor;
116 GINAC_ASSERT(all_of_type_lorentzidx());
119 simp_lor::simp_lor(simp_lor_types const t, const std::string & n, const exvector & iv)
120 : indexed(iv), type(t), name(n)
122 debugmsg("simp_lor constructor from simp_lor_types,string,exvector",LOGLEVEL_CONSTRUCT);
123 tinfo_key=TINFO_simp_lor;
124 GINAC_ASSERT(all_of_type_lorentzidx());
127 simp_lor::simp_lor(simp_lor_types const t, const std::string & n, exvector * ivp)
128 : indexed(ivp), type(t), name(n)
130 debugmsg("simp_lor constructor from simp_lor_types,string,exvector*",LOGLEVEL_CONSTRUCT);
131 tinfo_key=TINFO_simp_lor;
132 GINAC_ASSERT(all_of_type_lorentzidx());
136 // functions overriding virtual functions from bases classes
141 basic * simp_lor::duplicate() const
143 debugmsg("simp_lor duplicate",LOGLEVEL_DUPLICATE);
144 return new simp_lor(*this);
147 void simp_lor::printraw(std::ostream & os) const
149 debugmsg("simp_lor printraw",LOGLEVEL_PRINT);
150 os << "simp_lor(type=" << (unsigned)type
151 << ",name=" << name << ",indices=";
153 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
156 void simp_lor::printtree(std::ostream & os, unsigned indent) const
158 debugmsg("simp_lor printtree",LOGLEVEL_PRINT);
159 os << std::string(indent,' ') << "simp_lor object: "
160 << "type=" << (unsigned)type
161 << ", name=" << name << ", ";
162 os << seq.size() << " indices" << std::endl;
163 printtreeindices(os,indent);
164 os << std::string(indent,' ') << "hash=" << hashvalue
165 << " (0x" << std::hex << hashvalue << std::dec << ")"
166 << ", flags=" << flags << std::endl;
169 void simp_lor::print(std::ostream & os, unsigned upper_precedence) const
171 debugmsg("simp_lor print",LOGLEVEL_PRINT);
181 os << "INVALID_SIMP_LOR_OBJECT";
187 void simp_lor::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
189 debugmsg("simp_lor print csrc",LOGLEVEL_PRINT);
190 print(os,upper_precedence);
193 bool simp_lor::info(unsigned inf) const
195 return indexed::info(inf);
198 ex simp_lor::eval(int level) const
200 if (type==simp_lor_g) {
201 // canonicalize indices
203 int sig=canonicalize_indices(iv,false); // symmetric
205 // something has changed while sorting indices, more evaluations later
206 if (sig==0) return _ex0();
207 return ex(sig)*simp_lor(type,name,iv);
209 const lorentzidx & idx1=ex_to_lorentzidx(seq[0]);
210 const lorentzidx & idx2=ex_to_lorentzidx(seq[1]);
211 if ((!idx1.is_symbolic())&&(!idx2.is_symbolic())) {
212 // both indices are numeric
213 if ((idx1.get_value()==idx2.get_value())) {
215 if (idx1.get_value()==0) {
219 if (idx1.is_covariant()!=idx2.is_covariant()) {
220 // (_i,~i) or (~i,_i), i=1..3
223 // (_i,_i) or (~i,~i), i=1..3
228 // at least one off-diagonal
231 } else if (idx1.is_symbolic() &&
232 idx1.is_co_contra_pair(idx2)) {
242 int simp_lor::compare_same_type(const basic & other) const
244 GINAC_ASSERT(other.tinfo() == TINFO_simp_lor);
245 const simp_lor *o = static_cast<const simp_lor *>(&other);
248 return indexed::compare_same_type(other);
250 return name.compare(o->name);
252 return type < o->type ? -1 : 1;
255 bool simp_lor::is_equal_same_type(const basic & other) const
257 GINAC_ASSERT(other.tinfo() == TINFO_simp_lor);
258 const simp_lor *o = static_cast<const simp_lor *>(&other);
259 if (type!=o->type) return false;
260 if (name!=o->name) return false;
261 return indexed::is_equal_same_type(other);
264 unsigned simp_lor::return_type(void) const
266 return return_types::commutative;
269 unsigned simp_lor::return_type_tinfo(void) const
274 ex simp_lor::thisexprseq(const exvector & v) const
276 return simp_lor(type,name,v);
279 ex simp_lor::thisexprseq(exvector * vp) const
281 return simp_lor(type,name,vp);
285 // virtual functions which can be overridden by derived classes
291 // non-virtual functions in this class
296 bool simp_lor::all_of_type_lorentzidx(void) const
298 // used only inside of ASSERTs
299 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
300 if (!is_ex_of_type(*cit,lorentzidx)) return false;
306 // static member variables
315 simp_lor lor_g(const ex & mu, const ex & nu)
317 return simp_lor(simp_lor::simp_lor_g,mu,nu);
320 simp_lor lor_vec(const std::string & n, const ex & mu)
322 return simp_lor(simp_lor::simp_lor_vec,n,mu);
325 ex simplify_simp_lor_mul(const ex & m, const scalar_products & sp)
327 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
328 exvector v_contracted;
330 // collect factors in an exvector, store squares twice
332 v_contracted.reserve(2*n);
333 for (unsigned i=0; i<n; ++i) {
335 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
336 v_contracted.push_back(f.op(0));
337 v_contracted.push_back(f.op(0));
339 v_contracted.push_back(f);
343 unsigned replacements;
344 bool something_changed=false;
346 exvector::iterator it=v_contracted.begin();
347 while (it!=v_contracted.end()) {
348 // process only lor_g objects
349 if (is_ex_exactly_of_type(*it,simp_lor) &&
350 (ex_to_simp_lor(*it).type==simp_lor::simp_lor_g)) {
351 const simp_lor & g=ex_to_simp_lor(*it);
352 GINAC_ASSERT(g.seq.size()==2);
353 const idx & first_idx=ex_to_lorentzidx(g.seq[0]);
354 const idx & second_idx=ex_to_lorentzidx(g.seq[1]);
355 // g_{mu,mu} should have been contracted in simp_lor::eval()
356 GINAC_ASSERT(!first_idx.is_equal(second_idx));
357 ex saved_g=*it; // save to restore it later
359 // try to contract first index
361 if (first_idx.is_symbolic()) {
362 replacements = subs_index_in_exvector(v_contracted, first_idx.toggle_covariant(),second_idx);
363 if (replacements==0) {
364 // not contracted, restore g object
367 // a contracted index should occur exactly once
368 GINAC_ASSERT(replacements==1);
370 something_changed=true;
374 // try second index only if first was not contracted
375 if ((replacements==0)&&(second_idx.is_symbolic())) {
376 // first index not contracted, *it is again the original g object
377 replacements = subs_index_in_exvector(v_contracted, 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 GINAC_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 const simp_lor & vec1=ex_to_simp_lor(*it1);
403 const simp_lor & vec2=ex_to_simp_lor(*it2);
404 GINAC_ASSERT(vec1.seq.size()==1);
405 GINAC_ASSERT(vec2.seq.size()==1);
406 const lorentzidx & idx1=ex_to_lorentzidx(vec1.seq[0]);
407 const lorentzidx & 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(const ex & e, const scalar_products & 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 (unsigned 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
456 void scalar_products::reg(const simp_lor & v1, const simp_lor & v2,
459 if (v1.compare_same_type(v2)>0) {
463 spm[make_key(v1,v2)]=sp;
466 bool scalar_products::is_defined(const simp_lor & v1, const simp_lor & v2) const
468 if (v1.compare_same_type(v2)>0) {
469 return is_defined(v2,v1);
471 return spm.find(make_key(v1,v2))!=spm.end();
474 ex scalar_products::evaluate(const simp_lor & v1, const simp_lor & v2) const
476 if (v1.compare_same_type(v2)>0)
477 return evaluate(v2, v1);
479 return (*spm.find(make_key(v1,v2))).second;
482 void scalar_products::debugprint(void) const
484 std::cerr << "map size=" << spm.size() << std::endl;
485 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
486 const spmapkey & k=(*cit).first;
487 std::cerr << "item key=((" << k.first.first
488 << "," << k.first.second << "),";
489 k.second.printraw(std::cerr);
490 std::cerr << ") value=" << (*cit).second << std::endl;
494 spmapkey scalar_products::make_key(const simp_lor & v1, const simp_lor & v2)
496 GINAC_ASSERT(v1.type==simp_lor::simp_lor_vec);
497 GINAC_ASSERT(v2.type==simp_lor::simp_lor_vec);
498 lorentzidx anon=ex_to_lorentzidx(v1.seq[0]).create_anonymous_representative();
499 GINAC_ASSERT(anon.is_equal_same_type(ex_to_lorentzidx(v2.seq[0]).create_anonymous_representative()));
500 return spmapkey(strstrpair(v1.name,v2.name),anon);
503 #ifndef NO_NAMESPACE_GINAC
505 #endif // ndef NO_NAMESPACE_GINAC