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.1 (Berkeley) 06/04/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 extern double j0(),j1(),log(),fabs(),sqrt(),cos(),sin(),y0(),y1(); 80 static double 81 invsqrtpi= 5.641895835477562869480794515607725858441e-0001, 82 two = 2.0, 83 zero = 0.0, 84 one = 1.0; 85 86 double jn(n,x) 87 int n; double x; 88 { 89 int i, sgn; 90 double a, b, temp; 91 double z, w; 92 93 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) 94 * Thus, J(-n,x) = J(n,-x) 95 */ 96 /* if J(n,NaN) is NaN */ 97 if (_IEEE && isnan(x)) return x+x; 98 if (n<0){ 99 n = -n; 100 x = -x; 101 } 102 if (n==0) return(j0(x)); 103 if (n==1) return(j1(x)); 104 sgn = (n&1)&(x < zero); /* even n -- 0, odd n -- sign(x) */ 105 x = fabs(x); 106 if (x == 0 || !finite (x)) /* if x is 0 or inf */ 107 b = zero; 108 else if ((double) n <= x) { 109 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ 110 if (_IEEE && x >= 8.148143905337944345e+090) { 111 /* x >= 2**302 */ 112 /* (x >> n**2) 113 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) 114 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) 115 * Let s=sin(x), c=cos(x), 116 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then 117 * 118 * n sin(xn)*sqt2 cos(xn)*sqt2 119 * ---------------------------------- 120 * 0 s-c c+s 121 * 1 -s-c -c+s 122 * 2 -s+c -c-s 123 * 3 s+c c-s 124 */ 125 switch(n&3) { 126 case 0: temp = cos(x)+sin(x); break; 127 case 1: temp = -cos(x)+sin(x); break; 128 case 2: temp = -cos(x)-sin(x); break; 129 case 3: temp = cos(x)-sin(x); break; 130 } 131 b = invsqrtpi*temp/sqrt(x); 132 } else { 133 a = j0(x); 134 b = j1(x); 135 for(i=1;i<n;i++){ 136 temp = b; 137 b = b*((double)(i+i)/x) - a; /* avoid underflow */ 138 a = temp; 139 } 140 } 141 } else { 142 if (x < 1.86264514923095703125e-009) { /* x < 2**-29 */ 143 /* x is tiny, return the first Taylor expansion of J(n,x) 144 * J(n,x) = 1/n!*(x/2)^n - ... 145 */ 146 if (n > 33) /* underflow */ 147 b = zero; 148 else { 149 temp = x*0.5; b = temp; 150 for (a=one,i=2;i<=n;i++) { 151 a *= (double)i; /* a = n! */ 152 b *= temp; /* b = (x/2)^n */ 153 } 154 b = b/a; 155 } 156 } else { 157 /* use backward recurrence */ 158 /* x x^2 x^2 159 * J(n,x)/J(n-1,x) = ---- ------ ------ ..... 160 * 2n - 2(n+1) - 2(n+2) 161 * 162 * 1 1 1 163 * (for large x) = ---- ------ ------ ..... 164 * 2n 2(n+1) 2(n+2) 165 * -- - ------ - ------ - 166 * x x x 167 * 168 * Let w = 2n/x and h=2/x, then the above quotient 169 * is equal to the continued fraction: 170 * 1 171 * = ----------------------- 172 * 1 173 * w - ----------------- 174 * 1 175 * w+h - --------- 176 * w+2h - ... 177 * 178 * To determine how many terms needed, let 179 * Q(0) = w, Q(1) = w(w+h) - 1, 180 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), 181 * When Q(k) > 1e4 good for single 182 * When Q(k) > 1e9 good for double 183 * When Q(k) > 1e17 good for quadruple 184 */ 185 /* determine k */ 186 double t,v; 187 double q0,q1,h,tmp; int k,m; 188 w = (n+n)/(double)x; h = 2.0/(double)x; 189 q0 = w; z = w+h; q1 = w*z - 1.0; k=1; 190 while (q1<1.0e9) { 191 k += 1; z += h; 192 tmp = z*q1 - q0; 193 q0 = q1; 194 q1 = tmp; 195 } 196 m = n+n; 197 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t); 198 a = t; 199 b = one; 200 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) 201 * Hence, if n*(log(2n/x)) > ... 202 * single 8.8722839355e+01 203 * double 7.09782712893383973096e+02 204 * long double 1.1356523406294143949491931077970765006170e+04 205 * then recurrent value may overflow and the result will 206 * likely underflow to zero 207 */ 208 tmp = n; 209 v = two/x; 210 tmp = tmp*log(fabs(v*tmp)); 211 for (i=n-1;i>0;i--){ 212 temp = b; 213 b = ((i+i)/x)*b - a; 214 a = temp; 215 /* scale b to avoid spurious overflow */ 216 # if defined(vax) || defined(tahoe) 217 # define BMAX 1e13 218 # else 219 # define BMAX 1e100 220 # endif /* defined(vax) || defined(tahoe) */ 221 if (b > BMAX) { 222 a /= b; 223 t /= b; 224 b = one; 225 } 226 } 227 b = (t*j0(x)/b); 228 } 229 } 230 return ((sgn == 1) ? -b : b); 231 } 232 double yn(n,x) 233 int n; double x; 234 { 235 int i, sign; 236 double a, b, temp; 237 238 /* Y(n,NaN), Y(n, x < 0) is NaN */ 239 if (x <= 0 || (_IEEE && x != x)) 240 if (_IEEE && x < 0) return zero/zero; 241 else if (x < 0) return (infnan(EDOM)); 242 else if (_IEEE) return -one/zero; 243 else return(infnan(-ERANGE)); 244 else if (!finite(x)) return(0); 245 sign = 1; 246 if (n<0){ 247 n = -n; 248 sign = 1 - ((n&1)<<2); 249 } 250 if (n == 0) return(y0(x)); 251 if (n == 1) return(sign*y1(x)); 252 if(_IEEE && x >= 8.148143905337944345e+090) { /* x > 2**302 */ 253 /* (x >> n**2) 254 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) 255 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) 256 * Let s=sin(x), c=cos(x), 257 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then 258 * 259 * n sin(xn)*sqt2 cos(xn)*sqt2 260 * ---------------------------------- 261 * 0 s-c c+s 262 * 1 -s-c -c+s 263 * 2 -s+c -c-s 264 * 3 s+c c-s 265 */ 266 switch (n&3) { 267 case 0: temp = sin(x)-cos(x); break; 268 case 1: temp = -sin(x)-cos(x); break; 269 case 2: temp = -sin(x)+cos(x); break; 270 case 3: temp = sin(x)+cos(x); break; 271 } 272 b = invsqrtpi*temp/sqrt(x); 273 } else { 274 a = y0(x); 275 b = y1(x); 276 /* quit if b is -inf */ 277 for (i = 1; i < n && !finite(b); i++){ 278 temp = b; 279 b = ((double)(i+i)/x)*b - a; 280 a = temp; 281 } 282 } 283 if (!_IEEE && !finite(b)) 284 return (infnan(-sign * ERANGE)); 285 return ((sign > 0) ? b : -b); 286 } 287