1 // Copyright (c) 2006 Xiaogang Zhang
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)
7 // XZ wrote the original of this file as part of the Google
8 // Summer of Code 2006. JM modified it to fit into the
9 // Boost.Math conceptual framework better, and to correctly
10 // handle the y < 0 case.
13 #ifndef BOOST_MATH_ELLINT_RC_HPP
14 #define BOOST_MATH_ELLINT_RC_HPP
20 #include <boost/math/policies/error_handling.hpp>
21 #include <boost/math/tools/config.hpp>
22 #include <boost/math/special_functions/math_fwd.hpp>
24 // Carlson's degenerate elliptic integral
25 // R_C(x, y) = R_F(x, y, y) = 0.5 * \int_{0}^{\infty} (t+x)^{-1/2} (t+y)^{-1} dt
26 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
28 namespace boost { namespace math { namespace detail{
30 template <typename T, typename Policy>
31 T ellint_rc_imp(T x, T y, const Policy& pol)
33 T value, S, u, lambda, tolerance, prefix;
37 using namespace boost::math::tools;
39 static const char* function = "boost::math::ellint_rc<%1%>(%1%,%1%)";
43 return policies::raise_domain_error<T>(function,
44 "Argument x must be non-negative but got %1%", x, pol);
48 return policies::raise_domain_error<T>(function,
49 "Argument y must not be zero but got %1%", y, pol);
52 // error scales as the 6th power of tolerance
53 tolerance = pow(4 * tools::epsilon<T>(), T(1) / 6);
55 // for y < 0, the integral is singular, return Cauchy principal value
58 prefix = sqrt(x / (x - y));
70 S = y / u - 1; // 1 - x / u = 2 * S
72 if (2 * abs(S) < tolerance)
77 lambda = 2 * sx * sy + y;
81 }while(k < policies::get_max_series_iterations<Policy>());
82 // Check to see if we gave up too soon:
83 policies::check_series_iterations<T>(function, k, pol);
85 // Taylor series expansion to the 5th order
86 value = (1 + S * S * (T(3) / 10 + S * (T(1) / 7 + S * (T(3) / 8 + S * T(9) / 22)))) / sqrt(u);
88 return value * prefix;
93 template <class T1, class T2, class Policy>
94 inline typename tools::promote_args<T1, T2>::type
95 ellint_rc(T1 x, T2 y, const Policy& pol)
97 typedef typename tools::promote_args<T1, T2>::type result_type;
98 typedef typename policies::evaluation<result_type, Policy>::type value_type;
99 return policies::checked_narrowing_cast<result_type, Policy>(
100 detail::ellint_rc_imp(
101 static_cast<value_type>(x),
102 static_cast<value_type>(y), pol), "boost::math::ellint_rc<%1%>(%1%,%1%)");
105 template <class T1, class T2>
106 inline typename tools::promote_args<T1, T2>::type
107 ellint_rc(T1 x, T2 y)
109 return ellint_rc(x, y, policies::policy<>());
114 #endif // BOOST_MATH_ELLINT_RC_HPP