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  * jn(n, x), 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 division by zero 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 "fdlibm.h"
40 
41 #ifdef _NEED_FLOAT64
42 
43 static const __float64
44     invsqrtpi = _F_64(5.64189583547756279280e-01), /* 0x3FE20DD7, 0x50429B6D */
45     two = _F_64(2.00000000000000000000e+00), /* 0x40000000, 0x00000000 */
46     one = _F_64(1.00000000000000000000e+00); /* 0x3FF00000, 0x00000000 */
47 
48 static const __float64 zero = _F_64(0.00000000000000000000e+00);
49 
50 __float64
jn64(int n,__float64 x)51 jn64(int n, __float64 x)
52 {
53     __int32_t i, hx, ix, lx, sgn;
54     __float64 a, b, temp, di;
55     __float64 z, w;
56 
57     if (isnan(x))
58         return x + x;
59 
60     if (isinf(x))
61         return _F_64(0.0);
62 
63     /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
64      * Thus, J(-n,x) = J(n,-x)
65      */
66     EXTRACT_WORDS(hx, lx, x);
67     ix = 0x7fffffff & hx;
68 
69     if (n < 0) {
70         n = -n;
71         x = -x;
72         hx ^= 0x80000000;
73     }
74     if (n == 0)
75         return (j064(x));
76     if (n == 1)
77         return (j164(x));
78     sgn = (n & 1) & (hx >> 31); /* even n -- 0, odd n -- sign(x) */
79     x = fabs64(x);
80     if ((ix | lx) == 0 || ix >= 0x7ff00000) /* if x is 0 or inf */
81         b = zero;
82     else if ((__float64)n <= x) {
83         /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
84         if (ix >= 0x52D00000) { /* x > 2**302 */
85             /* (x >> n**2)
86      *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
87      *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
88      *	    Let s=sin(x), c=cos(x),
89      *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
90      *
91      *		   n	sin(xn)*sqt2	cos(xn)*sqt2
92      *		----------------------------------
93      *		   0	 s-c		 c+s
94      *		   1	-s-c 		-c+s
95      *		   2	-s+c		-c-s
96      *		   3	 s+c		 c-s
97      */
98             switch (n & 3) {
99             case 0:
100             default:
101                 temp = cos64(x) + sin64(x);
102                 break;
103             case 1:
104                 temp = -cos64(x) + sin64(x);
105                 break;
106             case 2:
107                 temp = -cos64(x) - sin64(x);
108                 break;
109             case 3:
110                 temp = cos64(x) - sin64(x);
111                 break;
112             }
113             b = invsqrtpi * temp / sqrt64(x);
114         } else {
115             a = j064(x);
116             b = j164(x);
117             for (i = 1; i < n; i++) {
118                 temp = b;
119                 b = b * ((__float64)(i + i) / x) - a; /* avoid underflow */
120                 a = temp;
121             }
122         }
123     } else {
124         if (ix < 0x3e100000) { /* x < 2**-29 */
125             /* x is tiny, return the first Taylor expansion of J(n,x)
126      * J(n,x) = 1/n!*(x/2)^n  - ...
127      */
128             if (n > 33) /* underflow */
129                 b = zero;
130             else {
131                 temp = x * _F_64(0.5);
132                 b = temp;
133                 for (a = one, i = 2; i <= n; i++) {
134                     a *= (__float64)i; /* a = n! */
135                     b *= temp; /* b = (x/2)^n */
136                 }
137                 b = b / a;
138             }
139         } else {
140             /* use backward recurrence */
141             /* 			x      x^2      x^2
142 		 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
143 		 *			2n  - 2(n+1) - 2(n+2)
144 		 *
145 		 * 			1      1        1
146 		 *  (for large x)   =  ----  ------   ------   .....
147 		 *			2n   2(n+1)   2(n+2)
148 		 *			-- - ------ - ------ -
149 		 *			 x     x         x
150 		 *
151 		 * Let w = 2n/x and h=2/x, then the above quotient
152 		 * is equal to the continued fraction:
153 		 *		    1
154 		 *	= -----------------------
155 		 *		       1
156 		 *	   w - -----------------
157 		 *			  1
158 		 * 	        w+h - ---------
159 		 *		       w+2h - ...
160 		 *
161 		 * To determine how many terms needed, let
162 		 * Q(0) = w, Q(1) = w(w+h) - 1,
163 		 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
164 		 * When Q(k) > 1e4	good for single
165 		 * When Q(k) > 1e9	good for double
166 		 * When Q(k) > 1e17	good for quadruple
167 		 */
168             /* determine k */
169             __float64 t, v;
170             __float64 q0, q1, h, tmp;
171             __int32_t k, m;
172             w = (n + n) / (__float64)x;
173             h = _F_64(2.0) / (__float64)x;
174             q0 = w;
175             z = w + h;
176             q1 = w * z - _F_64(1.0);
177             k = 1;
178             while (q1 < _F_64(1.0e9)) {
179                 k += 1;
180                 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 * log(fabs64(v * tmp));
201             if (tmp < _F_64(7.09782712893383973096e+02)) {
202                 for (i = n - 1, di = (__float64)(i + i); i > 0; i--) {
203                     temp = b;
204                     b *= di;
205                     b = b / x - a;
206                     a = temp;
207                     di -= two;
208                 }
209             } else {
210                 for (i = n - 1, di = (__float64)(i + i); i > 0; i--) {
211                     temp = b;
212                     b *= di;
213                     b = b / x - a;
214                     a = temp;
215                     di -= two;
216                     /* scale b to avoid spurious overflow */
217                     if (b > _F_64(1e100)) {
218                         a /= b;
219                         t /= b;
220                         b = one;
221                     }
222                 }
223             }
224             b = (t * j064(x) / b);
225         }
226     }
227     if (sgn == 1)
228         return -b;
229     else
230         return b;
231 }
232 
_MATH_ALIAS_d_id(jn)233 _MATH_ALIAS_d_id(jn)
234 
235 __float64
236 yn64(int n, __float64 x)
237 {
238     __int32_t i, hx, ix, lx;
239     __int32_t sign;
240     __float64 a, b, temp;
241 
242     EXTRACT_WORDS(hx, lx, x);
243     ix = 0x7fffffff & hx;
244     /* if Y(n,NaN) is NaN */
245 
246     if ((ix | lx) == 0)
247         return __math_divzero(1);
248 
249     if (isnan(x))
250         return x + x;
251 
252     if (hx < 0)
253         return __math_invalid(x);
254 
255     if (ix == 0x7ff00000)
256         return _F_64(0.0);
257 
258     sign = 1;
259     if (n < 0) {
260         n = -n;
261         sign = 1 - ((n & 1) << 1);
262     }
263     if (n == 0)
264         return (y064(x));
265     if (n == 1)
266         return (sign * y164(x));
267 
268     if (ix >= 0x52D00000) { /* x > 2**302 */
269         /* (x >> n**2)
270      *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
271      *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
272      *	    Let s=sin(x), c=cos(x),
273      *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
274      *
275      *		   n	sin(xn)*sqt2	cos(xn)*sqt2
276      *		----------------------------------
277      *		   0	 s-c		 c+s
278      *		   1	-s-c 		-c+s
279      *		   2	-s+c		-c-s
280      *		   3	 s+c		 c-s
281      */
282         switch (n & 3) {
283         case 0:
284         default:
285             temp = sin64(x) - cos64(x);
286             break;
287         case 1:
288             temp = -sin64(x) - cos64(x);
289             break;
290         case 2:
291             temp = -sin64(x) + cos64(x);
292             break;
293         case 3:
294             temp = sin64(x) + cos64(x);
295             break;
296         }
297         b = invsqrtpi * temp / sqrt64(x);
298     } else {
299         __uint32_t high;
300         a = y064(x);
301         b = y164(x);
302         /* quit if b is -inf */
303         GET_HIGH_WORD(high, b);
304         for (i = 1; i < n && high != 0xfff00000; i++) {
305             temp = b;
306             b = ((__float64)(i + i) / x) * b - a;
307             GET_HIGH_WORD(high, b);
308             a = temp;
309         }
310     }
311     if (sign > 0)
312         return b;
313     else
314         return -b;
315 }
316 
317 _MATH_ALIAS_d_id(yn)
318 
319 #endif /* _NEED_FLOAT64 */
320