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 is NOT the form of a*b" << endl;
161 abort();
162 }
163
164 if(ns.op(i)==-1) {
165 ns_p.append(i);
166 } else if(ns.op(i)!=0) {
167 cout << f << endl;
168 cout << "n of qi.pi is NOT -1" << endl;
169 abort();
170 } else { // ns.op(i)==0
171 auto pa = pi.op(0), pb = pi.op(1);
172 ex pab = expand(pow(pa+pb,2)).subs(Replacement);
173 ps.let_op(i) = pab;
174 bool ic = is_complete(ps, sp2x);
175 if(!ic && is_loop(pa)) {
176 ps.let_op(i) = expand(pow(pa,2)).subs(Replacement);
177 ic = is_complete(ps, sp2x);
178 }
179 if(!ic && is_loop(pb)) {
180 ps.let_op(i) = expand(pow(pb,2)).subs(Replacement);
181 ic = is_complete(ps, sp2x);
182 }
183 if(!ic) {
184 cout << "1st: ic is NOT true!" << endl;
185 abort();
186 }
187 }
188 }
189 }
190 if(ns_p.nops()>1) {
191 cout << f << endl;
192 abort();
193 } else if(ns_p.nops()>0) {
194 auto idx = ex2int(ns_p.op(0));
195 ns.let_op(idx) = 0;
196 auto pidx = ps.op(idx);
197
198 ex pa = pidx.op(0);
199 ex pb = pidx.op(1);
200
201 ex pab = expand(pow(pa+pb,2)).subs(Replacement);
202 ps.let_op(idx) = pab;
203 bool ic = is_complete(ps, sp2x);
204 if(!ic && is_loop(pa)) {
205 ps.let_op(idx) = expand(pow(pa,2)).subs(Replacement);
206 ic = is_complete(ps, sp2x);
207 }
208 if(!ic && is_loop(pb)) {
209 ps.let_op(idx) = expand(pow(pb,2)).subs(Replacement);
210 ic = is_complete(ps, sp2x);
211 }
212 if(!ic) {
213 cout << "2nd: ic is NOT true!" << endl;
214 abort();
215 }
216
217 auto xps = subs(ps,sp2x);
218 auto xpidx = pidx.subs(sp2x);
219 for(auto ip : Internal) {
220 if(xps.has(ip)) {
221 cout << "xps still has ip" << endl;
222 cout << "xps: " << xps << endl;
223 abort();
224 }
225 if(xpidx.has(ip)) {
226 cout << "xpidx still has ip" << endl;
227 cout << "xpidx: " << xpidx << endl;
228 abort();
229 }
230 }
231
232 ex xeq = 0, eq = 0, fres = 0;
233 lst ys;
234 for(int ii=0; ii<ps.nops(); ii++) {
235 symbol yi;
236 xeq += yi*xps.op(ii);
237 eq += yi*ps.op(ii);
238 lst nn = ns;
239 nn.let_op(ii) = ns.op(ii)-1;
240 fres += yi*F(ps, nn);
241 ys.append(yi);
242 }
243 lst eqs;
244 for(int ii=0; ii<ps.nops(); ii++) {
245 auto xi = x(ii);
246 eqs.append(xeq.coeff(xi)==xpidx.coeff(xi));
247 }
248 auto sol = lsolve(eqs,ys);
249
250 if(sol.nops()<1 || sol.has(x(w))) {
251 cout << "wrong sol: " << sol << endl;
252 }
253
254 auto rem = pidx-eq.subs(sol);
255 fres = fres.subs(sol);
256 if(!is_zero(rem)) {
257 fres += rem * F(ps, ns);
258 }
259
260 pq_f2f[f] = fres;
261 ex chk = exnormal(F2ex(f-pq_f2f[f]));
262 if(chk!=0) {
263 cout << "Failed: chk=" << chk << endl;
264 }
265 } else if(!f.op(0).is_equal(ps)) {
266 pq_f2f[f] = F(ps, ns);
267 ex chk = exnormal(F2ex(f-pq_f2f[f]));
268 if(chk!=0) {
269 cout << "Failed: chk=" << chk << endl;
270 }
271 }
272 }
273
274 cout << "Collecting ..." << endl;
275
276 res = MapFunction([&](const ex & e, MapFunction &self)->ex{
277 if(e.match(F(w1, w2))) {
278 auto itr = pq_f2f.find(e);
279 if(itr==pq_f2f.end()) {
280 cout << "F Not Found" << endl;
281 abort();
282 }
283 return itr->second;
284 } else return e.map(self);
285 })(res);
286
287 res = collect_ex(res, F(w1,w2));
288
289 exset fs;
290 find(res, F(w1,w2), fs);
291 map<ex,Family,ex_is_less> p2f;
292 exmap f2f;
293 int pn = 0;
294 for(auto f : fs) {
295 auto itr = p2f.find(f.op(0));
296 if(itr == p2f.end()) {
297 itr = p2f.insert({f.op(0), Family(pn)}).first;
298 pn++;
299 }
300 itr->second.intg.append(f.op(1));
301 f2f[f] = F(itr->second.pn, f.op(1));
302 }
303
304 res = MapFunction([&](const ex & e, MapFunction &self)->ex{
305 if(e.match(F(w1, w2))) {
306 auto itr = f2f.find(e);
307 if(itr==f2f.end()) {
308 cout << "F Not Found" << endl;
309 abort();
310 }
311 return itr->second;
312 } else return e.map(self);
313 })(res);
314
315 cout << "Exporting res.txt ..." << endl;
316 ex2file(res.subs(Numeric), dir+"/res.txt");
317
318 string sres =
319R"EOF(
320current = If[$FrontEnd===Null,$InputFileName,NotebookFileName[]]//DirectoryName;
321<<HepLib`
322Module[{sols, res},
323sols = Flatten@Table[Import[FileNameJoin[{current,"sol"<>ToString[i]<>".m"}]], {i,0,<<NN>>}];
324res = C2M@Import[FileNameJoin[{current, "res.txt"}]];
325res = res/.{F[p_, n_] :> j[Symbol["I"<>ToString[p]], Sequence@@n]} /. Dispatch@sols;
326res
327]
328)EOF";
329
330 string_replace_all(sres, "<<NN>>", to_string(pn-1));
331 str2file(sres, dir+"/res.m");
332
333 int tot = p2f.size(), cur = 0;
334 for(auto kv : p2f) {
335 cur++;
336 cout << "[ " << cur << " / " << tot << "] pn = " << kv.second.pn << endl;
337 cout << kv.first << endl;
338 cout << kv.second.intg << endl;
339 cout << endl;
340
341 lst prop = ex_to<lst>(kv.first);
342 auto & f = kv.second;
343 for(int i=0; i<prop.nops(); i++) {
344 bool ok = false;
345 for(auto ip : Internal) {
346 if(prop.op(i).has(ip*ip)) {
347 ok = true;
348 break;
349 }
350 }
351 if(!ok) {
352 cout << "Propagators: " << prop.op(i) << " @ pos: " << i << endl;
353 cout << "Integrals: " << f.intg << endl;
354 abort();
355 }
356 }
357
358 GiNaC_Parallel_Verb["Exp2AMF"] = 0;
359 auto order_vec = GiNaC_Parallel(f.intg.nops(), [&](int idx)->ex{
360 auto ns = f.intg.op(idx);
361 ex cc = res.coeff(F(f.pn, ns)).subs(Numeric);
362 cc = cc.subs(d==4-2*ep);
363 auto ldeg = ep_ldegree(cc);
364 return ldeg;
365 }, "Exp2AMF");
366
367 int order = 0;
368 for(auto ldeg : order_vec) {
369 if(order>ldeg) order = ex2int(ldeg);
370 }
371
372 ex2file(lst{ prop, f.intg, order }, dir+"/"+to_string(f.pn)+".m");
373 }
374
375 }
376
377}
HEP header file.
int pn
Definition Others.cpp:25
lst intg
Definition Others.cpp:26
int NThread
Definition HEP.h:647
lst Replacement
Definition HEP.h:644
lst Internal
Definition HEP.h:642
int Precision
Definition HEP.h:648
void Export(const ex &expr, const string &dir)
Definition Others.cpp:84
lst External
Definition HEP.h:643
lst Numeric
Definition HEP.h:645
class to wrap map_function of GiNaC
Definition BASIC.h:632
HepLib namespace.
Definition BASIC.cpp:17
void ex2file(const ex &expr, string filename)
export expression file
Definition BASIC.cpp:845
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:1202
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:1916
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:715
void str2file(const string &ostr, string filename)
export string to a file
Definition BASIC.cpp:787
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:893
ex subs(const ex &e, init_list sl)
Definition BASIC.h:51