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_DERIVATIVES_HPP
7 #define BOOST_MATH_BESSEL_DERIVATIVES_HPP
13 #include <boost/math/special_functions/math_fwd.hpp>
14 #include <boost/math/special_functions/bessel.hpp>
15 #include <boost/math/special_functions/detail/bessel_jy_derivatives_asym.hpp>
16 #include <boost/math/special_functions/detail/bessel_jy_derivatives_series.hpp>
17 #include <boost/math/special_functions/detail/bessel_derivatives_linear.hpp>
19 namespace boost{ namespace math{
23 template <class Tag, class T, class Policy>
24 inline T cyl_bessel_j_prime_imp(T v, T x, const Policy& pol)
26 static const char* const function = "boost::math::cyl_bessel_j_prime<%1%>(%1%,%1%)";
29 // Prevent complex result:
31 if (x < 0 && floor(v) != v)
32 return boost::math::policies::raise_domain_error<T>(
34 "Got x = %1%, but function requires x >= 0", x, pol);
36 // Special cases for x == 0:
44 else if (floor(v) == v || v > 1)
46 else return boost::math::policies::raise_domain_error<T>(
48 "Got x = %1%, but function is indeterminate for this order", x, pol);
51 // Special case for large x: use asymptotic expansion:
53 if (boost::math::detail::asymptotic_bessel_derivative_large_x_limit(v, x))
54 return boost::math::detail::asymptotic_bessel_j_derivative_large_x_2(v, x);
56 // Special case for small x: use Taylor series:
58 if ((abs(x) < 5) || (abs(v) > x * x / 4))
60 bool inversed = false;
61 if (floor(v) == v && v < 0)
64 if (itrunc(v, pol) & 1)
67 T r = boost::math::detail::bessel_j_derivative_small_z_series(v, x, pol);
68 return inversed ? T(-r) : r;
71 // Special case for v == 0:
74 return -boost::math::detail::cyl_bessel_j_imp<T>(1, x, Tag(), pol);
78 return boost::math::detail::bessel_j_derivative_linear(v, x, Tag(), pol);
81 template <class T, class Policy>
82 inline T sph_bessel_j_prime_imp(unsigned v, T x, const Policy& pol)
84 static const char* const function = "boost::math::sph_bessel_prime<%1%>(%1%,%1%)";
86 // Prevent complex result:
89 return boost::math::policies::raise_domain_error<T>(
91 "Got x = %1%, but function requires x >= 0.", x, pol);
93 // Special case for v == 0:
96 return (x == 0) ? boost::math::policies::raise_overflow_error<T>(function, 0, pol)
97 : static_cast<T>(-boost::math::detail::sph_bessel_j_imp<T>(1, x, pol));
99 // Special case for x == 0 and v > 0:
102 return boost::math::policies::raise_domain_error<T>(
104 "Got x = %1%, but function is indeterminate for this order", x, pol);
108 return boost::math::detail::sph_bessel_j_derivative_linear(v, x, pol);
111 template <class T, class Policy>
112 inline T cyl_bessel_i_prime_imp(T v, T x, const Policy& pol)
114 static const char* const function = "boost::math::cyl_bessel_i_prime<%1%>(%1%,%1%)";
117 // Prevent complex result:
119 if (x < 0 && floor(v) != v)
120 return boost::math::policies::raise_domain_error<T>(
122 "Got x = %1%, but function requires x >= 0", x, pol);
124 // Special cases for x == 0:
128 if (v == 1 || v == -1)
130 else if (floor(v) == v || v > 1)
132 else return boost::math::policies::raise_domain_error<T>(
134 "Got x = %1%, but function is indeterminate for this order", x, pol);
137 // Special case for v == 0:
140 return boost::math::detail::cyl_bessel_i_imp<T>(1, x, pol);
144 return boost::math::detail::bessel_i_derivative_linear(v, x, pol);
147 template <class Tag, class T, class Policy>
148 inline T cyl_bessel_k_prime_imp(T v, T x, const Policy& pol)
151 // Prevent complex and indeterminate results:
154 return boost::math::policies::raise_domain_error<T>(
155 "boost::math::cyl_bessel_k_prime<%1%>(%1%,%1%)",
156 "Got x = %1%, but function requires x > 0", x, pol);
158 // Special case for v == 0:
161 return -boost::math::detail::cyl_bessel_k_imp<T>(1, x, Tag(), pol);
165 return boost::math::detail::bessel_k_derivative_linear(v, x, Tag(), pol);
168 template <class Tag, class T, class Policy>
169 inline T cyl_neumann_prime_imp(T v, T x, const Policy& pol)
173 // Prevent complex and indeterminate results:
176 return boost::math::policies::raise_domain_error<T>(
177 "boost::math::cyl_neumann_prime<%1%>(%1%,%1%)",
178 "Got x = %1%, but function requires x > 0", x, pol);
180 // Special case for large x: use asymptotic expansion:
182 if (boost::math::detail::asymptotic_bessel_derivative_large_x_limit(v, x))
183 return boost::math::detail::asymptotic_bessel_y_derivative_large_x_2(v, x);
185 // Special case for small x: use Taylor series:
187 if (v > 0 && floor(v) != v)
189 const T eps = boost::math::policies::get_epsilon<T, Policy>();
190 if (log(eps / 2) > v * log((x * x) / (v * 4)))
191 return boost::math::detail::bessel_y_derivative_small_z_series(v, x, pol);
194 // Special case for v == 0:
197 return -boost::math::detail::cyl_neumann_imp<T>(1, x, Tag(), pol);
201 return boost::math::detail::bessel_y_derivative_linear(v, x, Tag(), pol);
204 template <class T, class Policy>
205 inline T sph_neumann_prime_imp(unsigned v, T x, const Policy& pol)
208 // Prevent complex and indeterminate result:
211 return boost::math::policies::raise_domain_error<T>(
212 "boost::math::sph_neumann_prime<%1%>(%1%,%1%)",
213 "Got x = %1%, but function requires x > 0.", x, pol);
215 // Special case for v == 0:
218 return -boost::math::detail::sph_neumann_imp<T>(1, x, pol);
222 return boost::math::detail::sph_neumann_derivative_linear(v, x, pol);
225 } // namespace detail
227 template <class T1, class T2, class Policy>
228 inline typename detail::bessel_traits<T1, T2, Policy>::result_type cyl_bessel_j_prime(T1 v, T2 x, const Policy& /* pol */)
230 BOOST_FPU_EXCEPTION_GUARD
231 typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
232 typedef typename detail::bessel_traits<T1, T2, Policy>::optimisation_tag tag_type;
233 typedef typename policies::evaluation<result_type, Policy>::type value_type;
234 typedef typename policies::normalise<
236 policies::promote_float<false>,
237 policies::promote_double<false>,
238 policies::discrete_quantile<>,
239 policies::assert_undefined<> >::type forwarding_policy;
240 return policies::checked_narrowing_cast<result_type, Policy>(detail::cyl_bessel_j_prime_imp<tag_type, value_type>(static_cast<value_type>(v), static_cast<value_type>(x), forwarding_policy()), "boost::math::cyl_bessel_j_prime<%1%,%1%>(%1%,%1%)");
243 template <class T1, class T2>
244 inline typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type cyl_bessel_j_prime(T1 v, T2 x)
246 return cyl_bessel_j_prime(v, x, policies::policy<>());
249 template <class T, class Policy>
250 inline typename detail::bessel_traits<T, T, Policy>::result_type sph_bessel_prime(unsigned v, T x, const Policy& /* pol */)
252 BOOST_FPU_EXCEPTION_GUARD
253 typedef typename detail::bessel_traits<T, T, Policy>::result_type result_type;
254 typedef typename policies::evaluation<result_type, Policy>::type value_type;
255 typedef typename policies::normalise<
257 policies::promote_float<false>,
258 policies::promote_double<false>,
259 policies::discrete_quantile<>,
260 policies::assert_undefined<> >::type forwarding_policy;
261 return policies::checked_narrowing_cast<result_type, Policy>(detail::sph_bessel_j_prime_imp<value_type>(v, static_cast<value_type>(x), forwarding_policy()), "boost::math::sph_bessel_j_prime<%1%>(%1%,%1%)");
265 inline typename detail::bessel_traits<T, T, policies::policy<> >::result_type sph_bessel_prime(unsigned v, T x)
267 return sph_bessel_prime(v, x, policies::policy<>());
270 template <class T1, class T2, class Policy>
271 inline typename detail::bessel_traits<T1, T2, Policy>::result_type cyl_bessel_i_prime(T1 v, T2 x, const Policy& /* pol */)
273 BOOST_FPU_EXCEPTION_GUARD
274 typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
275 typedef typename policies::evaluation<result_type, Policy>::type value_type;
276 typedef typename policies::normalise<
278 policies::promote_float<false>,
279 policies::promote_double<false>,
280 policies::discrete_quantile<>,
281 policies::assert_undefined<> >::type forwarding_policy;
282 return policies::checked_narrowing_cast<result_type, Policy>(detail::cyl_bessel_i_prime_imp<value_type>(static_cast<value_type>(v), static_cast<value_type>(x), forwarding_policy()), "boost::math::cyl_bessel_i_prime<%1%>(%1%,%1%)");
285 template <class T1, class T2>
286 inline typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type cyl_bessel_i_prime(T1 v, T2 x)
288 return cyl_bessel_i_prime(v, x, policies::policy<>());
291 template <class T1, class T2, class Policy>
292 inline typename detail::bessel_traits<T1, T2, Policy>::result_type cyl_bessel_k_prime(T1 v, T2 x, const Policy& /* pol */)
294 BOOST_FPU_EXCEPTION_GUARD
295 typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
296 typedef typename detail::bessel_traits<T1, T2, Policy>::optimisation_tag tag_type;
297 typedef typename policies::evaluation<result_type, Policy>::type value_type;
298 typedef typename policies::normalise<
300 policies::promote_float<false>,
301 policies::promote_double<false>,
302 policies::discrete_quantile<>,
303 policies::assert_undefined<> >::type forwarding_policy;
304 return policies::checked_narrowing_cast<result_type, Policy>(detail::cyl_bessel_k_prime_imp<tag_type, value_type>(static_cast<value_type>(v), static_cast<value_type>(x), forwarding_policy()), "boost::math::cyl_bessel_k_prime<%1%,%1%>(%1%,%1%)");
307 template <class T1, class T2>
308 inline typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type cyl_bessel_k_prime(T1 v, T2 x)
310 return cyl_bessel_k_prime(v, x, policies::policy<>());
313 template <class T1, class T2, class Policy>
314 inline typename detail::bessel_traits<T1, T2, Policy>::result_type cyl_neumann_prime(T1 v, T2 x, const Policy& /* pol */)
316 BOOST_FPU_EXCEPTION_GUARD
317 typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
318 typedef typename detail::bessel_traits<T1, T2, Policy>::optimisation_tag tag_type;
319 typedef typename policies::evaluation<result_type, Policy>::type value_type;
320 typedef typename policies::normalise<
322 policies::promote_float<false>,
323 policies::promote_double<false>,
324 policies::discrete_quantile<>,
325 policies::assert_undefined<> >::type forwarding_policy;
326 return policies::checked_narrowing_cast<result_type, Policy>(detail::cyl_neumann_prime_imp<tag_type, value_type>(static_cast<value_type>(v), static_cast<value_type>(x), forwarding_policy()), "boost::math::cyl_neumann_prime<%1%,%1%>(%1%,%1%)");
329 template <class T1, class T2>
330 inline typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type cyl_neumann_prime(T1 v, T2 x)
332 return cyl_neumann_prime(v, x, policies::policy<>());
335 template <class T, class Policy>
336 inline typename detail::bessel_traits<T, T, Policy>::result_type sph_neumann_prime(unsigned v, T x, const Policy& /* pol */)
338 BOOST_FPU_EXCEPTION_GUARD
339 typedef typename detail::bessel_traits<T, T, Policy>::result_type result_type;
340 typedef typename policies::evaluation<result_type, Policy>::type value_type;
341 typedef typename policies::normalise<
343 policies::promote_float<false>,
344 policies::promote_double<false>,
345 policies::discrete_quantile<>,
346 policies::assert_undefined<> >::type forwarding_policy;
347 return policies::checked_narrowing_cast<result_type, Policy>(detail::sph_neumann_prime_imp<value_type>(v, static_cast<value_type>(x), forwarding_policy()), "boost::math::sph_neumann_prime<%1%>(%1%,%1%)");
351 inline typename detail::bessel_traits<T, T, policies::policy<> >::result_type sph_neumann_prime(unsigned v, T x)
353 return sph_neumann_prime(v, x, policies::policy<>());
359 #endif // BOOST_MATH_BESSEL_DERIVATIVES_HPP