EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bessel.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file bessel.cpp
1 
2 //
3 // Copyright 2010
4 //
5 // This file is part of starlight.
6 //
7 // starlight is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // starlight is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with starlight. If not, see <http://www.gnu.org/licenses/>.
19 //
21 //
22 // File and Version Information:
23 // $Rev:: 28 $: revision of last commit
24 // $Author:: bgrube $: author of last commit
25 // $Date:: 2010-12-10 18:30:01 +0000 #$: date of last commit
26 //
27 // Description:
28 // Bessel functions taken from ROOT
29 //
30 //
32 
33 
34 #include <iostream>
35 #include <fstream>
36 #include <cmath>
37 
38 #include "bessel.h"
39 
40 
41 using namespace std;
42 
43 
44 //______________________________________________________________________________
45 double bessel::besI0(double x)
46 {
47  //FROM ROOT...
48  // Parameters of the polynomial approximation
49  const double p1=1.0, p2=3.5156229, p3=3.0899424,
50  p4=1.2067492, p5=0.2659732, p6=3.60768e-2, p7=4.5813e-3;
51 
52  const double q1= 0.39894228, q2= 1.328592e-2, q3= 2.25319e-3,
53  q4=-1.57565e-3, q5= 9.16281e-3, q6=-2.057706e-2,
54  q7= 2.635537e-2, q8=-1.647633e-2, q9= 3.92377e-3;
55 
56  const double k1 = 3.75;
57  double ax = fabs(x);
58 
59  double y=0., result=0.;
60 
61  if (ax < k1) {
62  double xx = x/k1;
63  y = xx*xx;
64  result = p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7)))));
65  } else {
66  y = k1/ax;
67  result = (exp(ax)/sqrt(ax))*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*(q7+y*(q8+y*q9))))))));
68  }
69  return result;
70 }
71 
72 
73 //______________________________________________________________________________
74 double bessel::dbesk0(double x)
75 {
76  // Compute the modified Bessel function K_0(x) for positive real x. //should be k0?
77  //
78  // M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
79  // Applied Mathematics Series vol. 55 (1964), Washington.
80  //
81  //--- NvE 12-mar-2000 UU-SAP Utrecht
82 
83  // Parameters of the polynomial approximation
84  const double p1= -0.57721566, p2= 0.42278420, p3=0.23069756,
85  p4=3.488590e-2, p5=2.62698e-3, p6=1.0750e-4, p7=7.4e-6;
86 
87  const double q1= 1.25331414, q2= -7.832358e-2, q3=2.189568e-2,
88  q4= -1.062446e-2, q5=5.87872e-3, q6= -2.51540e-3, q7= 5.3208e-4;
89 
90  if (x <= 0) {
91  cout << "BesselK0 *K0* Invalid argument x = " << x << endl; //Should be k0?
92  return 0;
93  }
94 
95  double y=0.,result=0.;
96 
97  if (x <= 2) {
98  y = x*x/4.;
99  result = (-log(x/2.)*bessel::besI0(x))+(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))));
100  } else {
101  y = 2./x;
102  result = (exp(-x)/sqrt(x))*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*q7))))));
103  }
104  return result;
105 }
106 
107 
108 //______________________________________________________________________________
109 double bessel::besI1(double x)
110 {
111  // Compute the modified Bessel function I_1(x) for any real x.
112  //
113  // M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
114  // Applied Mathematics Series vol. 55 (1964), Washington.
115  //
116  //--- NvE 12-mar-2000 UU-SAP Utrecht
117 
118  // Parameters of the polynomial approximation
119  const double p1=0.5, p2=0.87890594, p3=0.51498869,
120  p4=0.15084934, p5=2.658733e-2, p6=3.01532e-3, p7=3.2411e-4;
121 
122  const double q1= 0.39894228, q2=-3.988024e-2, q3=-3.62018e-3,
123  q4= 1.63801e-3, q5=-1.031555e-2, q6= 2.282967e-2,
124  q7=-2.895312e-2, q8= 1.787654e-2, q9=-4.20059e-3;
125 
126  const double k1 = 3.75;
127  double ax = fabs(x);
128 
129  double y=0., result=0.;
130 
131  if (ax < k1) {
132  double xx = x/k1;
133  y = xx*xx;
134  result = x*(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))));
135  } else {
136  y = k1/ax;
137  result = (exp(ax)/sqrt(ax))*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*(q7+y*(q8+y*q9))))))));
138  if (x < 0) result = -result;
139  }
140  return result;
141 }
142 
143 
144 //______________________________________________________________________________
145 double bessel::dbesk1(double x)
146 {
147  // Compute the modified Bessel function K_1(x) for positive real x.
148  //
149  // M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
150  // Applied Mathematics Series vol. 55 (1964), Washington.
151  //
152  //--- NvE 12-mar-2000 UU-SAP Utrecht
153 
154  // Parameters of the polynomial approximation
155  const double p1= 1., p2= 0.15443144, p3=-0.67278579,
156  p4=-0.18156897, p5=-1.919402e-2, p6=-1.10404e-3, p7=-4.686e-5;
157 
158  const double q1= 1.25331414, q2= 0.23498619, q3=-3.655620e-2,
159  q4= 1.504268e-2, q5=-7.80353e-3, q6= 3.25614e-3, q7=-6.8245e-4;
160 
161  if (x <= 0) {
162  cout << "bessel:dbesk1 *K1* Invalid argument x = " << x << endl;
163  return 0;
164  }
165 
166  double y=0.,result=0.;
167 
168  if (x <= 2) {
169  y = x*x/4.;
170  result = (log(x/2.)*bessel::besI1(x))+(1./x)*(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))));
171  } else {
172  y = 2./x;
173  result = (exp(-x)/sqrt(x))*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*q7))))));
174  }
175  return result;
176 }