1 | |
---|
2 | /* @(#)e_jn.c 5.1 93/09/24 */ |
---|
3 | /* |
---|
4 | * ==================================================== |
---|
5 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
---|
6 | * |
---|
7 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
---|
8 | * Permission to use, copy, modify, and distribute this |
---|
9 | * software is freely granted, provided that this notice |
---|
10 | * is preserved. |
---|
11 | * ==================================================== |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * __ieee754_jn(n, x), __ieee754_yn(n, x) |
---|
16 | * floating point Bessel's function of the 1st and 2nd kind |
---|
17 | * of order n |
---|
18 | * |
---|
19 | * Special cases: |
---|
20 | * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal; |
---|
21 | * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal. |
---|
22 | * Note 2. About jn(n,x), yn(n,x) |
---|
23 | * For n=0, j0(x) is called, |
---|
24 | * for n=1, j1(x) is called, |
---|
25 | * for n<x, forward recursion us used starting |
---|
26 | * from values of j0(x) and j1(x). |
---|
27 | * for n>x, a continued fraction approximation to |
---|
28 | * j(n,x)/j(n-1,x) is evaluated and then backward |
---|
29 | * recursion is used starting from a supposed value |
---|
30 | * for j(n,x). The resulting value of j(0,x) is |
---|
31 | * compared with the actual value to correct the |
---|
32 | * supposed value of j(n,x). |
---|
33 | * |
---|
34 | * yn(n,x) is similar in all respects, except |
---|
35 | * that forward recursion is used for all |
---|
36 | * values of n>1. |
---|
37 | * |
---|
38 | */ |
---|
39 | |
---|
40 | #include <libm/fdlibm.h> |
---|
41 | |
---|
42 | #ifdef __STDC__ |
---|
43 | static const double |
---|
44 | #else |
---|
45 | static double |
---|
46 | #endif |
---|
47 | invsqrtpi= 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */ |
---|
48 | two = 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */ |
---|
49 | one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */ |
---|
50 | |
---|
51 | static double zero = 0.00000000000000000000e+00; |
---|
52 | |
---|
53 | #ifdef __STDC__ |
---|
54 | double __ieee754_jn(int n, double x) |
---|
55 | #else |
---|
56 | double __ieee754_jn(n,x) |
---|
57 | int n; double x; |
---|
58 | #endif |
---|
59 | { |
---|
60 | int i,n0,hx,ix,lx, sgn; |
---|
61 | double a, b, temp=0, di; |
---|
62 | double z, w; |
---|
63 | |
---|
64 | /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) |
---|
65 | * Thus, J(-n,x) = J(n,-x) |
---|
66 | */ |
---|
67 | n0 = 1^((*(int*)&one)>>29); |
---|
68 | hx = *(n0+(int*)&x); |
---|
69 | ix = 0x7fffffff&hx; |
---|
70 | lx = *(1-n0+(int*)&x); |
---|
71 | /* if J(n,NaN) is NaN */ |
---|
72 | if((ix|((unsigned)(lx|-lx))>>31)>0x7ff00000) return x+x; |
---|
73 | if(n<0){ |
---|
74 | n = -n; |
---|
75 | x = -x; |
---|
76 | hx ^= 0x80000000; |
---|
77 | } |
---|
78 | if(n==0) return(__ieee754_j0(x)); |
---|
79 | if(n==1) return(__ieee754_j1(x)); |
---|
80 | sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */ |
---|
81 | x = fabs(x); |
---|
82 | if((ix|lx)==0||ix>=0x7ff00000) /* if x is 0 or inf */ |
---|
83 | b = zero; |
---|
84 | else if((double)n<=x) { |
---|
85 | /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ |
---|
86 | if(ix>=0x52D00000) { /* x > 2**302 */ |
---|
87 | /* (x >> n**2) |
---|
88 | * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) |
---|
89 | * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) |
---|
90 | * Let s=sin(x), c=cos(x), |
---|
91 | * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then |
---|
92 | * |
---|
93 | * n sin(xn)*sqt2 cos(xn)*sqt2 |
---|
94 | * ---------------------------------- |
---|
95 | * 0 s-c c+s |
---|
96 | * 1 -s-c -c+s |
---|
97 | * 2 -s+c -c-s |
---|
98 | * 3 s+c c-s |
---|
99 | */ |
---|
100 | switch(n&3) { |
---|
101 | case 0: temp = cos(x)+sin(x); break; |
---|
102 | case 1: temp = -cos(x)+sin(x); break; |
---|
103 | case 2: temp = -cos(x)-sin(x); break; |
---|
104 | case 3: temp = cos(x)-sin(x); break; |
---|
105 | } |
---|
106 | b = invsqrtpi*temp/sqrt(x); |
---|
107 | } else { |
---|
108 | a = __ieee754_j0(x); |
---|
109 | b = __ieee754_j1(x); |
---|
110 | for(i=1;i<n;i++){ |
---|
111 | temp = b; |
---|
112 | b = b*((double)(i+i)/x) - a; /* avoid underflow */ |
---|
113 | a = temp; |
---|
114 | } |
---|
115 | } |
---|
116 | } else { |
---|
117 | if(ix<0x3e100000) { /* x < 2**-29 */ |
---|
118 | /* x is tiny, return the first Taylor expansion of J(n,x) |
---|
119 | * J(n,x) = 1/n!*(x/2)^n - ... |
---|
120 | */ |
---|
121 | if(n>33) /* underflow */ |
---|
122 | b = zero; |
---|
123 | else { |
---|
124 | temp = x*0.5; b = temp; |
---|
125 | for (a=one,i=2;i<=n;i++) { |
---|
126 | a *= (double)i; /* a = n! */ |
---|
127 | b *= temp; /* b = (x/2)^n */ |
---|
128 | } |
---|
129 | b = b/a; |
---|
130 | } |
---|
131 | } else { |
---|
132 | /* use backward recurrence */ |
---|
133 | /* x x^2 x^2 |
---|
134 | * J(n,x)/J(n-1,x) = ---- ------ ------ ..... |
---|
135 | * 2n - 2(n+1) - 2(n+2) |
---|
136 | * |
---|
137 | * 1 1 1 |
---|
138 | * (for large x) = ---- ------ ------ ..... |
---|
139 | * 2n 2(n+1) 2(n+2) |
---|
140 | * -- - ------ - ------ - |
---|
141 | * x x x |
---|
142 | * |
---|
143 | * Let w = 2n/x and h=2/x, then the above quotient |
---|
144 | * is equal to the continued fraction: |
---|
145 | * 1 |
---|
146 | * = ----------------------- |
---|
147 | * 1 |
---|
148 | * w - ----------------- |
---|
149 | * 1 |
---|
150 | * w+h - --------- |
---|
151 | * w+2h - ... |
---|
152 | * |
---|
153 | * To determine how many terms needed, let |
---|
154 | * Q(0) = w, Q(1) = w(w+h) - 1, |
---|
155 | * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), |
---|
156 | * When Q(k) > 1e4 good for single |
---|
157 | * When Q(k) > 1e9 good for double |
---|
158 | * When Q(k) > 1e17 good for quadruple |
---|
159 | */ |
---|
160 | /* determine k */ |
---|
161 | double t,v; |
---|
162 | double q0,q1,h,tmp; int k,m; |
---|
163 | w = (n+n)/(double)x; h = 2.0/(double)x; |
---|
164 | q0 = w; z = w+h; q1 = w*z - 1.0; k=1; |
---|
165 | while(q1<1.0e9) { |
---|
166 | k += 1; z += h; |
---|
167 | tmp = z*q1 - q0; |
---|
168 | q0 = q1; |
---|
169 | q1 = tmp; |
---|
170 | } |
---|
171 | m = n+n; |
---|
172 | for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t); |
---|
173 | a = t; |
---|
174 | b = one; |
---|
175 | /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) |
---|
176 | * Hence, if n*(log(2n/x)) > ... |
---|
177 | * single 8.8722839355e+01 |
---|
178 | * double 7.09782712893383973096e+02 |
---|
179 | * long double 1.1356523406294143949491931077970765006170e+04 |
---|
180 | * then recurrent value may overflow and the result is |
---|
181 | * likely underflow to zero |
---|
182 | */ |
---|
183 | tmp = n; |
---|
184 | v = two/x; |
---|
185 | tmp = tmp*__ieee754_log(fabs(v*tmp)); |
---|
186 | if(tmp<7.09782712893383973096e+02) { |
---|
187 | for(i=n-1,di=(double)(i+i);i>0;i--){ |
---|
188 | temp = b; |
---|
189 | b *= di; |
---|
190 | b = b/x - a; |
---|
191 | a = temp; |
---|
192 | di -= two; |
---|
193 | } |
---|
194 | } else { |
---|
195 | for(i=n-1,di=(double)(i+i);i>0;i--){ |
---|
196 | temp = b; |
---|
197 | b *= di; |
---|
198 | b = b/x - a; |
---|
199 | a = temp; |
---|
200 | di -= two; |
---|
201 | /* scale b to avoid spurious overflow */ |
---|
202 | if(b>1e100) { |
---|
203 | a /= b; |
---|
204 | t /= b; |
---|
205 | b = one; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | b = (t*__ieee754_j0(x)/b); |
---|
210 | } |
---|
211 | } |
---|
212 | if(sgn==1) return -b; else return b; |
---|
213 | } |
---|
214 | |
---|
215 | #ifdef __STDC__ |
---|
216 | double __ieee754_yn(int n, double x) |
---|
217 | #else |
---|
218 | double __ieee754_yn(n,x) |
---|
219 | int n; double x; |
---|
220 | #endif |
---|
221 | { |
---|
222 | int i,n0,hx,ix,lx; |
---|
223 | int sign; |
---|
224 | double a, b, temp=0; |
---|
225 | |
---|
226 | n0 = 1^((*(int*)&one)>>29); |
---|
227 | hx = *(n0+(int*)&x); |
---|
228 | ix = 0x7fffffff&hx; |
---|
229 | lx = *(1-n0+(int*)&x); |
---|
230 | /* if Y(n,NaN) is NaN */ |
---|
231 | if((ix|((unsigned)(lx|-lx))>>31)>0x7ff00000) return x+x; |
---|
232 | if((ix|lx)==0) return -one/zero; |
---|
233 | if(hx<0) return zero/zero; |
---|
234 | sign = 1; |
---|
235 | if(n<0){ |
---|
236 | n = -n; |
---|
237 | sign = 1 - ((n&1)<<2); |
---|
238 | } |
---|
239 | if(n==0) return(__ieee754_y0(x)); |
---|
240 | if(n==1) return(sign*__ieee754_y1(x)); |
---|
241 | if(ix==0x7ff00000) return zero; |
---|
242 | if(ix>=0x52D00000) { /* x > 2**302 */ |
---|
243 | /* (x >> n**2) |
---|
244 | * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) |
---|
245 | * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) |
---|
246 | * Let s=sin(x), c=cos(x), |
---|
247 | * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then |
---|
248 | * |
---|
249 | * n sin(xn)*sqt2 cos(xn)*sqt2 |
---|
250 | * ---------------------------------- |
---|
251 | * 0 s-c c+s |
---|
252 | * 1 -s-c -c+s |
---|
253 | * 2 -s+c -c-s |
---|
254 | * 3 s+c c-s |
---|
255 | */ |
---|
256 | switch(n&3) { |
---|
257 | case 0: temp = sin(x)-cos(x); break; |
---|
258 | case 1: temp = -sin(x)-cos(x); break; |
---|
259 | case 2: temp = -sin(x)+cos(x); break; |
---|
260 | case 3: temp = sin(x)+cos(x); break; |
---|
261 | } |
---|
262 | b = invsqrtpi*temp/sqrt(x); |
---|
263 | } else { |
---|
264 | a = __ieee754_y0(x); |
---|
265 | b = __ieee754_y1(x); |
---|
266 | /* quit if b is -inf */ |
---|
267 | for(i=1;i<n && ((unsigned int)(*(n0+(int*)&b))!= 0xfff00000);i++){ |
---|
268 | temp = b; |
---|
269 | b = ((double)(i+i)/x)*b - a; |
---|
270 | a = temp; |
---|
271 | } |
---|
272 | } |
---|
273 | if(sign>0) return b; else return -b; |
---|
274 | } |
---|