1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)jn.c 8.2 (Berkeley) 11/30/93";
10 #endif /* not lint */
11
12 /*
13 * 16 December 1992
14 * Minor modifications by Peter McIlroy to adapt non-IEEE architecture.
15 */
16
17 /*
18 * ====================================================
19 * Copyright (C) 1992 by Sun Microsystems, Inc.
20 *
21 * Developed at SunPro, a Sun Microsystems, Inc. business.
22 * Permission to use, copy, modify, and distribute this
23 * software is freely granted, provided that this notice
24 * is preserved.
25 * ====================================================
26 *
27 * ******************* WARNING ********************
28 * This is an alpha version of SunPro's FDLIBM (Freely
29 * Distributable Math Library) for IEEE double precision
30 * arithmetic. FDLIBM is a basic math library written
31 * in C that runs on machines that conform to IEEE
32 * Standard 754/854. This alpha version is distributed
33 * for testing purpose. Those who use this software
34 * should report any bugs to
35 *
36 * fdlibm-comments@sunpro.eng.sun.com
37 *
38 * -- K.C. Ng, Oct 12, 1992
39 * ************************************************
40 */
41
42 /*
43 * jn(int n, double x), yn(int n, double x)
44 * floating point Bessel's function of the 1st and 2nd kind
45 * of order n
46 *
47 * Special cases:
48 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
49 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
50 * Note 2. About jn(n,x), yn(n,x)
51 * For n=0, j0(x) is called,
52 * for n=1, j1(x) is called,
53 * for n<x, forward recursion us used starting
54 * from values of j0(x) and j1(x).
55 * for n>x, a continued fraction approximation to
56 * j(n,x)/j(n-1,x) is evaluated and then backward
57 * recursion is used starting from a supposed value
58 * for j(n,x). The resulting value of j(0,x) is
59 * compared with the actual value to correct the
60 * supposed value of j(n,x).
61 *
62 * yn(n,x) is similar in all respects, except
63 * that forward recursion is used for all
64 * values of n>1.
65 *
66 */
67
68 #include <math.h>
69 #include <float.h>
70 #include <errno.h>
71
72 #if defined(vax) || defined(tahoe)
73 #define _IEEE 0
74 #else
75 #define _IEEE 1
76 #define infnan(x) (0.0)
77 #endif
78
79 static double
80 invsqrtpi= 5.641895835477562869480794515607725858441e-0001,
81 two = 2.0,
82 zero = 0.0,
83 one = 1.0;
84
jn(n,x)85 double jn(n,x)
86 int n; double x;
87 {
88 int i, sgn;
89 double a, b, temp;
90 double z, w;
91
92 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
93 * Thus, J(-n,x) = J(n,-x)
94 */
95 /* if J(n,NaN) is NaN */
96 if (_IEEE && isnan(x)) return x+x;
97 if (n<0){
98 n = -n;
99 x = -x;
100 }
101 if (n==0) return(j0(x));
102 if (n==1) return(j1(x));
103 sgn = (n&1)&(x < zero); /* even n -- 0, odd n -- sign(x) */
104 x = fabs(x);
105 if (x == 0 || !finite (x)) /* if x is 0 or inf */
106 b = zero;
107 else if ((double) n <= x) {
108 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
109 if (_IEEE && x >= 8.148143905337944345e+090) {
110 /* x >= 2**302 */
111 /* (x >> n**2)
112 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
113 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
114 * Let s=sin(x), c=cos(x),
115 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
116 *
117 * n sin(xn)*sqt2 cos(xn)*sqt2
118 * ----------------------------------
119 * 0 s-c c+s
120 * 1 -s-c -c+s
121 * 2 -s+c -c-s
122 * 3 s+c c-s
123 */
124 switch(n&3) {
125 case 0: temp = cos(x)+sin(x); break;
126 case 1: temp = -cos(x)+sin(x); break;
127 case 2: temp = -cos(x)-sin(x); break;
128 case 3: temp = cos(x)-sin(x); break;
129 }
130 b = invsqrtpi*temp/sqrt(x);
131 } else {
132 a = j0(x);
133 b = j1(x);
134 for(i=1;i<n;i++){
135 temp = b;
136 b = b*((double)(i+i)/x) - a; /* avoid underflow */
137 a = temp;
138 }
139 }
140 } else {
141 if (x < 1.86264514923095703125e-009) { /* x < 2**-29 */
142 /* x is tiny, return the first Taylor expansion of J(n,x)
143 * J(n,x) = 1/n!*(x/2)^n - ...
144 */
145 if (n > 33) /* underflow */
146 b = zero;
147 else {
148 temp = x*0.5; b = temp;
149 for (a=one,i=2;i<=n;i++) {
150 a *= (double)i; /* a = n! */
151 b *= temp; /* b = (x/2)^n */
152 }
153 b = b/a;
154 }
155 } else {
156 /* use backward recurrence */
157 /* x x^2 x^2
158 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
159 * 2n - 2(n+1) - 2(n+2)
160 *
161 * 1 1 1
162 * (for large x) = ---- ------ ------ .....
163 * 2n 2(n+1) 2(n+2)
164 * -- - ------ - ------ -
165 * x x x
166 *
167 * Let w = 2n/x and h=2/x, then the above quotient
168 * is equal to the continued fraction:
169 * 1
170 * = -----------------------
171 * 1
172 * w - -----------------
173 * 1
174 * w+h - ---------
175 * w+2h - ...
176 *
177 * To determine how many terms needed, let
178 * Q(0) = w, Q(1) = w(w+h) - 1,
179 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
180 * When Q(k) > 1e4 good for single
181 * When Q(k) > 1e9 good for double
182 * When Q(k) > 1e17 good for quadruple
183 */
184 /* determine k */
185 double t,v;
186 double q0,q1,h,tmp; int k,m;
187 w = (n+n)/(double)x; h = 2.0/(double)x;
188 q0 = w; z = w+h; q1 = w*z - 1.0; k=1;
189 while (q1<1.0e9) {
190 k += 1; z += h;
191 tmp = z*q1 - q0;
192 q0 = q1;
193 q1 = tmp;
194 }
195 m = n+n;
196 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
197 a = t;
198 b = one;
199 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
200 * Hence, if n*(log(2n/x)) > ...
201 * single 8.8722839355e+01
202 * double 7.09782712893383973096e+02
203 * long double 1.1356523406294143949491931077970765006170e+04
204 * then recurrent value may overflow and the result will
205 * likely underflow to zero
206 */
207 tmp = n;
208 v = two/x;
209 tmp = tmp*log(fabs(v*tmp));
210 for (i=n-1;i>0;i--){
211 temp = b;
212 b = ((i+i)/x)*b - a;
213 a = temp;
214 /* scale b to avoid spurious overflow */
215 # if defined(vax) || defined(tahoe)
216 # define BMAX 1e13
217 # else
218 # define BMAX 1e100
219 # endif /* defined(vax) || defined(tahoe) */
220 if (b > BMAX) {
221 a /= b;
222 t /= b;
223 b = one;
224 }
225 }
226 b = (t*j0(x)/b);
227 }
228 }
229 return ((sgn == 1) ? -b : b);
230 }
yn(n,x)231 double yn(n,x)
232 int n; double x;
233 {
234 int i, sign;
235 double a, b, temp;
236
237 /* Y(n,NaN), Y(n, x < 0) is NaN */
238 if (x <= 0 || (_IEEE && x != x))
239 if (_IEEE && x < 0) return zero/zero;
240 else if (x < 0) return (infnan(EDOM));
241 else if (_IEEE) return -one/zero;
242 else return(infnan(-ERANGE));
243 else if (!finite(x)) return(0);
244 sign = 1;
245 if (n<0){
246 n = -n;
247 sign = 1 - ((n&1)<<2);
248 }
249 if (n == 0) return(y0(x));
250 if (n == 1) return(sign*y1(x));
251 if(_IEEE && x >= 8.148143905337944345e+090) { /* x > 2**302 */
252 /* (x >> n**2)
253 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
254 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
255 * Let s=sin(x), c=cos(x),
256 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
257 *
258 * n sin(xn)*sqt2 cos(xn)*sqt2
259 * ----------------------------------
260 * 0 s-c c+s
261 * 1 -s-c -c+s
262 * 2 -s+c -c-s
263 * 3 s+c c-s
264 */
265 switch (n&3) {
266 case 0: temp = sin(x)-cos(x); break;
267 case 1: temp = -sin(x)-cos(x); break;
268 case 2: temp = -sin(x)+cos(x); break;
269 case 3: temp = sin(x)+cos(x); break;
270 }
271 b = invsqrtpi*temp/sqrt(x);
272 } else {
273 a = y0(x);
274 b = y1(x);
275 /* quit if b is -inf */
276 for (i = 1; i < n && !finite(b); i++){
277 temp = b;
278 b = ((double)(i+i)/x)*b - a;
279 a = temp;
280 }
281 }
282 if (!_IEEE && !finite(b))
283 return (infnan(-sign * ERANGE));
284 return ((sign > 0) ? b : -b);
285 }
286