e_jn.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* @(#)e_jn.c 5.1 93/09/24 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. #if defined(LIBM_SCCS) && !defined(lint)
  13. static char rcsid[] = "$NetBSD: e_jn.c,v 1.9 1995/05/10 20:45:34 jtc Exp $";
  14. #endif
  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. #include "math.h"
  41. #include "math_private.h"
  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. #ifdef __STDC__
  51. static const double zero = 0.00000000000000000000e+00;
  52. #else
  53. static double zero = 0.00000000000000000000e+00;
  54. #endif
  55. #ifdef __STDC__
  56. double attribute_hidden __ieee754_jn(int n, double x)
  57. #else
  58. double attribute_hidden __ieee754_jn(n,x)
  59. int n; double x;
  60. #endif
  61. {
  62. int32_t i,hx,ix,lx, sgn;
  63. double a, b, temp=0, di;
  64. double z, w;
  65. /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
  66. * Thus, J(-n,x) = J(n,-x)
  67. */
  68. EXTRACT_WORDS(hx,lx,x);
  69. ix = 0x7fffffff&hx;
  70. /* if J(n,NaN) is NaN */
  71. if((ix|((u_int32_t)(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; int32_t 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. #ifdef __STDC__
  214. double attribute_hidden __ieee754_yn(int n, double x)
  215. #else
  216. double attribute_hidden __ieee754_yn(n,x)
  217. int n; double x;
  218. #endif
  219. {
  220. int32_t i,hx,ix,lx;
  221. int32_t sign;
  222. double a, b, temp=0;
  223. EXTRACT_WORDS(hx,lx,x);
  224. ix = 0x7fffffff&hx;
  225. /* if Y(n,NaN) is NaN */
  226. if((ix|((u_int32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
  227. if((ix|lx)==0) return -one/zero;
  228. if(hx<0) return zero/zero;
  229. sign = 1;
  230. if(n<0){
  231. n = -n;
  232. sign = 1 - ((n&1)<<1);
  233. }
  234. if(n==0) return(__ieee754_y0(x));
  235. if(n==1) return(sign*__ieee754_y1(x));
  236. if(ix==0x7ff00000) return zero;
  237. if(ix>=0x52D00000) { /* x > 2**302 */
  238. /* (x >> n**2)
  239. * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  240. * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  241. * Let s=sin(x), c=cos(x),
  242. * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
  243. *
  244. * n sin(xn)*sqt2 cos(xn)*sqt2
  245. * ----------------------------------
  246. * 0 s-c c+s
  247. * 1 -s-c -c+s
  248. * 2 -s+c -c-s
  249. * 3 s+c c-s
  250. */
  251. switch(n&3) {
  252. case 0: temp = sin(x)-cos(x); break;
  253. case 1: temp = -sin(x)-cos(x); break;
  254. case 2: temp = -sin(x)+cos(x); break;
  255. case 3: temp = sin(x)+cos(x); break;
  256. }
  257. b = invsqrtpi*temp/sqrt(x);
  258. } else {
  259. u_int32_t high;
  260. a = __ieee754_y0(x);
  261. b = __ieee754_y1(x);
  262. /* quit if b is -inf */
  263. GET_HIGH_WORD(high,b);
  264. for(i=1;i<n&&high!=0xfff00000;i++){
  265. temp = b;
  266. b = ((double)(i+i)/x)*b - a;
  267. GET_HIGH_WORD(high,b);
  268. a = temp;
  269. }
  270. }
  271. if(sign>0) return b; else return -b;
  272. }