// Copyright (c) 2006 Xiaogang Zhang // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // History: // XZ wrote the original of this file as part of the Google // Summer of Code 2006. JM modified it to fit into the // Boost.Math conceptual framework better, and to correctly // handle the p < 0 case. // #ifndef BOOST_MATH_ELLINT_RJ_HPP #define BOOST_MATH_ELLINT_RJ_HPP #ifdef _MSC_VER #pragma once #endif #include #include #include #include #include // Carlson's elliptic integral of the third kind // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt // Carlson, Numerische Mathematik, vol 33, 1 (1979) namespace boost { namespace math { namespace detail{ template T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol) { T value, u, lambda, alpha, beta, sigma, factor, tolerance; T X, Y, Z, P, EA, EB, EC, E2, E3, S1, S2, S3; unsigned long k; BOOST_MATH_STD_USING using namespace boost::math::tools; static const char* function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)"; if (x < 0) { return policies::raise_domain_error(function, "Argument x must be non-negative, but got x = %1%", x, pol); } if(y < 0) { return policies::raise_domain_error(function, "Argument y must be non-negative, but got y = %1%", y, pol); } if(z < 0) { return policies::raise_domain_error(function, "Argument z must be non-negative, but got z = %1%", z, pol); } if(p == 0) { return policies::raise_domain_error(function, "Argument p must not be zero, but got p = %1%", p, pol); } if (x + y == 0 || y + z == 0 || z + x == 0) { return policies::raise_domain_error(function, "At most one argument can be zero, " "only possible result is %1%.", std::numeric_limits::quiet_NaN(), pol); } // error scales as the 6th power of tolerance tolerance = pow(T(1) * tools::epsilon() / 3, T(1) / 6); // for p < 0, the integral is singular, return Cauchy principal value if (p < 0) { // // We must ensure that (z - y) * (y - x) is positive. // Since the integral is symmetrical in x, y and z // we can just permute the values: // if(x > y) std::swap(x, y); if(y > z) std::swap(y, z); if(x > y) std::swap(x, y); T q = -p; T pmy = (z - y) * (y - x) / (y + q); // p - y BOOST_ASSERT(pmy >= 0); T p = pmy + y; value = boost::math::ellint_rj(x, y, z, p, pol); value *= pmy; value -= 3 * boost::math::ellint_rf(x, y, z, pol); value += 3 * sqrt((x * y * z) / (x * z + p * q)) * boost::math::ellint_rc(x * z + p * q, p * q, pol); value /= (y + q); return value; } // duplication sigma = 0; factor = 1; k = 1; do { u = (x + y + z + p + p) / 5; X = (u - x) / u; Y = (u - y) / u; Z = (u - z) / u; P = (u - p) / u; if ((tools::max)(abs(X), abs(Y), abs(Z), abs(P)) < tolerance) break; T sx = sqrt(x); T sy = sqrt(y); T sz = sqrt(z); lambda = sy * (sx + sz) + sz * sx; alpha = p * (sx + sy + sz) + sx * sy * sz; alpha *= alpha; beta = p * (p + lambda) * (p + lambda); sigma += factor * boost::math::ellint_rc(alpha, beta, pol); factor /= 4; x = (x + lambda) / 4; y = (y + lambda) / 4; z = (z + lambda) / 4; p = (p + lambda) / 4; ++k; } while(k < policies::get_max_series_iterations()); // Check to see if we gave up too soon: policies::check_series_iterations(function, k, pol); // Taylor series expansion to the 5th order EA = X * Y + Y * Z + Z * X; EB = X * Y * Z; EC = P * P; E2 = EA - 3 * EC; E3 = EB + 2 * P * (EA - EC); S1 = 1 + E2 * (E2 * T(9) / 88 - E3 * T(9) / 52 - T(3) / 14); S2 = EB * (T(1) / 6 + P * (T(-6) / 22 + P * T(3) / 26)); S3 = P * ((EA - EC) / 3 - P * EA * T(3) / 22); value = 3 * sigma + factor * (S1 + S2 + S3) / (u * sqrt(u)); return value; } } // namespace detail template inline typename tools::promote_args::type ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol) { typedef typename tools::promote_args::type result_type; typedef typename policies::evaluation::type value_type; return policies::checked_narrowing_cast( detail::ellint_rj_imp( static_cast(x), static_cast(y), static_cast(z), static_cast(p), pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)"); } template inline typename tools::promote_args::type ellint_rj(T1 x, T2 y, T3 z, T4 p) { return ellint_rj(x, y, z, p, policies::policy<>()); } }} // namespaces #endif // BOOST_MATH_ELLINT_RJ_HPP