148402Sbostic /*-
261308Sbostic * Copyright (c) 1992, 1993
361308Sbostic * The Regents of the University of California. All rights reserved.
448402Sbostic *
557151Sbostic * %sccs.include.redist.c%
634119Sbostic */
734119Sbostic
824599Szliu #ifndef lint
9*64991Smckusick static char sccsid[] = "@(#)jn.c 8.2 (Berkeley) 11/30/93";
1034119Sbostic #endif /* not lint */
1124599Szliu
1224599Szliu /*
1357151Sbostic * 16 December 1992
1457151Sbostic * Minor modifications by Peter McIlroy to adapt non-IEEE architecture.
1557151Sbostic */
1624599Szliu
1757151Sbostic /*
1857151Sbostic * ====================================================
1957151Sbostic * Copyright (C) 1992 by Sun Microsystems, Inc.
2057151Sbostic *
2157151Sbostic * Developed at SunPro, a Sun Microsystems, Inc. business.
2257151Sbostic * Permission to use, copy, modify, and distribute this
2357151Sbostic * software is freely granted, provided that this notice
2457151Sbostic * is preserved.
2557151Sbostic * ====================================================
2657151Sbostic *
2757151Sbostic * ******************* WARNING ********************
2857151Sbostic * This is an alpha version of SunPro's FDLIBM (Freely
2957151Sbostic * Distributable Math Library) for IEEE double precision
3057151Sbostic * arithmetic. FDLIBM is a basic math library written
3157151Sbostic * in C that runs on machines that conform to IEEE
3257151Sbostic * Standard 754/854. This alpha version is distributed
3357151Sbostic * for testing purpose. Those who use this software
3457151Sbostic * should report any bugs to
3557151Sbostic *
3657151Sbostic * fdlibm-comments@sunpro.eng.sun.com
3757151Sbostic *
3857151Sbostic * -- K.C. Ng, Oct 12, 1992
3957151Sbostic * ************************************************
4057151Sbostic */
4124599Szliu
4257151Sbostic /*
4357151Sbostic * jn(int n, double x), yn(int n, double x)
4457151Sbostic * floating point Bessel's function of the 1st and 2nd kind
4557151Sbostic * of order n
4657151Sbostic *
4757151Sbostic * Special cases:
4857151Sbostic * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
4957151Sbostic * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
5057151Sbostic * Note 2. About jn(n,x), yn(n,x)
5157151Sbostic * For n=0, j0(x) is called,
5257151Sbostic * for n=1, j1(x) is called,
5357151Sbostic * for n<x, forward recursion us used starting
5457151Sbostic * from values of j0(x) and j1(x).
5557151Sbostic * for n>x, a continued fraction approximation to
5657151Sbostic * j(n,x)/j(n-1,x) is evaluated and then backward
5757151Sbostic * recursion is used starting from a supposed value
5857151Sbostic * for j(n,x). The resulting value of j(0,x) is
5957151Sbostic * compared with the actual value to correct the
6057151Sbostic * supposed value of j(n,x).
6157151Sbostic *
6257151Sbostic * yn(n,x) is similar in all respects, except
6357151Sbostic * that forward recursion is used for all
6457151Sbostic * values of n>1.
6557151Sbostic *
6657151Sbostic */
6724599Szliu
6857151Sbostic #include <math.h>
6957151Sbostic #include <float.h>
7057151Sbostic #include <errno.h>
7124599Szliu
7257151Sbostic #if defined(vax) || defined(tahoe)
7357151Sbostic #define _IEEE 0
7457151Sbostic #else
7557151Sbostic #define _IEEE 1
7657151Sbostic #define infnan(x) (0.0)
7757151Sbostic #endif
7824599Szliu
7957151Sbostic static double
8057151Sbostic invsqrtpi= 5.641895835477562869480794515607725858441e-0001,
8157151Sbostic two = 2.0,
8257151Sbostic zero = 0.0,
8357151Sbostic one = 1.0;
8424599Szliu
jn(n,x)8557151Sbostic double jn(n,x)
8657151Sbostic int n; double x;
8757151Sbostic {
8857151Sbostic int i, sgn;
8924599Szliu double a, b, temp;
9057151Sbostic double z, w;
9124599Szliu
9257151Sbostic /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
9357151Sbostic * Thus, J(-n,x) = J(n,-x)
9457151Sbostic */
9557151Sbostic /* if J(n,NaN) is NaN */
9657151Sbostic if (_IEEE && isnan(x)) return x+x;
9757151Sbostic if (n<0){
9824599Szliu n = -n;
9924599Szliu x = -x;
10024599Szliu }
10157151Sbostic if (n==0) return(j0(x));
10257151Sbostic if (n==1) return(j1(x));
10357151Sbostic sgn = (n&1)&(x < zero); /* even n -- 0, odd n -- sign(x) */
10457151Sbostic x = fabs(x);
10557151Sbostic if (x == 0 || !finite (x)) /* if x is 0 or inf */
10657151Sbostic b = zero;
10757151Sbostic else if ((double) n <= x) {
10857151Sbostic /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
10957151Sbostic if (_IEEE && x >= 8.148143905337944345e+090) {
11057151Sbostic /* x >= 2**302 */
11157151Sbostic /* (x >> n**2)
11257151Sbostic * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
11357151Sbostic * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
11457151Sbostic * Let s=sin(x), c=cos(x),
11557151Sbostic * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
11657151Sbostic *
11757151Sbostic * n sin(xn)*sqt2 cos(xn)*sqt2
11857151Sbostic * ----------------------------------
11957151Sbostic * 0 s-c c+s
12057151Sbostic * 1 -s-c -c+s
12157151Sbostic * 2 -s+c -c-s
12257151Sbostic * 3 s+c c-s
12357151Sbostic */
12457151Sbostic switch(n&3) {
12557151Sbostic case 0: temp = cos(x)+sin(x); break;
12657151Sbostic case 1: temp = -cos(x)+sin(x); break;
12757151Sbostic case 2: temp = -cos(x)-sin(x); break;
12857151Sbostic case 3: temp = cos(x)-sin(x); break;
12957151Sbostic }
13057151Sbostic b = invsqrtpi*temp/sqrt(x);
13157151Sbostic } else {
13257151Sbostic a = j0(x);
13357151Sbostic b = j1(x);
13457151Sbostic for(i=1;i<n;i++){
13557151Sbostic temp = b;
13657151Sbostic b = b*((double)(i+i)/x) - a; /* avoid underflow */
13757151Sbostic a = temp;
13857151Sbostic }
13957151Sbostic }
14057151Sbostic } else {
14157151Sbostic if (x < 1.86264514923095703125e-009) { /* x < 2**-29 */
14257151Sbostic /* x is tiny, return the first Taylor expansion of J(n,x)
14357151Sbostic * J(n,x) = 1/n!*(x/2)^n - ...
14457151Sbostic */
14557151Sbostic if (n > 33) /* underflow */
14657151Sbostic b = zero;
14757151Sbostic else {
14857151Sbostic temp = x*0.5; b = temp;
14957151Sbostic for (a=one,i=2;i<=n;i++) {
15057151Sbostic a *= (double)i; /* a = n! */
15157151Sbostic b *= temp; /* b = (x/2)^n */
15257151Sbostic }
15357151Sbostic b = b/a;
15457151Sbostic }
15557151Sbostic } else {
15657151Sbostic /* use backward recurrence */
15757151Sbostic /* x x^2 x^2
15857151Sbostic * J(n,x)/J(n-1,x) = ---- ------ ------ .....
15957151Sbostic * 2n - 2(n+1) - 2(n+2)
16057151Sbostic *
16157151Sbostic * 1 1 1
16257151Sbostic * (for large x) = ---- ------ ------ .....
16357151Sbostic * 2n 2(n+1) 2(n+2)
16457151Sbostic * -- - ------ - ------ -
16557151Sbostic * x x x
16657151Sbostic *
16757151Sbostic * Let w = 2n/x and h=2/x, then the above quotient
16857151Sbostic * is equal to the continued fraction:
16957151Sbostic * 1
17057151Sbostic * = -----------------------
17157151Sbostic * 1
17257151Sbostic * w - -----------------
17357151Sbostic * 1
17457151Sbostic * w+h - ---------
17557151Sbostic * w+2h - ...
17657151Sbostic *
17757151Sbostic * To determine how many terms needed, let
17857151Sbostic * Q(0) = w, Q(1) = w(w+h) - 1,
17957151Sbostic * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
18057151Sbostic * When Q(k) > 1e4 good for single
18157151Sbostic * When Q(k) > 1e9 good for double
18257151Sbostic * When Q(k) > 1e17 good for quadruple
18357151Sbostic */
18457151Sbostic /* determine k */
18557151Sbostic double t,v;
18657151Sbostic double q0,q1,h,tmp; int k,m;
18757151Sbostic w = (n+n)/(double)x; h = 2.0/(double)x;
18857151Sbostic q0 = w; z = w+h; q1 = w*z - 1.0; k=1;
18957151Sbostic while (q1<1.0e9) {
19057151Sbostic k += 1; z += h;
19157151Sbostic tmp = z*q1 - q0;
19257151Sbostic q0 = q1;
19357151Sbostic q1 = tmp;
19457151Sbostic }
19557151Sbostic m = n+n;
19657151Sbostic for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
19757151Sbostic a = t;
19857151Sbostic b = one;
19957151Sbostic /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
20057151Sbostic * Hence, if n*(log(2n/x)) > ...
20157151Sbostic * single 8.8722839355e+01
20257151Sbostic * double 7.09782712893383973096e+02
20357151Sbostic * long double 1.1356523406294143949491931077970765006170e+04
20457151Sbostic * then recurrent value may overflow and the result will
20557151Sbostic * likely underflow to zero
20657151Sbostic */
20757151Sbostic tmp = n;
20857151Sbostic v = two/x;
20957151Sbostic tmp = tmp*log(fabs(v*tmp));
21057151Sbostic for (i=n-1;i>0;i--){
21157151Sbostic temp = b;
21257151Sbostic b = ((i+i)/x)*b - a;
21357151Sbostic a = temp;
21457151Sbostic /* scale b to avoid spurious overflow */
21557151Sbostic # if defined(vax) || defined(tahoe)
21657151Sbostic # define BMAX 1e13
21757151Sbostic # else
21857151Sbostic # define BMAX 1e100
21957151Sbostic # endif /* defined(vax) || defined(tahoe) */
22057151Sbostic if (b > BMAX) {
22157151Sbostic a /= b;
22257151Sbostic t /= b;
22357151Sbostic b = one;
22457151Sbostic }
22557151Sbostic }
22657151Sbostic b = (t*j0(x)/b);
22757151Sbostic }
22824599Szliu }
22957151Sbostic return ((sgn == 1) ? -b : b);
23024599Szliu }
yn(n,x)23157151Sbostic double yn(n,x)
23257151Sbostic int n; double x;
23357151Sbostic {
23457151Sbostic int i, sign;
23524599Szliu double a, b, temp;
23624599Szliu
23757151Sbostic /* Y(n,NaN), Y(n, x < 0) is NaN */
23857151Sbostic if (x <= 0 || (_IEEE && x != x))
23957151Sbostic if (_IEEE && x < 0) return zero/zero;
24057151Sbostic else if (x < 0) return (infnan(EDOM));
24157151Sbostic else if (_IEEE) return -one/zero;
24257151Sbostic else return(infnan(-ERANGE));
24357151Sbostic else if (!finite(x)) return(0);
24424599Szliu sign = 1;
24557151Sbostic if (n<0){
24624599Szliu n = -n;
24757151Sbostic sign = 1 - ((n&1)<<2);
24824599Szliu }
24957151Sbostic if (n == 0) return(y0(x));
25057151Sbostic if (n == 1) return(sign*y1(x));
25157151Sbostic if(_IEEE && x >= 8.148143905337944345e+090) { /* x > 2**302 */
25257151Sbostic /* (x >> n**2)
25357151Sbostic * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
25457151Sbostic * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
25557151Sbostic * Let s=sin(x), c=cos(x),
25657151Sbostic * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
25757151Sbostic *
25857151Sbostic * n sin(xn)*sqt2 cos(xn)*sqt2
25957151Sbostic * ----------------------------------
26057151Sbostic * 0 s-c c+s
26157151Sbostic * 1 -s-c -c+s
26257151Sbostic * 2 -s+c -c-s
26357151Sbostic * 3 s+c c-s
26457151Sbostic */
26557151Sbostic switch (n&3) {
26657151Sbostic case 0: temp = sin(x)-cos(x); break;
26757151Sbostic case 1: temp = -sin(x)-cos(x); break;
26857151Sbostic case 2: temp = -sin(x)+cos(x); break;
26957151Sbostic case 3: temp = sin(x)+cos(x); break;
27057151Sbostic }
27157151Sbostic b = invsqrtpi*temp/sqrt(x);
27257151Sbostic } else {
27357151Sbostic a = y0(x);
27457151Sbostic b = y1(x);
27557151Sbostic /* quit if b is -inf */
27657151Sbostic for (i = 1; i < n && !finite(b); i++){
27724599Szliu temp = b;
27857151Sbostic b = ((double)(i+i)/x)*b - a;
27924599Szliu a = temp;
28057151Sbostic }
28124599Szliu }
28257151Sbostic if (!_IEEE && !finite(b))
28357151Sbostic return (infnan(-sign * ERANGE));
28457151Sbostic return ((sign > 0) ? b : -b);
28524599Szliu }
286