bessel_jn.hpp 3.8 KB

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