1 // Copyright (c) 2013 Anton Bikineev
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_JY_DERIVATIVES_SERIES_HPP
7 #define BOOST_MATH_BESSEL_JY_DERIVATIVES_SERIES_HPP
13 namespace boost{ namespace math{ namespace detail{
15 template <class T, class Policy>
16 struct bessel_j_derivative_small_z_series_term
18 typedef T result_type;
20 bessel_j_derivative_small_z_series_term(T v_, T x)
21 : N(0), v(v_), term(1), mult(x / 2)
24 // iterate if v == 0; otherwise result of
25 // first term is 0 and tools::sum_series stops
31 T r = term * (v + 2 * N);
39 term *= mult / (N * (N + v));
47 // Series evaluation for BesselJ'(v, z) as z -> 0.
48 // It's derivative of http://functions.wolfram.com/Bessel-TypeFunctions/BesselJ/06/01/04/01/01/0003/
49 // Converges rapidly for all z << v.
51 template <class T, class Policy>
52 inline T bessel_j_derivative_small_z_series(T v, T x, const Policy& pol)
56 if (v < boost::math::max_factorial<T>::value)
58 prefix = pow(x / 2, v - 1) / 2 / boost::math::tgamma(v + 1, pol);
62 prefix = (v - 1) * log(x / 2) - constants::ln_two<T>() - boost::math::lgamma(v + 1, pol);
68 bessel_j_derivative_small_z_series_term<T, Policy> s(v, x);
69 boost::uintmax_t max_iter = boost::math::policies::get_max_series_iterations<Policy>();
70 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
72 T result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, zero);
74 T result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter);
76 boost::math::policies::check_series_iterations<T>("boost::math::bessel_j_derivative_small_z_series<%1%>(%1%,%1%)", max_iter, pol);
77 return prefix * result;
80 template <class T, class Policy>
81 struct bessel_y_derivative_small_z_series_term_a
83 typedef T result_type;
85 bessel_y_derivative_small_z_series_term_a(T v_, T x)
94 T r = term * (-v + 2 * N);
96 term *= mult / (N * (N - v));
106 template <class T, class Policy>
107 struct bessel_y_derivative_small_z_series_term_b
109 typedef T result_type;
111 bessel_y_derivative_small_z_series_term_b(T v_, T x)
120 T r = term * (v + 2 * N);
122 term *= mult / (N * (N + v));
132 // Series form for BesselY' as z -> 0,
133 // It's derivative of http://functions.wolfram.com/Bessel-TypeFunctions/BesselY/06/01/04/01/01/0003/
134 // This series is only useful when the second term is small compared to the first
135 // otherwise we get catestrophic cancellation errors.
137 // Approximating tgamma(v) by v^v, and assuming |tgamma(-z)| < eps we end up requiring:
138 // eps/2 * v^v(x/2)^-v > (x/2)^v or log(eps/2) > v log((x/2)^2/v)
140 template <class T, class Policy>
141 inline T bessel_y_derivative_small_z_series(T v, T x, const Policy& pol)
144 static const char* function = "bessel_y_derivative_small_z_series<%1%>(%1%,%1%)";
149 bool need_logs = (v >= boost::math::max_factorial<T>::value) || (boost::math::tools::log_max_value<T>() / v < fabs(p));
152 gam = boost::math::tgamma(v, pol);
153 p = pow(x / 2, v + 1) * 2;
154 if (boost::math::tools::max_value<T>() * p < gam)
158 if (boost::math::tools::max_value<T>() * p < gam)
160 // This term will overflow to -INF, when combined with the series below it becomes +INF:
161 return boost::math::policies::raise_overflow_error<T>(function, 0, pol);
164 prefix = -gam / (boost::math::constants::pi<T>() * p);
168 gam = boost::math::lgamma(v, pol);
169 p = (v + 1) * p + constants::ln_two<T>();
170 prefix = gam - log(boost::math::constants::pi<T>()) - p;
171 if (boost::math::tools::log_max_value<T>() < prefix)
173 prefix -= log(boost::math::tools::max_value<T>() / 4);
174 scale /= (boost::math::tools::max_value<T>() / 4);
175 if (boost::math::tools::log_max_value<T>() < prefix)
177 return boost::math::policies::raise_overflow_error<T>(function, 0, pol);
180 prefix = -exp(prefix);
182 bessel_y_derivative_small_z_series_term_a<T, Policy> s(v, x);
183 boost::uintmax_t max_iter = boost::math::policies::get_max_series_iterations<Policy>();
184 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
186 T result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, zero);
188 T result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter);
190 boost::math::policies::check_series_iterations<T>("boost::math::bessel_y_derivative_small_z_series<%1%>(%1%,%1%)", max_iter, pol);
193 p = pow(x / 2, v - 1) / 2;
196 prefix = boost::math::tgamma(-v, pol) * boost::math::cos_pi(v) * p / boost::math::constants::pi<T>();
201 prefix = boost::math::lgamma(-v, &sgn, pol) + (v - 1) * log(x / 2) - constants::ln_two<T>();
202 prefix = exp(prefix) * sgn / boost::math::constants::pi<T>();
204 bessel_y_derivative_small_z_series_term_b<T, Policy> s2(v, x);
205 max_iter = boost::math::policies::get_max_series_iterations<Policy>();
206 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
207 T b = boost::math::tools::sum_series(s2, boost::math::policies::get_epsilon<T, Policy>(), max_iter, zero);
209 T b = boost::math::tools::sum_series(s2, boost::math::policies::get_epsilon<T, Policy>(), max_iter);
211 result += scale * prefix * b;
215 // Calculating of BesselY'(v,x) with small x (x < epsilon) and integer x using derivatives
216 // of formulas in http://functions.wolfram.com/Bessel-TypeFunctions/BesselY/06/01/04/01/02/
217 // seems to lose precision. Instead using linear combination of regular Bessel is preferred.
221 #endif // BOOST_MATH_BESSEL_JY_DERIVATVIES_SERIES_HPP