bessel_jy_asym.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (c) 2007 John Maddock
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // This is a partial header, do not include on it's own!!!
  7. //
  8. // Contains asymptotic expansions for Bessel J(v,x) and Y(v,x)
  9. // functions, as x -> INF.
  10. //
  11. #ifndef BOOST_MATH_SF_DETAIL_BESSEL_JY_ASYM_HPP
  12. #define BOOST_MATH_SF_DETAIL_BESSEL_JY_ASYM_HPP
  13. #ifdef _MSC_VER
  14. #pragma once
  15. #endif
  16. #include <boost/math/special_functions/factorials.hpp>
  17. namespace boost{ namespace math{ namespace detail{
  18. template <class T>
  19. inline T asymptotic_bessel_amplitude(T v, T x)
  20. {
  21. // Calculate the amplitude of J(v, x) and Y(v, x) for large
  22. // x: see A&S 9.2.28.
  23. BOOST_MATH_STD_USING
  24. T s = 1;
  25. T mu = 4 * v * v;
  26. T txq = 2 * x;
  27. txq *= txq;
  28. s += (mu - 1) / (2 * txq);
  29. s += 3 * (mu - 1) * (mu - 9) / (txq * txq * 8);
  30. s += 15 * (mu - 1) * (mu - 9) * (mu - 25) / (txq * txq * txq * 8 * 6);
  31. return sqrt(s * 2 / (constants::pi<T>() * x));
  32. }
  33. template <class T>
  34. T asymptotic_bessel_phase_mx(T v, T x)
  35. {
  36. //
  37. // Calculate the phase of J(v, x) and Y(v, x) for large x.
  38. // See A&S 9.2.29.
  39. // Note that the result returned is the phase less (x - PI(v/2 + 1/4))
  40. // which we'll factor in later when we calculate the sines/cosines of the result:
  41. //
  42. T mu = 4 * v * v;
  43. T denom = 4 * x;
  44. T denom_mult = denom * denom;
  45. T s = 0;
  46. s += (mu - 1) / (2 * denom);
  47. denom *= denom_mult;
  48. s += (mu - 1) * (mu - 25) / (6 * denom);
  49. denom *= denom_mult;
  50. s += (mu - 1) * (mu * mu - 114 * mu + 1073) / (5 * denom);
  51. denom *= denom_mult;
  52. s += (mu - 1) * (5 * mu * mu * mu - 1535 * mu * mu + 54703 * mu - 375733) / (14 * denom);
  53. return s;
  54. }
  55. template <class T, class Policy>
  56. inline T asymptotic_bessel_y_large_x_2(T v, T x, const Policy& pol)
  57. {
  58. // See A&S 9.2.19.
  59. BOOST_MATH_STD_USING
  60. // Get the phase and amplitude:
  61. T ampl = asymptotic_bessel_amplitude(v, x);
  62. T phase = asymptotic_bessel_phase_mx(v, x);
  63. BOOST_MATH_INSTRUMENT_VARIABLE(ampl);
  64. BOOST_MATH_INSTRUMENT_VARIABLE(phase);
  65. //
  66. // Calculate the sine of the phase, using
  67. // sine/cosine addition rules to factor in
  68. // the x - PI(v/2 + 1/4) term not added to the
  69. // phase when we calculated it.
  70. //
  71. T cx = cos(x);
  72. T sx = sin(x);
  73. T ci = boost::math::cos_pi(v / 2 + 0.25f, pol);
  74. T si = boost::math::sin_pi(v / 2 + 0.25f, pol);
  75. T sin_phase = sin(phase) * (cx * ci + sx * si) + cos(phase) * (sx * ci - cx * si);
  76. BOOST_MATH_INSTRUMENT_CODE(sin(phase));
  77. BOOST_MATH_INSTRUMENT_CODE(cos(x));
  78. BOOST_MATH_INSTRUMENT_CODE(cos(phase));
  79. BOOST_MATH_INSTRUMENT_CODE(sin(x));
  80. return sin_phase * ampl;
  81. }
  82. template <class T, class Policy>
  83. inline T asymptotic_bessel_j_large_x_2(T v, T x, const Policy& pol)
  84. {
  85. // See A&S 9.2.19.
  86. BOOST_MATH_STD_USING
  87. // Get the phase and amplitude:
  88. T ampl = asymptotic_bessel_amplitude(v, x);
  89. T phase = asymptotic_bessel_phase_mx(v, x);
  90. BOOST_MATH_INSTRUMENT_VARIABLE(ampl);
  91. BOOST_MATH_INSTRUMENT_VARIABLE(phase);
  92. //
  93. // Calculate the sine of the phase, using
  94. // sine/cosine addition rules to factor in
  95. // the x - PI(v/2 + 1/4) term not added to the
  96. // phase when we calculated it.
  97. //
  98. BOOST_MATH_INSTRUMENT_CODE(cos(phase));
  99. BOOST_MATH_INSTRUMENT_CODE(cos(x));
  100. BOOST_MATH_INSTRUMENT_CODE(sin(phase));
  101. BOOST_MATH_INSTRUMENT_CODE(sin(x));
  102. T cx = cos(x);
  103. T sx = sin(x);
  104. T ci = boost::math::cos_pi(v / 2 + 0.25f, pol);
  105. T si = boost::math::sin_pi(v / 2 + 0.25f, pol);
  106. T sin_phase = cos(phase) * (cx * ci + sx * si) - sin(phase) * (sx * ci - cx * si);
  107. BOOST_MATH_INSTRUMENT_VARIABLE(sin_phase);
  108. return sin_phase * ampl;
  109. }
  110. template <class T>
  111. inline bool asymptotic_bessel_large_x_limit(int v, const T& x)
  112. {
  113. BOOST_MATH_STD_USING
  114. //
  115. // Determines if x is large enough compared to v to take the asymptotic
  116. // forms above. From A&S 9.2.28 we require:
  117. // v < x * eps^1/8
  118. // and from A&S 9.2.29 we require:
  119. // v^12/10 < 1.5 * x * eps^1/10
  120. // using the former seems to work OK in practice with broadly similar
  121. // error rates either side of the divide for v < 10000.
  122. // At double precision eps^1/8 ~= 0.01.
  123. //
  124. BOOST_MATH_ASSERT(v >= 0);
  125. return (v ? v : 1) < x * 0.004f;
  126. }
  127. template <class T>
  128. inline bool asymptotic_bessel_large_x_limit(const T& v, const T& x)
  129. {
  130. BOOST_MATH_STD_USING
  131. //
  132. // Determines if x is large enough compared to v to take the asymptotic
  133. // forms above. From A&S 9.2.28 we require:
  134. // v < x * eps^1/8
  135. // and from A&S 9.2.29 we require:
  136. // v^12/10 < 1.5 * x * eps^1/10
  137. // using the former seems to work OK in practice with broadly similar
  138. // error rates either side of the divide for v < 10000.
  139. // At double precision eps^1/8 ~= 0.01.
  140. //
  141. return (std::max)(T(fabs(v)), T(1)) < x * sqrt(tools::forth_root_epsilon<T>());
  142. }
  143. template <class T, class Policy>
  144. void temme_asymptotic_y_small_x(T v, T x, T* Y, T* Y1, const Policy& pol)
  145. {
  146. T c = 1;
  147. T p = (v / boost::math::sin_pi(v, pol)) * pow(x / 2, -v) / boost::math::tgamma(1 - v, pol);
  148. T q = (v / boost::math::sin_pi(v, pol)) * pow(x / 2, v) / boost::math::tgamma(1 + v, pol);
  149. T f = (p - q) / v;
  150. T g_prefix = boost::math::sin_pi(v / 2, pol);
  151. g_prefix *= g_prefix * 2 / v;
  152. T g = f + g_prefix * q;
  153. T h = p;
  154. T c_mult = -x * x / 4;
  155. T y(c * g), y1(c * h);
  156. for(int k = 1; k < policies::get_max_series_iterations<Policy>(); ++k)
  157. {
  158. f = (k * f + p + q) / (k*k - v*v);
  159. p /= k - v;
  160. q /= k + v;
  161. c *= c_mult / k;
  162. T c1 = pow(-x * x / 4, T(k)) / factorial<T>(k, pol);
  163. g = f + g_prefix * q;
  164. h = -k * g + p;
  165. y += c * g;
  166. y1 += c * h;
  167. if(c * g / tools::epsilon<T>() < y)
  168. break;
  169. }
  170. *Y = -y;
  171. *Y1 = (-2 / x) * y1;
  172. }
  173. template <class T, class Policy>
  174. T asymptotic_bessel_i_large_x(T v, T x, const Policy& pol)
  175. {
  176. BOOST_MATH_STD_USING // ADL of std names
  177. T s = 1;
  178. T mu = 4 * v * v;
  179. T ex = 8 * x;
  180. T num = mu - 1;
  181. T denom = ex;
  182. s -= num / denom;
  183. num *= mu - 9;
  184. denom *= ex * 2;
  185. s += num / denom;
  186. num *= mu - 25;
  187. denom *= ex * 3;
  188. s -= num / denom;
  189. // Try and avoid overflow to the last minute:
  190. T e = exp(x/2);
  191. s = e * (e * s / sqrt(2 * x * constants::pi<T>()));
  192. return (boost::math::isfinite)(s) ?
  193. s : policies::raise_overflow_error<T>("boost::math::asymptotic_bessel_i_large_x<%1%>(%1%,%1%)", nullptr, pol);
  194. }
  195. }}} // namespaces
  196. #endif