j0f.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* j0f.c
  2. *
  3. * Bessel function of order zero
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, j0f();
  10. *
  11. * y = j0f( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns Bessel function of order zero of the argument.
  18. *
  19. * The domain is divided into the intervals [0, 2] and
  20. * (2, infinity). In the first interval the following polynomial
  21. * approximation is used:
  22. *
  23. *
  24. * 2 2 2
  25. * (w - r ) (w - r ) (w - r ) P(w)
  26. * 1 2 3
  27. *
  28. * 2
  29. * where w = x and the three r's are zeros of the function.
  30. *
  31. * In the second interval, the modulus and phase are approximated
  32. * by polynomials of the form Modulus(x) = sqrt(1/x) Q(1/x)
  33. * and Phase(x) = x + 1/x R(1/x^2) - pi/4. The function is
  34. *
  35. * j0(x) = Modulus(x) cos( Phase(x) ).
  36. *
  37. *
  38. *
  39. * ACCURACY:
  40. *
  41. * Absolute error:
  42. * arithmetic domain # trials peak rms
  43. * IEEE 0, 2 100000 1.3e-7 3.6e-8
  44. * IEEE 2, 32 100000 1.9e-7 5.4e-8
  45. *
  46. */
  47. /* y0f.c
  48. *
  49. * Bessel function of the second kind, order zero
  50. *
  51. *
  52. *
  53. * SYNOPSIS:
  54. *
  55. * float x, y, y0f();
  56. *
  57. * y = y0f( x );
  58. *
  59. *
  60. *
  61. * DESCRIPTION:
  62. *
  63. * Returns Bessel function of the second kind, of order
  64. * zero, of the argument.
  65. *
  66. * The domain is divided into the intervals [0, 2] and
  67. * (2, infinity). In the first interval a rational approximation
  68. * R(x) is employed to compute
  69. *
  70. * 2 2 2
  71. * y0(x) = (w - r ) (w - r ) (w - r ) R(x) + 2/pi ln(x) j0(x).
  72. * 1 2 3
  73. *
  74. * Thus a call to j0() is required. The three zeros are removed
  75. * from R(x) to improve its numerical stability.
  76. *
  77. * In the second interval, the modulus and phase are approximated
  78. * by polynomials of the form Modulus(x) = sqrt(1/x) Q(1/x)
  79. * and Phase(x) = x + 1/x S(1/x^2) - pi/4. Then the function is
  80. *
  81. * y0(x) = Modulus(x) sin( Phase(x) ).
  82. *
  83. *
  84. *
  85. *
  86. * ACCURACY:
  87. *
  88. * Absolute error, when y0(x) < 1; else relative error:
  89. *
  90. * arithmetic domain # trials peak rms
  91. * IEEE 0, 2 100000 2.4e-7 3.4e-8
  92. * IEEE 2, 32 100000 1.8e-7 5.3e-8
  93. *
  94. */
  95. /*
  96. Cephes Math Library Release 2.2: June, 1992
  97. Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier
  98. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  99. */
  100. #include <math.h>
  101. static float MO[8] = {
  102. -6.838999669318810E-002f,
  103. 1.864949361379502E-001f,
  104. -2.145007480346739E-001f,
  105. 1.197549369473540E-001f,
  106. -3.560281861530129E-003f,
  107. -4.969382655296620E-002f,
  108. -3.355424622293709E-006f,
  109. 7.978845717621440E-001f
  110. };
  111. static float PH[8] = {
  112. 3.242077816988247E+001f,
  113. -3.630592630518434E+001f,
  114. 1.756221482109099E+001f,
  115. -4.974978466280903E+000f,
  116. 1.001973420681837E+000f,
  117. -1.939906941791308E-001f,
  118. 6.490598792654666E-002f,
  119. -1.249992184872738E-001f
  120. };
  121. static float YP[5] = {
  122. 9.454583683980369E-008f,
  123. -9.413212653797057E-006f,
  124. 5.344486707214273E-004f,
  125. -1.584289289821316E-002f,
  126. 1.707584643733568E-001f
  127. };
  128. float YZ1 = 0.43221455686510834878f;
  129. float YZ2 = 22.401876406482861405f;
  130. float YZ3 = 64.130620282338755553f;
  131. static float DR1 = 5.78318596294678452118f;
  132. /*
  133. static float DR2 = 30.4712623436620863991;
  134. static float DR3 = 74.887006790695183444889;
  135. */
  136. static float JP[5] = {
  137. -6.068350350393235E-008f,
  138. 6.388945720783375E-006f,
  139. -3.969646342510940E-004f,
  140. 1.332913422519003E-002f,
  141. -1.729150680240724E-001f
  142. };
  143. extern float PIO4F;
  144. float polevlf(float, float *, int);
  145. float logf(float), sinf(float), cosf(float), sqrtf(float);
  146. float j0f( float xx )
  147. {
  148. float x, w, z, p, q, xn;
  149. if( xx < 0 )
  150. x = -xx;
  151. else
  152. x = xx;
  153. if( x <= 2.0f )
  154. {
  155. z = x * x;
  156. if( x < 1.0e-3f )
  157. return( 1.0f - 0.25f*z );
  158. p = (z-DR1) * polevlf( z, JP, 4);
  159. return( p );
  160. }
  161. q = 1.0f/x;
  162. w = sqrtf(q);
  163. p = w * polevlf( q, MO, 7);
  164. w = q*q;
  165. xn = q * polevlf( w, PH, 7) - PIO4F;
  166. p = p * cosf(xn + x);
  167. return(p);
  168. }
  169. /* y0() 2 */
  170. /* Bessel function of second kind, order zero */
  171. /* Rational approximation coefficients YP[] are used for x < 6.5.
  172. * The function computed is y0(x) - 2 ln(x) j0(x) / pi,
  173. * whose value at x = 0 is 2 * ( log(0.5) + EUL ) / pi
  174. * = 0.073804295108687225 , EUL is Euler's constant.
  175. */
  176. static float TWOOPI = 0.636619772367581343075535f; /* 2/pi */
  177. extern float MAXNUMF;
  178. float y0f( float xx )
  179. {
  180. float x, w, z, p, q, xn;
  181. x = xx;
  182. if( x <= 2.0f )
  183. {
  184. if( x <= 0.0f )
  185. {
  186. mtherr( "y0f", DOMAIN );
  187. return( -MAXNUMF );
  188. }
  189. z = x * x;
  190. /* w = (z-YZ1)*(z-YZ2)*(z-YZ3) * polevlf( z, YP, 4);*/
  191. w = (z-YZ1) * polevlf( z, YP, 4);
  192. w += TWOOPI * logf(x) * j0f(x);
  193. return( w );
  194. }
  195. q = 1.0f/x;
  196. w = sqrtf(q);
  197. p = w * polevlf( q, MO, 7);
  198. w = q*q;
  199. xn = q * polevlf( w, PH, 7) - PIO4F;
  200. p = p * sinf(xn + x);
  201. return( p );
  202. }