2 ///////////////////////////////////////////////////////////////////////////////
3 // Copyright 2018 John Maddock
4 // Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_MATH_HYPERGEOMETRIC_PFQ_HPP
9 #define BOOST_MATH_HYPERGEOMETRIC_PFQ_HPP
11 #include <boost/config.hpp>
13 #if defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) || defined(BOOST_NO_CXX11_LAMBDAS) || defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) || defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
14 # error "hypergeometric_pFq requires a C++11 compiler"
17 #include <boost/math/special_functions/detail/hypergeometric_pFq_checked_series.hpp>
18 #include <boost/chrono.hpp>
19 #include <initializer_list>
26 struct pFq_termination_exception : public std::runtime_error
28 pFq_termination_exception(const char* p) : std::runtime_error(p) {}
31 struct timed_iteration_terminator
33 timed_iteration_terminator(boost::uintmax_t i, double t) : max_iter(i), max_time(t), start_time(boost::chrono::system_clock::now()) {}
35 bool operator()(boost::uintmax_t iter)const
38 boost::throw_exception(boost::math::detail::pFq_termination_exception("pFq exceeded maximum permitted iterations."));
39 if (boost::chrono::duration<double>(boost::chrono::system_clock::now() - start_time).count() > max_time)
40 boost::throw_exception(boost::math::detail::pFq_termination_exception("pFq exceeded maximum permitted evaluation time."));
44 boost::uintmax_t max_iter;
46 boost::chrono::system_clock::time_point start_time;
51 template <class Seq, class Real, class Policy>
52 inline typename tools::promote_args<Real, typename Seq::value_type>::type hypergeometric_pFq(const Seq& aj, const Seq& bj, const Real& z, Real* p_abs_error, const Policy& pol)
54 typedef typename tools::promote_args<Real, typename Seq::value_type>::type result_type;
55 typedef typename policies::evaluation<result_type, Policy>::type value_type;
56 typedef typename policies::normalise<
58 policies::promote_float<false>,
59 policies::promote_double<false>,
60 policies::discrete_quantile<>,
61 policies::assert_undefined<> >::type forwarding_policy;
66 std::pair<value_type, value_type> r = boost::math::detail::hypergeometric_pFq_checked_series_impl(aj, bj, value_type(z), pol, boost::math::detail::iteration_terminator(boost::math::policies::get_max_series_iterations<forwarding_policy>()), scale);
67 r.first *= exp(Real(scale));
68 r.second *= exp(Real(scale));
70 *p_abs_error = static_cast<Real>(r.second) * boost::math::tools::epsilon<Real>();
71 return policies::checked_narrowing_cast<result_type, Policy>(r.first, "boost::math::hypergeometric_pFq<%1%>(%1%,%1%,%1%)");
74 template <class Seq, class Real>
75 inline typename tools::promote_args<Real, typename Seq::value_type>::type hypergeometric_pFq(const Seq& aj, const Seq& bj, const Real& z, Real* p_abs_error = 0)
77 return hypergeometric_pFq(aj, bj, z, p_abs_error, boost::math::policies::policy<>());
80 template <class R, class Real, class Policy>
81 inline typename tools::promote_args<Real, R>::type hypergeometric_pFq(const std::initializer_list<R>& aj, const std::initializer_list<R>& bj, const Real& z, Real* p_abs_error, const Policy& pol)
83 return hypergeometric_pFq<std::initializer_list<R>, Real, Policy>(aj, bj, z, p_abs_error, pol);
86 template <class R, class Real>
87 inline typename tools::promote_args<Real, R>::type hypergeometric_pFq(const std::initializer_list<R>& aj, const std::initializer_list<R>& bj, const Real& z, Real* p_abs_error = 0)
89 return hypergeometric_pFq<std::initializer_list<R>, Real>(aj, bj, z, p_abs_error);
93 struct scoped_precision
95 scoped_precision(unsigned p)
97 old_p = T::default_precision();
98 T::default_precision(p);
102 T::default_precision(old_p);
107 template <class Seq, class Real, class Policy>
108 Real hypergeometric_pFq_precision(const Seq& aj, const Seq& bj, Real z, unsigned digits10, double timeout, const Policy& pol)
110 unsigned current_precision = digits10 + 5;
112 for (auto ai = aj.begin(); ai != aj.end(); ++ai)
114 current_precision = (std::max)(current_precision, ai->precision());
116 for (auto bi = bj.begin(); bi != bj.end(); ++bi)
118 current_precision = (std::max)(current_precision, bi->precision());
120 current_precision = (std::max)(current_precision, z.precision());
123 std::vector<Real> aa(aj), bb(bj);
126 scoped_precision<Real> p(current_precision);
127 for (auto ai = aa.begin(); ai != aa.end(); ++ai)
128 ai->precision(current_precision);
129 for (auto bi = bb.begin(); bi != bb.end(); ++bi)
130 bi->precision(current_precision);
131 z.precision(current_precision);
135 std::pair<Real, Real> rp = boost::math::detail::hypergeometric_pFq_checked_series_impl(aa, bb, z, pol, boost::math::detail::timed_iteration_terminator(boost::math::policies::get_max_series_iterations<Policy>(), timeout), scale);
136 rp.first *= exp(Real(scale));
137 rp.second *= exp(Real(scale));
142 unsigned cancellation;
144 cancellation = itrunc(log10(abs(norm / r)));
146 catch (const boost::math::rounding_error&)
148 // Happens when r is near enough zero:
149 cancellation = UINT_MAX;
151 if (cancellation >= current_precision - 1)
153 current_precision *= 2;
156 unsigned precision_obtained = current_precision - 1 - cancellation;
157 if (precision_obtained < digits10)
159 current_precision += digits10 - precision_obtained + 5;
164 catch (const boost::math::evaluation_error&)
166 current_precision *= 2;
168 catch (const detail::pFq_termination_exception& e)
171 // Either we have exhausted the number of series iterations, or the timeout.
172 // Either way we quit now.
173 throw boost::math::evaluation_error(e.what());
179 template <class Seq, class Real>
180 Real hypergeometric_pFq_precision(const Seq& aj, const Seq& bj, const Real& z, unsigned digits10, double timeout = 0.5)
182 return hypergeometric_pFq_precision(aj, bj, z, digits10, timeout, boost::math::policies::policy<>());
185 template <class Real, class Policy>
186 Real hypergeometric_pFq_precision(const std::initializer_list<Real>& aj, const std::initializer_list<Real>& bj, const Real& z, unsigned digits10, double timeout, const Policy& pol)
188 return hypergeometric_pFq_precision< std::initializer_list<Real>, Real>(aj, bj, z, digits10, timeout, pol);
190 template <class Real>
191 Real hypergeometric_pFq_precision(const std::initializer_list<Real>& aj, const std::initializer_list<Real>& bj, const Real& z, unsigned digits10, double timeout = 0.5)
193 return hypergeometric_pFq_precision< std::initializer_list<Real>, Real>(aj, bj, z, digits10, timeout, boost::math::policies::policy<>());
199 #endif // BOOST_MATH_BESSEL_ITERATORS_HPP