jn.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_jn.c */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, 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. /*
  13. * jn(n, x), yn(n, x)
  14. * floating point Bessel's function of the 1st and 2nd kind
  15. * of order n
  16. *
  17. * Special cases:
  18. * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
  19. * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
  20. * Note 2. About jn(n,x), yn(n,x)
  21. * For n=0, j0(x) is called,
  22. * for n=1, j1(x) is called,
  23. * for n<=x, forward recursion is used starting
  24. * from values of j0(x) and j1(x).
  25. * for n>x, a continued fraction approximation to
  26. * j(n,x)/j(n-1,x) is evaluated and then backward
  27. * recursion is used starting from a supposed value
  28. * for j(n,x). The resulting value of j(0,x) is
  29. * compared with the actual value to correct the
  30. * supposed value of j(n,x).
  31. *
  32. * yn(n,x) is similar in all respects, except
  33. * that forward recursion is used for all
  34. * values of n>1.
  35. */
  36. #include "libm.h"
  37. static const double invsqrtpi = 5.64189583547756279280e-01; /* 0x3FE20DD7, 0x50429B6D */
  38. double jn(int n, double x)
  39. {
  40. uint32_t ix, lx;
  41. int nm1, i, sign;
  42. double a, b, temp;
  43. EXTRACT_WORDS(ix, lx, x);
  44. sign = ix>>31;
  45. ix &= 0x7fffffff;
  46. if ((ix | (lx|-lx)>>31) > 0x7ff00000) /* nan */
  47. return x;
  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. /* nm1 = |n|-1 is used instead of |n| to handle n==INT_MIN */
  52. if (n == 0)
  53. return j0(x);
  54. if (n < 0) {
  55. nm1 = -(n+1);
  56. x = -x;
  57. sign ^= 1;
  58. } else
  59. nm1 = n-1;
  60. if (nm1 == 0)
  61. return j1(x);
  62. sign &= n; /* even n: 0, odd n: signbit(x) */
  63. x = fabs(x);
  64. if ((ix|lx) == 0 || ix == 0x7ff00000) /* if x is 0 or inf */
  65. b = 0.0;
  66. else if (nm1 < 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(nm1&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. default:
  87. case 3: temp = cos(x)+sin(x); break;
  88. }
  89. b = invsqrtpi*temp/sqrt(x);
  90. } else {
  91. a = j0(x);
  92. b = j1(x);
  93. for (i=0; i<nm1; ) {
  94. i++;
  95. temp = b;
  96. b = b*(2.0*i/x) - a; /* avoid underflow */
  97. a = temp;
  98. }
  99. }
  100. } else {
  101. if (ix < 0x3e100000) { /* x < 2**-29 */
  102. /* x is tiny, return the first Taylor expansion of J(n,x)
  103. * J(n,x) = 1/n!*(x/2)^n - ...
  104. */
  105. if (nm1 > 32) /* underflow */
  106. b = 0.0;
  107. else {
  108. temp = x*0.5;
  109. b = temp;
  110. a = 1.0;
  111. for (i=2; i<=nm1+1; i++) {
  112. a *= (double)i; /* a = n! */
  113. b *= temp; /* b = (x/2)^n */
  114. }
  115. b = b/a;
  116. }
  117. } else {
  118. /* use backward recurrence */
  119. /* x x^2 x^2
  120. * J(n,x)/J(n-1,x) = ---- ------ ------ .....
  121. * 2n - 2(n+1) - 2(n+2)
  122. *
  123. * 1 1 1
  124. * (for large x) = ---- ------ ------ .....
  125. * 2n 2(n+1) 2(n+2)
  126. * -- - ------ - ------ -
  127. * x x x
  128. *
  129. * Let w = 2n/x and h=2/x, then the above quotient
  130. * is equal to the continued fraction:
  131. * 1
  132. * = -----------------------
  133. * 1
  134. * w - -----------------
  135. * 1
  136. * w+h - ---------
  137. * w+2h - ...
  138. *
  139. * To determine how many terms needed, let
  140. * Q(0) = w, Q(1) = w(w+h) - 1,
  141. * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
  142. * When Q(k) > 1e4 good for single
  143. * When Q(k) > 1e9 good for double
  144. * When Q(k) > 1e17 good for quadruple
  145. */
  146. /* determine k */
  147. double t,q0,q1,w,h,z,tmp,nf;
  148. int k;
  149. nf = nm1 + 1.0;
  150. w = 2*nf/x;
  151. h = 2/x;
  152. z = w+h;
  153. q0 = w;
  154. q1 = w*z - 1.0;
  155. k = 1;
  156. while (q1 < 1.0e9) {
  157. k += 1;
  158. z += h;
  159. tmp = z*q1 - q0;
  160. q0 = q1;
  161. q1 = tmp;
  162. }
  163. for (t=0.0, i=k; i>=0; i--)
  164. t = 1/(2*(i+nf)/x - t);
  165. a = t;
  166. b = 1.0;
  167. /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
  168. * Hence, if n*(log(2n/x)) > ...
  169. * single 8.8722839355e+01
  170. * double 7.09782712893383973096e+02
  171. * long double 1.1356523406294143949491931077970765006170e+04
  172. * then recurrent value may overflow and the result is
  173. * likely underflow to zero
  174. */
  175. tmp = nf*log(fabs(w));
  176. if (tmp < 7.09782712893383973096e+02) {
  177. for (i=nm1; i>0; i--) {
  178. temp = b;
  179. b = b*(2.0*i)/x - a;
  180. a = temp;
  181. }
  182. } else {
  183. for (i=nm1; i>0; i--) {
  184. temp = b;
  185. b = b*(2.0*i)/x - a;
  186. a = temp;
  187. /* scale b to avoid spurious overflow */
  188. if (b > 0x1p500) {
  189. a /= b;
  190. t /= b;
  191. b = 1.0;
  192. }
  193. }
  194. }
  195. z = j0(x);
  196. w = j1(x);
  197. if (fabs(z) >= fabs(w))
  198. b = t*z/b;
  199. else
  200. b = t*w/a;
  201. }
  202. }
  203. return sign ? -b : b;
  204. }
  205. double yn(int n, double x)
  206. {
  207. uint32_t ix, lx, ib;
  208. int nm1, sign, i;
  209. double a, b, temp;
  210. EXTRACT_WORDS(ix, lx, x);
  211. sign = ix>>31;
  212. ix &= 0x7fffffff;
  213. if ((ix | (lx|-lx)>>31) > 0x7ff00000) /* nan */
  214. return x;
  215. if (sign && (ix|lx)!=0) /* x < 0 */
  216. return 0/0.0;
  217. if (ix == 0x7ff00000)
  218. return 0.0;
  219. if (n == 0)
  220. return y0(x);
  221. if (n < 0) {
  222. nm1 = -(n+1);
  223. sign = n&1;
  224. } else {
  225. nm1 = n-1;
  226. sign = 0;
  227. }
  228. if (nm1 == 0)
  229. return sign ? -y1(x) : y1(x);
  230. if (ix >= 0x52d00000) { /* x > 2**302 */
  231. /* (x >> n**2)
  232. * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  233. * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  234. * Let s=sin(x), c=cos(x),
  235. * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
  236. *
  237. * n sin(xn)*sqt2 cos(xn)*sqt2
  238. * ----------------------------------
  239. * 0 s-c c+s
  240. * 1 -s-c -c+s
  241. * 2 -s+c -c-s
  242. * 3 s+c c-s
  243. */
  244. switch(nm1&3) {
  245. case 0: temp = -sin(x)-cos(x); break;
  246. case 1: temp = -sin(x)+cos(x); break;
  247. case 2: temp = sin(x)+cos(x); break;
  248. default:
  249. case 3: temp = sin(x)-cos(x); break;
  250. }
  251. b = invsqrtpi*temp/sqrt(x);
  252. } else {
  253. a = y0(x);
  254. b = y1(x);
  255. /* quit if b is -inf */
  256. GET_HIGH_WORD(ib, b);
  257. for (i=0; i<nm1 && ib!=0xfff00000; ){
  258. i++;
  259. temp = b;
  260. b = (2.0*i/x)*b - a;
  261. GET_HIGH_WORD(ib, b);
  262. a = temp;
  263. }
  264. }
  265. return sign ? -b : b;
  266. }