ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vbessel.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: Anders Nielsen
5  * Copyright (c) 2008-2012 Regents of the University of California
6  *
7  * Inspired from NR.
8  */
13 #include <fvar.hpp>
14 
15 #define ACC 40.0
16 #define BIGNO 1.0e10
17 #define BIGNI 1.0e-10
18 
20  dvariable ax,z;
21  dvariable xx,y,ans,ans1,ans2;
22  if (value(ax=fabs(x)) < 8.0) {
23  y=x*x;
24  ans1=57568490574.0+y*(-13362590354.0+y*(651619640.7+y*(-11214424.18+y*(77392.33017+y*(-184.9052456)))));
25  ans2=57568490411.0+y*(1029532985.0+y*(9494680.718+y*(59272.64853+y*(267.8532712+y*1.0))));
26  ans=ans1/ans2;
27  } else {
28  z=8.0/ax;
29  y=z*z;
30  xx=ax-0.785398164;
31  ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4+y*(-0.2073370639e-5+y*0.2093887211e-6)));
32  ans2 = -0.1562499995e-1+y*(0.1430488765e-3+y*(-0.6911147651e-5+y*(0.7621095161e-6-y*0.934945152e-7)));
33  ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
34  }
35  return ans;
36 }
37 
39 {
41  dvariable z;
42  dvariable xx,y,ans,ans1,ans2;
43  if (value(x) < 8.0) {
44  y=x*x;
45  ans1 = -2957821389.0+y*(7062834065.0+y*(-512359803.6+y*(10879881.29+y*(-86327.92757+y*228.4622733))));
46  ans2=40076544269.0+y*(745249964.8+y*(7189466.438+y*(47447.26470+y*(226.1030244+y*1.0))));
47  ans=(ans1/ans2)+0.636619772*bessj0(x)*log(x);
48  } else {
49  z=8.0/x;
50  y=z*z;
51  xx=x-0.785398164;
52  ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4+y*(-0.2073370639e-5+y*0.2093887211e-6)));
53  ans2 = -0.1562499995e-1+y*(0.1430488765e-3+y*(-0.6911147651e-5+y*(0.7621095161e-6+y*(-0.934945152e-7))));
54  ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2);
55  }
56  return ans;
57 }
58 
60 {
61  dvariable ax,z;
62  dvariable xx,y,ans,ans1,ans2;
63  if (value(ax=fabs(x)) < 8.0) {
64  y=x*x;
65  ans1=x*(72362614232.0+y*(-7895059235.0+y*(242396853.1+y*(-2972611.439+y*(15704.48260+y*(-30.16036606))))));
66  ans2=144725228442.0+y*(2300535178.0+y*(18583304.74+y*(99447.43394+y*(376.9991397+y*1.0))));
67  ans=ans1/ans2;
68  } else {
69  z=8.0/ax;
70  y=z*z;
71  xx=ax-2.356194491;
72  ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4+y*(0.2457520174e-5+y*(-0.240337019e-6))));
73  ans2=0.04687499995+y*(-0.2002690873e-3+y*(0.8449199096e-5+y*(-0.88228987e-6+y*0.105787412e-6)));
74  ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
75  if (value(x) < 0.0) ans = -ans;
76  }
77  return ans;
78 }
79 
81 {
83  dvariable z;
84  dvariable xx,y,ans,ans1,ans2;
85  if (value(x) < 8.0) {
86  y=x*x;
87  ans1=x*(-0.4900604943e13+y*(0.1275274390e13+y*(-0.5153438139e11+y*(0.7349264551e9+y*(-0.4237922726e7+y*0.8511937935e4)))));
88  ans2=0.2499580570e14+y*(0.4244419664e12+y*(0.3733650367e10+y*(0.2245904002e8+y*(0.1020426050e6+y*(0.3549632885e3+y)))));
89  ans=(ans1/ans2)+0.636619772*(bessj1(x)*log(x)-1.0/x);
90  } else {
91  z=8.0/x;
92  y=z*z;
93  xx=x-2.356194491;
94  ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4+y*(0.2457520174e-5+y*(-0.240337019e-6))));
95  ans2=0.04687499995+y*(-0.2002690873e-3+y*(0.8449199096e-5+y*(-0.88228987e-6+y*0.105787412e-6)));
96  ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2);
97  }
98  return ans;
99 }
100 
102 {
105  int j;
106  dvariable by,bym,byp,tox;
107  if (n < 2) cerr << "Index n less than 2 in bessy" << endl;
108  tox=2.0/x;
109  by=bessy1(x);
110  bym=bessy0(x);
111  for (j=1;j<n;j++) {
112  byp=j*tox*by-bym;
113  bym=by;
114  by=byp;
115  }
116  return by;
117 }
118 
120 {
123  int j,jsum,m;
124  dvariable ax,bj,bjm,bjp,sum,tox,ans;
125  if (n < 2) cerr << "Index n less than 2 in bessj" << endl;
126  ax=sfabs(x);
127  if (value(ax) == 0.0)
128  return 0.0;
129  else if (value(ax) > (double) n) {
130  tox=2.0/ax;
131  bjm=bessj0(ax);
132  bj=bessj1(ax);
133  for (j=1;j<n;j++) {
134  bjp=j*tox*bj-bjm;
135  bjm=bj;
136  bj=bjp;
137  }
138  ans=bj;
139  } else {
140  tox = 2.0/ax;
141  m=2*((n+(int) sqrt(ACC*n))/2);
142  jsum=0;
143  bjp=0; ans=0; sum=0;
144  bj=1.0;
145  for (j=m;j>0;j--) {
146  bjm=j*tox*bj-bjp;
147  bjp=bj;
148  bj=bjm;
149  if (value(fabs(bj)) > BIGNO) {
150  bj *= BIGNI;
151  bjp *= BIGNI;
152  ans *= BIGNI;
153  sum *= BIGNI;
154  }
155  if (jsum) sum += bj;
156  jsum=!jsum;
157  if (j == n) ans=bjp;
158  }
159  sum=2.0*sum-bj;
160  ans /= sum;
161  }
162  return value(x) < 0.0 && (n & 1) ? -ans : ans;
163 }
164 
166 {
167  dvariable ax,ans;
168  dvariable y;
169  if (value(ax=fabs(x)) < 3.75) {
170  y=x/3.75;
171  y*=y;
172  ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492+y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
173  } else {
174  y=3.75/ax;
175  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))))))));
176  }
177  return ans;
178 }
179 
181 {
183  dvariable y,ans;
184  if (value(x) <= 2.0) {
185  y=x*x/4.0;
186  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))))));
187  } else {
188  y=2.0/x;
189  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))))));
190  }
191  return ans;
192 }
193 
195 {
196  dvariable ax,ans;
197  dvariable y;
198  if (value(ax=fabs(x)) < 3.75) {
199  y=x/3.75;
200  y*=y;
201  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))))));
202  } else {
203  y=3.75/ax;
204  ans=0.2282967e-1+y*(-0.2895312e-1+y*(0.1787654e-1-y*0.420059e-2));
205  ans=0.39894228+y*(-0.3988024e-1+y*(-0.362018e-2+y*(0.163801e-2+y*(-0.1031555e-1+y*ans))));
206  ans *= (exp(ax)/sqrt(ax));
207  }
208  return value(x) < 0.0 ? -ans : ans;
209 }
210 
212 {
214  dvariable y,ans;
215  if (value(x) <= 2.0) {
216  y=x*x/4.0;
217  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)))))));
218  } else {
219  y=2.0/x;
220  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)))))));
221  }
222  return ans;
223 }
224 
226 {
229  int j;
230  dvariable bk,bkm,bkp,tox;
231  if (n < 2) cerr<<"Index n less than 2 in bessk"<<endl;
232  tox=2.0/x;
233  bkm=bessk0(x);
234  bk=bessk1(x);
235  for (j=1;j<n;j++) {
236  bkp=bkm+j*tox*bk;
237  bkm=bk;
238  bk=bkp;
239  }
240  return bk;
241 }
242 
244 {
246  int j;
247  dvariable bi,bim,bip,tox,ans;
248  if (n < 2) cerr<<"Index n less than 2 in bessi"<<endl;
249  if (value(x) == 0.0)
250  return 0.0;
251  else {
252  tox=2.0/fabs(x);
253  bip=0; ans=0;
254  bi=1.0;
255  for (j=2*(n+(int) sqrt(ACC*n));j>0;j--) {
256  bim=bip+j*tox*bi;
257  bip=bi;
258  bi=bim;
259  if (value(fabs(bi)) > BIGNO) {
260  ans *= BIGNI;
261  bi *= BIGNI;
262  bip *= BIGNI;
263  }
264  if (j == n) ans=bip;
265  }
266  ans *= bessi0(x)/bi;
267  return value(x) < 0.0 && (n & 1) ? -ans : ans;
268  }
269 }
270 
272  if(nu==0){
273  return bessi0(x);
274  }else{
275  if(nu==1){
276  return bessi1(x);
277  }else{
278  return bessi(nu,x);
279  }
280  }
281 }
282 
284  if(nu==0){
285  return bessk0(x);
286  }else{
287  if(nu==1){
288  return bessk1(x);
289  }else{
290  return bessk(nu,x);
291  }
292  }
293 }
294 
295 
297  if(nu==0){
298  return bessj0(x);
299  }else{
300  if(nu==1){
301  return bessj1(x);
302  }else{
303  return bessj(nu,x);
304  }
305  }
306 }
307 
309  if(nu==0){
310  return bessy0(x);
311  }else{
312  if(nu==1){
313  return bessy1(x);
314  }else{
315  return bessy(nu,x);
316  }
317  }
318 }
#define BIGNI
Definition: vbessel.cpp:17
double bessi1(double x)
Definition: cbessel.cpp:190
double bessy1(double x)
Definition: cbessel.cpp:76
#define ACC
Definition: vbessel.cpp:15
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
double besselI(double x, int nu)
Definition: cbessel.cpp:267
double bessi0(double x)
Definition: cbessel.cpp:161
double sfabs(const double v1)
Description not yet available.
Definition: dvect14.cpp:20
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
#define BIGNO
Definition: vbessel.cpp:16
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
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
double bessk1(double x)
Definition: cbessel.cpp:207
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
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