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