jn.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 us 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. */
  37. #include "libm.h"
  38. static const double invsqrtpi = 5.64189583547756279280e-01; /* 0x3FE20DD7, 0x50429B6D */
  39. double jn(int n, double x)
  40. {
  41. int32_t i,hx,ix,lx,sgn;
  42. double a, b, temp, di;
  43. double z, w;
  44. /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
  45. * Thus, J(-n,x) = J(n,-x)
  46. */
  47. EXTRACT_WORDS(hx, lx, x);
  48. ix = 0x7fffffff & hx;
  49. /* if J(n,NaN) is NaN */
  50. if ((ix|((uint32_t)(lx|-lx))>>31) > 0x7ff00000)
  51. return x+x;
  52. if (n < 0) {
  53. n = -n;
  54. x = -x;
  55. hx ^= 0x80000000;
  56. }
  57. if (n == 0) return j0(x);
  58. if (n == 1) return j1(x);
  59. sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
  60. x = fabs(x);
  61. if ((ix|lx) == 0 || ix >= 0x7ff00000) /* if x is 0 or inf */
  62. b = 0.0;
  63. else if ((double)n <= x) {
  64. /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
  65. if (ix >= 0x52D00000) { /* x > 2**302 */
  66. /* (x >> n**2)
  67. * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  68. * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  69. * Let s=sin(x), c=cos(x),
  70. * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
  71. *
  72. * n sin(xn)*sqt2 cos(xn)*sqt2
  73. * ----------------------------------
  74. * 0 s-c c+s
  75. * 1 -s-c -c+s
  76. * 2 -s+c -c-s
  77. * 3 s+c c-s
  78. */
  79. switch(n&3) {
  80. case 0: temp = cos(x)+sin(x); break;
  81. case 1: temp = -cos(x)+sin(x); break;
  82. case 2: temp = -cos(x)-sin(x); break;
  83. case 3: temp = cos(x)-sin(x); break;
  84. }
  85. b = invsqrtpi*temp/sqrt(x);
  86. } else {
  87. a = j0(x);
  88. b = j1(x);
  89. for (i=1; i<n; i++){
  90. temp = b;
  91. b = b*((double)(i+i)/x) - a; /* avoid underflow */
  92. a = temp;
  93. }
  94. }
  95. } else {
  96. if (ix < 0x3e100000) { /* x < 2**-29 */
  97. /* x is tiny, return the first Taylor expansion of J(n,x)
  98. * J(n,x) = 1/n!*(x/2)^n - ...
  99. */
  100. if (n > 33) /* underflow */
  101. b = 0.0;
  102. else {
  103. temp = x*0.5;
  104. b = temp;
  105. for (a=1.0,i=2; i<=n; i++) {
  106. a *= (double)i; /* a = n! */
  107. b *= temp; /* b = (x/2)^n */
  108. }
  109. b = b/a;
  110. }
  111. } else {
  112. /* use backward recurrence */
  113. /* x x^2 x^2
  114. * J(n,x)/J(n-1,x) = ---- ------ ------ .....
  115. * 2n - 2(n+1) - 2(n+2)
  116. *
  117. * 1 1 1
  118. * (for large x) = ---- ------ ------ .....
  119. * 2n 2(n+1) 2(n+2)
  120. * -- - ------ - ------ -
  121. * x x x
  122. *
  123. * Let w = 2n/x and h=2/x, then the above quotient
  124. * is equal to the continued fraction:
  125. * 1
  126. * = -----------------------
  127. * 1
  128. * w - -----------------
  129. * 1
  130. * w+h - ---------
  131. * w+2h - ...
  132. *
  133. * To determine how many terms needed, let
  134. * Q(0) = w, Q(1) = w(w+h) - 1,
  135. * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
  136. * When Q(k) > 1e4 good for single
  137. * When Q(k) > 1e9 good for double
  138. * When Q(k) > 1e17 good for quadruple
  139. */
  140. /* determine k */
  141. double t,v;
  142. double q0,q1,h,tmp;
  143. int32_t k,m;
  144. w = (n+n)/(double)x; h = 2.0/(double)x;
  145. q0 = w;
  146. z = w+h;
  147. q1 = w*z - 1.0;
  148. k = 1;
  149. while (q1 < 1.0e9) {
  150. k += 1;
  151. z += h;
  152. tmp = z*q1 - q0;
  153. q0 = q1;
  154. q1 = tmp;
  155. }
  156. m = n+n;
  157. for (t=0.0, i = 2*(n+k); i>=m; i -= 2)
  158. t = 1.0/(i/x-t);
  159. a = t;
  160. b = 1.0;
  161. /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
  162. * Hence, if n*(log(2n/x)) > ...
  163. * single 8.8722839355e+01
  164. * double 7.09782712893383973096e+02
  165. * long double 1.1356523406294143949491931077970765006170e+04
  166. * then recurrent value may overflow and the result is
  167. * likely underflow to zero
  168. */
  169. tmp = n;
  170. v = 2.0/x;
  171. tmp = tmp*log(fabs(v*tmp));
  172. if (tmp < 7.09782712893383973096e+02) {
  173. for (i=n-1,di=(double)(i+i); i>0; i--) {
  174. temp = b;
  175. b *= di;
  176. b = b/x - a;
  177. a = temp;
  178. di -= 2.0;
  179. }
  180. } else {
  181. for (i=n-1,di=(double)(i+i); i>0; i--) {
  182. temp = b;
  183. b *= di;
  184. b = b/x - a;
  185. a = temp;
  186. di -= 2.0;
  187. /* scale b to avoid spurious overflow */
  188. if (b > 1e100) {
  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. if (sgn==1) return -b;
  204. return b;
  205. }
  206. double yn(int n, double x)
  207. {
  208. int32_t i,hx,ix,lx;
  209. int32_t sign;
  210. double a, b, temp;
  211. EXTRACT_WORDS(hx, lx, x);
  212. ix = 0x7fffffff & hx;
  213. /* if Y(n,NaN) is NaN */
  214. if ((ix|((uint32_t)(lx|-lx))>>31) > 0x7ff00000)
  215. return x+x;
  216. if ((ix|lx) == 0)
  217. return -1.0/0.0;
  218. if (hx < 0)
  219. return 0.0/0.0;
  220. sign = 1;
  221. if (n < 0) {
  222. n = -n;
  223. sign = 1 - ((n&1)<<1);
  224. }
  225. if (n == 0)
  226. return y0(x);
  227. if (n == 1)
  228. return sign*y1(x);
  229. if (ix == 0x7ff00000)
  230. return 0.0;
  231. if (ix >= 0x52D00000) { /* x > 2**302 */
  232. /* (x >> n**2)
  233. * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  234. * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  235. * Let s=sin(x), c=cos(x),
  236. * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
  237. *
  238. * n sin(xn)*sqt2 cos(xn)*sqt2
  239. * ----------------------------------
  240. * 0 s-c c+s
  241. * 1 -s-c -c+s
  242. * 2 -s+c -c-s
  243. * 3 s+c c-s
  244. */
  245. switch(n&3) {
  246. case 0: temp = sin(x)-cos(x); break;
  247. case 1: temp = -sin(x)-cos(x); break;
  248. case 2: temp = -sin(x)+cos(x); break;
  249. case 3: temp = sin(x)+cos(x); break;
  250. }
  251. b = invsqrtpi*temp/sqrt(x);
  252. } else {
  253. uint32_t high;
  254. a = y0(x);
  255. b = y1(x);
  256. /* quit if b is -inf */
  257. GET_HIGH_WORD(high, b);
  258. for (i=1; i<n && high!=0xfff00000; i++){
  259. temp = b;
  260. b = ((double)(i+i)/x)*b - a;
  261. GET_HIGH_WORD(high, b);
  262. a = temp;
  263. }
  264. }
  265. if (sign > 0) return b;
  266. return -b;
  267. }