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
38 #ifndef NO_NAMESPACE_GINAC
40 #endif // ndef NO_NAMESPACE_GINAC
42 GINAC_IMPLEMENT_REGISTERED_CLASS(simp_lor, indexed)
45 // default constructor, destructor, copy constructor assignment operator and helpers
50 simp_lor::simp_lor() : type(invalid)
52 debugmsg("simp_lor default constructor",LOGLEVEL_CONSTRUCT);
53 tinfo_key=TINFO_simp_lor;
58 void simp_lor::copy(const simp_lor & other)
65 void simp_lor::destroy(bool call_parent)
68 indexed::destroy(call_parent);
78 simp_lor::simp_lor(simp_lor_types const t) : type(t)
80 debugmsg("simp_lor constructor from simp_lor_types",LOGLEVEL_CONSTRUCT);
81 tinfo_key=TINFO_simp_lor;
84 simp_lor::simp_lor(simp_lor_types const t, const ex & i1, const ex & i2)
85 : indexed(i1,i2), type(t)
87 debugmsg("simp_lor constructor from simp_lor_types,ex,ex",LOGLEVEL_CONSTRUCT);
88 tinfo_key=TINFO_simp_lor;
89 GINAC_ASSERT(all_of_type_lorentzidx());
92 simp_lor::simp_lor(simp_lor_types const t, const std::string & n, const ex & i1)
93 : indexed(i1), type(t), name(n)
95 debugmsg("simp_lor constructor from simp_lor_types,string,ex",LOGLEVEL_CONSTRUCT);
96 tinfo_key=TINFO_simp_lor;
97 GINAC_ASSERT(all_of_type_lorentzidx());
100 simp_lor::simp_lor(simp_lor_types const t, const std::string & n, const exvector & iv)
101 : indexed(iv), type(t), name(n)
103 debugmsg("simp_lor constructor from simp_lor_types,string,exvector",LOGLEVEL_CONSTRUCT);
104 tinfo_key=TINFO_simp_lor;
105 GINAC_ASSERT(all_of_type_lorentzidx());
108 simp_lor::simp_lor(simp_lor_types const t, const std::string & n, exvector * ivp)
109 : indexed(ivp), type(t), name(n)
111 debugmsg("simp_lor constructor from simp_lor_types,string,exvector*",LOGLEVEL_CONSTRUCT);
112 tinfo_key=TINFO_simp_lor;
113 GINAC_ASSERT(all_of_type_lorentzidx());
120 /** Construct object from archive_node. */
121 simp_lor::simp_lor(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
123 debugmsg("simp_lor constructor from archive_node", LOGLEVEL_CONSTRUCT);
125 if (!(n.find_unsigned("type", ty)))
126 throw (std::runtime_error("unknown simp_lor type in archive"));
127 type = (simp_lor_types)ty;
128 n.find_string("name", name);
131 /** Unarchive the object. */
132 ex simp_lor::unarchive(const archive_node &n, const lst &sym_lst)
134 return (new simp_lor(n, sym_lst))->setflag(status_flags::dynallocated);
137 /** Archive the object. */
138 void simp_lor::archive(archive_node &n) const
140 inherited::archive(n);
141 n.add_unsigned("type", type);
142 n.add_string("name", name);
147 // functions overriding virtual functions from bases classes
152 void simp_lor::printraw(std::ostream & os) const
154 debugmsg("simp_lor printraw",LOGLEVEL_PRINT);
155 os << "simp_lor(type=" << (unsigned)type
156 << ",name=" << name << ",indices=";
158 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
161 void simp_lor::printtree(std::ostream & os, unsigned indent) const
163 debugmsg("simp_lor printtree",LOGLEVEL_PRINT);
164 os << std::string(indent,' ') << "simp_lor object: "
165 << "type=" << (unsigned)type
166 << ", name=" << name << ", ";
167 os << seq.size() << " indices" << std::endl;
168 printtreeindices(os,indent);
169 os << std::string(indent,' ') << "hash=" << hashvalue
170 << " (0x" << std::hex << hashvalue << std::dec << ")"
171 << ", flags=" << flags << std::endl;
174 void simp_lor::print(std::ostream & os, unsigned upper_precedence) const
176 debugmsg("simp_lor print",LOGLEVEL_PRINT);
186 os << "INVALID_SIMP_LOR_OBJECT";
192 void simp_lor::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
194 debugmsg("simp_lor print csrc",LOGLEVEL_PRINT);
195 print(os,upper_precedence);
198 bool simp_lor::info(unsigned inf) const
200 return indexed::info(inf);
203 ex simp_lor::eval(int level) const
205 if (type==simp_lor_g) {
206 // canonicalize indices
208 int sig=canonicalize_indices(iv,false); // symmetric
210 // something has changed while sorting indices, more evaluations later
211 if (sig==0) return _ex0();
212 return ex(sig)*simp_lor(type,name,iv);
214 const lorentzidx & idx1=ex_to_lorentzidx(seq[0]);
215 const lorentzidx & idx2=ex_to_lorentzidx(seq[1]);
216 if ((!idx1.is_symbolic())&&(!idx2.is_symbolic())) {
217 // both indices are numeric
218 if ((idx1.get_value()==idx2.get_value())) {
220 if (idx1.get_value()==0) {
224 if (idx1.is_covariant()!=idx2.is_covariant()) {
225 // (_i,~i) or (~i,_i), i=1..3
228 // (_i,_i) or (~i,~i), i=1..3
233 // at least one off-diagonal
236 } else if (idx1.is_symbolic() && idx1.is_co_contra_pair(idx2)) {
237 if (idx1.is_orthogonal_only())
238 return Dim() - idx1.get_dim_parallel_space();
249 int simp_lor::compare_same_type(const basic & other) const
251 GINAC_ASSERT(other.tinfo() == TINFO_simp_lor);
252 const simp_lor *o = static_cast<const simp_lor *>(&other);
255 return indexed::compare_same_type(other);
257 return name.compare(o->name);
259 return type < o->type ? -1 : 1;
262 bool simp_lor::is_equal_same_type(const basic & other) const
264 GINAC_ASSERT(other.tinfo() == TINFO_simp_lor);
265 const simp_lor *o = static_cast<const simp_lor *>(&other);
266 if (type!=o->type) return false;
267 if (name!=o->name) return false;
268 return indexed::is_equal_same_type(other);
271 unsigned simp_lor::return_type(void) const
273 return return_types::commutative;
276 unsigned simp_lor::return_type_tinfo(void) const
281 ex simp_lor::thisexprseq(const exvector & v) const
283 return simp_lor(type,name,v);
286 ex simp_lor::thisexprseq(exvector * vp) const
288 return simp_lor(type,name,vp);
292 // virtual functions which can be overridden by derived classes
298 // non-virtual functions in this class
303 bool simp_lor::all_of_type_lorentzidx(void) const
305 // used only inside of ASSERTs
306 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
307 if (!is_ex_of_type(*cit,lorentzidx)) return false;
313 // static member variables
322 simp_lor lor_g(const ex & mu, const ex & nu)
324 return simp_lor(simp_lor::simp_lor_g,mu,nu);
327 simp_lor lor_vec(const std::string & n, const ex & mu)
329 return simp_lor(simp_lor::simp_lor_vec,n,mu);
332 ex simplify_simp_lor_mul(const ex & m, const scalar_products & sp)
334 GINAC_ASSERT(is_ex_exactly_of_type(m,mul));
335 exvector v_contracted;
337 // collect factors in an exvector, store squares twice
339 v_contracted.reserve(2*n);
340 for (unsigned i=0; i<n; ++i) {
342 if (is_ex_exactly_of_type(f,power)&&f.op(1).is_equal(_ex2())) {
343 v_contracted.push_back(f.op(0));
344 v_contracted.push_back(f.op(0));
346 v_contracted.push_back(f);
350 unsigned replacements;
351 bool something_changed=false;
353 exvector::iterator it=v_contracted.begin();
354 while (it!=v_contracted.end()) {
355 // process only lor_g objects
356 if (is_ex_exactly_of_type(*it,simp_lor) &&
357 (ex_to_simp_lor(*it).type==simp_lor::simp_lor_g)) {
358 const simp_lor & g=ex_to_simp_lor(*it);
359 GINAC_ASSERT(g.seq.size()==2);
360 const idx & first_idx=ex_to_lorentzidx(g.seq[0]);
361 const idx & second_idx=ex_to_lorentzidx(g.seq[1]);
362 // g_{mu,mu} should have been contracted in simp_lor::eval()
363 GINAC_ASSERT(!first_idx.is_equal(second_idx));
364 ex saved_g=*it; // save to restore it later
366 // try to contract first index
368 if (first_idx.is_symbolic()) {
369 replacements = subs_index_in_exvector(v_contracted, first_idx.toggle_covariant(),second_idx);
370 if (replacements==0) {
371 // not contracted, restore g object
374 // a contracted index should occur exactly once
375 GINAC_ASSERT(replacements==1);
377 something_changed=true;
381 // try second index only if first was not contracted
382 if ((replacements==0)&&(second_idx.is_symbolic())) {
383 // first index not contracted, *it is again the original g object
384 replacements = subs_index_in_exvector(v_contracted, 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 const simp_lor & vec1=ex_to_simp_lor(*it1);
410 const simp_lor & vec2=ex_to_simp_lor(*it2);
411 GINAC_ASSERT(vec1.seq.size()==1);
412 GINAC_ASSERT(vec2.seq.size()==1);
413 const lorentzidx & idx1=ex_to_lorentzidx(vec1.seq[0]);
414 const lorentzidx & 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(const ex & e, const scalar_products & 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 (unsigned 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
463 void scalar_products::reg(const simp_lor & v1, const simp_lor & v2,
466 if (v1.compare_same_type(v2)>0) {
470 spm[make_key(v1,v2)]=sp;
473 bool scalar_products::is_defined(const simp_lor & v1, const simp_lor & v2) const
475 if (v1.compare_same_type(v2)>0) {
476 return is_defined(v2,v1);
478 return spm.find(make_key(v1,v2))!=spm.end();
481 ex scalar_products::evaluate(const simp_lor & v1, const simp_lor & v2) const
483 if (v1.compare_same_type(v2)>0)
484 return evaluate(v2, v1);
486 return (*spm.find(make_key(v1,v2))).second;
489 void scalar_products::debugprint(void) const
491 std::cerr << "map size=" << spm.size() << std::endl;
492 for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
493 const spmapkey & k=(*cit).first;
494 std::cerr << "item key=((" << k.first.first
495 << "," << k.first.second << "),";
496 k.second.printraw(std::cerr);
497 std::cerr << ") value=" << (*cit).second << std::endl;
501 spmapkey scalar_products::make_key(const simp_lor & v1, const simp_lor & v2)
503 GINAC_ASSERT(v1.type==simp_lor::simp_lor_vec);
504 GINAC_ASSERT(v2.type==simp_lor::simp_lor_vec);
505 lorentzidx anon=ex_to_lorentzidx(v1.seq[0]).create_anonymous_representative();
506 GINAC_ASSERT(anon.is_equal_same_type(ex_to_lorentzidx(v2.seq[0]).create_anonymous_representative()));
507 return spmapkey(strstrpair(v1.name,v2.name),anon);
510 #ifndef NO_NAMESPACE_GINAC
512 #endif // ndef NO_NAMESPACE_GINAC