bessel_jn.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_JN_HPP
  6. #define BOOST_MATH_BESSEL_JN_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/detail/bessel_j0.hpp>
  11. #include <boost/math/special_functions/detail/bessel_j1.hpp>
  12. #include <boost/math/special_functions/detail/bessel_jy.hpp>
  13. #include <boost/math/special_functions/detail/bessel_jy_asym.hpp>
  14. #include <boost/math/special_functions/detail/bessel_jy_series.hpp>
  15. // Bessel function of the first kind of integer order
  16. // J_n(z) is the minimal solution
  17. // n < abs(z), forward recurrence stable and usable
  18. // n >= abs(z), forward recurrence unstable, use Miller's algorithm
  19. namespace boost { namespace math { namespace detail{
  20. template <typename T, typename Policy>
  21. T bessel_jn(int n, T x, const Policy& pol)
  22. {
  23. T value(0), factor, current, prev, next;
  24. BOOST_MATH_STD_USING
  25. //
  26. // Reflection has to come first:
  27. //
  28. if (n < 0)
  29. {
  30. factor = static_cast<T>((n & 0x1) ? -1 : 1); // J_{-n}(z) = (-1)^n J_n(z)
  31. n = -n;
  32. }
  33. else
  34. {
  35. factor = 1;
  36. }
  37. if(x < 0)
  38. {
  39. factor *= (n & 0x1) ? -1 : 1; // J_{n}(-z) = (-1)^n J_n(z)
  40. x = -x;
  41. }
  42. //
  43. // Special cases:
  44. //
  45. if(asymptotic_bessel_large_x_limit(T(n), x))
  46. return factor * asymptotic_bessel_j_large_x_2<T>(T(n), x, pol);
  47. if (n == 0)
  48. {
  49. return factor * bessel_j0(x);
  50. }
  51. if (n == 1)
  52. {
  53. return factor * bessel_j1(x);
  54. }
  55. if (x == 0) // n >= 2
  56. {
  57. return static_cast<T>(0);
  58. }
  59. BOOST_MATH_ASSERT(n > 1);
  60. T scale = 1;
  61. if (n < abs(x)) // forward recurrence
  62. {
  63. prev = bessel_j0(x);
  64. current = bessel_j1(x);
  65. policies::check_series_iterations<T>("boost::math::bessel_j_n<%1%>(%1%,%1%)", n, pol);
  66. for (int k = 1; k < n; k++)
  67. {
  68. value = (2 * k * current / x) - prev;
  69. prev = current;
  70. current = value;
  71. }
  72. }
  73. else if((x < 1) || (n > x * x / 4) || (x < 5))
  74. {
  75. return factor * bessel_j_small_z_series(T(n), x, pol);
  76. }
  77. else // backward recurrence
  78. {
  79. T fn; int s; // fn = J_(n+1) / J_n
  80. // |x| <= n, fast convergence for continued fraction CF1
  81. boost::math::detail::CF1_jy(static_cast<T>(n), x, &fn, &s, pol);
  82. prev = fn;
  83. current = 1;
  84. // Check recursion won't go on too far:
  85. policies::check_series_iterations<T>("boost::math::bessel_j_n<%1%>(%1%,%1%)", n, pol);
  86. for (int k = n; k > 0; k--)
  87. {
  88. T fact = 2 * k / x;
  89. if((fabs(fact) > 1) && ((tools::max_value<T>() - fabs(prev)) / fabs(fact) < fabs(current)))
  90. {
  91. prev /= current;
  92. scale /= current;
  93. current = 1;
  94. }
  95. next = fact * current - prev;
  96. prev = current;
  97. current = next;
  98. }
  99. value = bessel_j0(x) / current; // normalization
  100. scale = 1 / scale;
  101. }
  102. value *= factor;
  103. if(tools::max_value<T>() * scale < fabs(value))
  104. return policies::raise_overflow_error<T>("boost::math::bessel_jn<%1%>(%1%,%1%)", nullptr, pol); // LCOV_EXCL_LINE we should never get here!
  105. return value / scale;
  106. }
  107. }}} // namespaces
  108. #endif // BOOST_MATH_BESSEL_JN_HPP