e_jn.c 6.8 KB

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