1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright 2014 Anton Bikineev
3 // Copyright 2014 Christopher Kormanyos
4 // Copyright 2014 John Maddock
5 // Copyright 2014 Paul Bristow
6 // Distributed under the Boost
7 // Software License, Version 1.0. (See accompanying file
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_MATH_HYPERGEOMETRIC_0F1_HPP
11 #define BOOST_MATH_HYPERGEOMETRIC_0F1_HPP
13 #include <boost/math/policies/policy.hpp>
14 #include <boost/math/policies/error_handling.hpp>
15 #include <boost/math/special_functions/detail/hypergeometric_series.hpp>
16 #include <boost/math/special_functions/detail/hypergeometric_0F1_bessel.hpp>
18 namespace boost { namespace math { namespace detail {
22 struct hypergeometric_0F1_cf
25 // We start this continued fraction at b on index -1
26 // and treat the -1 and 0 cases as special cases.
27 // We do this to avoid adding the continued fraction result
28 // to 1 so that we can accurately evaluate for small results
29 // as well as large ones. See http://functions.wolfram.com/07.17.10.0002.01
33 hypergeometric_0F1_cf(T b_, T z_) : b(b_), z(z_), k(-2) {}
34 typedef std::pair<T, T> result_type;
36 result_type operator()()
40 return std::make_pair(z / b, 1);
41 return std::make_pair(-z / ((k + 1) * (b + k)), 1 + z / ((k + 1) * (b + k)));
45 template <class T, class Policy>
46 T hypergeometric_0F1_cf_imp(T b, T z, const Policy& pol, const char* function)
48 hypergeometric_0F1_cf<T> evaluator(b, z);
49 boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
50 T cf = tools::continued_fraction_b(evaluator, policies::get_epsilon<T, Policy>(), max_iter);
51 policies::check_series_iterations<T>(function, max_iter, pol);
56 template <class T, class Policy>
57 inline T hypergeometric_0F1_imp(const T& b, const T& z, const Policy& pol)
59 const char* function = "boost::math::hypergeometric_0f1<%1%,%1%>(%1%, %1%)";
66 if ((b <= 0) && (b == floor(b)))
67 return policies::raise_pole_error<T>(
69 "Evaluation of 0f1 with nonpositive integer b = %1%.", b, pol);
73 // Series is alternating and divergent, need to do something else here,
74 // Bessel function relation is much more accurate, unless |b| is similarly
75 // large to |z|, otherwise the CF formula suffers from cancellation when
76 // the result would be very small.
78 return hypergeometric_0F1_bessel(b, z, pol);
79 return hypergeometric_0F1_cf_imp(b, z, pol, function);
81 // evaluation through Taylor series looks
82 // more precisious than Bessel relation:
83 // detail::hypergeometric_0f1_bessel(b, z, pol);
84 return detail::hypergeometric_0F1_generic_series(b, z, pol);
89 template <class T1, class T2, class Policy>
90 inline typename tools::promote_args<T1, T2>::type hypergeometric_0F1(T1 b, T2 z, const Policy& /* pol */)
92 BOOST_FPU_EXCEPTION_GUARD
93 typedef typename tools::promote_args<T1, T2>::type result_type;
94 typedef typename policies::evaluation<result_type, Policy>::type value_type;
95 typedef typename policies::normalise<
97 policies::promote_float<false>,
98 policies::promote_double<false>,
99 policies::discrete_quantile<>,
100 policies::assert_undefined<> >::type forwarding_policy;
101 return policies::checked_narrowing_cast<result_type, Policy>(
102 detail::hypergeometric_0F1_imp<value_type>(
103 static_cast<value_type>(b),
104 static_cast<value_type>(z),
105 forwarding_policy()),
106 "boost::math::hypergeometric_0F1<%1%>(%1%,%1%)");
109 template <class T1, class T2>
110 inline typename tools::promote_args<T1, T2>::type hypergeometric_0F1(T1 b, T2 z)
112 return hypergeometric_0F1(b, z, policies::policy<>());
116 } } // namespace boost::math
118 #endif // BOOST_MATH_HYPERGEOMETRIC_HPP