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 p < 0 case.
13 #ifndef BOOST_MATH_ELLINT_RJ_HPP
14 #define BOOST_MATH_ELLINT_RJ_HPP
20 #include <boost/math/special_functions/math_fwd.hpp>
21 #include <boost/math/tools/config.hpp>
22 #include <boost/math/policies/error_handling.hpp>
23 #include <boost/math/special_functions/ellint_rc.hpp>
24 #include <boost/math/special_functions/ellint_rf.hpp>
26 // Carlson's elliptic integral of the third kind
27 // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt
28 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
30 namespace boost { namespace math { namespace detail{
32 template <typename T, typename Policy>
33 T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
35 T value, u, lambda, alpha, beta, sigma, factor, tolerance;
36 T X, Y, Z, P, EA, EB, EC, E2, E3, S1, S2, S3;
40 using namespace boost::math::tools;
42 static const char* function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
46 return policies::raise_domain_error<T>(function,
47 "Argument x must be non-negative, but got x = %1%", x, pol);
51 return policies::raise_domain_error<T>(function,
52 "Argument y must be non-negative, but got y = %1%", y, pol);
56 return policies::raise_domain_error<T>(function,
57 "Argument z must be non-negative, but got z = %1%", z, pol);
61 return policies::raise_domain_error<T>(function,
62 "Argument p must not be zero, but got p = %1%", p, pol);
64 if (x + y == 0 || y + z == 0 || z + x == 0)
66 return policies::raise_domain_error<T>(function,
67 "At most one argument can be zero, "
68 "only possible result is %1%.", std::numeric_limits<T>::quiet_NaN(), pol);
71 // error scales as the 6th power of tolerance
72 tolerance = pow(T(1) * tools::epsilon<T>() / 3, T(1) / 6);
74 // for p < 0, the integral is singular, return Cauchy principal value
78 // We must ensure that (z - y) * (y - x) is positive.
79 // Since the integral is symmetrical in x, y and z
80 // we can just permute the values:
90 T pmy = (z - y) * (y - x) / (y + q); // p - y
92 BOOST_ASSERT(pmy >= 0);
95 value = boost::math::ellint_rj(x, y, z, p, pol);
97 value -= 3 * boost::math::ellint_rf(x, y, z, pol);
98 value += 3 * sqrt((x * y * z) / (x * z + p * q)) * boost::math::ellint_rc(x * z + p * q, p * q, pol);
109 u = (x + y + z + p + p) / 5;
115 if ((tools::max)(abs(X), abs(Y), abs(Z), abs(P)) < tolerance)
122 lambda = sy * (sx + sz) + sz * sx;
123 alpha = p * (sx + sy + sz) + sx * sy * sz;
125 beta = p * (p + lambda) * (p + lambda);
126 sigma += factor * boost::math::ellint_rc(alpha, beta, pol);
128 x = (x + lambda) / 4;
129 y = (y + lambda) / 4;
130 z = (z + lambda) / 4;
131 p = (p + lambda) / 4;
134 while(k < policies::get_max_series_iterations<Policy>());
136 // Check to see if we gave up too soon:
137 policies::check_series_iterations<T>(function, k, pol);
139 // Taylor series expansion to the 5th order
140 EA = X * Y + Y * Z + Z * X;
144 E3 = EB + 2 * P * (EA - EC);
145 S1 = 1 + E2 * (E2 * T(9) / 88 - E3 * T(9) / 52 - T(3) / 14);
146 S2 = EB * (T(1) / 6 + P * (T(-6) / 22 + P * T(3) / 26));
147 S3 = P * ((EA - EC) / 3 - P * EA * T(3) / 22);
148 value = 3 * sigma + factor * (S1 + S2 + S3) / (u * sqrt(u));
153 } // namespace detail
155 template <class T1, class T2, class T3, class T4, class Policy>
156 inline typename tools::promote_args<T1, T2, T3, T4>::type
157 ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
159 typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
160 typedef typename policies::evaluation<result_type, Policy>::type value_type;
161 return policies::checked_narrowing_cast<result_type, Policy>(
162 detail::ellint_rj_imp(
163 static_cast<value_type>(x),
164 static_cast<value_type>(y),
165 static_cast<value_type>(z),
166 static_cast<value_type>(p),
167 pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
170 template <class T1, class T2, class T3, class T4>
171 inline typename tools::promote_args<T1, T2, T3, T4>::type
172 ellint_rj(T1 x, T2 y, T3 z, T4 p)
174 return ellint_rj(x, y, z, p, policies::policy<>());
179 #endif // BOOST_MATH_ELLINT_RJ_HPP