1 /* origin: FreeBSD /usr/src/lib/msun/src/e_jn.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
14 * floating point Bessel's function of the 1st and 2nd kind
18 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
19 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
20 * Note 2. About jn(n,x), yn(n,x)
21 * For n=0, j0(x) is called,
22 * for n=1, j1(x) is called,
23 * for n<=x, forward recursion is used starting
24 * from values of j0(x) and j1(x).
25 * for n>x, a continued fraction approximation to
26 * j(n,x)/j(n-1,x) is evaluated and then backward
27 * recursion is used starting from a supposed value
28 * for j(n,x). The resulting value of j(0,x) is
29 * compared with the actual value to correct the
30 * supposed value of j(n,x).
32 * yn(n,x) is similar in all respects, except
33 * that forward recursion is used for all
39 static const double invsqrtpi = 5.64189583547756279280e-01; /* 0x3FE20DD7, 0x50429B6D */
41 double jn(int n, double x)
47 EXTRACT_WORDS(ix, lx, x);
51 if ((ix | (lx|-lx)>>31) > 0x7ff00000) /* nan */
54 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
55 * Thus, J(-n,x) = J(n,-x)
57 /* nm1 = |n|-1 is used instead of |n| to handle n==INT_MIN */
69 sign &= n; /* even n: 0, odd n: signbit(x) */
71 if ((ix|lx) == 0 || ix == 0x7ff00000) /* if x is 0 or inf */
74 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
75 if (ix >= 0x52d00000) { /* x > 2**302 */
77 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
78 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
79 * Let s=sin(x), c=cos(x),
80 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
82 * n sin(xn)*sqt2 cos(xn)*sqt2
83 * ----------------------------------
90 case 0: temp = -cos(x)+sin(x); break;
91 case 1: temp = -cos(x)-sin(x); break;
92 case 2: temp = cos(x)-sin(x); break;
94 case 3: temp = cos(x)+sin(x); break;
96 b = invsqrtpi*temp/sqrt(x);
103 b = b*(2.0*i/x) - a; /* avoid underflow */
108 if (ix < 0x3e100000) { /* x < 2**-29 */
109 /* x is tiny, return the first Taylor expansion of J(n,x)
110 * J(n,x) = 1/n!*(x/2)^n - ...
112 if (nm1 > 32) /* underflow */
118 for (i=2; i<=nm1+1; i++) {
119 a *= (double)i; /* a = n! */
120 b *= temp; /* b = (x/2)^n */
125 /* use backward recurrence */
127 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
128 * 2n - 2(n+1) - 2(n+2)
131 * (for large x) = ---- ------ ------ .....
133 * -- - ------ - ------ -
136 * Let w = 2n/x and h=2/x, then the above quotient
137 * is equal to the continued fraction:
139 * = -----------------------
141 * w - -----------------
146 * To determine how many terms needed, let
147 * Q(0) = w, Q(1) = w(w+h) - 1,
148 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
149 * When Q(k) > 1e4 good for single
150 * When Q(k) > 1e9 good for double
151 * When Q(k) > 1e17 good for quadruple
154 double t,q0,q1,w,h,z,tmp,nf;
171 for (t=0.0, i=k; i>=0; i--)
172 t = 1/(2*(i+nf)/x - t);
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
183 tmp = nf*log(fabs(w));
184 if (tmp < 7.09782712893383973096e+02) {
185 for (i=nm1; i>0; i--) {
191 for (i=nm1; i>0; i--) {
195 /* scale b to avoid spurious overflow */
205 if (fabs(z) >= fabs(w))
211 return sign ? -b : b;
215 double yn(int n, double x)
221 EXTRACT_WORDS(ix, lx, x);
225 if ((ix | (lx|-lx)>>31) > 0x7ff00000) /* nan */
227 if (sign && (ix|lx)!=0) /* x < 0 */
229 if (ix == 0x7ff00000)
242 return sign ? -y1(x) : y1(x);
244 if (ix >= 0x52d00000) { /* x > 2**302 */
246 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
247 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
248 * Let s=sin(x), c=cos(x),
249 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
251 * n sin(xn)*sqt2 cos(xn)*sqt2
252 * ----------------------------------
259 case 0: temp = -sin(x)-cos(x); break;
260 case 1: temp = -sin(x)+cos(x); break;
261 case 2: temp = sin(x)+cos(x); break;
263 case 3: temp = sin(x)-cos(x); break;
265 b = invsqrtpi*temp/sqrt(x);
269 /* quit if b is -inf */
270 GET_HIGH_WORD(ib, b);
271 for (i=0; i<nm1 && ib!=0xfff00000; ){
275 GET_HIGH_WORD(ib, b);
279 return sign ? -b : b;