8 #include "Pythia8/MathTools.h"
17 double GammaCoef[9] = {
18 0.99999999999980993, 676.5203681218851, -1259.1392167224028,
19 771.32342877765313, -176.61502916214059, 12.507343278686905,
20 -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7};
22 double GammaReal(
double x) {
25 if (x < 0.5)
return M_PI / (sin(M_PI * x) * GammaReal(1 - x));
29 double gamma = GammaCoef[0];
30 for (
int i = 1; i < 9; ++i) gamma += GammaCoef[i] / (z + i);
34 gamma *= sqrt(2. * M_PI) * pow(t, z + 0.5) * exp(-t);
44 double besselI0(
double x){
54 result = 1.0 + 3.5156229 * t2 + 3.0899424 * pow2(t2)
55 + 1.2067492 * pow3(t2) + 0.2659732 * pow4(t2)
56 + 0.0360768 * pow5(t2) + 0.0045813 * pow6(t2);
59 result = exp(x) / sqrt(x) * ( 0.39894228 + 0.01328592 * u
60 + 0.00225319 * pow2(u) - 0.00157565 * pow3(u)
61 + 0.00916281 * pow4(u) - 0.02057706 * pow5(u)
62 + 0.02635537 * pow6(u) - 0.01647633 * pow7(u)
63 + 0.00392377 * pow8(u) );
74 double besselI1(
double x){
84 result = x * ( 0.5 + 0.87890594 * t2 + 0.51498869 * pow2(t2)
85 + 0.15084934 * pow3(t2) + 0.02658733 * pow4(t2)
86 + 0.00301532 * pow5(t2) + 0.00032411 * pow6(t2) );
89 result = exp(x) / sqrt(x) * ( 0.39894228 - 0.03988024 * u
90 - 0.00368018 * pow2(u) + 0.00163801 * pow3(u)
91 - 0.01031555 * pow4(u) + 0.02282967 * pow5(u)
92 - 0.02895312 * pow6(u) + 0.01787654 * pow7(u)
93 - 0.00420059 * pow8(u) );
104 double besselK0(
double x){
111 double x2 = pow2(0.5 * x);
112 result = -log(0.5 * x) * besselI0(x) - 0.57721566
113 + 0.42278420 * x2 + 0.23069756 * pow2(x2)
114 + 0.03488590 * pow3(x2) + 0.00262698 * pow4(x2)
115 + 0.00010750 * pow5(x2) + 0.00000740 * pow6(x2);
118 result = exp(-x) / sqrt(x) * ( 1.25331414 - 0.07832358 * z
119 + 0.02189568 * pow2(z) - 0.01062446 * pow3(z)
120 + 0.00587872 * pow4(z) - 0.00251540 * pow5(z)
121 + 0.00053208 * pow6(z) );
132 double besselK1(
double x){
139 double x2 = pow2(0.5 * x);
140 result = log(0.5 * x) * besselI1(x) + 1./x * ( 1. + 0.15443144 * x2
141 - 0.67278579 * pow2(x2) - 0.18156897 * pow3(x2)
142 - 0.01919402 * pow4(x2) - 0.00110404 * pow5(x2)
143 - 0.00004686 * pow6(x2) );
146 result = exp(-x) / sqrt(x) * ( 1.25331414 + 0.23498619 * z
147 - 0.03655620 * pow2(z) + 0.01504268 * pow3(z)
148 - 0.00780353 * pow4(z) + 0.00325614 * pow5(z)
149 - 0.00068245 * pow6(z) );
163 bool integrateGauss(
double& resultOut,
function<
double(
double)> f,
164 double xLo,
double xHi,
double tol) {
176 static double x8[4]={ 0.96028985649753623, 0.79666647741362674,
177 0.52553240991632899, 0.18343464249564980};
178 static double w8[4]={ 0.10122853629037626, 0.22238103445337447,
179 0.31370664587788729, 0.36268378337836198};
181 static double x16[8]={ 0.98940093499164993, 0.94457502307323258,
182 0.86563120238783174, 0.75540440835500303, 0.61787624440264375,
183 0.45801677765722739, 0.28160355077925891, 0.09501250983763744};
184 static double w16[8]={ 0.027152459411754095, 0.062253523938647893,
185 0.095158511682492785, 0.12462897125553387, 0.14959598881657673,
186 0.16915651939500254, 0.18260341504492359, 0.18945061045506850};
189 double c = 0.001/abs(xHi-xLo);
196 double zMid = 0.5*(zHi+zLo);
197 double zDel = 0.5*(zHi-zLo);
201 for (
int i=0;i<4;i++) {
202 double dz = zDel * x8[i];
203 double f1 = f(zMid+dz);
204 double f2 = f(zMid-dz);
205 s8 += w8[i]*(f1 + f2);
209 for (
int i=0;i<8;i++) {
210 double dz = zDel * x16[i];
211 double f1 = f(zMid+dz);
212 double f2 = f(zMid-dz);
213 s16 += w16[i]*(f1 + f2);
218 if (abs(s16-s8) < tol*(1+abs(s16))) {
224 if ( zLo == zHi ) nextbin =
false;
228 if (1.0 + c*abs(zDel) == 1.0) {
251 bool brent(
double& solutionOut,
function<
double(
double)> f,
double target,
252 double xLo,
double xHi,
double tol,
int maxIter) {
255 if (xLo > xHi)
return false;
258 double f1 = f(xLo) - target;
264 double f2 = f(xHi) - target;
271 if ( f1 * f2 > 0.0)
return false;
276 double x3 = 0.5 * (xLo + xHi);
279 while(++iter < maxIter) {
281 double f3 = f(x3) - target;
288 if (f1 * f3 < 0.0) xHi = x3;
291 if ((xHi - xLo) < tol * (abs(xHi) < 1.0 ? xHi : 1.0)) {
292 solutionOut = 0.5 * (xLo + xHi);
297 double den = (f2 - f1) * (f3 - f1) * (f2 - f3);
298 double num = x3 * (f1 - f2) * (f2 - f3 + f1) + f2 * x1 * (f2 - f3)
299 + f1 * x2 * (f3 - f1);
300 double dx = xHi - xLo;
301 if (den != 0.0) dx = f3 * num / den;
307 if ((xHi - x) * (x - xLo) < 0.0) {
308 dx = 0.5 * (xHi - xLo);
335 double LinearInterpolator::operator()(
double xIn)
const {
337 if (xIn == rightSave)
338 return ysSave.back();
341 double t = (xIn - leftSave) / (rightSave - leftSave);
342 int lastIdx = ysSave.size() - 1;
343 int j = (int)floor(t * lastIdx);
346 if (j < 0 || j >= lastIdx)
return 0.;
349 double s = (xIn - (leftSave + j * dx())) / dx();
350 return (1 - s) * ysSave[j] + s * ysSave[j + 1];
358 Hist LinearInterpolator::plot(
string title)
const {
359 return plot(title, leftSave, rightSave);
362 Hist LinearInterpolator::plot(
string title,
double xMin,
double xMax)
const {
364 int nBins = ceil((xMax - xMin) / (rightSave - leftSave) * ysSave.size());
366 Hist result(title, nBins, xMin, xMax,
false);
367 double dxNow = (xMax - xMin) / nBins;
369 for (
int i = 0; i < nBins; ++i) {
370 double x = xMin + dxNow * (0.5 + i);
371 result.fill(x,
operator()(x));