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, false) * SP(p.op(1), idx, false);
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, false);
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 auto g = ex_to<DGamma>(e);
70 if(!is_equal_any(g.pi,loop_ps)) return e;
71 else {
72 Index idx(prefix+to_string(++lproj));
73 return DGamma(idx, g.rl) * SP(g.pi, idx, false);
74 }
75 } else if (e.match(TR(w))) {
76 auto ret = self(e.op(0));
77 auto cvs = collect_lst(ret, loop_ps);
78 ret = 0;
79 for(auto const & cv : cvs) {
80 ret += TR(cv.op(0)) * cv.op(1);
81 }
82 return ret;
83 } else if (e.match(GMat(w1,w2,w3))) {
84 auto ret = self(e.op(0));
85 auto cvs = collect_lst(ret, loop_ps);
86 ret = 0;
87 for(auto const & cv : cvs) {
88 ret += GMat(cv.op(0), e.op(1), e.op(2)) * cv.op(1);
89 }
90 return ret;
91 } else if(is_a<add>(e)) {
92 int lpj = lproj;
93 int lpj_max = -100;
94 ex ret = 0;
95 for(auto item : e) {
96 lproj = lpj;
97 ret += self(item);
98 if(lpj_max<lproj) lpj_max = lproj;
99 }
100 lproj = lpj_max;
101 return ret;
102 } else if(is_a<power>(e)) {
103 if(!e.op(1).info(info_flags::posint)) return e; // no need to handle negative powers
104 ex ret = 1;
105 int pn = ex_to<numeric>(e.op(1)).to_int();
106 for(int i=0; i<pn; i++) {
107 ret *= self(e.op(0));
108 }
109 return ret;
110 } else return e.map(self);
111 })(expr);
112 }
113
121 ex TIR(const ex &expr_in, const lst &loop_ps, const lst &ext_ps) {
122 for(auto pi : loop_ps) {
123 if(!is_a<Vector>(pi)) throw Error("TIR invalid 2nd argument");
124 }
125 for(auto pi : ext_ps) {
126 if(!is_a<Vector>(pi)) throw Error("TIR invalid 3rd argument");
127 }
128
129 Fermat &fermat = Fermat::get();
130 int &v_max = fermat.vmax;
131 static exmap cache_map;
132
133 auto expr = UnContract(expr_in, loop_ps); // UnContract
134 auto cvs = collect_lst(expr, [&loop_ps](const ex & e)->bool{
135 if(!Index::hasv(e)) return false;
136 for(const_preorder_iterator i = e.preorder_begin(); i != e.preorder_end(); ++i) {
137 auto item = *i;
138 if(is_a<Pair>(item) && is_a<Index>(item.op(1)) && loop_ps.has(item.op(0))) return true;
139 }
140 return false;
141 });
142
143 expr = 0;
144 for(auto cv : cvs) {
145 auto const & cc = cv.op(0);
146 auto const & vv = cv.op(1);
147 if(vv.is_equal(1)) {
148 expr += cc * vv;
149 continue;
150 }
151
152 ex map_key = lst{vv, ext_ps};
153 if(using_cache) {
154 auto itr = cache_map.find(map_key);
155 if(itr!=cache_map.end()) {
156 expr += cc * itr->second;
157 continue;
158 }
159 }
160 lst vis, lps;
161 map<ex,int,ex_is_less> pc;
162 if(is_a<mul>(vv)) {
163 for(auto item : vv) {
164 vis.append(item);
165 lps.append(item.op(0));
166 pc[item.op(0)]++;
167 }
168 } else {
169 vis.append(vv);
170 lps.append(vv.op(0));
171 }
172 lps.sort();
173 lps.unique();
174
175 ex res;
176 auto visn = vis.nops();
177 if(lps.nops()<2) {
178 ex eqL=1;
179 lst is, bis;
180 for(auto vi : vis) {
181 eqL *= vi;
182 is.append(vi.op(1));
183 }
184
185 //for(int er=((visn%2==0)?0:1); er<=visn; er+=2) {
186 for(int er=visn; er>=((visn%2==0)?0:1); er-=2) {
187 auto ep_comb = combs(ext_ps, er);
188 for(auto item : ep_comb) {
189 ex bi=1;
190 for(int j=0; j<er; j++) bi *= SP(item.op(j), is.op(j),false); // false needed, is may be in SP_map, and actually should be a dummy index
191 for(int j=er; j<visn; j=j+2) bi *= SP(is.op(j), is.op(j+1),false); // false needed, reason above
192 bi = bi.symmetrize(is);
193 bis.append(bi);
194 }}
195
196 int n = bis.nops();
197 lst mat;
198 for(auto bi : bis) {
199 for(auto bj : bis) mat.append(bi*bj);
200 }
201 for(auto bj : bis) mat.append(eqL*bj);
202
203 // we need to remap the dummy index, to avoid SP_map set the index object to 0
204 if(true) {
205 lst isu = is;
206 isu.sort();
207 isu.unique();
208 exmap i2u, u2i;
209 int c=0;
210 for(auto item : isu) {
211 auto ii = Index("TIR"+to_string(c++));
212 i2u[item] = ii;
213 u2i[ii] = item;
214 }
215 mat = ex_to<lst>(subs(mat,i2u));
216 mat = ex_to<lst>(form(mat).subs(u2i));
217 }
218
219 lst rep_vs;
220 ex tree = mat;
221 for(const_preorder_iterator i = tree.preorder_begin(); i != tree.preorder_end(); ++i) {
222 auto e = (*i);
223 if(is_a<symbol>(e) || is_a<Pair>(e) || is_a<Eps>(e)) {
224 rep_vs.append(e);
225 }
226 }
227 rep_vs.sort();
228 rep_vs.unique();
229 sort_lst(rep_vs);
230
231 exmap v2f;
232 symtab st;
233 int fvi = 0;
234 for(auto vi : rep_vs) {
235 auto name = "v" + to_string(fvi);
236 v2f[vi] = Symbol(name);
237 st[name] = vi;
238 fvi++;
239 }
240
241 stringstream ss;
242 if(fvi>111) {
243 cout << rep_vs << endl;
244 throw Error("Fermat: Too many variables.");
245 }
246 if(fvi>v_max) {
247 for(int i=v_max; i<fvi; i++) ss << "&(J=v" << i << ");" << endl;
248 fermat.Execute(ss.str());
249 ss.clear();
250 ss.str("");
251 v_max = fvi;
252 }
253
254 ss << "Array m[" << n << "," << n+1 << "];" << endl;
255 fermat.Execute(ss.str());
256 ss.clear();
257 ss.str("");
258
259 ss << "[m]:=[(";
260 for(auto item : mat) {
261 ss << item.subs(v2f) << ",";
262 }
263 ss << ")];" << endl;
264 ss << "Redrowech([m]);" << endl;
265 auto tmp = ss.str();
266
267 string_replace_all(tmp,",)]",")]");
268 fermat.Execute(tmp);
269 ss.clear();
270 ss.str("");
271
272 ss << "&(U=1);" << endl; // ugly printing, the whitespace matters
273 ss << "![m" << endl;
274 auto ostr = fermat.Execute(ss.str());
275 ss.clear();
276 ss.str("");
277
278 ss << "&(U=0);" << endl; // disable ugly printing
279 ss << "@([m]);" << endl;
280 ss << "&_G;" << endl;
281 fermat.Execute(ss.str());
282 ss.clear();
283 ss.str("");
284
285 // make sure last char is 0
286 if(ostr[ostr.length()-1]!='0') throw Error("TIR: last char is NOT 0.");
287 ostr = ostr.substr(0, ostr.length()-1);
288 string_trim(ostr);
289
290 ostr.erase(0, ostr.find(":=")+2);
291 string_replace_all(ostr, "[", "{");
292 string_replace_all(ostr, "]", "}");
293 Parser fp(st);
294 auto mat2 = fp.Read(ostr);
295
296 res = 0;
297 for(int i=0; i<n; i++) {
298 if(is_zero(mat2.op(i).op(i)) && !is_zero(mat2.op(i).op(n))) {
299 cout << cv << endl;
300 cout << mat << " :> " << mat2 << endl;
301 throw Error("No Solution in TIR.");
302 }
303 res += bis.op(i) * mat2.op(i).op(n);
304 }
305 res = res.subs(SP_map);
306 } else {
307 int cmin=10000, cmax=-1;
308 ex pmin, pmax;
309 for(auto lpi : lps) {
310 auto cc = pc[lpi];
311 if(cc<cmin) { cmin = cc; pmin = lpi; }
312 if(cc>cmax) { cmax = cc; pmax = lpi; }
313 }
314
315 auto lp0 = pmin;
316 auto ext_ps2 = ext_ps;
317 for(auto lpi : lps) if(!is_zero(lpi-lp0)) ext_ps2.append(lpi);
318 res = TIR(vv, lst{ lp0 }, ext_ps2);
319 res = TIR(res, loop_ps, ext_ps);
320 }
321
322 res = exnormal(res);
323 if(using_cache) cache_map.insert({map_key,res});
324 expr += cc * res;
325 }
326
327 return expr.subs(SP_map);
328 }
329
330}
331
HEP header file.
int pn
Definition Others.cpp:25
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:245
interface to communicate with Fermat program
Definition BASIC.h:844
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:240
class to wrap map_function of GiNaC
Definition BASIC.h:679
static bool has(const ex &e)
class to parse for string or file, helpful with Symbol class
Definition BASIC.h:692
ex Read(const string &instr, bool s2S=true)
class extended to GiNaC symbol class, represent a positive symbol
Definition BASIC.h:116
HepLib namespace.
Definition BASIC.cpp:17
bool is_equal_any(ex expr, lst ps)
Definition BASIC.h:64
exmap SP_map
Definition Init.cpp:188
bool using_cache
Definition Init.cpp:160
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:1920
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:1223
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:503
ex w3
Definition BASIC.h:503
ex UnContract(const ex expr, const lst &loop_ps, const lst &ext_ps=lst{})
Definition TIR.cpp:43
ex SP(const ex &a, bool use_map=true)
Definition Pair.cpp:166
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:503
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:581
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:121
ex subs(const ex &e, init_list sl)
Definition BASIC.h:51