HepLib
Loading...
Searching...
No Matches
Others.cpp
Go to the documentation of this file.
1
6#include "HEP.h"
7#include <cmath>
8
9namespace HepLib {
10
11 namespace {
12 int ep_ldegree(const ex & e) {
13 auto ee = exnormal(e);
14 auto nd = ee.numer_denom();
15 if(nd.op(0).is_polynomial(ep) && nd.op(1).is_polynomial(ep)) {
16 return nd.op(0).ldegree(ep) - nd.op(1).ldegree(ep);
17 }
18 cout << "numer or denom is NOT a polynormial of ep." << endl;
19 abort();
20 }
21
22
23 class Family {
24 public:
25 int pn;
26 lst intg;
27 Family(int pn_) : pn(pn_) { }
28 };
29
30 exmap _sp2x_(const ex & ips, const ex & eps) {
31 int ni = 0;
32 exmap sp2x;
33 for(auto ip : ips) {
34 for(auto ipx : ips) {
35 if(sp2x.find(ip*ipx)==sp2x.end()) {
36 auto xi = x(ni);
37 sp2x[ip*ipx] = xi;
38 sp2x[w*ip*ipx] = w*xi;
39 ni++;
40 }
41 }
42 for(auto ep : eps) {
43 auto xi = x(ni);
44 sp2x[ep*ip] = xi;
45 sp2x[w*ep*ip] = w*xi;
46 ni++;
47 }
48 }
49 return sp2x;
50 }
51
52 bool is_complete(const ex & props, const ex & ips, const ex & eps) {
53 auto sp2x = _sp2x_(ips, eps);
54 auto ps = subs(props, sp2x);
55 int n = ps.nops();
56 matrix mat(n,n);
57 for(int r=0; r<n; r++) {
58 auto xi = x(r);
59 for(int c=0; c<n; c++) {
60 mat(r,c) = ps.op(c).coeff(xi);
61 }
62 }
63 return mat.rank()==n;
64 }
65 bool is_complete(const ex & props, const exmap & sp2x) {
66 auto ps = subs(props, sp2x);
67 int n = ps.nops();
68 matrix mat(n,n);
69 for(int r=0; r<n; r++) {
70 auto xi = x(r);
71 for(int c=0; c<n; c++) {
72 mat(r,c) = ps.op(c).coeff(xi);
73 }
74 }
75 return mat.rank()==n;
76 }
77 }
78
79 bool Exp2AMF::is_loop(const ex & e) {
80 for(auto ei : Internal) if(ei.is_equal(e)) return true;
81 return false;
82 }
83
84 void Exp2AMF::Export(const ex & expr, const string & dir) {
85 static string wlo =
86R"EOF(
87If[Length[$ScriptCommandLine]<2, Print["Usage: wolframescript -f "<>ToString[$ScriptCommandLine[[1]]]<>" <n>"];Quit[]];
88current = DirectoryName@If[$FrontEnd===Null,$InputFileName,NotebookFileName[]];
89
90<<AMFlow`AMFlow`
91
92SetReductionOptions["IBPReducer"->"Blade"];
93ID = ToExpression[$ScriptCommandLine[[2]]];
94PropsInts=Get[FileNameJoin[{current,ToString[ID]<>".m"}]];
95
96AMFlowInfo["Family"] = Symbol["I"<>ToString[ID]];
97AMFlowInfo["Loop"] = <<Loop>>;
98AMFlowInfo["Leg"] = <<Leg>>;
99AMFlowInfo["Conservation"] = { };
100AMFlowInfo["Replacement"] = <<Replacement>>;
101AMFlowInfo["Propagator"] = PropsInts[[1]]/.<<M2M2>>;
102AMFlowInfo["Numeric"] = <<Numeric>>;
103AMFlowInfo["NThread"] = <<NThread>>;
104
105integrals = Map[(j[Symbol["I"<>ToString[ID]], Sequence@@#])&, PropsInts[[2]]];
106precision = <<Precision>>;
107epsorder = 2*Length@AMFlowInfo["Loop"]-PropsInts[[3]] + <<Order>>;
108sol = SolveIntegrals[integrals, precision, epsorder];
109Put[sol, FileNameJoin[{current, "sol"<>ToString[ID]<>".m"}]];
110
111Quit[];
112)EOF";
113
114 string sLoop = ex2str(Internal);
115 string_replace_all(sLoop, "==", "->");
116 string sLeg = ex2str(External);
117 string_replace_all(sLeg, "==", "->");
118 string sReplacement = ex2str(Replacement);
119 string_replace_all(sReplacement, "==", "->");
120 string sNumeric = ex2str(Numeric);
121 string_replace_all(sNumeric, "==", "->");
122 string sM2M2 = ex2str(M2M2);
123 string_replace_all(sM2M2, "==", "->");
124 string wl = wlo;
125 string_replace_all(wl, "<<Loop>>", sLoop);
126 string_replace_all(wl, "<<Leg>>", sLeg);
127 string_replace_all(wl, "<<Replacement>>", sReplacement);
128 string_replace_all(wl, "<<Numeric>>", sNumeric);
129 string_replace_all(wl, "<<NThread>>", to_string(NThread));
130 string_replace_all(wl, "<<M2M2>>", sM2M2);
131 string_replace_all(wl, "<<Order>>", ex2str(Order));
132 string_replace_all(wl, "<<Precision>>", ex2str(Precision));
133
134 system(("mkdir -p "+dir).c_str());
135 str2file(wl, dir+"/run.wl");
136
137 auto res = expr;
138
139 // handle (qi*pi)^-n
140 cout << "Replacing linear propagators ..." << endl;
141 sp2x = _sp2x_(Internal, External);
142 exset pq_fs;
143 find(res, F(w1,w2), pq_fs);
144 exmap pq_f2f;
145 for(auto f : pq_fs) {
146 lst ps = ex_to<lst>(f.op(0));
147 lst ns = ex_to<lst>(f.op(1));
148 lst ns_p; // problematic ns
149 for(int i=0; i<ps.nops(); i++) {
150 bool ok = false;
151 for(auto ip : Internal) {
152 if(ps.op(i).has(ip*ip)) {
153 ok = true;
154 break;
155 }
156 }
157 if(!ok) {
158 ex pi = ps.op(i);
159 if(!is_a<mul>(pi) || !pi.nops()==2) {
160 cout << "prop: " << pi << endl;
161 cout << "prop is NOT the form of a*b" << endl;
162 abort();
163 }
164
165 if(ns.op(i)==-1) {
166 ns_p.append(i);
167 } else if(ns.op(i)!=0) {
168 cout << f << endl;
169 cout << "n of qi.pi is NOT -1" << endl;
170 abort();
171 } else { // ns.op(i)==0
172 auto pa = pi.op(0), pb = pi.op(1);
173 ex pab = expand(pow(pa+pb,2)).subs(Replacement);
174 ps[i] = pab;
175 bool ic = is_complete(ps, sp2x);
176 if(!ic && is_loop(pa)) {
177 ps[i] = expand(pow(pa,2)).subs(Replacement);
178 ic = is_complete(ps, sp2x);
179 }
180 if(!ic && is_loop(pb)) {
181 ps[i] = expand(pow(pb,2)).subs(Replacement);
182 ic = is_complete(ps, sp2x);
183 }
184 if(!ic) {
185 cout << "1st: ic is NOT true!" << endl;
186 abort();
187 }
188 }
189 }
190 }
191 if(ns_p.nops()>1) {
192 cout << f << endl;
193 abort();
194 } else if(ns_p.nops()>0) {
195 auto idx = ex2int(ns_p.op(0));
196 ns[idx] = 0;
197 auto pidx = ps.op(idx);
198
199 ex pa = pidx.op(0);
200 ex pb = pidx.op(1);
201
202 ex pab = expand(pow(pa+pb,2)).subs(Replacement);
203 ps[idx] = pab;
204 bool ic = is_complete(ps, sp2x);
205 if(!ic && is_loop(pa)) {
206 ps[idx] = expand(pow(pa,2)).subs(Replacement);
207 ic = is_complete(ps, sp2x);
208 }
209 if(!ic && is_loop(pb)) {
210 ps[idx] = expand(pow(pb,2)).subs(Replacement);
211 ic = is_complete(ps, sp2x);
212 }
213 if(!ic) {
214 cout << "2nd: ic is NOT true!" << endl;
215 abort();
216 }
217
218 auto xps = subs(ps,sp2x);
219 auto xpidx = pidx.subs(sp2x);
220 for(auto ip : Internal) {
221 if(xps.has(ip)) {
222 cout << "xps still has ip" << endl;
223 cout << "xps: " << xps << endl;
224 abort();
225 }
226 if(xpidx.has(ip)) {
227 cout << "xpidx still has ip" << endl;
228 cout << "xpidx: " << xpidx << endl;
229 abort();
230 }
231 }
232
233 ex xeq = 0, eq = 0, fres = 0;
234 lst ys;
235 for(int ii=0; ii<ps.nops(); ii++) {
236 symbol yi;
237 xeq += yi*xps.op(ii);
238 eq += yi*ps.op(ii);
239 lst nn = ns;
240 nn[ii] = ns.op(ii)-1;
241 fres += yi*F(ps, nn);
242 ys.append(yi);
243 }
244 lst eqs;
245 for(int ii=0; ii<ps.nops(); ii++) {
246 auto xi = x(ii);
247 eqs.append(xeq.coeff(xi)==xpidx.coeff(xi));
248 }
249 auto sol = lsolve(eqs,ys);
250
251 if(sol.nops()<1 || sol.has(x(w))) {
252 cout << "wrong sol: " << sol << endl;
253 }
254
255 auto rem = pidx-eq.subs(sol);
256 fres = fres.subs(sol);
257 if(!is_zero(rem)) {
258 fres += rem * F(ps, ns);
259 }
260
261 pq_f2f[f] = fres;
262 ex chk = exnormal(F2ex(f-pq_f2f[f]));
263 if(chk!=0) {
264 cout << "Failed: chk=" << chk << endl;
265 }
266 } else if(!f.op(0).is_equal(ps)) {
267 pq_f2f[f] = F(ps, ns);
268 ex chk = exnormal(F2ex(f-pq_f2f[f]));
269 if(chk!=0) {
270 cout << "Failed: chk=" << chk << endl;
271 }
272 }
273 }
274
275 cout << "Collecting ..." << endl;
276
277 res = MapFunction([&](const ex & e, MapFunction &self)->ex{
278 if(e.match(F(w1, w2))) {
279 auto itr = pq_f2f.find(e);
280 if(itr==pq_f2f.end()) return e;
281 else return itr->second;
282 } else return e.map(self);
283 })(res);
284
285 res = collect_ex(res, F(w1,w2));
286
287 exset fs;
288 find(res, F(w1,w2), fs);
289 map<ex,Family,ex_is_less> p2f;
290 exmap f2f;
291 int pn = 0;
292 for(auto f : fs) {
293 auto itr = p2f.find(f.op(0));
294 if(itr == p2f.end()) {
295 itr = p2f.insert({f.op(0), Family(pn)}).first;
296 pn++;
297 }
298 itr->second.intg.append(f.op(1));
299 f2f[f] = F(itr->second.pn, f.op(1));
300 }
301
302 res = MapFunction([&](const ex & e, MapFunction &self)->ex{
303 if(e.match(F(w1, w2))) {
304 auto itr = f2f.find(e);
305 if(itr==f2f.end()) {
306 cout << "F Not Found" << endl;
307 abort();
308 }
309 return itr->second;
310 } else return e.map(self);
311 })(res);
312
313 cout << "Exporting res.txt ..." << endl;
314 ex2file(res.subs(Numeric), dir+"/res.txt");
315
316 string sres =
317R"EOF(
318current = If[$FrontEnd===Null,$InputFileName,NotebookFileName[]]//DirectoryName;
319<<HepLib`
320Module[{sols, res},
321sols = Flatten@Table[Import[FileNameJoin[{current,"sol"<>ToString[i]<>".m"}]], {i,0,<<NN>>}];
322res = C2M@Import[FileNameJoin[{current, "res.txt"}]];
323res = res/.{F[p_, n_] :> j[Symbol["I"<>ToString[p]], Sequence@@n]} /. Dispatch@sols;
324res
325]
326)EOF";
327
328 string_replace_all(sres, "<<NN>>", to_string(pn-1));
329 str2file(sres, dir+"/res.m");
330
331 int tot = p2f.size(), cur = 0;
332 for(auto kv : p2f) {
333 cur++;
334 cout << "[ " << cur << " / " << tot << "] pn = " << kv.second.pn << endl;
335 cout << kv.first << endl;
336 cout << kv.second.intg << endl;
337 cout << endl;
338
339 lst prop = ex_to<lst>(kv.first);
340 auto & f = kv.second;
341 for(int i=0; i<prop.nops(); i++) {
342 bool ok = false;
343 for(auto ip : Internal) {
344 if(prop.op(i).has(ip*ip)) {
345 ok = true;
346 break;
347 }
348 }
349 if(!ok) {
350 cout << "Propagators: " << prop.op(i) << " @ pos: " << i << endl;
351 cout << "Integrals: " << f.intg << endl;
352 abort();
353 }
354 }
355
356 GiNaC_Parallel_Verb["Exp2AMF"] = 0;
357 auto order_vec = GiNaC_Parallel(f.intg.nops(), [&](int idx)->ex{
358 auto ns = f.intg.op(idx);
359 ex cc = res.coeff(F(f.pn, ns)).subs(Numeric);
360 cc = cc.subs(d==4-2*ep);
361 auto ldeg = ep_ldegree(cc);
362 return ldeg;
363 }, "Exp2AMF");
364
365 int order = 0;
366 for(auto ldeg : order_vec) {
367 if(order>ldeg) order = ex2int(ldeg);
368 }
369
370 ex2file(lst{ prop, f.intg, order }, dir+"/"+to_string(f.pn)+".m");
371 }
372
373 }
374
375}
HEP header file.
int pn
Definition Others.cpp:25
lst intg
Definition Others.cpp:26
int NThread
Definition HEP.h:656
lst Replacement
Definition HEP.h:653
lst Internal
Definition HEP.h:651
int Precision
Definition HEP.h:657
void Export(const ex &expr, const string &dir)
Definition Others.cpp:84
lst External
Definition HEP.h:652
lst Numeric
Definition HEP.h:654
class to wrap map_function of GiNaC
Definition BASIC.h:675
HepLib namespace.
Definition BASIC.cpp:17
void ex2file(const ex &expr, string filename)
export expression file
Definition BASIC.cpp:847
const Symbol ep
ex collect_ex(ex const &expr_in, std::function< bool(const ex &)> has_func, int opt)
the collect function like Mathematica
Definition BASIC.cpp:1204
const Symbol eps
ex F2ex(const ex &expr_in)
convert F(ps, ns) to normal ex, ns is like FIRE convention
Definition ApartIBP.cpp:87
ex w
Definition Init.cpp:93
map< string, int > GiNaC_Parallel_Verb
Definition Init.cpp:148
ex exnormal(const ex &expr, int opt)
normalize a expression
Definition BASIC.cpp:1918
void string_replace_all(string &str, const string &from, const string &to)
string ex2str(const ex &expr)
convert ex to output string, the defalut printer format will be used
Definition BASIC.cpp:717
void str2file(const string &ostr, string filename)
export string to a file
Definition BASIC.cpp:789
ex w1
Definition BASIC.h:499
exvector GiNaC_Parallel(int ntotal, std::function< ex(int)> f, const string &key)
GiNaC Parallel Evaluation using fork.
Definition BASIC.cpp:260
ex w2
Definition BASIC.h:499
int ex2int(ex num)
ex to integer
Definition BASIC.cpp:895
ex subs(const ex &e, init_list sl)
Definition BASIC.h:51