ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cbessel.cpp
Go to the documentation of this file.
1 
9 #include <fvar.hpp>
10 
11 #define ACC 40.0
12 #define BIGNO 1.0e10
13 #define BIGNI 1.0e-10
14 
15 double bessj0(double x){
16  double ax,z;
17  double xx,y,ans,ans1,ans2;
18  if ((ax=fabs(x)) < 8.0) {
19  y=x*x;
20  ans1=57568490574.0+y*(-13362590354.0+y*(651619640.7+y*(-11214424.18+y*(77392.33017+y*(-184.9052456)))));
21  ans2=57568490411.0+y*(1029532985.0+y*(9494680.718+y*(59272.64853+y*(267.8532712+y*1.0))));
22  ans=ans1/ans2;
23  } else {
24  z=8.0/ax;
25  y=z*z;
26  xx=ax-0.785398164;
27  ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4+y*(-0.2073370639e-5+y*0.2093887211e-6)));
28  ans2 = -0.1562499995e-1+y*(0.1430488765e-3+y*(-0.6911147651e-5+y*(0.7621095161e-6-y*0.934945152e-7)));
29  ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
30  }
31  return ans;
32 }
33 
34 double bessy0(double x)
35 {
36  double bessj0(double x);
37  double z;
38  double xx,y,ans,ans1,ans2;
39  if ((x) < 8.0) {
40  y=x*x;
41  ans1 = -2957821389.0+y*(7062834065.0+y*(-512359803.6+y*(10879881.29+y*(-86327.92757+y*228.4622733))));
42  ans2=40076544269.0+y*(745249964.8+y*(7189466.438+y*(47447.26470+y*(226.1030244+y*1.0))));
43  ans=(ans1/ans2)+0.636619772*bessj0(x)*log(x);
44  } else {
45  z=8.0/x;
46  y=z*z;
47  xx=x-0.785398164;
48  ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4+y*(-0.2073370639e-5+y*0.2093887211e-6)));
49  ans2 = -0.1562499995e-1+y*(0.1430488765e-3+y*(-0.6911147651e-5+y*(0.7621095161e-6+y*(-0.934945152e-7))));
50  ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2);
51  }
52  return ans;
53 }
54 
55 double bessj1(double x)
56 {
57  double ax,z;
58  double xx,y,ans,ans1,ans2;
59  if ((ax=fabs(x)) < 8.0) {
60  y=x*x;
61  ans1=x*(72362614232.0+y*(-7895059235.0+y*(242396853.1+y*(-2972611.439+y*(15704.48260+y*(-30.16036606))))));
62  ans2=144725228442.0+y*(2300535178.0+y*(18583304.74+y*(99447.43394+y*(376.9991397+y*1.0))));
63  ans=ans1/ans2;
64  } else {
65  z=8.0/ax;
66  y=z*z;
67  xx=ax-2.356194491;
68  ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4+y*(0.2457520174e-5+y*(-0.240337019e-6))));
69  ans2=0.04687499995+y*(-0.2002690873e-3+y*(0.8449199096e-5+y*(-0.88228987e-6+y*0.105787412e-6)));
70  ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
71  if ((x) < 0.0) ans = -ans;
72  }
73  return ans;
74 }
75 
76 double bessy1(double x)
77 {
78  double bessj1(double x);
79  double z;
80  double xx,y,ans,ans1,ans2;
81  if ((x) < 8.0) {
82  y=x*x;
83  ans1=x*(-0.4900604943e13+y*(0.1275274390e13+y*(-0.5153438139e11+y*(0.7349264551e9+y*(-0.4237922726e7+y*0.8511937935e4)))));
84  ans2=0.2499580570e14+y*(0.4244419664e12+y*(0.3733650367e10+y*(0.2245904002e8+y*(0.1020426050e6+y*(0.3549632885e3+y)))));
85  ans=(ans1/ans2)+0.636619772*(bessj1(x)*log(x)-1.0/x);
86  } else {
87  z=8.0/x;
88  y=z*z;
89  xx=x-2.356194491;
90  ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4+y*(0.2457520174e-5+y*(-0.240337019e-6))));
91  ans2=0.04687499995+y*(-0.2002690873e-3+y*(0.8449199096e-5+y*(-0.88228987e-6+y*0.105787412e-6)));
92  ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2);
93  }
94  return ans;
95 }
96 
97 double bessy(int n, double x)
98 {
99  double bessy0(double x);
100  double bessy1(double x);
101  int j;
102  double by,bym,byp,tox;
103  if (n < 2) cerr << "Index n less than 2 in bessy" << endl;
104  tox=2.0/x;
105  by=bessy1(x);
106  bym=bessy0(x);
107  for (j=1;j<n;j++) {
108  byp=j*tox*by-bym;
109  bym=by;
110  by=byp;
111  }
112  return by;
113 }
114 
115 double bessj(int n, double x)
116 {
117  double bessj0(double x);
118  double bessj1(double x);
119  int j,jsum,m;
120  double ax,bj,bjm,bjp,sum,tox,ans;
121  if (n < 2) cerr << "Index n less than 2 in bessj" << endl;
122  ax=fabs(x);
123  if ((ax) == 0.0)
124  return 0.0;
125  else if ((ax) > (double) n) {
126  tox=2.0/ax;
127  bjm=bessj0(ax);
128  bj=bessj1(ax);
129  for (j=1;j<n;j++) {
130  bjp=j*tox*bj-bjm;
131  bjm=bj;
132  bj=bjp;
133  }
134  ans=bj;
135  } else {
136  tox = 2.0/ax;
137  m=2*((n+(int) sqrt(ACC*n))/2);
138  jsum=0;
139  bjp=0; ans=0; sum=0;
140  bj=1.0;
141  for (j=m;j>0;j--) {
142  bjm=j*tox*bj-bjp;
143  bjp=bj;
144  bj=bjm;
145  if ((fabs(bj)) > BIGNO) {
146  bj *= BIGNI;
147  bjp *= BIGNI;
148  ans *= BIGNI;
149  sum *= BIGNI;
150  }
151  if (jsum) sum += bj;
152  jsum=!jsum;
153  if (j == n) ans=bjp;
154  }
155  sum=2.0*sum-bj;
156  ans /= sum;
157  }
158  return (x) < 0.0 && (n & 1) ? -ans : ans;
159 }
160 
161 double bessi0(double x)
162 {
163  double ax,ans;
164  double y;
165  if ((ax=fabs(x)) < 3.75) {
166  y=x/3.75;
167  y*=y;
168  ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492+y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
169  } else {
170  y=3.75/ax;
171  ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1+y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2+y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1+y*0.392377e-2))))))));
172  }
173  return ans;
174 }
175 
176 double bessk0(double x)
177 {
178  double bessi0(double x);
179  double y,ans;
180  if ((x) <= 2.0) {
181  y=x*x/4.0;
182  ans=(-log(x/2.0)*bessi0(x))+(-0.57721566+y*(0.42278420+y*(0.23069756+y*(0.3488590e-1+y*(0.262698e-2+y*(0.10750e-3+y*0.74e-5))))));
183  } else {
184  y=2.0/x;
185  ans=(exp(-x)/sqrt(x))*(1.25331414+y*(-0.7832358e-1+y*(0.2189568e-1+y*(-0.1062446e-1+y*(0.587872e-2+y*(-0.251540e-2+y*0.53208e-3))))));
186  }
187  return ans;
188 }
189 
190 double bessi1(double x)
191 {
192  double ax,ans;
193  double y;
194  if ((ax=fabs(x)) < 3.75) {
195  y=x/3.75;
196  y*=y;
197  ans=ax*(0.5+y*(0.87890594+y*(0.51498869+y*(0.15084934+y*(0.2658733e-1+y*(0.301532e-2+y*0.32411e-3))))));
198  } else {
199  y=3.75/ax;
200  ans=0.2282967e-1+y*(-0.2895312e-1+y*(0.1787654e-1-y*0.420059e-2));
201  ans=0.39894228+y*(-0.3988024e-1+y*(-0.362018e-2+y*(0.163801e-2+y*(-0.1031555e-1+y*ans))));
202  ans *= (exp(ax)/sqrt(ax));
203  }
204  return (x) < 0.0 ? -ans : ans;
205 }
206 
207 double bessk1(double x)
208 {
209  double bessi1(double x);
210  double y,ans;
211  if ((x) <= 2.0) {
212  y=x*x/4.0;
213  ans=(log(x/2.0)*bessi1(x))+(1.0/x)*(1.0+y*(0.15443144+y*(-0.67278579+y*(-0.18156897+y*(-0.1919402e-1+y*(-0.110404e-2+y*(-0.4686e-4)))))));
214  } else {
215  y=2.0/x;
216  ans=(exp(-x)/sqrt(x))*(1.25331414+y*(0.23498619+y*(-0.3655620e-1+y*(0.1504268e-1+y*(-0.780353e-2+y*(0.325614e-2+y*(-0.68245e-3)))))));
217  }
218  return ans;
219 }
220 
221 double bessk(int n, double x)
222 {
223  double bessk0(double x);
224  double bessk1(double x);
225  int j;
226  double bk,bkm,bkp,tox;
227  if (n < 2) cerr<<"Index n less than 2 in bessk"<<endl;
228  tox=2.0/x;
229  bkm=bessk0(x);
230  bk=bessk1(x);
231  for (j=1;j<n;j++) {
232  bkp=bkm+j*tox*bk;
233  bkm=bk;
234  bk=bkp;
235  }
236  return bk;
237 }
238 
239 double bessi(int n, double x)
240 {
241  double bessi0(double x);
242  int j;
243  double bi,bim,bip,tox,ans;
244  if (n < 2) cerr<<"Index n less than 2 in bessi"<<endl;
245  if ((x) == 0.0)
246  return 0.0;
247  else {
248  tox=2.0/fabs(x);
249  bip=0; ans=0;
250  bi=1.0;
251  for (j=2*(n+(int) sqrt(ACC*n));j>0;j--) {
252  bim=bip+j*tox*bi;
253  bip=bi;
254  bi=bim;
255  if ((fabs(bi)) > BIGNO) {
256  ans *= BIGNI;
257  bi *= BIGNI;
258  bip *= BIGNI;
259  }
260  if (j == n) ans=bip;
261  }
262  ans *= bessi0(x)/bi;
263  return (x) < 0.0 && (n & 1) ? -ans : ans;
264  }
265 }
266 
267 double besselI(double x, int nu){
268  if(nu==0){
269  return bessi0(x);
270  }else{
271  if(nu==1){
272  return bessi1(x);
273  }else{
274  return bessi(nu,x);
275  }
276  }
277 }
278 
279 double besselK(double x, int nu){
280  if(nu==0){
281  return bessk0(x);
282  }else{
283  if(nu==1){
284  return bessk1(x);
285  }else{
286  return bessk(nu,x);
287  }
288  }
289 }
290 
291 
292 double besselJ(double x, int nu){
293  if(nu==0){
294  return bessj0(x);
295  }else{
296  if(nu==1){
297  return bessj1(x);
298  }else{
299  return bessj(nu,x);
300  }
301  }
302 }
303 
304 double besselY(double x, int nu){
305  if(nu==0){
306  return bessy0(x);
307  }else{
308  if(nu==1){
309  return bessy1(x);
310  }else{
311  return bessy(nu,x);
312  }
313  }
314 }
#define ACC
Definition: cbessel.cpp:11
double bessi1(double x)
Definition: cbessel.cpp:190
double bessy1(double x)
Definition: cbessel.cpp:76
double besselY(double x, int nu)
Definition: cbessel.cpp:304
#define x
double besselJ(double x, int nu)
Definition: cbessel.cpp:292
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
double bessk(int n, double x)
Definition: cbessel.cpp:221
#define BIGNI
Definition: cbessel.cpp:13
double besselI(double x, int nu)
Definition: cbessel.cpp:267
double bessi0(double x)
Definition: cbessel.cpp:161
double bessk0(double x)
Definition: cbessel.cpp:176
prnstream & endl(prnstream &)
double besselK(double x, int nu)
Definition: cbessel.cpp:279
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
double bessj1(double x)
Definition: cbessel.cpp:55
double bessy(int n, double x)
Definition: cbessel.cpp:97
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
double bessj0(double x)
Definition: cbessel.cpp:15
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
double bessi(int n, double x)
Definition: cbessel.cpp:239
double bessj(int n, double x)
Definition: cbessel.cpp:115
d3_array cos(const d3_array &arr3)
Returns d3_array results with computed cos from elements in arr3.
Definition: d3arr2a.cpp:58
#define BIGNO
Definition: cbessel.cpp:12
double bessk1(double x)
Definition: cbessel.cpp:207
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
double bessy0(double x)
Definition: cbessel.cpp:34