bessel_j0.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_J0_HPP
  6. #define BOOST_MATH_BESSEL_J0_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/constants/constants.hpp>
  11. #include <boost/math/tools/rational.hpp>
  12. #include <boost/math/tools/big_constant.hpp>
  13. #include <boost/math/tools/assert.hpp>
  14. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  15. //
  16. // This is the only way we can avoid
  17. // warning: non-standard suffix on floating constant [-Wpedantic]
  18. // when building with -Wall -pedantic. Neither __extension__
  19. // nor #pragma diagnostic ignored work :(
  20. //
  21. #pragma GCC system_header
  22. #endif
  23. // Bessel function of the first kind of order zero
  24. // x <= 8, minimax rational approximations on root-bracketing intervals
  25. // x > 8, Hankel asymptotic expansion in Hart, Computer Approximations, 1968
  26. namespace boost { namespace math { namespace detail{
  27. template <typename T>
  28. T bessel_j0(T x);
  29. template <typename T>
  30. T bessel_j0(T x)
  31. {
  32. #ifdef BOOST_MATH_INSTRUMENT
  33. static bool b = false;
  34. if (!b)
  35. {
  36. std::cout << "bessel_j0 called with " << typeid(x).name() << std::endl;
  37. std::cout << "double = " << typeid(double).name() << std::endl;
  38. std::cout << "long double = " << typeid(long double).name() << std::endl;
  39. b = true;
  40. }
  41. #endif
  42. static const T P1[] = {
  43. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -4.1298668500990866786e+11)),
  44. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.7282507878605942706e+10)),
  45. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -6.2140700423540120665e+08)),
  46. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.6302997904833794242e+06)),
  47. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -3.6629814655107086448e+04)),
  48. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0344222815443188943e+02)),
  49. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2117036164593528341e-01))
  50. };
  51. static const T Q1[] = {
  52. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.3883787996332290397e+12)),
  53. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.6328198300859648632e+10)),
  54. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.3985097372263433271e+08)),
  55. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.5612696224219938200e+05)),
  56. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 9.3614022392337710626e+02)),
  57. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)),
  58. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.0))
  59. };
  60. static const T P2[] = {
  61. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.8319397969392084011e+03)),
  62. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2254078161378989535e+04)),
  63. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -7.2879702464464618998e+03)),
  64. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0341910641583726701e+04)),
  65. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.1725046279757103576e+04)),
  66. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.4176707025325087628e+03)),
  67. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.4321196680624245801e+02)),
  68. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.8591703355916499363e+01))
  69. };
  70. static const T Q2[] = {
  71. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -3.5783478026152301072e+05)),
  72. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.4599102262586308984e+05)),
  73. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.4055062591169562211e+04)),
  74. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.8680990008359188352e+04)),
  75. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.9458766545509337327e+03)),
  76. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.3307310774649071172e+02)),
  77. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.5258076240801555057e+01)),
  78. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0))
  79. };
  80. static const T PC[] = {
  81. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2779090197304684302e+04)),
  82. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.1345386639580765797e+04)),
  83. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1170523380864944322e+04)),
  84. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4806486443249270347e+03)),
  85. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.5376201909008354296e+02)),
  86. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.8961548424210455236e-01))
  87. };
  88. static const T QC[] = {
  89. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2779090197304684318e+04)),
  90. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.1370412495510416640e+04)),
  91. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1215350561880115730e+04)),
  92. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.5028735138235608207e+03)),
  93. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.5711159858080893649e+02)),
  94. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0))
  95. };
  96. static const T PS[] = {
  97. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.9226600200800094098e+01)),
  98. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.8591953644342993800e+02)),
  99. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.1183429920482737611e+02)),
  100. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2300261666214198472e+01)),
  101. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2441026745835638459e+00)),
  102. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.8033303048680751817e-03))
  103. };
  104. static const T QS[] = {
  105. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.7105024128512061905e+03)),
  106. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.1951131543434613647e+04)),
  107. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.2642780169211018836e+03)),
  108. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.4887231232283756582e+03)),
  109. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 9.0593769594993125859e+01)),
  110. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0))
  111. };
  112. static const T x1 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.4048255576957727686e+00)),
  113. x2 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.5200781102863106496e+00)),
  114. x11 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.160e+02)),
  115. x12 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.42444230422723137837e-03)),
  116. x21 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.4130e+03)),
  117. x22 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.46860286310649596604e-04));
  118. T value, factor, r, rc, rs;
  119. BOOST_MATH_STD_USING
  120. using namespace boost::math::tools;
  121. using namespace boost::math::constants;
  122. BOOST_MATH_ASSERT(x >= 0); // reflection handled elsewhere.
  123. if (x == 0)
  124. {
  125. return static_cast<T>(1);
  126. }
  127. if (x <= 4) // x in (0, 4]
  128. {
  129. T y = x * x;
  130. BOOST_MATH_ASSERT(sizeof(P1) == sizeof(Q1));
  131. r = evaluate_rational(P1, Q1, y);
  132. factor = (x + x1) * ((x - x11/256) - x12);
  133. value = factor * r;
  134. }
  135. else if (x <= 8.0) // x in (4, 8]
  136. {
  137. T y = 1 - (x * x)/64;
  138. BOOST_MATH_ASSERT(sizeof(P2) == sizeof(Q2));
  139. r = evaluate_rational(P2, Q2, y);
  140. factor = (x + x2) * ((x - x21/256) - x22);
  141. value = factor * r;
  142. }
  143. else // x in (8, \infty)
  144. {
  145. T y = 8 / x;
  146. T y2 = y * y;
  147. BOOST_MATH_ASSERT(sizeof(PC) == sizeof(QC));
  148. BOOST_MATH_ASSERT(sizeof(PS) == sizeof(QS));
  149. rc = evaluate_rational(PC, QC, y2);
  150. rs = evaluate_rational(PS, QS, y2);
  151. factor = constants::one_div_root_pi<T>() / sqrt(x);
  152. //
  153. // What follows is really just:
  154. //
  155. // T z = x - pi/4;
  156. // value = factor * (rc * cos(z) - y * rs * sin(z));
  157. //
  158. // But using the addition formulae for sin and cos, plus
  159. // the special values for sin/cos of pi/4.
  160. //
  161. T sx = sin(x);
  162. T cx = cos(x);
  163. BOOST_MATH_INSTRUMENT_VARIABLE(rc);
  164. BOOST_MATH_INSTRUMENT_VARIABLE(rs);
  165. BOOST_MATH_INSTRUMENT_VARIABLE(factor);
  166. BOOST_MATH_INSTRUMENT_VARIABLE(sx);
  167. BOOST_MATH_INSTRUMENT_VARIABLE(cx);
  168. value = factor * (rc * (cx + sx) - y * rs * (sx - cx));
  169. }
  170. return value;
  171. }
  172. }}} // namespaces
  173. #endif // BOOST_MATH_BESSEL_J0_HPP