1 /* derived from /netlib/fdlibm */ 2 3 /* @(#)e_jn.c 1.4 95/01/18 */ 4 /* 5 * ==================================================== 6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 7 * 8 * Developed at SunSoft, a Sun Microsystems, Inc. business. 9 * Permission to use, copy, modify, and distribute this 10 * software is freely granted, provided that this notice 11 * is preserved. 12 * ==================================================== 13 */ 14 15 /* 16 * __ieee754_jn(n, x), __ieee754_yn(n, x) 17 * floating point Bessel's function of the 1st and 2nd kind 18 * of order n 19 * 20 * Special cases: 21 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal; 22 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal. 23 * Note 2. About jn(n,x), yn(n,x) 24 * For n=0, j0(x) is called, 25 * for n=1, j1(x) is called, 26 * for n<x, forward recursion us used starting 27 * from values of j0(x) and j1(x). 28 * for n>x, a continued fraction approximation to 29 * j(n,x)/j(n-1,x) is evaluated and then backward 30 * recursion is used starting from a supposed value 31 * for j(n,x). The resulting value of j(0,x) is 32 * compared with the actual value to correct the 33 * supposed value of j(n,x). 34 * 35 * yn(n,x) is similar in all respects, except 36 * that forward recursion is used for all 37 * values of n>1. 38 * 39 */ 40 41 #include "fdlibm.h" 42 43 static const double 44 invsqrtpi= 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */ 45 two = 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */ 46 one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */ 47 48 static double zero = 0.00000000000000000000e+00; 49 __ieee754_jn(int n,double x)50 double __ieee754_jn(int n, double x) 51 { 52 int i,hx,ix,lx, sgn; 53 double a, b, temp, di; 54 double z, w; 55 56 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) 57 * Thus, J(-n,x) = J(n,-x) 58 */ 59 hx = __HI(x); 60 ix = 0x7fffffff&hx; 61 lx = __LO(x); 62 /* if J(n,NaN) is NaN */ 63 if((ix|((unsigned)(lx|-lx))>>31)>0x7ff00000) return x+x; 64 if(n<0){ 65 n = -n; 66 x = -x; 67 hx ^= 0x80000000; 68 } 69 if(n==0) return(__ieee754_j0(x)); 70 if(n==1) return(__ieee754_j1(x)); 71 sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */ 72 x = fabs(x); 73 if((ix|lx)==0||ix>=0x7ff00000) /* if x is 0 or inf */ 74 b = zero; 75 else if((double)n<=x) { 76 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ 77 if(ix>=0x52D00000) { /* x > 2**302 */ 78 /* (x >> n**2) 79 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) 80 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) 81 * Let s=sin(x), c=cos(x), 82 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then 83 * 84 * n sin(xn)*sqt2 cos(xn)*sqt2 85 * ---------------------------------- 86 * 0 s-c c+s 87 * 1 -s-c -c+s 88 * 2 -s+c -c-s 89 * 3 s+c c-s 90 */ 91 switch(n&3) { 92 case 0: temp = cos(x)+sin(x); break; 93 case 1: temp = -cos(x)+sin(x); break; 94 case 2: temp = -cos(x)-sin(x); break; 95 case 3: temp = cos(x)-sin(x); break; 96 } 97 b = invsqrtpi*temp/sqrt(x); 98 } else { 99 a = __ieee754_j0(x); 100 b = __ieee754_j1(x); 101 for(i=1;i<n;i++){ 102 temp = b; 103 b = b*((double)(i+i)/x) - a; /* avoid underflow */ 104 a = temp; 105 } 106 } 107 } else { 108 if(ix<0x3e100000) { /* x < 2**-29 */ 109 /* x is tiny, return the first Taylor expansion of J(n,x) 110 * J(n,x) = 1/n!*(x/2)^n - ... 111 */ 112 if(n>33) /* underflow */ 113 b = zero; 114 else { 115 temp = x*0.5; b = temp; 116 for (a=one,i=2;i<=n;i++) { 117 a *= (double)i; /* a = n! */ 118 b *= temp; /* b = (x/2)^n */ 119 } 120 b = b/a; 121 } 122 } else { 123 /* use backward recurrence */ 124 /* x x^2 x^2 125 * J(n,x)/J(n-1,x) = ---- ------ ------ ..... 126 * 2n - 2(n+1) - 2(n+2) 127 * 128 * 1 1 1 129 * (for large x) = ---- ------ ------ ..... 130 * 2n 2(n+1) 2(n+2) 131 * -- - ------ - ------ - 132 * x x x 133 * 134 * Let w = 2n/x and h=2/x, then the above quotient 135 * is equal to the continued fraction: 136 * 1 137 * = ----------------------- 138 * 1 139 * w - ----------------- 140 * 1 141 * w+h - --------- 142 * w+2h - ... 143 * 144 * To determine how many terms needed, let 145 * Q(0) = w, Q(1) = w(w+h) - 1, 146 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), 147 * When Q(k) > 1e4 good for single 148 * When Q(k) > 1e9 good for double 149 * When Q(k) > 1e17 good for quadruple 150 */ 151 /* determine k */ 152 double t,v; 153 double q0,q1,h,tmp; int k,m; 154 w = (n+n)/(double)x; h = 2.0/(double)x; 155 q0 = w; z = w+h; q1 = w*z - 1.0; k=1; 156 while(q1<1.0e9) { 157 k += 1; z += h; 158 tmp = z*q1 - q0; 159 q0 = q1; 160 q1 = tmp; 161 } 162 m = n+n; 163 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t); 164 a = t; 165 b = one; 166 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) 167 * Hence, if n*(log(2n/x)) > ... 168 * single 8.8722839355e+01 169 * double 7.09782712893383973096e+02 170 * long double 1.1356523406294143949491931077970765006170e+04 171 * then recurrent value may overflow and the result is 172 * likely underflow to zero 173 */ 174 tmp = n; 175 v = two/x; 176 tmp = tmp*__ieee754_log(fabs(v*tmp)); 177 if(tmp<7.09782712893383973096e+02) { 178 for(i=n-1,di=(double)(i+i);i>0;i--){ 179 temp = b; 180 b *= di; 181 b = b/x - a; 182 a = temp; 183 di -= two; 184 } 185 } else { 186 for(i=n-1,di=(double)(i+i);i>0;i--){ 187 temp = b; 188 b *= di; 189 b = b/x - a; 190 a = temp; 191 di -= two; 192 /* scale b to avoid spurious overflow */ 193 if(b>1e100) { 194 a /= b; 195 t /= b; 196 b = one; 197 } 198 } 199 } 200 b = (t*__ieee754_j0(x)/b); 201 } 202 } 203 if(sgn==1) return -b; else return b; 204 } 205 __ieee754_yn(int n,double x)206 double __ieee754_yn(int n, double x) 207 { 208 int i,hx,ix,lx; 209 int sign; 210 double a, b, temp; 211 212 hx = __HI(x); 213 ix = 0x7fffffff&hx; 214 lx = __LO(x); 215 /* if Y(n,NaN) is NaN */ 216 if((ix|((unsigned)(lx|-lx))>>31)>0x7ff00000) return x+x; 217 if((ix|lx)==0) return -one/zero; 218 if(hx<0) return zero/zero; 219 sign = 1; 220 if(n<0){ 221 n = -n; 222 sign = 1 - ((n&1)<<1); 223 } 224 if(n==0) return(__ieee754_y0(x)); 225 if(n==1) return(sign*__ieee754_y1(x)); 226 if(ix==0x7ff00000) return zero; 227 if(ix>=0x52D00000) { /* x > 2**302 */ 228 /* (x >> n**2) 229 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) 230 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) 231 * Let s=sin(x), c=cos(x), 232 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then 233 * 234 * n sin(xn)*sqt2 cos(xn)*sqt2 235 * ---------------------------------- 236 * 0 s-c c+s 237 * 1 -s-c -c+s 238 * 2 -s+c -c-s 239 * 3 s+c c-s 240 */ 241 switch(n&3) { 242 case 0: temp = sin(x)-cos(x); break; 243 case 1: temp = -sin(x)-cos(x); break; 244 case 2: temp = -sin(x)+cos(x); break; 245 case 3: temp = sin(x)+cos(x); break; 246 } 247 b = invsqrtpi*temp/sqrt(x); 248 } else { 249 a = __ieee754_y0(x); 250 b = __ieee754_y1(x); 251 /* quit if b is -inf */ 252 for(i=1;i<n&&(__HI(b) != 0xfff00000);i++){ 253 temp = b; 254 b = ((double)(i+i)/x)*b - a; 255 a = temp; 256 } 257 } 258 if(sign>0) return b; else return -b; 259 } 260