e_jn.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* @(#)e_jn.c 1.4 95/01/18 */
  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. #include "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/e_jn.c,v 1.11 2010/11/13 10:54:10 uqs Exp $");
  14. /*
  15. * __ieee754_jn(n, x), __ieee754_yn(n, x)
  16. * floating point Bessel's function of the 1st and 2nd kind
  17. * of order n
  18. *
  19. * Special cases:
  20. * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
  21. * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
  22. * Note 2. About jn(n,x), yn(n,x)
  23. * For n=0, j0(x) is called,
  24. * for n=1, j1(x) is called,
  25. * for n<x, forward recursion us used starting
  26. * from values of j0(x) and j1(x).
  27. * for n>x, a continued fraction approximation to
  28. * j(n,x)/j(n-1,x) is evaluated and then backward
  29. * recursion is used starting from a supposed value
  30. * for j(n,x). The resulting value of j(0,x) is
  31. * compared with the actual value to correct the
  32. * supposed value of j(n,x).
  33. *
  34. * yn(n,x) is similar in all respects, except
  35. * that forward recursion is used for all
  36. * values of n>1.
  37. *
  38. */
  39. #include <openlibm_math.h>
  40. #include "math_private.h"
  41. static const double
  42. invsqrtpi= 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
  43. two = 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
  44. one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
  45. static const double zero = 0.00000000000000000000e+00;
  46. OLM_DLLEXPORT double
  47. __ieee754_jn(int n, double x)
  48. {
  49. int32_t i,hx,ix,lx, sgn;
  50. double a, b, temp, di;
  51. double z, w;
  52. /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
  53. * Thus, J(-n,x) = J(n,-x)
  54. */
  55. EXTRACT_WORDS(hx,lx,x);
  56. ix = 0x7fffffff&hx;
  57. /* if J(n,NaN) is NaN */
  58. if((ix|((u_int32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
  59. if(n<0){
  60. n = -n;
  61. x = -x;
  62. hx ^= 0x80000000;
  63. }
  64. if(n==0) return(__ieee754_j0(x));
  65. if(n==1) return(__ieee754_j1(x));
  66. sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
  67. x = fabs(x);
  68. if((ix|lx)==0||ix>=0x7ff00000) /* if x is 0 or inf */
  69. b = zero;
  70. else if((double)n<=x) {
  71. /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
  72. if(ix>=0x52D00000) { /* x > 2**302 */
  73. /* (x >> n**2)
  74. * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  75. * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  76. * Let s=sin(x), c=cos(x),
  77. * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
  78. *
  79. * n sin(xn)*sqt2 cos(xn)*sqt2
  80. * ----------------------------------
  81. * 0 s-c c+s
  82. * 1 -s-c -c+s
  83. * 2 -s+c -c-s
  84. * 3 s+c c-s
  85. */
  86. switch(n&3) {
  87. case 0: temp = cos(x)+sin(x); break;
  88. case 1: temp = -cos(x)+sin(x); break;
  89. case 2: temp = -cos(x)-sin(x); break;
  90. case 3: temp = cos(x)-sin(x); break;
  91. }
  92. b = invsqrtpi*temp/sqrt(x);
  93. } else {
  94. a = __ieee754_j0(x);
  95. b = __ieee754_j1(x);
  96. for(i=1;i<n;i++){
  97. temp = b;
  98. b = b*((double)(i+i)/x) - a; /* avoid underflow */
  99. a = temp;
  100. }
  101. }
  102. } else {
  103. if(ix<0x3e100000) { /* x < 2**-29 */
  104. /* x is tiny, return the first Taylor expansion of J(n,x)
  105. * J(n,x) = 1/n!*(x/2)^n - ...
  106. */
  107. if(n>33) /* underflow */
  108. b = zero;
  109. else {
  110. temp = x*0.5; b = temp;
  111. for (a=one,i=2;i<=n;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,v;
  148. double q0,q1,h,tmp; int32_t k,m;
  149. w = (n+n)/(double)x; h = 2.0/(double)x;
  150. q0 = w; z = w+h; q1 = w*z - 1.0; k=1;
  151. while(q1<1.0e9) {
  152. k += 1; z += h;
  153. tmp = z*q1 - q0;
  154. q0 = q1;
  155. q1 = tmp;
  156. }
  157. m = n+n;
  158. for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
  159. a = t;
  160. b = one;
  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 = two/x;
  171. tmp = tmp*__ieee754_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 -= two;
  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 -= two;
  187. /* scale b to avoid spurious overflow */
  188. if(b>1e100) {
  189. a /= b;
  190. t /= b;
  191. b = one;
  192. }
  193. }
  194. }
  195. z = __ieee754_j0(x);
  196. w = __ieee754_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; else return b;
  204. }
  205. OLM_DLLEXPORT double
  206. __ieee754_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|((u_int32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
  215. if((ix|lx)==0) return -one/zero;
  216. if(hx<0) return zero/zero;
  217. sign = 1;
  218. if(n<0){
  219. n = -n;
  220. sign = 1 - ((n&1)<<1);
  221. }
  222. if(n==0) return(__ieee754_y0(x));
  223. if(n==1) return(sign*__ieee754_y1(x));
  224. if(ix==0x7ff00000) return zero;
  225. if(ix>=0x52D00000) { /* x > 2**302 */
  226. /* (x >> n**2)
  227. * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  228. * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
  229. * Let s=sin(x), c=cos(x),
  230. * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
  231. *
  232. * n sin(xn)*sqt2 cos(xn)*sqt2
  233. * ----------------------------------
  234. * 0 s-c c+s
  235. * 1 -s-c -c+s
  236. * 2 -s+c -c-s
  237. * 3 s+c c-s
  238. */
  239. switch(n&3) {
  240. case 0: temp = sin(x)-cos(x); break;
  241. case 1: temp = -sin(x)-cos(x); break;
  242. case 2: temp = -sin(x)+cos(x); break;
  243. case 3: temp = sin(x)+cos(x); break;
  244. }
  245. b = invsqrtpi*temp/sqrt(x);
  246. } else {
  247. u_int32_t high;
  248. a = __ieee754_y0(x);
  249. b = __ieee754_y1(x);
  250. /* quit if b is -inf */
  251. GET_HIGH_WORD(high,b);
  252. for(i=1;i<n&&high!=0xfff00000;i++){
  253. temp = b;
  254. b = ((double)(i+i)/x)*b - a;
  255. GET_HIGH_WORD(high,b);
  256. a = temp;
  257. }
  258. }
  259. if(sign>0) return b; else return -b;
  260. }