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