bessel_y0.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (c) 2006 Xiaogang Zhang
  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. #ifndef BOOST_MATH_BESSEL_Y0_HPP
  6. #define BOOST_MATH_BESSEL_Y0_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #pragma warning(push)
  10. #pragma warning(disable:4702) // Unreachable code (release mode only warning)
  11. #endif
  12. #include <boost/math/special_functions/detail/bessel_j0.hpp>
  13. #include <boost/math/constants/constants.hpp>
  14. #include <boost/math/tools/rational.hpp>
  15. #include <boost/math/tools/big_constant.hpp>
  16. #include <boost/math/policies/error_handling.hpp>
  17. #include <boost/math/tools/assert.hpp>
  18. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  19. //
  20. // This is the only way we can avoid
  21. // warning: non-standard suffix on floating constant [-Wpedantic]
  22. // when building with -Wall -pedantic. Neither __extension__
  23. // nor #pragma diagnostic ignored work :(
  24. //
  25. #pragma GCC system_header
  26. #endif
  27. // Bessel function of the second kind of order zero
  28. // x <= 8, minimax rational approximations on root-bracketing intervals
  29. // x > 8, Hankel asymptotic expansion in Hart, Computer Approximations, 1968
  30. namespace boost { namespace math { namespace detail{
  31. template <typename T, typename Policy>
  32. T bessel_y0(T x, const Policy&);
  33. template <typename T, typename Policy>
  34. T bessel_y0(T x, const Policy&)
  35. {
  36. static const T P1[] = {
  37. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0723538782003176831e+11)),
  38. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.3716255451260504098e+09)),
  39. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.0422274357376619816e+08)),
  40. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.1287548474401797963e+06)),
  41. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0102532948020907590e+04)),
  42. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.8402381979244993524e+01)),
  43. };
  44. static const T Q1[] = {
  45. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.8873865738997033405e+11)),
  46. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.1617187777290363573e+09)),
  47. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.5662956624278251596e+07)),
  48. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.3889393209447253406e+05)),
  49. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.6475986689240190091e+02)),
  50. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)),
  51. };
  52. static const T P2[] = {
  53. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2213976967566192242e+13)),
  54. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -5.5107435206722644429e+11)),
  55. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.3600098638603061642e+10)),
  56. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -6.9590439394619619534e+08)),
  57. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.6905288611678631510e+06)),
  58. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.4566865832663635920e+04)),
  59. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.7427031242901594547e+01)),
  60. };
  61. static const T Q2[] = {
  62. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.3386146580707264428e+14)),
  63. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.4266824419412347550e+12)),
  64. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4015103849971240096e+10)),
  65. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.3960202770986831075e+08)),
  66. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.0669982352539552018e+05)),
  67. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.3030857612070288823e+02)),
  68. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)),
  69. };
  70. static const T P3[] = {
  71. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.0728726905150210443e+15)),
  72. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.7016641869173237784e+14)),
  73. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2829912364088687306e+11)),
  74. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.9363051266772083678e+11)),
  75. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1958827170518100757e+09)),
  76. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.0085539923498211426e+07)),
  77. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1363534169313901632e+04)),
  78. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.7439661319197499338e+01)),
  79. };
  80. static const T Q3[] = {
  81. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4563724628846457519e+17)),
  82. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.9272425569640309819e+15)),
  83. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2598377924042897629e+13)),
  84. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.6926121104209825246e+10)),
  85. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.4727219475672302327e+08)),
  86. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.3924739209768057030e+05)),
  87. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.7903362168128450017e+02)),
  88. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)),
  89. };
  90. static const T PC[] = {
  91. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2779090197304684302e+04)),
  92. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.1345386639580765797e+04)),
  93. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1170523380864944322e+04)),
  94. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4806486443249270347e+03)),
  95. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.5376201909008354296e+02)),
  96. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.8961548424210455236e-01)),
  97. };
  98. static const T QC[] = {
  99. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2779090197304684318e+04)),
  100. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.1370412495510416640e+04)),
  101. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1215350561880115730e+04)),
  102. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.5028735138235608207e+03)),
  103. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.5711159858080893649e+02)),
  104. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)),
  105. };
  106. static const T PS[] = {
  107. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.9226600200800094098e+01)),
  108. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.8591953644342993800e+02)),
  109. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.1183429920482737611e+02)),
  110. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2300261666214198472e+01)),
  111. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2441026745835638459e+00)),
  112. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.8033303048680751817e-03)),
  113. };
  114. static const T QS[] = {
  115. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.7105024128512061905e+03)),
  116. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.1951131543434613647e+04)),
  117. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.2642780169211018836e+03)),
  118. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.4887231232283756582e+03)),
  119. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 9.0593769594993125859e+01)),
  120. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)),
  121. };
  122. static const T x1 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.9357696627916752158e-01)),
  123. x2 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.9576784193148578684e+00)),
  124. x3 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.0860510603017726976e+00)),
  125. x11 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.280e+02)),
  126. x12 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.9519662791675215849e-03)),
  127. x21 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0130e+03)),
  128. x22 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.4716931485786837568e-04)),
  129. x31 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.8140e+03)),
  130. x32 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.1356030177269762362e-04))
  131. ;
  132. T value, factor, r, rc, rs;
  133. BOOST_MATH_STD_USING
  134. using namespace boost::math::tools;
  135. using namespace boost::math::constants;
  136. BOOST_MATH_ASSERT(x > 0);
  137. if (x <= 3) // x in (0, 3]
  138. {
  139. T y = x * x;
  140. T z = 2 * log(x/x1) * bessel_j0(x) / pi<T>();
  141. r = evaluate_rational(P1, Q1, y);
  142. factor = (x + x1) * ((x - x11/256) - x12);
  143. value = z + factor * r;
  144. }
  145. else if (x <= 5.5f) // x in (3, 5.5]
  146. {
  147. T y = x * x;
  148. T z = 2 * log(x/x2) * bessel_j0(x) / pi<T>();
  149. r = evaluate_rational(P2, Q2, y);
  150. factor = (x + x2) * ((x - x21/256) - x22);
  151. value = z + factor * r;
  152. }
  153. else if (x <= 8) // x in (5.5, 8]
  154. {
  155. T y = x * x;
  156. T z = 2 * log(x/x3) * bessel_j0(x) / pi<T>();
  157. r = evaluate_rational(P3, Q3, y);
  158. factor = (x + x3) * ((x - x31/256) - x32);
  159. value = z + factor * r;
  160. }
  161. else // x in (8, \infty)
  162. {
  163. T y = 8 / x;
  164. T y2 = y * y;
  165. rc = evaluate_rational(PC, QC, y2);
  166. rs = evaluate_rational(PS, QS, y2);
  167. factor = constants::one_div_root_pi<T>() / sqrt(x);
  168. //
  169. // The following code is really just:
  170. //
  171. // T z = x - 0.25f * pi<T>();
  172. // value = factor * (rc * sin(z) + y * rs * cos(z));
  173. //
  174. // But using the sin/cos addition formulae and constant values for
  175. // sin/cos of PI/4 which then cancel part of the "factor" term as they're all
  176. // 1 / sqrt(2):
  177. //
  178. T sx = sin(x);
  179. T cx = cos(x);
  180. value = factor * (rc * (sx - cx) + y * rs * (cx + sx));
  181. }
  182. return value;
  183. }
  184. }}} // namespaces
  185. #ifdef _MSC_VER
  186. #pragma warning(pop)
  187. #endif
  188. #endif // BOOST_MATH_BESSEL_Y0_HPP