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 = (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)
55 return factor * bessel_j0(x);
59 return factor * bessel_j1(x);
64 return static_cast<T>(0);
67 if(asymptotic_bessel_large_x_limit(T(n), x))
68 return factor * asymptotic_bessel_j_large_x_2<T>(n, x);
72 if (n < abs(x)) // forward recurrence
75 current = bessel_j1(x);
76 policies::check_series_iterations<T>("boost::math::bessel_j_n<%1%>(%1%,%1%)", n, pol);
77 for (int k = 1; k < n; k++)
81 // rescale if we would overflow or underflow:
83 if((fabs(fact) > 1) && ((tools::max_value<T>() - fabs(prev)) / fabs(fact) < fabs(current)))
89 value = fact * current - prev;
94 else if((x < 1) || (n > x * x / 4) || (x < 5))
96 return factor * bessel_j_small_z_series(T(n), x, pol);
98 else // backward recurrence
100 T fn; int s; // fn = J_(n+1) / J_n
101 // |x| <= n, fast convergence for continued fraction CF1
102 boost::math::detail::CF1_jy(static_cast<T>(n), x, &fn, &s, pol);
105 // Check recursion won't go on too far:
106 policies::check_series_iterations<T>("boost::math::bessel_j_n<%1%>(%1%,%1%)", n, pol);
107 for (int k = n; k > 0; k--)
110 if((fabs(fact) > 1) && ((tools::max_value<T>() - fabs(prev)) / fabs(fact) < fabs(current)))
116 next = fact * current - prev;
120 value = bessel_j0(x) / current; // normalization
125 if(tools::max_value<T>() * scale < fabs(value))
126 return policies::raise_overflow_error<T>("boost::math::bessel_jn<%1%>(%1%,%1%)", 0, pol);
128 return value / scale;
133 #endif // BOOST_MATH_BESSEL_JN_HPP