Chris@101: // Copyright (c) 2006 Xiaogang Zhang, 2015 John Maddock Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // History: Chris@16: // XZ wrote the original of this file as part of the Google Chris@16: // Summer of Code 2006. JM modified it to fit into the Chris@16: // Boost.Math conceptual framework better, and to handle Chris@16: // types longer than 80-bit reals. Chris@101: // Updated 2015 to use Carlson's latest methods. Chris@16: // Chris@16: #ifndef BOOST_MATH_ELLINT_RF_HPP Chris@16: #define BOOST_MATH_ELLINT_RF_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: // Carlson's elliptic integral of the first kind Chris@16: // R_F(x, y, z) = 0.5 * \int_{0}^{\infty} [(t+x)(t+y)(t+z)]^{-1/2} dt Chris@16: // Carlson, Numerische Mathematik, vol 33, 1 (1979) Chris@16: Chris@16: namespace boost { namespace math { namespace detail{ Chris@16: Chris@101: template Chris@101: T ellint_rf_imp(T x, T y, T z, const Policy& pol) Chris@101: { Chris@101: BOOST_MATH_STD_USING Chris@101: using namespace boost::math; Chris@101: using std::swap; Chris@16: Chris@101: static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)"; Chris@16: Chris@101: if(x < 0 || y < 0 || z < 0) Chris@101: { Chris@101: return policies::raise_domain_error(function, Chris@16: "domain error, all arguments must be non-negative, " Chris@16: "only sensible result is %1%.", Chris@16: std::numeric_limits::quiet_NaN(), pol); Chris@101: } Chris@101: if(x + y == 0 || y + z == 0 || z + x == 0) Chris@101: { Chris@101: return policies::raise_domain_error(function, Chris@16: "domain error, at most one argument can be zero, " Chris@16: "only sensible result is %1%.", Chris@16: std::numeric_limits::quiet_NaN(), pol); Chris@101: } Chris@101: // Chris@101: // Special cases from http://dlmf.nist.gov/19.20#i Chris@101: // Chris@101: if(x == y) Chris@101: { Chris@101: if(x == z) Chris@101: { Chris@101: // x, y, z equal: Chris@101: return 1 / sqrt(x); Chris@101: } Chris@101: else Chris@101: { Chris@101: // 2 equal, x and y: Chris@101: if(z == 0) Chris@101: return constants::pi() / (2 * sqrt(x)); Chris@101: else Chris@101: return ellint_rc_imp(z, x, pol); Chris@101: } Chris@101: } Chris@101: if(x == z) Chris@101: { Chris@101: if(y == 0) Chris@101: return constants::pi() / (2 * sqrt(x)); Chris@101: else Chris@101: return ellint_rc_imp(y, x, pol); Chris@101: } Chris@101: if(y == z) Chris@101: { Chris@101: if(x == 0) Chris@101: return constants::pi() / (2 * sqrt(y)); Chris@101: else Chris@101: return ellint_rc_imp(x, y, pol); Chris@101: } Chris@101: if(x == 0) Chris@101: swap(x, z); Chris@101: else if(y == 0) Chris@101: swap(y, z); Chris@101: if(z == 0) Chris@101: { Chris@101: // Chris@101: // Special case for one value zero: Chris@101: // Chris@101: T xn = sqrt(x); Chris@101: T yn = sqrt(y); Chris@16: Chris@101: while(fabs(xn - yn) >= 2.7 * tools::root_epsilon() * fabs(xn)) Chris@101: { Chris@101: T t = sqrt(xn * yn); Chris@101: xn = (xn + yn) / 2; Chris@101: yn = t; Chris@101: } Chris@101: return constants::pi() / (xn + yn); Chris@101: } Chris@16: Chris@101: T xn = x; Chris@101: T yn = y; Chris@101: T zn = z; Chris@101: T An = (x + y + z) / 3; Chris@101: T A0 = An; Chris@101: T Q = pow(3 * boost::math::tools::epsilon(), T(-1) / 8) * (std::max)((std::max)(fabs(An - xn), fabs(An - yn)), fabs(An - zn)); Chris@101: T fn = 1; Chris@16: Chris@16: Chris@101: // duplication Chris@101: unsigned k = 1; Chris@101: for(; k < boost::math::policies::get_max_series_iterations(); ++k) Chris@101: { Chris@101: T root_x = sqrt(xn); Chris@101: T root_y = sqrt(yn); Chris@101: T root_z = sqrt(zn); Chris@101: T lambda = root_x * root_y + root_x * root_z + root_y * root_z; Chris@101: An = (An + lambda) / 4; Chris@101: xn = (xn + lambda) / 4; Chris@101: yn = (yn + lambda) / 4; Chris@101: zn = (zn + lambda) / 4; Chris@101: Q /= 4; Chris@101: fn *= 4; Chris@101: if(Q < fabs(An)) Chris@101: break; Chris@101: } Chris@101: // Check to see if we gave up too soon: Chris@101: policies::check_series_iterations(function, k, pol); Chris@101: BOOST_MATH_INSTRUMENT_VARIABLE(k); Chris@16: Chris@101: T X = (A0 - x) / (An * fn); Chris@101: T Y = (A0 - y) / (An * fn); Chris@101: T Z = -X - Y; Chris@16: Chris@101: // Taylor series expansion to the 7th order Chris@101: T E2 = X * Y - Z * Z; Chris@101: T E3 = X * Y * Z; Chris@101: return (1 + E3 * (T(1) / 14 + 3 * E3 / 104) + E2 * (T(-1) / 10 + E2 / 24 - (3 * E3) / 44 - 5 * E2 * E2 / 208 + E2 * E3 / 16)) / sqrt(An); Chris@101: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ellint_rf(T1 x, T2 y, T3 z, const Policy& pol) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: typedef typename policies::evaluation::type value_type; Chris@16: return policies::checked_narrowing_cast( Chris@16: detail::ellint_rf_imp( Chris@16: static_cast(x), Chris@16: static_cast(y), Chris@16: static_cast(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)"); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ellint_rf(T1 x, T2 y, T3 z) Chris@16: { Chris@16: return ellint_rf(x, y, z, policies::policy<>()); Chris@16: } Chris@16: Chris@16: }} // namespaces Chris@16: Chris@16: #endif // BOOST_MATH_ELLINT_RF_HPP Chris@16: