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)
6 #ifndef BOOST_MATH_BESSEL_JN_HPP
7 #define BOOST_MATH_BESSEL_JN_HPP
13 #include <boost/math/special_functions/detail/bessel_j0.hpp>
14 #include <boost/math/special_functions/detail/bessel_j1.hpp>
15 #include <boost/math/special_functions/detail/bessel_jy.hpp>
16 #include <boost/math/special_functions/detail/bessel_jy_asym.hpp>
17 #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
24 namespace boost { namespace math { namespace detail{
26 template <typename T, typename Policy>
27 T bessel_jn(int n, T x, const Policy& pol)
29 T value(0), factor, current, prev, next;
34 // Reflection has to come first:
38 factor = static_cast<T>((n & 0x1) ? -1 : 1); // J_{-n}(z) = (-1)^n J_n(z)
47 factor *= (n & 0x1) ? -1 : 1; // J_{n}(-z) = (-1)^n J_n(z)
53 if(asymptotic_bessel_large_x_limit(T(n), x))
54 return factor * asymptotic_bessel_j_large_x_2<T>(T(n), x);
57 return factor * bessel_j0(x);
61 return factor * bessel_j1(x);
66 return static_cast<T>(0);
71 if (n < abs(x)) // forward recurrence
74 current = bessel_j1(x);
75 policies::check_series_iterations<T>("boost::math::bessel_j_n<%1%>(%1%,%1%)", n, pol);
76 for (int k = 1; k < n; k++)
80 // rescale if we would overflow or underflow:
82 if((fabs(fact) > 1) && ((tools::max_value<T>() - fabs(prev)) / fabs(fact) < fabs(current)))
88 value = fact * current - prev;
93 else if((x < 1) || (n > x * x / 4) || (x < 5))
95 return factor * bessel_j_small_z_series(T(n), x, pol);
97 else // backward recurrence
99 T fn; int s; // fn = J_(n+1) / J_n
100 // |x| <= n, fast convergence for continued fraction CF1
101 boost::math::detail::CF1_jy(static_cast<T>(n), x, &fn, &s, pol);
104 // Check recursion won't go on too far:
105 policies::check_series_iterations<T>("boost::math::bessel_j_n<%1%>(%1%,%1%)", n, pol);
106 for (int k = n; k > 0; k--)
109 if((fabs(fact) > 1) && ((tools::max_value<T>() - fabs(prev)) / fabs(fact) < fabs(current)))
115 next = fact * current - prev;
119 value = bessel_j0(x) / current; // normalization
124 if(tools::max_value<T>() * scale < fabs(value))
125 return policies::raise_overflow_error<T>("boost::math::bessel_jn<%1%>(%1%,%1%)", 0, pol);
127 return value / scale;
132 #endif // BOOST_MATH_BESSEL_JN_HPP