1 | /* @(#)e_jn.c 5.1 93/09/24 */ |
2 | /* |
3 | * ==================================================== |
4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
5 | * |
6 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
7 | * Permission to use, copy, modify, and distribute this |
8 | * software is freely granted, provided that this notice |
9 | * is preserved. |
10 | * ==================================================== |
11 | */ |
12 | |
13 | /* |
14 | * __ieee754_jn(n, x), __ieee754_yn(n, x) |
15 | * floating point Bessel's function of the 1st and 2nd kind |
16 | * of order n |
17 | * |
18 | * Special cases: |
19 | * y0(0)=y1(0)=yn(n,0) = -inf with overflow signal; |
20 | * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal. |
21 | * Note 2. About jn(n,x), yn(n,x) |
22 | * For n=0, j0(x) is called, |
23 | * for n=1, j1(x) is called, |
24 | * for n<x, forward recursion us used starting |
25 | * from values of j0(x) and j1(x). |
26 | * for n>x, a continued fraction approximation to |
27 | * j(n,x)/j(n-1,x) is evaluated and then backward |
28 | * recursion is used starting from a supposed value |
29 | * for j(n,x). The resulting value of j(0,x) is |
30 | * compared with the actual value to correct the |
31 | * supposed value of j(n,x). |
32 | * |
33 | * yn(n,x) is similar in all respects, except |
34 | * that forward recursion is used for all |
35 | * values of n>1. |
36 | * |
37 | */ |
38 | |
39 | #include <errno.h> |
40 | #include <float.h> |
41 | #include <math.h> |
42 | #include <math_private.h> |
43 | |
44 | static const double |
45 | invsqrtpi = 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */ |
46 | two = 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */ |
47 | one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */ |
48 | |
49 | static const double zero = 0.00000000000000000000e+00; |
50 | |
51 | double |
52 | __ieee754_jn (int n, double x) |
53 | { |
54 | int32_t i, hx, ix, lx, sgn; |
55 | double a, b, temp, di, ret; |
56 | double z, w; |
57 | |
58 | /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) |
59 | * Thus, J(-n,x) = J(n,-x) |
60 | */ |
61 | EXTRACT_WORDS (hx, lx, x); |
62 | ix = 0x7fffffff & hx; |
63 | /* if J(n,NaN) is NaN */ |
64 | if (__glibc_unlikely ((ix | ((uint32_t) (lx | -lx)) >> 31) > 0x7ff00000)) |
65 | return x + x; |
66 | if (n < 0) |
67 | { |
68 | n = -n; |
69 | x = -x; |
70 | hx ^= 0x80000000; |
71 | } |
72 | if (n == 0) |
73 | return (__ieee754_j0 (x)); |
74 | if (n == 1) |
75 | return (__ieee754_j1 (x)); |
76 | sgn = (n & 1) & (hx >> 31); /* even n -- 0, odd n -- sign(x) */ |
77 | x = fabs (x); |
78 | { |
79 | SET_RESTORE_ROUND (FE_TONEAREST); |
80 | if (__glibc_unlikely ((ix | lx) == 0 || ix >= 0x7ff00000)) |
81 | /* if x is 0 or inf */ |
82 | return sgn == 1 ? -zero : zero; |
83 | else if ((double) n <= x) |
84 | { |
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 | double s; |
101 | double c; |
102 | __sincos (x, &s, &c); |
103 | switch (n & 3) |
104 | { |
105 | case 0: temp = c + s; break; |
106 | case 1: temp = -c + s; break; |
107 | case 2: temp = -c - s; break; |
108 | case 3: temp = c - s; break; |
109 | } |
110 | b = invsqrtpi * temp / __ieee754_sqrt (x); |
111 | } |
112 | else |
113 | { |
114 | a = __ieee754_j0 (x); |
115 | b = __ieee754_j1 (x); |
116 | for (i = 1; i < n; i++) |
117 | { |
118 | temp = b; |
119 | b = b * ((double) (i + i) / x) - a; /* avoid underflow */ |
120 | a = temp; |
121 | } |
122 | } |
123 | } |
124 | else |
125 | { |
126 | if (ix < 0x3e100000) /* x < 2**-29 */ |
127 | { /* x is tiny, return the first Taylor expansion of J(n,x) |
128 | * J(n,x) = 1/n!*(x/2)^n - ... |
129 | */ |
130 | if (n > 33) /* underflow */ |
131 | b = zero; |
132 | else |
133 | { |
134 | temp = x * 0.5; b = temp; |
135 | for (a = one, i = 2; i <= n; i++) |
136 | { |
137 | a *= (double) i; /* a = n! */ |
138 | b *= temp; /* b = (x/2)^n */ |
139 | } |
140 | b = b / a; |
141 | } |
142 | } |
143 | else |
144 | { |
145 | /* use backward recurrence */ |
146 | /* x x^2 x^2 |
147 | * J(n,x)/J(n-1,x) = ---- ------ ------ ..... |
148 | * 2n - 2(n+1) - 2(n+2) |
149 | * |
150 | * 1 1 1 |
151 | * (for large x) = ---- ------ ------ ..... |
152 | * 2n 2(n+1) 2(n+2) |
153 | * -- - ------ - ------ - |
154 | * x x x |
155 | * |
156 | * Let w = 2n/x and h=2/x, then the above quotient |
157 | * is equal to the continued fraction: |
158 | * 1 |
159 | * = ----------------------- |
160 | * 1 |
161 | * w - ----------------- |
162 | * 1 |
163 | * w+h - --------- |
164 | * w+2h - ... |
165 | * |
166 | * To determine how many terms needed, let |
167 | * Q(0) = w, Q(1) = w(w+h) - 1, |
168 | * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), |
169 | * When Q(k) > 1e4 good for single |
170 | * When Q(k) > 1e9 good for double |
171 | * When Q(k) > 1e17 good for quadruple |
172 | */ |
173 | /* determine k */ |
174 | double t, v; |
175 | double q0, q1, h, tmp; int32_t k, m; |
176 | w = (n + n) / (double) x; h = 2.0 / (double) x; |
177 | q0 = w; z = w + h; q1 = w * z - 1.0; k = 1; |
178 | while (q1 < 1.0e9) |
179 | { |
180 | k += 1; z += h; |
181 | tmp = z * q1 - q0; |
182 | q0 = q1; |
183 | q1 = tmp; |
184 | } |
185 | m = n + n; |
186 | for (t = zero, i = 2 * (n + k); i >= m; i -= 2) |
187 | t = one / (i / x - t); |
188 | a = t; |
189 | b = one; |
190 | /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) |
191 | * Hence, if n*(log(2n/x)) > ... |
192 | * single 8.8722839355e+01 |
193 | * double 7.09782712893383973096e+02 |
194 | * long double 1.1356523406294143949491931077970765006170e+04 |
195 | * then recurrent value may overflow and the result is |
196 | * likely underflow to zero |
197 | */ |
198 | tmp = n; |
199 | v = two / x; |
200 | tmp = tmp * __ieee754_log (fabs (v * tmp)); |
201 | if (tmp < 7.09782712893383973096e+02) |
202 | { |
203 | for (i = n - 1, di = (double) (i + i); i > 0; i--) |
204 | { |
205 | temp = b; |
206 | b *= di; |
207 | b = b / x - a; |
208 | a = temp; |
209 | di -= two; |
210 | } |
211 | } |
212 | else |
213 | { |
214 | for (i = n - 1, di = (double) (i + i); i > 0; i--) |
215 | { |
216 | temp = b; |
217 | b *= di; |
218 | b = b / x - a; |
219 | a = temp; |
220 | di -= two; |
221 | /* scale b to avoid spurious overflow */ |
222 | if (b > 1e100) |
223 | { |
224 | a /= b; |
225 | t /= b; |
226 | b = one; |
227 | } |
228 | } |
229 | } |
230 | /* j0() and j1() suffer enormous loss of precision at and |
231 | * near zero; however, we know that their zero points never |
232 | * coincide, so just choose the one further away from zero. |
233 | */ |
234 | z = __ieee754_j0 (x); |
235 | w = __ieee754_j1 (x); |
236 | if (fabs (z) >= fabs (w)) |
237 | b = (t * z / b); |
238 | else |
239 | b = (t * w / a); |
240 | } |
241 | } |
242 | if (sgn == 1) |
243 | ret = -b; |
244 | else |
245 | ret = b; |
246 | ret = math_narrow_eval (ret); |
247 | } |
248 | if (ret == 0) |
249 | { |
250 | ret = math_narrow_eval (__copysign (DBL_MIN, ret) * DBL_MIN); |
251 | __set_errno (ERANGE); |
252 | } |
253 | else |
254 | math_check_force_underflow (ret); |
255 | return ret; |
256 | } |
257 | strong_alias (__ieee754_jn, __jn_finite) |
258 | |
259 | double |
260 | __ieee754_yn (int n, double x) |
261 | { |
262 | int32_t i, hx, ix, lx; |
263 | int32_t sign; |
264 | double a, b, temp, ret; |
265 | |
266 | EXTRACT_WORDS (hx, lx, x); |
267 | ix = 0x7fffffff & hx; |
268 | /* if Y(n,NaN) is NaN */ |
269 | if (__glibc_unlikely ((ix | ((uint32_t) (lx | -lx)) >> 31) > 0x7ff00000)) |
270 | return x + x; |
271 | sign = 1; |
272 | if (n < 0) |
273 | { |
274 | n = -n; |
275 | sign = 1 - ((n & 1) << 1); |
276 | } |
277 | if (n == 0) |
278 | return (__ieee754_y0 (x)); |
279 | if (__glibc_unlikely ((ix | lx) == 0)) |
280 | return -sign / zero; |
281 | /* -inf and overflow exception. */; |
282 | if (__glibc_unlikely (hx < 0)) |
283 | return zero / (zero * x); |
284 | { |
285 | SET_RESTORE_ROUND (FE_TONEAREST); |
286 | if (n == 1) |
287 | { |
288 | ret = sign * __ieee754_y1 (x); |
289 | goto out; |
290 | } |
291 | if (__glibc_unlikely (ix == 0x7ff00000)) |
292 | return zero; |
293 | if (ix >= 0x52D00000) /* x > 2**302 */ |
294 | { /* (x >> n**2) |
295 | * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) |
296 | * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) |
297 | * Let s=sin(x), c=cos(x), |
298 | * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then |
299 | * |
300 | * n sin(xn)*sqt2 cos(xn)*sqt2 |
301 | * ---------------------------------- |
302 | * 0 s-c c+s |
303 | * 1 -s-c -c+s |
304 | * 2 -s+c -c-s |
305 | * 3 s+c c-s |
306 | */ |
307 | double c; |
308 | double s; |
309 | __sincos (x, &s, &c); |
310 | switch (n & 3) |
311 | { |
312 | case 0: temp = s - c; break; |
313 | case 1: temp = -s - c; break; |
314 | case 2: temp = -s + c; break; |
315 | case 3: temp = s + c; break; |
316 | } |
317 | b = invsqrtpi * temp / __ieee754_sqrt (x); |
318 | } |
319 | else |
320 | { |
321 | uint32_t high; |
322 | a = __ieee754_y0 (x); |
323 | b = __ieee754_y1 (x); |
324 | /* quit if b is -inf */ |
325 | GET_HIGH_WORD (high, b); |
326 | for (i = 1; i < n && high != 0xfff00000; i++) |
327 | { |
328 | temp = b; |
329 | b = ((double) (i + i) / x) * b - a; |
330 | GET_HIGH_WORD (high, b); |
331 | a = temp; |
332 | } |
333 | /* If B is +-Inf, set up errno accordingly. */ |
334 | if (!isfinite (b)) |
335 | __set_errno (ERANGE); |
336 | } |
337 | if (sign > 0) |
338 | ret = b; |
339 | else |
340 | ret = -b; |
341 | } |
342 | out: |
343 | if (isinf (ret)) |
344 | ret = __copysign (DBL_MAX, ret) * DBL_MAX; |
345 | return ret; |
346 | } |
347 | strong_alias (__ieee754_yn, __yn_finite) |
348 | |