HepLib
Loading...
Searching...
No Matches
Helpers.cpp
Go to the documentation of this file.
1
6#include "SD.h"
7
8namespace HepLib::SD {
9
10 /*-----------------------------------------------------*/
11 // Global Functions
12 /*-----------------------------------------------------*/
13
14 exvector get_xy_from(ex pol) {
15 exset xyset;
16 bool ok = pol.find(x(w), xyset);
17 if(!ok) {
18 ok = pol.find(y(w), xyset);
19 if(!ok) return exvector();
20 }
21 exvector xys(xyset.size());
22 copy(xyset.begin(), xyset.end(), xys.begin());
23 sort(xys.begin(), xys.end(), [](const auto &a, const auto &b){
24 return normal((b-a)).subs(lst{ x(w)==w, y(w)==w }).info(info_flags::positive);
25 });
26 return exvector(std::move(xys));
27 }
28
29 exvector get_x_from(ex pol) {
30 exset xset;
31 bool ok = pol.find(x(w), xset);
32 if(!ok) {
33 exvector xs(0);
34 return xs;
35 }
36 exvector xs(xset.size());
37 copy(xset.begin(), xset.end(), xs.begin());
38 sort(xs.begin(), xs.end(), [](const auto &a, const auto &b){
39 return normal((b-a)).subs(lst{ x(w)==w }).info(info_flags::positive);
40 });
41 return exvector(std::move(xs));
42 }
43
44 exvector get_y_from(ex pol) {
45 exset yset;
46 bool ok = pol.find(y(w), yset);
47 if(!ok) {
48 exvector ys(0);
49 return ys;
50 }
51 exvector ys(yset.size());
52 copy(yset.begin(), yset.end(), ys.begin());
53 sort(ys.begin(), ys.end(), [](const auto &a, const auto &b){
54 return normal((b-a)).subs(lst{ y(w)==w }).info(info_flags::positive);
55 });
56 return exvector(std::move(ys));
57 }
58
59 exvector get_z_from(ex pol) {
60 exset zset;
61 bool ok = pol.find(z(w), zset);
62 if(!ok) {
63 exvector zs(0);
64 return zs;
65 }
66 exvector zs(zset.size());
67 copy(zset.begin(), zset.end(), zs.begin());
68 sort(zs.begin(), zs.end(), [](const auto &a, const auto &b){
69 return normal((b-a)).subs(lst{ z(w)==w }).info(info_flags::positive);
70 });
71 return exvector(std::move(zs));
72 }
73
74 exvector get_pl_from(ex pol) {
75 exset plset;
76 bool ok = pol.find(PL(w), plset);
77 if(!ok) {
78 exvector pls(0);
79 return pls;
80 }
81 exvector pls(plset.size());
82 copy(plset.begin(), plset.end(), pls.begin());
83 sort(pls.begin(), pls.end(), [](const auto &a, const auto &b){
84 return normal((b-a)).subs(lst{ PL(w)==w }).info(info_flags::positive);
85 });
86 return exvector(std::move(pls));
87 }
88
89 /*-----------------------------------------------------*/
90 // VE
91 /*-----------------------------------------------------*/
92 ex VESimplify(ex expr) {
93 auto ee = NN(EvalF(expr));
94 auto cvs = collect_lst(ee, [](const ex & e)->bool { return !is_a<numeric>(e) && !e.match(VE(w1,w2)); });
95 ex ret = 0;
96 for(auto cv : cvs) {
97 auto vf = cv.op(1);
98 auto cc_vv_lst = collect_lst(cv.op(0), VE(w1,w2));
99 ex vIR=0, eI2 = 0, eR2 = 0;
100 for(auto cc_vv : cc_vv_lst) {
101 auto co = NN(cc_vv.op(0));
102 auto ve = cc_vv.op(1);
103 if(!is_a<numeric>(co)) {
104 cout << cv << endl;
105 throw Error("VESimplify: coefficient of VE is not numeric.");
106 }
107 if(abs(co)>numeric("1E300")) return NaN;
108 vIR += co * ve.op(0);
109 numeric nco = ex_to<numeric>(co);
110 ex ee = ve.op(1) * ve.op(1);
111 eR2 += nco.real_part() * nco.real_part() * ee;
112 eI2 += nco.imag_part() * nco.imag_part() * ee;
113 }
114 if(!is_zero(eR2) || !is_zero(vIR))
115 ret += VE(ex_to<numeric>(vIR).real_part(), sqrt(eR2)) * vf;
116 if(!is_zero(eI2) || !is_zero(vIR))
117 ret += VE(ex_to<numeric>(vIR).imag_part(), sqrt(eI2)) * vf * I;
118 }
119
120 return ret;
121 }
122
123 ex VEResult(ex expr) {
124 return expr.subs(VE(0,0)==0).subs(VE(w1,w2)==VEO(w1,w2));
125 }
126
127 ex VEResult2(ex expr) {
128 return expr.subs(VE(0,0)==0).subs(VE(w1,w2)==VEO2(w1,w2));
129 }
130
131 ex VEMaxErr(ex expr) {
132
133 auto ccRes = expr.expand();
134 lst ccResList;
135 if(is_a<add>(ccRes)) {
136 for(auto item : ccRes) ccResList.append(item);
137 } else {
138 ccResList.append(ccRes);
139 }
140
141 ccRes = -100;
142 for(auto item : ccResList) {
143 ex ntmp;
144 if(is_a<mul>(item)) {
145 ntmp = 1;
146 for(auto ii : item) {
147 if(is_a<numeric>(ii) || ii.match(VE(w1, w2))) {
148 ntmp *= ii;
149 }
150 }
151 } else if(is_a<numeric>(item) || item.match(VE(w1, w2))) {
152 ntmp = item;
153 }
154 ntmp = NN(abs(ntmp.subs(VE(w1,w2)==w2)));
155 if(ntmp>ccRes) ccRes = ntmp;
156 }
157
158 return ccRes;
159 }
160
161
162 /*-----------------------------------------------------*/
163 // Heplers used in SD
164 /*-----------------------------------------------------*/
165 int x_free_index(ex expr) {
166 auto xs = get_x_from(expr);
167 for(int i=0; i<=xs.size(); i++) {
168 bool ok = true;
169 for(auto xi : xs) {
170 if(xi.is_equal(x(i))) {
171 ok = false;
172 break;
173 }
174 }
175 if(ok) return i;
176 }
177 return -1;
178 }
179
180 int y_free_index(ex expr) {
181 auto ys = get_y_from(expr);
182 for(int i=0; i<=ys.size(); i++) {
183 bool ok = true;
184 for(auto yi : ys) {
185 if(yi.is_equal(y(i))) {
186 ok = false;
187 break;
188 }
189 }
190 if(ok) return i;
191 }
192 return -1;
193 }
194
195 int epsRank(ex expr_in, ex epi) {
196 static symbol s;
197 if(!expr_in.has(epi)) return 0;
198 int p = -5;
199 auto expr = expr_in.subs(epi==s);
200 expr = collect_ex(expr, s);
201 while(true) {
202 auto tmp = series_to_poly(expr.series(s, p));
203 if(!tmp.is_zero()) {
204 tmp = collect_ex(tmp, s);
205 return tmp.ldegree(s);
206 } else p++;
207 }
208 throw Error("epsRank error!");
209 }
210
211 int vsRank(ex expr_in) {
212 if(!expr_in.has(vs)) return 0;
213 int p = -5;
214 auto expr = collect_ex(expr_in, vs);
215 while(true) {
216 auto tmp = series_to_poly(expr.series(vs, p));
217 if(!tmp.is_zero()) {
218 tmp = collect_ex(tmp, vs);
219 return tmp.ldegree(vs);
220 } else p++;
221 }
222 throw Error("vsRank error!");
223 }
224
225 ex SecDec::VEResult(const ex & chop_err) {
226 if(chop_err.is_zero()) {
227 return HepLib::SD::VEResult(ResultError);
228 } else {
229 ex res = MapFunction([chop_err](const ex & e, MapFunction &self)->ex{
230 if(!e.has(VE(w1,w2))) return e;
231 else if(e.match(VE(w1, w2))) {
232 ex vv = e.op(0);
233 ex ee = e.op(1);
234 if(abs(vv)<chop_err && abs(ee)<chop_err) return 0;
235 else return e;
236 } else return e.map(self);
237 })(ResultError);
238 return HepLib::SD::VEResult(res);
239 }
240 }
241
242 void SecDec::VEPrint(bool endlQ) {
243 ex expr = HepLib::SD::VEResult(ResultError);
244 for(int i=expr.ldegree(eps); i<=expr.degree(eps); i++) {
245 ex exp1 = expr.coeff(eps, i);
246 for(int j=expr.ldegree(ep); j<=expr.degree(ep); j++) {
247 cout << Color_HighLight <<"(" << RESET;
248 cout << exp1.coeff(ep, j);
249 cout << Color_HighLight << ")" << RESET;
250 if(j!=0 || i!=0) cout << "*" << Color_HighLight << pow(ep,j)*pow(eps,i) << RESET;
251 if(j<expr.degree(ep)) cout << " + ";
252 }
253 }
254 if(endlQ) cout << endl;
255 }
256
257 ex Factor(const ex expr_in) {
258 ex expr = collect_common_factors(expr_in);
259 if(is_a<mul>(expr)) {
260 ex ret = 1;
261 for(auto item : expr) {
262 if(item.has(x(w)) || (item.has(y(w))) || (item.has(z(w)))) ret *= Factor(item);
263 else ret *= item;
264 }
265 return ret;
266 } else if(is_a<power>(expr) || expr.match(pow(w1,w2))) {
267 return pow(Factor(expr.op(0)), expr.op(1));
268 }
269
270 exset xyset;
271 expr.find(x(w), xyset);
272 expr.find(y(w), xyset);
273 expr.find(z(w), xyset);
274 expr.find(PL(w), xyset);
275 lst xy2s, s2xy;
276 for(auto xyi : xyset) {
277 symbol txy;
278 xy2s.append(xyi==txy);
279 s2xy.append(txy==xyi);
280 }
281 ex expr2 = xyz_pow_simplify(expr);
282 expr2 = collect_common_factors(expr2);
283 expr2 = expr2.subs(xy2s);
284 expr2 = factor(expr2);
285 expr2 = expr2.subs(s2xy);
286 expr2 = xyz_pow_simplify(expr2);
287 expr2 = collect_common_factors(expr2);
288 return expr2;
289 }
290
291 ex FactorOutX(const ex expr) {
292 exset xset;
293 expr.find(x(w), xset);
294 lst x2s, s2x;
295 for(auto xi : xset) {
296 symbol tx;
297 x2s.append(xi==tx);
298 s2x.append(tx==xi);
299 }
300 ex expr2 = xyz_pow_simplify(expr);
301 expr2 = collect_common_factors(expr2);
302 expr2 = expr2.subs(x2s);
303 expr2 = factor(expr2);
304 expr2 = expr2.subs(s2x);
305 expr2 = xyz_pow_simplify(expr2);
306 expr2 = collect_common_factors(expr2);
307 if(!is_a<mul>(expr2)) return expr2;
308 ex ret = 1;
309 for(auto item : expr2) {
310 if(!item.match(x(w)) && !item.match(pow(x(w1),w2))) ret *= item;
311 }
312 return ret;
313 }
314
315 ex FactorFT(const ex & expr) {
316 auto f = exfactor(expr);
317 if(!is_a<mul>(f)) f = lst{ f };
318 ex ft = 1;
319 for(auto item : f) {
320 auto s = xSign(item);
321 if(s>0) continue;
322 else if(s<0) ft *= -1;
323 else ft *= item;
324 }
325 return ft;
326 }
327
328 ex exp_simplify(const ex expr_in) {
329 auto expr = expr_in;
330 exmap sub_exp;
331 sub_exp[pow(exp(w1),w2)]=exp(w1*w2);
332 sub_exp[sqrt(exp(w1))]=exp(w1/2);
333 sub_exp[exp(w1)*exp(w2)*w0]=exp(w1+w2)*w0;
334 sub_exp[exp(w1)*exp(w2)]=exp(w1+w2);
335 while(true) {
336 auto expo = expr.subs(sub_exp);
337 if(is_zero(expo-expr)) break;
338 expr = expo;
339 }
340 return expr;
341 }
342
343 ex pow_simplify(const ex expr_in) {
344 auto expr = expr_in;
345 exmap sub_pow;
346 sub_pow[pow(pow(w1,w2),w3)] = pow(w1,w2*w3);
347 sub_pow[sqrt(pow(w1,w2))] = pow(w1,w2/2);
348 sub_pow[pow(sqrt(w1),w2)] = pow(w1,w2/2);
349 sub_pow[pow(w1,w2)*pow(w1,w3)*w0] = pow(w1,w2+w3)*w0;
350 sub_pow[pow(w1,w2)*w1*w0] = pow(w1,w2+1)*w0;
351 sub_pow[pow(w1,w2)/w1*w0] = pow(w1,w2-1)*w0;
352 sub_pow[pow(w1,w2)*sqrt(w1)] = pow(w1,w2+1/ex(2));
353 sub_pow[pow(w1,w2)/sqrt(w1)] = pow(w1,w2-1/ex(2));
354 while(true) {
355 auto expo = expr.subs(sub_pow);
356 if(is_zero(expo-expr)) break;
357 expr = expo;
358 }
359 return expr;
360 }
361
362 ex xyz_pow_simplify(const ex expr_in) {
363 ex expr = expr_in;
364
365 //copied from pow_simplify
366 exmap sub_pow;
367 sub_pow[pow(pow(w1,w2),w3)] = pow(w1,w2*w3);
368 sub_pow[sqrt(pow(w1,w2))] = pow(w1,w2/2);
369 sub_pow[pow(sqrt(w1),w2)] = pow(w1,w2/2);
370 sub_pow[pow(w1,w2)*pow(w1,w3)*w0] = pow(w1,w2+w3)*w0;
371 sub_pow[pow(w1,w2)*w1*w0] = pow(w1,w2+1)*w0;
372 sub_pow[pow(w1,w2)/w1*w0] = pow(w1,w2-1)*w0;
373 sub_pow[pow(w1,w2)*sqrt(w1)] = pow(w1,w2+1/ex(2));
374 sub_pow[pow(w1,w2)/sqrt(w1)] = pow(w1,w2-1/ex(2));
375
376 sub_pow[pow(x(w1)*w2,w3)] = pow(x(w1),w3)*pow(w2,w3);
377 sub_pow[pow(y(w1)*w2,w3)] = pow(y(w1),w3)*pow(w2,w3);
378 sub_pow[pow(z(w1)*w2,w3)] = pow(z(w1),w3)*pow(w2,w3);
379 while(true) {
380 auto expo = expr.subs(sub_pow);
381 if(is_zero(expo-expr)) break;
382 expr = expo;
383 }
384 return expr;
385 }
386
387 ex SecDec::PrefactorFIESTA(int nLoop) {
388 return pow(I*pow(Pi,2-ep)*exp(ex(0)-ep*Euler), ex(0)-ex(nLoop));
389 }
390
391
392 /*-----------------------------------------------------*/
393 // Refined F-Term
394 /*-----------------------------------------------------*/
395 ex SecDec::XRefined(ex const & in_ft) {
396 auto ft = Factor(in_ft);
397 while(true) {
398 auto ft0 = ft;
399 if(ft.match(pow(w1, w2))) {
400 ft = ft.op(0);
401 } else if(is_a<mul>(ft)) {
402 ex tmp = 1;
403 for(auto fti : ft) {
404 auto s = xSign(fti);
405 if(s>0) continue;
406 else if(s<0) tmp = ex(0)-tmp;
407 else tmp = tmp * fti;
408 }
409 ft = tmp;
410 if((ft-ft0).is_zero()) break;
411 continue;
412 }
413 break;
414 }
415 return ft;
416 }
417
418 lst SecDec::XRefined_lst(ex const & in_ft) {
419 auto ft = XRefined(in_ft);
420 lst ret;
421 if(is_a<mul>(ft)) {
422 for(auto const &item : ft) ret.append(item);
423 } else {
424 ret.append(ft);
425 }
426 return ret;
427 }
428
429}
int * a
#define RESET
Definition BASIC.h:79
SecDec header file.
class used to wrap error message
Definition BASIC.h:242
class to wrap map_function of GiNaC
Definition BASIC.h:675
namespace for Numerical integration with Sector Decomposition method
Definition AsyMB.cpp:10
ex FactorOutX(const ex expr)
Definition Helpers.cpp:291
exvector get_z_from(ex pol)
Definition Helpers.cpp:59
exvector get_xy_from(ex pol)
Definition Helpers.cpp:14
ex xyz_pow_simplify(const ex expr_in)
Definition Helpers.cpp:362
ex exp_simplify(const ex expr_in)
Definition Helpers.cpp:328
int x_free_index(ex expr)
Definition Helpers.cpp:165
int vsRank(ex expr_in)
Definition Helpers.cpp:211
ex VEResult(ex expr)
Definition Helpers.cpp:123
ex FactorFT(const ex &expr)
Definition Helpers.cpp:315
ex VESimplify(ex expr)
Definition Helpers.cpp:92
ex VEMaxErr(ex expr)
Definition Helpers.cpp:131
exvector get_pl_from(ex pol)
Definition Helpers.cpp:74
exvector get_y_from(ex pol)
Definition Helpers.cpp:44
exvector get_x_from(ex pol)
Definition Helpers.cpp:29
int y_free_index(ex expr)
Definition Helpers.cpp:180
int epsRank(ex expr_in, ex epi)
Definition Helpers.cpp:195
ex Factor(const ex expr_in)
Definition Helpers.cpp:257
ex VEResult2(ex expr)
Definition Helpers.cpp:127
ex pow_simplify(const ex expr_in)
Definition Helpers.cpp:343
const char * Color_HighLight
Definition BASIC.cpp:248
ex exfactor(const ex &expr_in, int opt)
factorize a expression
Definition BASIC.cpp:1856
ex EvalF(ex expr)
the nuerical evaluation, Digits=100 will be used
Definition BASIC.cpp:1247
ex NN(ex expr, int digits)
the nuerical evaluation
Definition BASIC.cpp:1280
ex w0
Definition BASIC.h:499
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 w
Definition Init.cpp:93
const Symbol vs
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:1224
ex w1
Definition BASIC.h:499
ex w3
Definition BASIC.h:499
int xSign(ex const expr)
the always sign for expr
Definition BASIC.cpp:1314
ex w2
Definition BASIC.h:499
const Symbol NaN