1 // Copyright (c) 2006 Xiaogang Zhang, 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)
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.
11 // Updated 2015 to use Carlson's latest methods.
14 #ifndef BOOST_MATH_ELLINT_RJ_HPP
15 #define BOOST_MATH_ELLINT_RJ_HPP
21 #include <boost/math/special_functions/math_fwd.hpp>
22 #include <boost/math/tools/config.hpp>
23 #include <boost/math/policies/error_handling.hpp>
24 #include <boost/math/special_functions/ellint_rc.hpp>
25 #include <boost/math/special_functions/ellint_rf.hpp>
26 #include <boost/math/special_functions/ellint_rd.hpp>
28 // Carlson's elliptic integral of the third kind
29 // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt
30 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
32 namespace boost { namespace math { namespace detail{
34 template <typename T, typename Policy>
35 T ellint_rc1p_imp(T y, const Policy& pol)
37 using namespace boost::math;
38 // Calculate RC(1, 1 + x)
41 static const char* function = "boost::math::ellint_rc<%1%>(%1%,%1%)";
45 return policies::raise_domain_error<T>(function,
46 "Argument y must not be zero but got %1%", y, pol);
49 // for 1 + y < 0, the integral is singular, return Cauchy principal value
53 result = sqrt(1 / -y) * detail::ellint_rc_imp(T(-y), T(-1 - y), pol);
61 result = atan(sqrt(y)) / sqrt(y);
68 result = (boost::math::log1p(arg) - boost::math::log1p(-arg)) / (2 * sqrt(-y));
72 result = log((1 + sqrt(-y)) / sqrt(1 + y)) / sqrt(-y);
78 template <typename T, typename Policy>
79 T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
83 static const char* function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
87 return policies::raise_domain_error<T>(function,
88 "Argument x must be non-negative, but got x = %1%", x, pol);
92 return policies::raise_domain_error<T>(function,
93 "Argument y must be non-negative, but got y = %1%", y, pol);
97 return policies::raise_domain_error<T>(function,
98 "Argument z must be non-negative, but got z = %1%", z, pol);
102 return policies::raise_domain_error<T>(function,
103 "Argument p must not be zero, but got p = %1%", p, pol);
105 if(x + y == 0 || y + z == 0 || z + x == 0)
107 return policies::raise_domain_error<T>(function,
108 "At most one argument can be zero, "
109 "only possible result is %1%.", std::numeric_limits<T>::quiet_NaN(), pol);
112 // for p < 0, the integral is singular, return Cauchy principal value
116 // We must ensure that x < y < z.
117 // Since the integral is symmetrical in x, y and z
118 // we can just permute the values:
127 BOOST_ASSERT(x <= y);
128 BOOST_ASSERT(y <= z);
131 p = (z * (x + y + q) - x * y) / (z + q);
133 BOOST_ASSERT(p >= 0);
135 T value = (p - z) * ellint_rj_imp(x, y, z, p, pol);
136 value -= 3 * ellint_rf_imp(x, y, z, pol);
137 value += 3 * sqrt((x * y * z) / (x * y + p * q)) * ellint_rc_imp(T(x * y + p * q), T(p * q), pol);
143 // Special cases from http://dlmf.nist.gov/19.20#iii
152 return 1 / (x * sqrt(x));
157 return 3 * (ellint_rc_imp(x, p, pol) - 1 / sqrt(x)) / (x - p);
162 // x = y only, permute so y = z:
167 return ellint_rd_imp(x, y, y, pol);
169 else if((std::max)(y, p) / (std::min)(y, p) > 1.2)
171 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
173 // Otherwise fall through to normal method, special case above will suffer too much cancellation...
181 return ellint_rd_imp(x, y, y, pol);
183 else if((std::max)(y, p) / (std::min)(y, p) > 1.2)
186 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
188 // Otherwise fall through to normal method, special case above will suffer too much cancellation...
192 return ellint_rd_imp(x, y, z, pol);
199 T An = (x + y + z + 2 * p) / 5;
201 T delta = (p - x) * (p - y) * (p - z);
202 T Q = pow(tools::epsilon<T>() / 5, -T(1) / 8) * (std::max)((std::max)(fabs(An - x), fabs(An - y)), (std::max)(fabs(An - z), fabs(An - p)));
212 for(n = 0; n < policies::get_max_series_iterations<Policy>(); ++n)
218 Dn = (rp + rx) * (rp + ry) * (rp + rz);
221 if((En < -0.5) && (En > -1.5))
224 // Occationally En ~ -1, we then have no means of calculating
225 // RC(1, 1+En) without terrible cancellation error, so we
226 // need to get to 1+En directly. By substitution we have
228 // 1+E_0 = 1 + (p-x)*(p-y)*(p-z)/((sqrt(p) + sqrt(x))*(sqrt(p)+sqrt(y))*(sqrt(p)+sqrt(z)))^2
229 // = 2*sqrt(p)*(p+sqrt(x) * (sqrt(y)+sqrt(z)) + sqrt(y)*sqrt(z)) / ((sqrt(p) + sqrt(x))*(sqrt(p) + sqrt(y)*(sqrt(p)+sqrt(z))))
231 // And since this is just an application of the duplication formula for RJ, the same
232 // expression works for 1+En if we use x,y,z,p_n etc.
233 // This branch is taken only once or twice at the start of iteration,
234 // after than En reverts to it's usual very small values.
236 T b = 2 * rp * (pn + rx * (ry + rz) + ry * rz) / Dn;
237 RC_sum += fmn / Dn * detail::ellint_rc_imp(T(1), b, pol);
241 RC_sum += fmn / Dn * ellint_rc1p_imp(En, pol);
243 lambda = rx * ry + rx * rz + ry * rz;
245 // From here on we move to n+1:
246 An = (An + lambda) / 4;
252 xn = (xn + lambda) / 4;
253 yn = (yn + lambda) / 4;
254 zn = (zn + lambda) / 4;
255 pn = (pn + lambda) / 4;
259 T X = fmn * (A0 - x) / An;
260 T Y = fmn * (A0 - y) / An;
261 T Z = fmn * (A0 - z) / An;
262 T P = (-X - Y - Z) / 2;
263 T E2 = X * Y + X * Z + Y * Z - 3 * P * P;
264 T E3 = X * Y * Z + 2 * E2 * P + 4 * P * P * P;
265 T E4 = (2 * X * Y * Z + E2 * P + 3 * P * P * P) * P;
266 T E5 = X * Y * Z * P * P;
267 T result = fmn * pow(An, T(-3) / 2) *
268 (1 - 3 * E2 / 14 + E3 / 6 + 9 * E2 * E2 / 88 - 3 * E4 / 22 - 9 * E2 * E3 / 52 + 3 * E5 / 26 - E2 * E2 * E2 / 16
269 + 3 * E3 * E3 / 40 + 3 * E2 * E4 / 20 + 45 * E2 * E2 * E3 / 272 - 9 * (E3 * E4 + E2 * E5) / 68);
271 result += 6 * RC_sum;
275 } // namespace detail
277 template <class T1, class T2, class T3, class T4, class Policy>
278 inline typename tools::promote_args<T1, T2, T3, T4>::type
279 ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
281 typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
282 typedef typename policies::evaluation<result_type, Policy>::type value_type;
283 return policies::checked_narrowing_cast<result_type, Policy>(
284 detail::ellint_rj_imp(
285 static_cast<value_type>(x),
286 static_cast<value_type>(y),
287 static_cast<value_type>(z),
288 static_cast<value_type>(p),
289 pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
292 template <class T1, class T2, class T3, class T4>
293 inline typename tools::promote_args<T1, T2, T3, T4>::type
294 ellint_rj(T1 x, T2 y, T3 z, T4 p)
296 return ellint_rj(x, y, z, p, policies::policy<>());
301 #endif // BOOST_MATH_ELLINT_RJ_HPP