1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////
20 // Various mathematical tools which may be very convenient while
21 // performing physics analysis.
23 // Example : Probability of a Chi-squared value
27 // Float_t chi2=20; // The chi-squared value
28 // Int_t ndf=12; // The number of degrees of freedom
29 // Float_t p=M.Prob(chi2,ndf); // The probability that at least a Chi-squared
30 // // value of chi2 will be observed, even for a
33 //--- Author: Nick van Eijndhoven 14-nov-1998 UU-SAP Utrecht
34 //- Modified: NvE $Date$ UU-SAP Utrecht
35 ///////////////////////////////////////////////////////////////////////////
38 #include "Riostream.h"
40 ClassImp(AliMath) // Class implementation to enable ROOT I/O
42 AliMath::AliMath() : TObject()
44 // Default constructor
46 ///////////////////////////////////////////////////////////////////////////
51 ///////////////////////////////////////////////////////////////////////////
52 AliMath::AliMath(const AliMath& m) : TObject(m)
56 ///////////////////////////////////////////////////////////////////////////
57 Double_t AliMath::Gamma(Double_t z) const
59 // Computation of gamma(z) for all z>0.
61 // The algorithm is based on the article by C.Lanczos [1] as denoted in
62 // Numerical Recipes 2nd ed. on p. 207 (W.H.Press et al.).
64 // [1] C.Lanczos, SIAM Journal of Numerical Analysis B1 (1964), 86.
66 //--- Nve 14-nov-1998 UU-SAP Utrecht
70 cout << "*Gamma(z)* Wrong argument z = " << z << endl;
74 Double_t v=LnGamma(z);
77 ///////////////////////////////////////////////////////////////////////////
78 Double_t AliMath::Gamma(Double_t a,Double_t x) const
80 // Computation of the incomplete gamma function P(a,x)
82 // The algorithm is based on the formulas and code as denoted in
83 // Numerical Recipes 2nd ed. on p. 210-212 (W.H.Press et al.).
85 //--- Nve 14-nov-1998 UU-SAP Utrecht
89 cout << "*Gamma(a,x)* Invalid argument a = " << a << endl;
95 if (x<0) cout << "*Gamma(a,x)* Invalid argument x = " << x << endl;
108 ///////////////////////////////////////////////////////////////////////////
109 Double_t AliMath::LnGamma(Double_t z) const
111 // Computation of ln[gamma(z)] for all z>0.
113 // The algorithm is based on the article by C.Lanczos [1] as denoted in
114 // Numerical Recipes 2nd ed. on p. 207 (W.H.Press et al.).
116 // [1] C.Lanczos, SIAM Journal of Numerical Analysis B1 (1964), 86.
118 // The accuracy of the result is better than 2e-10.
120 //--- Nve 14-nov-1998 UU-SAP Utrecht
124 cout << "*LnGamma(z)* Wrong argument z = " << z << endl;
128 // Coefficients for the series expansion
130 c[0]= 2.5066282746310005;
131 c[1]= 76.18009172947146;
132 c[2]=-86.50532032941677;
133 c[3]= 24.01409824083091;
134 c[4]= -1.231739572450155;
135 c[5]= 0.1208650973866179e-2;
136 c[6]= -0.5395239384953e-5;
141 tmp=(x+0.5)*log(tmp)-tmp;
142 Double_t ser=1.000000000190015;
143 for (Int_t i=1; i<7; i++)
148 Double_t v=tmp+log(c[0]*ser/x);
151 ///////////////////////////////////////////////////////////////////////////
152 Double_t AliMath::GamSer(Double_t a,Double_t x) const
154 // Computation of the incomplete gamma function P(a,x)
155 // via its series representation.
157 // The algorithm is based on the formulas and code as denoted in
158 // Numerical Recipes 2nd ed. on p. 210-212 (W.H.Press et al.).
160 //--- Nve 14-nov-1998 UU-SAP Utrecht
162 Int_t itmax=100; // Maximum number of iterations
163 Double_t eps=3.e-7; // Relative accuracy
167 cout << "*GamSer(a,x)* Invalid argument a = " << a << endl;
173 if (x<0) cout << "*GamSer(a,x)* Invalid argument x = " << x << endl;
177 Double_t gln=LnGamma(a);
181 for (Int_t n=1; n<=itmax; n++)
186 if (fabs(del)<fabs(sum*eps)) break;
187 if (n==itmax) cout << "*GamSer(a,x)* a too large or itmax too small" << endl;
189 Double_t v=sum*exp(-x+a*log(x)-gln);
192 ///////////////////////////////////////////////////////////////////////////
193 Double_t AliMath::GamCf(Double_t a,Double_t x) const
195 // Computation of the incomplete gamma function P(a,x)
196 // via its continued fraction representation.
198 // The algorithm is based on the formulas and code as denoted in
199 // Numerical Recipes 2nd ed. on p. 210-212 (W.H.Press et al.).
201 //--- Nve 14-nov-1998 UU-SAP Utrecht
203 Int_t itmax=100; // Maximum number of iterations
204 Double_t eps=3.e-7; // Relative accuracy
205 Double_t fpmin=1.e-30; // Smallest Double_t value allowed here
209 cout << "*GamCf(a,x)* Invalid argument a = " << a << endl;
215 if (x<0) cout << "*GamCf(a,x)* Invalid argument x = " << x << endl;
219 Double_t gln=LnGamma(a);
225 for (Int_t i=1; i<=itmax; i++)
227 an=double(-i)*(double(i)-a);
230 if (fabs(d)<fpmin) d=fpmin;
232 if (fabs(c)<fpmin) c=fpmin;
236 if (fabs(del-1.)<eps) break;
237 if (i==itmax) cout << "*GamCf(a,x)* a too large or itmax too small" << endl;
239 Double_t v=exp(-x+a*log(x)-gln)*h;
242 ///////////////////////////////////////////////////////////////////////////
243 Double_t AliMath::Erf(Double_t x) const
245 // Computation of the error function erf(x).
247 //--- NvE 14-nov-1998 UU-SAP Utrecht
251 ///////////////////////////////////////////////////////////////////////////
252 Double_t AliMath::Erfc(Double_t x) const
254 // Computation of the complementary error function erfc(x).
256 // The algorithm is based on a Chebyshev fit as denoted in
257 // Numerical Recipes 2nd ed. on p. 214 (W.H.Press et al.).
259 // The fractional error is always less than 1.2e-7.
261 //--- Nve 14-nov-1998 UU-SAP Utrecht
263 // The parameters of the Chebyshev fit
264 const Double_t ka1=-1.26551223, ka2=1.00002368,
265 ka3= 0.37409196, ka4=0.09678418,
266 ka5=-0.18628806, ka6=0.27886807,
267 ka7=-1.13520398, ka8=1.48851587,
268 ka9=-0.82215223, ka10=0.17087277;
270 Double_t v=1.; // The return value
274 if (z <= 0.) return v; // erfc(0)=1
276 Double_t t=1./(1.+0.5*z);
279 +ka1+t*(ka2+t*(ka3+t*(ka4+t*(ka5+t*(ka6+t*(ka7+t*(ka8+t*(ka9+t*ka10)))))))));
281 if (x < 0.) v=2.-v; // erfc(-x)=2-erfc(x)
285 ///////////////////////////////////////////////////////////////////////////
286 Double_t AliMath::Prob(Double_t chi2,Int_t ndf,Int_t mode) const
288 // Computation of the probability for a certain Chi-squared (chi2)
289 // and number of degrees of freedom (ndf).
291 // According to the value of the parameter "mode" various algorithms
294 // mode = 0 : Calculations are based on the incomplete gamma function P(a,x),
295 // where a=ndf/2 and x=chi2/2.
297 // 1 : Same as for mode=0. However, in case ndf=1 an exact expression
298 // based on the error function Erf() is used.
300 // 2 : Same as for mode=0. However, in case ndf>30 a Gaussian approximation
301 // is used instead of the gamma function.
303 // When invoked as Prob(chi2,ndf) the default mode=1 is used.
305 // P(a,x) represents the probability that the observed Chi-squared
306 // for a correct model should be less than the value chi2.
308 // The returned probability corresponds to 1-P(a,x),
309 // which denotes the probability that an observed Chi-squared exceeds
310 // the value chi2 by chance, even for a correct model.
312 //--- NvE 14-nov-1998 UU-SAP Utrecht
314 if (ndf <= 0) return 0; // Set CL to zero in case ndf<=0
332 case 1: // Exact expression for ndf=1 as alternative for the gamma function
333 if (ndf==1) v=1.-Erf(sqrt(chi2)/sqrt(2.));
336 case 2: // Gaussian approximation for large ndf (i.e. ndf>30) as alternative for the gamma function
339 Double_t q=sqrt(2.*chi2)-sqrt(double(2*ndf-1));
340 if (q>0.) v=0.5*(1.-Erf(q/sqrt(2.)));
347 // Evaluate the incomplete gamma function
348 Double_t a=double(ndf)/2.;
355 ///////////////////////////////////////////////////////////////////////////
356 Double_t AliMath::BesselI0(Double_t x) const
358 // Computation of the modified Bessel function I_0(x) for any real x.
360 // The algorithm is based on the article by Abramowitz and Stegun [1]
361 // as denoted in Numerical Recipes 2nd ed. on p. 230 (W.H.Press et al.).
363 // [1] M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
364 // Applied Mathematics Series vol. 55 (1964), Washington.
366 //--- NvE 12-mar-2000 UU-SAP Utrecht
368 // Parameters of the polynomial approximation
369 const Double_t kp1=1.0, kp2=3.5156229, kp3=3.0899424,
370 kp4=1.2067492, kp5=0.2659732, kp6=3.60768e-2, kp7=4.5813e-3;
372 const Double_t kq1= 0.39894228, kq2= 1.328592e-2, kq3= 2.25319e-3,
373 kq4=-1.57565e-3, kq5= 9.16281e-3, kq6=-2.057706e-2,
374 kq7= 2.635537e-2, kq8=-1.647633e-2, kq9= 3.92377e-3;
378 Double_t y=0,result=0;
383 result=kp1+y*(kp2+y*(kp3+y*(kp4+y*(kp5+y*(kp6+y*kp7)))));
388 result=(exp(ax)/sqrt(ax))
389 *(kq1+y*(kq2+y*(kq3+y*(kq4+y*(kq5+y*(kq6+y*(kq7+y*(kq8+y*kq9))))))));
394 ///////////////////////////////////////////////////////////////////////////
395 Double_t AliMath::BesselK0(Double_t x) const
397 // Computation of the modified Bessel function K_0(x) for positive real x.
399 // The algorithm is based on the article by Abramowitz and Stegun [1]
400 // as denoted in Numerical Recipes 2nd ed. on p. 230 (W.H.Press et al.).
402 // [1] M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
403 // Applied Mathematics Series vol. 55 (1964), Washington.
405 //--- NvE 12-mar-2000 UU-SAP Utrecht
407 // Parameters of the polynomial approximation
408 const Double_t kp1=-0.57721566, kp2=0.42278420, kp3=0.23069756,
409 kp4= 3.488590e-2, kp5=2.62698e-3, kp6=1.0750e-4, kp7=7.4e-6;
411 const Double_t kq1= 1.25331414, kq2=-7.832358e-2, kq3= 2.189568e-2,
412 kq4=-1.062446e-2, kq5= 5.87872e-3, kq6=-2.51540e-3, kq7=5.3208e-4;
416 cout << " *BesselK0* Invalid argument x = " << x << endl;
420 Double_t y=0,result=0;
425 result=(-log(x/2.)*BesselI0(x))
426 +(kp1+y*(kp2+y*(kp3+y*(kp4+y*(kp5+y*(kp6+y*kp7))))));
431 result=(exp(-x)/sqrt(x))
432 *(kq1+y*(kq2+y*(kq3+y*(kq4+y*(kq5+y*(kq6+y*kq7))))));
437 ///////////////////////////////////////////////////////////////////////////
438 Double_t AliMath::BesselI1(Double_t x) const
440 // Computation of the modified Bessel function I_1(x) for any real x.
442 // The algorithm is based on the article by Abramowitz and Stegun [1]
443 // as denoted in Numerical Recipes 2nd ed. on p. 230 (W.H.Press et al.).
445 // [1] M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
446 // Applied Mathematics Series vol. 55 (1964), Washington.
448 //--- NvE 12-mar-2000 UU-SAP Utrecht
450 // Parameters of the polynomial approximation
451 const Double_t kp1=0.5, kp2=0.87890594, kp3=0.51498869,
452 kp4=0.15084934, kp5=2.658733e-2, kp6=3.01532e-3, kp7=3.2411e-4;
454 const Double_t kq1= 0.39894228, kq2=-3.988024e-2, kq3=-3.62018e-3,
455 kq4= 1.63801e-3, kq5=-1.031555e-2, kq6= 2.282967e-2,
456 kq7=-2.895312e-2, kq8= 1.787654e-2, kq9=-4.20059e-3;
460 Double_t y=0,result=0;
465 result=x*(kp1+y*(kp2+y*(kp3+y*(kp4+y*(kp5+y*(kp6+y*kp7))))));
470 result=(exp(ax)/sqrt(ax))
471 *(kq1+y*(kq2+y*(kq3+y*(kq4+y*(kq5+y*(kq6+y*(kq7+y*(kq8+y*kq9))))))));
472 if (x < 0) result=-result;
477 ///////////////////////////////////////////////////////////////////////////
478 Double_t AliMath::BesselK1(Double_t x) const
480 // Computation of the modified Bessel function K_1(x) for positive real x.
482 // The algorithm is based on the article by Abramowitz and Stegun [1]
483 // as denoted in Numerical Recipes 2nd ed. on p. 230 (W.H.Press et al.).
485 // [1] M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
486 // Applied Mathematics Series vol. 55 (1964), Washington.
488 //--- NvE 12-mar-2000 UU-SAP Utrecht
490 // Parameters of the polynomial approximation
491 const Double_t kp1= 1., kp2= 0.15443144, kp3=-0.67278579,
492 kp4=-0.18156897, kp5=-1.919402e-2, kp6=-1.10404e-3, kp7=-4.686e-5;
494 const Double_t kq1= 1.25331414, kq2= 0.23498619, kq3=-3.655620e-2,
495 kq4= 1.504268e-2, kq5=-7.80353e-3, kq6= 3.25614e-3, kq7=-6.8245e-4;
499 cout << " *BesselK1* Invalid argument x = " << x << endl;
503 Double_t y=0,result=0;
508 result=(log(x/2.)*BesselI1(x))
509 +(1./x)*(kp1+y*(kp2+y*(kp3+y*(kp4+y*(kp5+y*(kp6+y*kp7))))));
514 result=(exp(-x)/sqrt(x))
515 *(kq1+y*(kq2+y*(kq3+y*(kq4+y*(kq5+y*(kq6+y*kq7))))));
520 ///////////////////////////////////////////////////////////////////////////
521 Double_t AliMath::BesselK(Int_t n,Double_t x) const
523 // Computation of the Integer Order Modified Bessel function K_n(x)
524 // for n=0,1,2,... and positive real x.
526 // The algorithm uses the recurrence relation
528 // K_n+1(x) = (2n/x)*K_n(x) + K_n-1(x)
530 // as denoted in Numerical Recipes 2nd ed. on p. 232 (W.H.Press et al.).
532 //--- NvE 12-mar-2000 UU-SAP Utrecht
536 cout << " *BesselK* Invalid argument(s) (n,x) = (" << n << " , " << x << ")" << endl;
540 if (n==0) return BesselK0(x);
542 if (n==1) return BesselK1(x);
544 // Perform upward recurrence for all x
546 Double_t bkm=BesselK0(x);
547 Double_t bk=BesselK1(x);
549 for (Int_t j=1; j<n; j++)
551 bkp=bkm+double(j)*tox*bk;
558 ///////////////////////////////////////////////////////////////////////////
559 Double_t AliMath::BesselI(Int_t n,Double_t x) const
561 // Computation of the Integer Order Modified Bessel function I_n(x)
562 // for n=0,1,2,... and any real x.
564 // The algorithm uses the recurrence relation
566 // I_n+1(x) = (-2n/x)*I_n(x) + I_n-1(x)
568 // as denoted in Numerical Recipes 2nd ed. on p. 232 (W.H.Press et al.).
570 //--- NvE 12-mar-2000 UU-SAP Utrecht
572 Int_t iacc=40; // Increase to enhance accuracy
573 Double_t bigno=1.e10, bigni=1.e-10;
577 cout << " *BesselI* Invalid argument (n,x) = (" << n << " , " << x << ")" << endl;
581 if (n==0) return BesselI0(x);
583 if (n==1) return BesselI1(x);
585 if (fabs(x) < 1.e-10) return 0;
587 Double_t tox=2./fabs(x);
588 Double_t bip=0,bim=0;
591 Int_t m=2*((n+int(sqrt(float(iacc*n))))); // Downward recurrence from even m
592 for (Int_t j=m; j<=1; j--)
594 bim=bip+double(j)*tox*bi;
597 if (fabs(bi) > bigno) // Renormalise to prevent overflows
603 if (j==n) result=bip;
606 result*=BesselI0(x)/bi; // Normalise with I0(x)
607 if ((x < 0) && (n%2 == 1)) result=-result;
611 ///////////////////////////////////////////////////////////////////////////