1 // Copyright (c) 2015 John Maddock
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_ELLINT_RG_HPP
7 #define BOOST_MATH_ELLINT_RG_HPP
13 #include <boost/math/special_functions/math_fwd.hpp>
14 #include <boost/math/tools/config.hpp>
15 #include <boost/math/constants/constants.hpp>
16 #include <boost/math/policies/error_handling.hpp>
17 #include <boost/math/special_functions/ellint_rd.hpp>
18 #include <boost/math/special_functions/ellint_rf.hpp>
19 #include <boost/math/special_functions/pow.hpp>
21 namespace boost { namespace math { namespace detail{
23 template <typename T, typename Policy>
24 T ellint_rg_imp(T x, T y, T z, const Policy& pol)
27 static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
29 if(x < 0 || y < 0 || z < 0)
31 return policies::raise_domain_error<T>(function,
32 "domain error, all arguments must be non-negative, "
33 "only sensible result is %1%.",
34 std::numeric_limits<T>::quiet_NaN(), pol);
37 // Function is symmetric in x, y and z, but we require
38 // (x - z)(y - z) >= 0 to avoid cancellation error in the result
39 // which implies (for example) x >= z >= y
49 BOOST_MATH_ASSERT(x >= z);
50 BOOST_MATH_ASSERT(z >= y);
52 // Special cases from http://dlmf.nist.gov/19.20#ii
59 // This also works for x = y = z = 0 presumably.
65 return constants::pi<T>() * sqrt(x) / 4;
71 return (x == 0) ? T(sqrt(z) / 2) : T((z * ellint_rc_imp(x, z, pol) + sqrt(x)) / 2);
77 return constants::pi<T>() * sqrt(y) / 4;
79 return (y == 0) ? T(sqrt(x) / 2) : T((y * ellint_rc_imp(x, y, pol) + sqrt(x)) / 2);
85 // Special handling for common case, from
86 // Numerical Computation of Real or Complex Elliptic Integrals, eq.46
95 while(fabs(xn - yn) >= 2.7 * tools::root_epsilon<T>() * fabs(xn))
101 sum += sum_pow * boost::math::pow<2>(xn - yn);
103 T RF = constants::pi<T>() / (xn + yn);
104 return ((boost::math::pow<2>((x0 + y0) / 2) - sum) * RF) / 2;
106 return (z * ellint_rf_imp(x, y, z, pol)
107 - (x - z) * (y - z) * ellint_rd_imp(x, y, z, pol) / 3
108 + sqrt(x * y / z)) / 2;
111 } // namespace detail
113 template <class T1, class T2, class T3, class Policy>
114 inline typename tools::promote_args<T1, T2, T3>::type
115 ellint_rg(T1 x, T2 y, T3 z, const Policy& pol)
117 typedef typename tools::promote_args<T1, T2, T3>::type result_type;
118 typedef typename policies::evaluation<result_type, Policy>::type value_type;
119 return policies::checked_narrowing_cast<result_type, Policy>(
120 detail::ellint_rg_imp(
121 static_cast<value_type>(x),
122 static_cast<value_type>(y),
123 static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
126 template <class T1, class T2, class T3>
127 inline typename tools::promote_args<T1, T2, T3>::type
128 ellint_rg(T1 x, T2 y, T3 z)
130 return ellint_rg(x, y, z, policies::policy<>());
135 #endif // BOOST_MATH_ELLINT_RG_HPP