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