e_jn.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. /*
  197. * wrapper jn(int n, double x)
  198. */
  199. #ifndef _IEEE_LIBM
  200. double jn(int n, double x)
  201. {
  202. double z = __ieee754_jn(n, x);
  203. if (_LIB_VERSION == _IEEE_ || isnan(x))
  204. return z;
  205. if (fabs(x) > X_TLOSS)
  206. return __kernel_standard((double)n, x, 38); /* jn(|x|>X_TLOSS,n) */
  207. return z;
  208. }
  209. #else
  210. strong_alias(__ieee754_jn, jn)
  211. #endif
  212. double attribute_hidden __ieee754_yn(int n, double x)
  213. {
  214. int32_t i,hx,ix,lx;
  215. int32_t sign;
  216. double a, b, temp=0;
  217. EXTRACT_WORDS(hx,lx,x);
  218. ix = 0x7fffffff&hx;
  219. /* if Y(n,NaN) is NaN */
  220. if((ix|((u_int32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
  221. if((ix|lx)==0) return -one/zero;
  222. if(hx<0) return zero/zero;
  223. sign = 1;
  224. if(n<0){
  225. n = -n;
  226. sign = 1 - ((n&1)<<1);
  227. }
  228. if(n==0) return(__ieee754_y0(x));
  229. if(n==1) return(sign*__ieee754_y1(x));
  230. if(ix==0x7ff00000) return zero;
  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. u_int32_t high;
  254. a = __ieee754_y0(x);
  255. b = __ieee754_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; else return -b;
  266. }
  267. /*
  268. * wrapper yn(int n, double x)
  269. */
  270. #ifndef _IEEE_LIBM
  271. double yn(int n, double x) /* wrapper yn */
  272. {
  273. double z = __ieee754_yn(n, x);
  274. if (_LIB_VERSION == _IEEE_ || isnan(x))
  275. return z;
  276. if (x <= 0.0) {
  277. if(x == 0.0) /* d= -one/(x-x); */
  278. return __kernel_standard((double)n, x, 12);
  279. /* d = zero/(x-x); */
  280. return __kernel_standard((double)n, x, 13);
  281. }
  282. if (x > X_TLOSS)
  283. return __kernel_standard((double)n, x, 39); /* yn(x>X_TLOSS,n) */
  284. return z;
  285. }
  286. #else
  287. strong_alias(__ieee754_yn, yn)
  288. #endif