bessel_jn.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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);
  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_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. T fact = 2 * k / x;
  69. //
  70. // rescale if we would overflow or underflow:
  71. //
  72. if((fabs(fact) > 1) && ((tools::max_value<T>() - fabs(prev)) / fabs(fact) < fabs(current)))
  73. {
  74. scale /= current;
  75. prev /= current;
  76. current = 1;
  77. }
  78. value = fact * current - prev;
  79. prev = current;
  80. current = value;
  81. }
  82. }
  83. else if((x < 1) || (n > x * x / 4) || (x < 5))
  84. {
  85. return factor * bessel_j_small_z_series(T(n), x, pol);
  86. }
  87. else // backward recurrence
  88. {
  89. T fn; int s; // fn = J_(n+1) / J_n
  90. // |x| <= n, fast convergence for continued fraction CF1
  91. boost::math::detail::CF1_jy(static_cast<T>(n), x, &fn, &s, pol);
  92. prev = fn;
  93. current = 1;
  94. // Check recursion won't go on too far:
  95. policies::check_series_iterations<T>("boost::math::bessel_j_n<%1%>(%1%,%1%)", n, pol);
  96. for (int k = n; k > 0; k--)
  97. {
  98. T fact = 2 * k / x;
  99. if((fabs(fact) > 1) && ((tools::max_value<T>() - fabs(prev)) / fabs(fact) < fabs(current)))
  100. {
  101. prev /= current;
  102. scale /= current;
  103. current = 1;
  104. }
  105. next = fact * current - prev;
  106. prev = current;
  107. current = next;
  108. }
  109. value = bessel_j0(x) / current; // normalization
  110. scale = 1 / scale;
  111. }
  112. value *= factor;
  113. if(tools::max_value<T>() * scale < fabs(value))
  114. return policies::raise_overflow_error<T>("boost::math::bessel_jn<%1%>(%1%,%1%)", 0, pol);
  115. return value / scale;
  116. }
  117. }}} // namespaces
  118. #endif // BOOST_MATH_BESSEL_JN_HPP