HepLib
Loading...
Searching...
No Matches
TIR.cpp
Go to the documentation of this file.
1
6#include "HEP.h"
7#include <cmath>
8
9namespace HepLib {
10
11 namespace {
12
13 lst combs(const lst & items, int n) {
14 lst ret;
15 if(n<1 || items.nops()<1) return lst{ret};
16 if(n==1) {
17 for(auto it : items) ret.append(lst{it});
18 return ret;
19 }
20 if(items.nops()==1) {
21 lst comb;
22 for(int i=0; i<n; i++) comb.append(items.op(0));
23 ret.append(comb);
24 return ret;
25 }
26 auto its = items;
27 ex first = its.op(0);
28 its.remove_first();
29 //for(int i=0; i<=n; i++) {
30 for(int i=n; i>=0; i--) {
31 auto rets = combs(its, n-i);
32 for(auto it : rets) {
33 lst item = ex_to<lst>(it);
34 for(int j=0; j<i; j++) item.append(first);
35 ret.append(item);
36 }
37 }
38 return ret;
39 }
40
41 }
42
43 ex UnContract(const ex expr, const lst &loop_ps, const lst &ext_ps) {
44 // handle Eps/DGamma/Pair and related power
45 int lproj = 0;
46 return MapFunction([&lproj,loop_ps,ext_ps](const ex &e, MapFunction &self)->ex {
47 string prefix = "HIdx";
48 int mode = 0;
49 if(ext_ps.nops()>0) mode = 1;
50 if(!Eps::has(e) && !DGamma::has(e) && (mode==0 || !Pair::has(e))) return e;
51 else if(mode==1 && is_a<Pair>(e) && has_any(e,loop_ps) && has_any(e,ext_ps)) {
52 Index idx(prefix+to_string(++lproj));
53 auto p = ex_to<Pair>(e);
54 return SP(p.op(0), idx) * SP(p.op(1), idx);
55 } else if(is_a<Eps>(e)) {
56 auto pis0 = ex_to<Eps>(e).pis;
57 ex pis[4];
58 ex cc = 1;
59 for(int i=0; i<4; i++) {
60 pis[i] = pis0[i];
61 if(is_equal_any(pis[i],loop_ps)) {
62 Index idx(prefix+to_string(++lproj));
63 cc *= SP(pis[i], idx);
64 pis[i] = idx;
65 } else if(has_any(pis[i],loop_ps)) throw Error("UnContract: Eps still has loops.");
66 }
67 return LC(pis[0], pis[1], pis[2], pis[3]) * cc;
68 } else if(is_a<DGamma>(e)) {
69 Index idx(prefix+to_string(++lproj));
70 auto g = ex_to<DGamma>(e);
71 if(!is_equal_any(g.pi,loop_ps)) return e;
72 else return DGamma(idx, g.rl) * SP(g.pi, idx);
73 } else if (e.match(TR(w))) {
74 auto ret = self(e.op(0));
75 auto cvs = collect_lst(ret, loop_ps);
76 ret = 0;
77 for(auto const & cv : cvs) {
78 ret += TR(cv.op(0)) * cv.op(1);
79 }
80 return ret;
81 } else if (e.match(GMat(w1,w2,w3))) {
82 auto ret = self(e.op(0));
83 auto cvs = collect_lst(ret, loop_ps);
84 ret = 0;
85 for(auto const & cv : cvs) {
86 ret += GMat(cv.op(0), e.op(1), e.op(2)) * cv.op(1);
87 }
88 return ret;
89 } else if(is_a<add>(e)) {
90 int lpj = lproj;
91 int lpj_max = -100;
92 ex ret = 0;
93 for(auto item : e) {
94 lproj = lpj;
95 ret += self(item);
96 if(lpj_max<lproj) lpj_max = lproj;
97 }
98 lproj = lpj_max;
99 return ret;
100 } else if(is_a<power>(e)) {
101 if(!e.op(1).info(info_flags::posint)) return e; // no need to handle negative powers
102 ex ret = 1;
103 int pn = ex_to<numeric>(e.op(1)).to_int();
104 for(int i=0; i<pn; i++) {
105 ret *= self(e.op(0));
106 }
107 return ret;
108 } else return e.map(self);
109 })(expr);
110 }
111
119 ex TIR(const ex &expr_in, const lst &loop_ps, const lst &ext_ps) {
120 for(auto pi : loop_ps) {
121 if(!is_a<Vector>(pi)) throw Error("TIR invalid 2nd argument");
122 }
123 for(auto pi : ext_ps) {
124 if(!is_a<Vector>(pi)) throw Error("TIR invalid 3rd argument");
125 }
126
127 Fermat &fermat = Fermat::get();
128 int &v_max = fermat.vmax;
129 static exmap cache_map;
130
131 auto expr = UnContract(expr_in, loop_ps); // UnContract
132 auto cvs = collect_lst(expr, [&loop_ps](const ex & e)->bool{
133 if(!Index::hasv(e)) return false;
134 for(const_preorder_iterator i = e.preorder_begin(); i != e.preorder_end(); ++i) {
135 auto item = *i;
136 if(is_a<Pair>(item) && is_a<Index>(item.op(1)) && loop_ps.has(item.op(0))) return true;
137 }
138 return false;
139 });
140
141 expr = 0;
142 for(auto cv : cvs) {
143 auto const & cc = cv.op(0);
144 auto const & vv = cv.op(1);
145 if(vv.is_equal(1)) {
146 expr += cc * vv;
147 continue;
148 }
149
150 ex map_key = lst{vv, ext_ps};
151 if(using_cache) {
152 auto itr = cache_map.find(map_key);
153 if(itr!=cache_map.end()) {
154 expr += cc * itr->second;
155 continue;
156 }
157 }
158 lst vis, lps;
159 map<ex,int,ex_is_less> pc;
160 if(is_a<mul>(vv)) {
161 for(auto item : vv) {
162 vis.append(item);
163 lps.append(item.op(0));
164 pc[item.op(0)]++;
165 }
166 } else {
167 vis.append(vv);
168 lps.append(vv.op(0));
169 }
170 lps.sort();
171 lps.unique();
172
173 ex res;
174 auto visn = vis.nops();
175 if(lps.nops()<2) {
176 ex eqL=1;
177 lst is, bis;
178 for(auto vi : vis) {
179 eqL *= vi;
180 is.append(vi.op(1));
181 }
182
183 //for(int er=((visn%2==0)?0:1); er<=visn; er+=2) {
184 for(int er=visn; er>=((visn%2==0)?0:1); er-=2) {
185 auto ep_comb = combs(ext_ps, er);
186 for(auto item : ep_comb) {
187 ex bi=1;
188 for(int j=0; j<er; j++) bi *= SP(item.op(j), is.op(j));
189 for(int j=er; j<visn; j=j+2) bi *= SP(is.op(j), is.op(j+1));
190 bi = bi.symmetrize(is);
191 bis.append(bi);
192 }}
193
194 int n = bis.nops();
195 lst mat;
196 for(auto bi : bis) {
197 for(auto bj : bis) mat.append(bi*bj);
198 }
199 for(auto bj : bis) mat.append(eqL*bj);
200
201 // we need to remap the dummy index, to avoid SP_map set the index object to 0
202 if(true) {
203 lst isu = is;
204 isu.sort();
205 isu.unique();
206 exmap i2u, u2i;
207 int c=0;
208 for(auto item : isu) {
209 auto ii = Index("TIR"+to_string(c++));
210 i2u[item] = ii;
211 u2i[ii] = item;
212 }
213 mat = ex_to<lst>(subs(mat,i2u));
214 mat = ex_to<lst>(form(mat).subs(u2i));
215 }
216
217 lst rep_vs;
218 ex tree = mat;
219 for(const_preorder_iterator i = tree.preorder_begin(); i != tree.preorder_end(); ++i) {
220 auto e = (*i);
221 if(is_a<symbol>(e) || is_a<Pair>(e) || is_a<Eps>(e)) {
222 rep_vs.append(e);
223 }
224 }
225 rep_vs.sort();
226 rep_vs.unique();
227 sort_lst(rep_vs);
228
229 exmap v2f;
230 symtab st;
231 int fvi = 0;
232 for(auto vi : rep_vs) {
233 auto name = "v" + to_string(fvi);
234 v2f[vi] = Symbol(name);
235 st[name] = vi;
236 fvi++;
237 }
238
239 stringstream ss;
240 if(fvi>111) {
241 cout << rep_vs << endl;
242 throw Error("Fermat: Too many variables.");
243 }
244 if(fvi>v_max) {
245 for(int i=v_max; i<fvi; i++) ss << "&(J=v" << i << ");" << endl;
246 fermat.Execute(ss.str());
247 ss.clear();
248 ss.str("");
249 v_max = fvi;
250 }
251
252 ss << "Array m[" << n << "," << n+1 << "];" << endl;
253 fermat.Execute(ss.str());
254 ss.clear();
255 ss.str("");
256
257 ss << "[m]:=[(";
258 for(auto item : mat) {
259 ss << item.subs(v2f) << ",";
260 }
261 ss << ")];" << endl;
262 ss << "Redrowech([m]);" << endl;
263 auto tmp = ss.str();
264
265 string_replace_all(tmp,",)]",")]");
266 fermat.Execute(tmp);
267 ss.clear();
268 ss.str("");
269
270 ss << "&(U=1);" << endl; // ugly printing, the whitespace matters
271 ss << "![m" << endl;
272 auto ostr = fermat.Execute(ss.str());
273 ss.clear();
274 ss.str("");
275
276 ss << "&(U=0);" << endl; // disable ugly printing
277 ss << "@([m]);" << endl;
278 ss << "&_G;" << endl;
279 fermat.Execute(ss.str());
280 ss.clear();
281 ss.str("");
282
283 // make sure last char is 0
284 if(ostr[ostr.length()-1]!='0') throw Error("TIR: last char is NOT 0.");
285 ostr = ostr.substr(0, ostr.length()-1);
286 string_trim(ostr);
287
288 ostr.erase(0, ostr.find(":=")+2);
289 string_replace_all(ostr, "[", "{");
290 string_replace_all(ostr, "]", "}");
291 Parser fp(st);
292 auto mat2 = fp.Read(ostr);
293
294 res = 0;
295 for(int i=0; i<n; i++) {
296 if(is_zero(mat2.op(i).op(i)) && !is_zero(mat2.op(i).op(n))) {
297 cout << cv << endl;
298 cout << mat << " :> " << mat2 << endl;
299 throw Error("No Solution in TIR.");
300 }
301 res += bis.op(i) * mat2.op(i).op(n);
302 }
303 res = res.subs(SP_map);
304 } else {
305 int cmin=10000, cmax=-1;
306 ex pmin, pmax;
307 for(auto lpi : lps) {
308 auto cc = pc[lpi];
309 if(cc<cmin) { cmin = cc; pmin = lpi; }
310 if(cc>cmax) { cmax = cc; pmax = lpi; }
311 }
312
313 auto lp0 = pmin;
314 auto ext_ps2 = ext_ps;
315 for(auto lpi : lps) if(!is_zero(lpi-lp0)) ext_ps2.append(lpi);
316 res = TIR(vv, lst{ lp0 }, ext_ps2);
317 res = TIR(res, loop_ps, ext_ps);
318 }
319
320 res = exnormal(res);
321 if(using_cache) cache_map.insert({map_key,res});
322 expr += cc * res;
323 }
324
325 return expr;
326 }
327
328}
329
HEP header file.
class for Dirac Gamma object
Definition HEP.h:437
static bool has(const ex &e)
static bool has(const ex &e)
class used to wrap error message
Definition BASIC.h:242
interface to communicate with Fermat program
Definition BASIC.h:797
static Fermat & get()
Definition Fermat.cpp:7
string Execute(string)
Definition Process.cpp:102
class for index object
Definition HEP.h:104
static bool hasv(const ex &e)
Definition Basic.cpp:234
class to wrap map_function of GiNaC
Definition BASIC.h:632
static bool has(const ex &e)
class to parse for string or file, helpful with Symbol class
Definition BASIC.h:645
ex Read(const string &instr, bool s2S=true)
class extended to GiNaC symbol class, represent a positive symbol
Definition BASIC.h:113
HepLib namespace.
Definition BASIC.cpp:17
bool is_equal_any(ex expr, lst ps)
Definition BASIC.h:64
exmap SP_map
Definition Init.cpp:182
bool using_cache
Definition Init.cpp:156
bool has_any(ex expr, lst ps)
Definition BASIC.h:55
ex w
Definition Init.cpp:93
ex exnormal(const ex &expr, int opt)
normalize a expression
Definition BASIC.cpp:1916
lst collect_lst(ex const &expr_in, std::function< bool(const ex &)> has_func, int opt)
the collect function like Mathematica, reture the lst { {c1,v1}, {c2,v2}, ... }
Definition BASIC.cpp:1222
void sort_lst(lst &ilst, bool less=true)
sort the list in less order, or the reverse
Definition Sort.cpp:79
void string_replace_all(string &str, const string &from, const string &to)
ex w1
Definition BASIC.h:499
ex w3
Definition BASIC.h:499
ex UnContract(const ex expr, const lst &loop_ps, const lst &ext_ps=lst{})
Definition TIR.cpp:43
ex LC(ex pi1, ex pi2, ex pi3, ex pi4)
function similar to LCD in FeynCalc
Definition Eps.cpp:179
ex w2
Definition BASIC.h:499
void string_trim(string &str)
ex form(const ex &iexpr, int verb)
evalulate expr in form program, see also the form_trace_mode and form_expand_mode
Definition Form.cpp:563
ex TIR(const ex &expr_in, const lst &loop_ps, const lst &ext_ps)
Tensor Index Reduction, note that we only handle numerator.
Definition TIR.cpp:119
ex SP(const ex &a, bool use_map=false)
Definition Pair.cpp:166
ex subs(const ex &e, init_list sl)
Definition BASIC.h:51