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() {
226 return HepLib::SD::VEResult(ResultError);
227 }
228
229 void SecDec::VEPrint(bool endlQ) {
230 ex expr = HepLib::SD::VEResult(ResultError);
231 for(int i=expr.ldegree(eps); i<=expr.degree(eps); i++) {
232 ex exp1 = expr.coeff(eps, i);
233 for(int j=expr.ldegree(ep); j<=expr.degree(ep); j++) {
234 cout << Color_HighLight <<"(" << RESET;
235 cout << exp1.coeff(ep, j);
236 cout << Color_HighLight << ")" << RESET;
237 if(j!=0 || i!=0) cout << "*" << Color_HighLight << pow(ep,j)*pow(eps,i) << RESET;
238 if(j<expr.degree(ep)) cout << " + ";
239 }
240 }
241 if(endlQ) cout << endl;
242 }
243
244 ex Factor(const ex expr_in) {
245 ex expr = collect_common_factors(expr_in);
246 if(is_a<mul>(expr)) {
247 ex ret = 1;
248 for(auto item : expr) {
249 if(item.has(x(w)) || (item.has(y(w))) || (item.has(z(w)))) ret *= Factor(item);
250 else ret *= item;
251 }
252 return ret;
253 } else if(is_a<power>(expr) || expr.match(pow(w1,w2))) {
254 return pow(Factor(expr.op(0)), expr.op(1));
255 }
256
257 exset xyset;
258 expr.find(x(w), xyset);
259 expr.find(y(w), xyset);
260 expr.find(z(w), xyset);
261 expr.find(PL(w), xyset);
262 lst xy2s, s2xy;
263 for(auto xyi : xyset) {
264 symbol txy;
265 xy2s.append(xyi==txy);
266 s2xy.append(txy==xyi);
267 }
268 ex expr2 = xyz_pow_simplify(expr);
269 expr2 = collect_common_factors(expr2);
270 expr2 = expr2.subs(xy2s);
271 expr2 = factor(expr2);
272 expr2 = expr2.subs(s2xy);
273 expr2 = xyz_pow_simplify(expr2);
274 expr2 = collect_common_factors(expr2);
275 return expr2;
276 }
277
278 ex FactorOutX(const ex expr) {
279 exset xset;
280 expr.find(x(w), xset);
281 lst x2s, s2x;
282 for(auto xi : xset) {
283 symbol tx;
284 x2s.append(xi==tx);
285 s2x.append(tx==xi);
286 }
287 ex expr2 = xyz_pow_simplify(expr);
288 expr2 = collect_common_factors(expr2);
289 expr2 = expr2.subs(x2s);
290 expr2 = factor(expr2);
291 expr2 = expr2.subs(s2x);
292 expr2 = xyz_pow_simplify(expr2);
293 expr2 = collect_common_factors(expr2);
294 if(!is_a<mul>(expr2)) return expr2;
295 ex ret = 1;
296 for(auto item : expr2) {
297 if(!item.match(x(w)) && !item.match(pow(x(w1),w2))) ret *= item;
298 }
299 return ret;
300 }
301
302 ex exp_simplify(const ex expr_in) {
303 auto expr = expr_in;
304 exmap sub_exp;
305 sub_exp[pow(exp(w1),w2)]=exp(w1*w2);
306 sub_exp[sqrt(exp(w1))]=exp(w1/2);
307 sub_exp[exp(w1)*exp(w2)*w0]=exp(w1+w2)*w0;
308 sub_exp[exp(w1)*exp(w2)]=exp(w1+w2);
309 while(true) {
310 auto expo = expr.subs(sub_exp);
311 if(is_zero(expo-expr)) break;
312 expr = expo;
313 }
314 return expr;
315 }
316
317 ex pow_simplify(const ex expr_in) {
318 auto expr = expr_in;
319 exmap sub_pow;
320 sub_pow[pow(pow(w1,w2),w3)] = pow(w1,w2*w3);
321 sub_pow[sqrt(pow(w1,w2))] = pow(w1,w2/2);
322 sub_pow[pow(sqrt(w1),w2)] = pow(w1,w2/2);
323 sub_pow[pow(w1,w2)*pow(w1,w3)*w0] = pow(w1,w2+w3)*w0;
324 sub_pow[pow(w1,w2)*w1*w0] = pow(w1,w2+1)*w0;
325 sub_pow[pow(w1,w2)/w1*w0] = pow(w1,w2-1)*w0;
326 sub_pow[pow(w1,w2)*sqrt(w1)] = pow(w1,w2+1/ex(2));
327 sub_pow[pow(w1,w2)/sqrt(w1)] = pow(w1,w2-1/ex(2));
328 while(true) {
329 auto expo = expr.subs(sub_pow);
330 if(is_zero(expo-expr)) break;
331 expr = expo;
332 }
333 return expr;
334 }
335
336 ex xyz_pow_simplify(const ex expr_in) {
337 ex expr = expr_in;
338
339 //copied from pow_simplify
340 exmap sub_pow;
341 sub_pow[pow(pow(w1,w2),w3)] = pow(w1,w2*w3);
342 sub_pow[sqrt(pow(w1,w2))] = pow(w1,w2/2);
343 sub_pow[pow(sqrt(w1),w2)] = pow(w1,w2/2);
344 sub_pow[pow(w1,w2)*pow(w1,w3)*w0] = pow(w1,w2+w3)*w0;
345 sub_pow[pow(w1,w2)*w1*w0] = pow(w1,w2+1)*w0;
346 sub_pow[pow(w1,w2)/w1*w0] = pow(w1,w2-1)*w0;
347 sub_pow[pow(w1,w2)*sqrt(w1)] = pow(w1,w2+1/ex(2));
348 sub_pow[pow(w1,w2)/sqrt(w1)] = pow(w1,w2-1/ex(2));
349
350 sub_pow[pow(x(w1)*w2,w3)] = pow(x(w1),w3)*pow(w2,w3);
351 sub_pow[pow(y(w1)*w2,w3)] = pow(y(w1),w3)*pow(w2,w3);
352 sub_pow[pow(z(w1)*w2,w3)] = pow(z(w1),w3)*pow(w2,w3);
353 while(true) {
354 auto expo = expr.subs(sub_pow);
355 if(is_zero(expo-expr)) break;
356 expr = expo;
357 }
358 return expr;
359 }
360
361 ex SecDec::PrefactorFIESTA(int nLoop) {
362 return pow(I*pow(Pi,2-ep)*exp(ex(0)-ep*Euler), ex(0)-ex(nLoop));
363 }
364
365
366 /*-----------------------------------------------------*/
367 // Refined F-Term
368 /*-----------------------------------------------------*/
369 ex SecDec::XRefined(ex const & in_ft) {
370 auto ft = Factor(in_ft);
371 while(true) {
372 auto ft0 = ft;
373 if(ft.match(pow(w1, w2))) {
374 ft = ft.op(0);
375 } else if(is_a<mul>(ft)) {
376 ex tmp = 1;
377 for(auto fti : ft) {
378 auto s = xSign(fti);
379 if(s>0) continue;
380 else if(s<0) tmp = ex(0)-tmp;
381 else tmp = tmp * fti;
382 }
383 ft = tmp;
384 if((ft-ft0).is_zero()) break;
385 continue;
386 }
387 break;
388 }
389 return ft;
390 }
391
392 lst SecDec::XRefined_lst(ex const & in_ft) {
393 auto ft = XRefined(in_ft);
394 lst ret;
395 if(is_a<mul>(ft)) {
396 for(auto const &item : ft) ret.append(item);
397 } else {
398 ret.append(ft);
399 }
400 return ret;
401 }
402
403}
int * a
#define RESET
Definition BASIC.h:79
SecDec header file.
class used to wrap error message
Definition BASIC.h:242
namespace for Numerical integration with Sector Decomposition method
Definition AsyMB.cpp:10
ex FactorOutX(const ex expr)
Definition Helpers.cpp:278
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:336
ex exp_simplify(const ex expr_in)
Definition Helpers.cpp:302
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 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:244
ex VEResult2(ex expr)
Definition Helpers.cpp:127
ex pow_simplify(const ex expr_in)
Definition Helpers.cpp:317
const char * Color_HighLight
Definition BASIC.cpp:248
ex EvalF(ex expr)
the nuerical evaluation, Digits=100 will be used
Definition BASIC.cpp:1245
ex NN(ex expr, int digits)
the nuerical evaluation
Definition BASIC.cpp:1278
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:1202
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:1222
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:1312
ex w2
Definition BASIC.h:499
const Symbol NaN