148402Sbostic /*- 2*61308Sbostic * Copyright (c) 1992, 1993 3*61308Sbostic * The Regents of the University of California. All rights reserved. 448402Sbostic * 557151Sbostic * %sccs.include.redist.c% 634119Sbostic */ 734119Sbostic 824599Szliu #ifndef lint 9*61308Sbostic static char sccsid[] = "@(#)jn.c 8.1 (Berkeley) 06/04/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 extern double j0(),j1(),log(),fabs(),sqrt(),cos(),sin(),y0(),y1(); 8057151Sbostic static double 8157151Sbostic invsqrtpi= 5.641895835477562869480794515607725858441e-0001, 8257151Sbostic two = 2.0, 8357151Sbostic zero = 0.0, 8457151Sbostic one = 1.0; 8524599Szliu 8657151Sbostic double jn(n,x) 8757151Sbostic int n; double x; 8857151Sbostic { 8957151Sbostic int i, sgn; 9024599Szliu double a, b, temp; 9157151Sbostic double z, w; 9224599Szliu 9357151Sbostic /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) 9457151Sbostic * Thus, J(-n,x) = J(n,-x) 9557151Sbostic */ 9657151Sbostic /* if J(n,NaN) is NaN */ 9757151Sbostic if (_IEEE && isnan(x)) return x+x; 9857151Sbostic if (n<0){ 9924599Szliu n = -n; 10024599Szliu x = -x; 10124599Szliu } 10257151Sbostic if (n==0) return(j0(x)); 10357151Sbostic if (n==1) return(j1(x)); 10457151Sbostic sgn = (n&1)&(x < zero); /* even n -- 0, odd n -- sign(x) */ 10557151Sbostic x = fabs(x); 10657151Sbostic if (x == 0 || !finite (x)) /* if x is 0 or inf */ 10757151Sbostic b = zero; 10857151Sbostic else if ((double) n <= x) { 10957151Sbostic /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ 11057151Sbostic if (_IEEE && x >= 8.148143905337944345e+090) { 11157151Sbostic /* x >= 2**302 */ 11257151Sbostic /* (x >> n**2) 11357151Sbostic * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) 11457151Sbostic * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) 11557151Sbostic * Let s=sin(x), c=cos(x), 11657151Sbostic * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then 11757151Sbostic * 11857151Sbostic * n sin(xn)*sqt2 cos(xn)*sqt2 11957151Sbostic * ---------------------------------- 12057151Sbostic * 0 s-c c+s 12157151Sbostic * 1 -s-c -c+s 12257151Sbostic * 2 -s+c -c-s 12357151Sbostic * 3 s+c c-s 12457151Sbostic */ 12557151Sbostic switch(n&3) { 12657151Sbostic case 0: temp = cos(x)+sin(x); break; 12757151Sbostic case 1: temp = -cos(x)+sin(x); break; 12857151Sbostic case 2: temp = -cos(x)-sin(x); break; 12957151Sbostic case 3: temp = cos(x)-sin(x); break; 13057151Sbostic } 13157151Sbostic b = invsqrtpi*temp/sqrt(x); 13257151Sbostic } else { 13357151Sbostic a = j0(x); 13457151Sbostic b = j1(x); 13557151Sbostic for(i=1;i<n;i++){ 13657151Sbostic temp = b; 13757151Sbostic b = b*((double)(i+i)/x) - a; /* avoid underflow */ 13857151Sbostic a = temp; 13957151Sbostic } 14057151Sbostic } 14157151Sbostic } else { 14257151Sbostic if (x < 1.86264514923095703125e-009) { /* x < 2**-29 */ 14357151Sbostic /* x is tiny, return the first Taylor expansion of J(n,x) 14457151Sbostic * J(n,x) = 1/n!*(x/2)^n - ... 14557151Sbostic */ 14657151Sbostic if (n > 33) /* underflow */ 14757151Sbostic b = zero; 14857151Sbostic else { 14957151Sbostic temp = x*0.5; b = temp; 15057151Sbostic for (a=one,i=2;i<=n;i++) { 15157151Sbostic a *= (double)i; /* a = n! */ 15257151Sbostic b *= temp; /* b = (x/2)^n */ 15357151Sbostic } 15457151Sbostic b = b/a; 15557151Sbostic } 15657151Sbostic } else { 15757151Sbostic /* use backward recurrence */ 15857151Sbostic /* x x^2 x^2 15957151Sbostic * J(n,x)/J(n-1,x) = ---- ------ ------ ..... 16057151Sbostic * 2n - 2(n+1) - 2(n+2) 16157151Sbostic * 16257151Sbostic * 1 1 1 16357151Sbostic * (for large x) = ---- ------ ------ ..... 16457151Sbostic * 2n 2(n+1) 2(n+2) 16557151Sbostic * -- - ------ - ------ - 16657151Sbostic * x x x 16757151Sbostic * 16857151Sbostic * Let w = 2n/x and h=2/x, then the above quotient 16957151Sbostic * is equal to the continued fraction: 17057151Sbostic * 1 17157151Sbostic * = ----------------------- 17257151Sbostic * 1 17357151Sbostic * w - ----------------- 17457151Sbostic * 1 17557151Sbostic * w+h - --------- 17657151Sbostic * w+2h - ... 17757151Sbostic * 17857151Sbostic * To determine how many terms needed, let 17957151Sbostic * Q(0) = w, Q(1) = w(w+h) - 1, 18057151Sbostic * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), 18157151Sbostic * When Q(k) > 1e4 good for single 18257151Sbostic * When Q(k) > 1e9 good for double 18357151Sbostic * When Q(k) > 1e17 good for quadruple 18457151Sbostic */ 18557151Sbostic /* determine k */ 18657151Sbostic double t,v; 18757151Sbostic double q0,q1,h,tmp; int k,m; 18857151Sbostic w = (n+n)/(double)x; h = 2.0/(double)x; 18957151Sbostic q0 = w; z = w+h; q1 = w*z - 1.0; k=1; 19057151Sbostic while (q1<1.0e9) { 19157151Sbostic k += 1; z += h; 19257151Sbostic tmp = z*q1 - q0; 19357151Sbostic q0 = q1; 19457151Sbostic q1 = tmp; 19557151Sbostic } 19657151Sbostic m = n+n; 19757151Sbostic for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t); 19857151Sbostic a = t; 19957151Sbostic b = one; 20057151Sbostic /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) 20157151Sbostic * Hence, if n*(log(2n/x)) > ... 20257151Sbostic * single 8.8722839355e+01 20357151Sbostic * double 7.09782712893383973096e+02 20457151Sbostic * long double 1.1356523406294143949491931077970765006170e+04 20557151Sbostic * then recurrent value may overflow and the result will 20657151Sbostic * likely underflow to zero 20757151Sbostic */ 20857151Sbostic tmp = n; 20957151Sbostic v = two/x; 21057151Sbostic tmp = tmp*log(fabs(v*tmp)); 21157151Sbostic for (i=n-1;i>0;i--){ 21257151Sbostic temp = b; 21357151Sbostic b = ((i+i)/x)*b - a; 21457151Sbostic a = temp; 21557151Sbostic /* scale b to avoid spurious overflow */ 21657151Sbostic # if defined(vax) || defined(tahoe) 21757151Sbostic # define BMAX 1e13 21857151Sbostic # else 21957151Sbostic # define BMAX 1e100 22057151Sbostic # endif /* defined(vax) || defined(tahoe) */ 22157151Sbostic if (b > BMAX) { 22257151Sbostic a /= b; 22357151Sbostic t /= b; 22457151Sbostic b = one; 22557151Sbostic } 22657151Sbostic } 22757151Sbostic b = (t*j0(x)/b); 22857151Sbostic } 22924599Szliu } 23057151Sbostic return ((sgn == 1) ? -b : b); 23124599Szliu } 23257151Sbostic double yn(n,x) 23357151Sbostic int n; double x; 23457151Sbostic { 23557151Sbostic int i, sign; 23624599Szliu double a, b, temp; 23724599Szliu 23857151Sbostic /* Y(n,NaN), Y(n, x < 0) is NaN */ 23957151Sbostic if (x <= 0 || (_IEEE && x != x)) 24057151Sbostic if (_IEEE && x < 0) return zero/zero; 24157151Sbostic else if (x < 0) return (infnan(EDOM)); 24257151Sbostic else if (_IEEE) return -one/zero; 24357151Sbostic else return(infnan(-ERANGE)); 24457151Sbostic else if (!finite(x)) return(0); 24524599Szliu sign = 1; 24657151Sbostic if (n<0){ 24724599Szliu n = -n; 24857151Sbostic sign = 1 - ((n&1)<<2); 24924599Szliu } 25057151Sbostic if (n == 0) return(y0(x)); 25157151Sbostic if (n == 1) return(sign*y1(x)); 25257151Sbostic if(_IEEE && x >= 8.148143905337944345e+090) { /* x > 2**302 */ 25357151Sbostic /* (x >> n**2) 25457151Sbostic * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) 25557151Sbostic * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) 25657151Sbostic * Let s=sin(x), c=cos(x), 25757151Sbostic * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then 25857151Sbostic * 25957151Sbostic * n sin(xn)*sqt2 cos(xn)*sqt2 26057151Sbostic * ---------------------------------- 26157151Sbostic * 0 s-c c+s 26257151Sbostic * 1 -s-c -c+s 26357151Sbostic * 2 -s+c -c-s 26457151Sbostic * 3 s+c c-s 26557151Sbostic */ 26657151Sbostic switch (n&3) { 26757151Sbostic case 0: temp = sin(x)-cos(x); break; 26857151Sbostic case 1: temp = -sin(x)-cos(x); break; 26957151Sbostic case 2: temp = -sin(x)+cos(x); break; 27057151Sbostic case 3: temp = sin(x)+cos(x); break; 27157151Sbostic } 27257151Sbostic b = invsqrtpi*temp/sqrt(x); 27357151Sbostic } else { 27457151Sbostic a = y0(x); 27557151Sbostic b = y1(x); 27657151Sbostic /* quit if b is -inf */ 27757151Sbostic for (i = 1; i < n && !finite(b); i++){ 27824599Szliu temp = b; 27957151Sbostic b = ((double)(i+i)/x)*b - a; 28024599Szliu a = temp; 28157151Sbostic } 28224599Szliu } 28357151Sbostic if (!_IEEE && !finite(b)) 28457151Sbostic return (infnan(-sign * ERANGE)); 28557151Sbostic return ((sign > 0) ? b : -b); 28624599Szliu } 287