Chris@16: // Copyright (c) 2006 Xiaogang Zhang Chris@16: // Copyright (c) 2006 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 ensure Chris@16: // that the code continues to work no matter how many digits Chris@16: // type T has. Chris@16: Chris@16: #ifndef BOOST_MATH_ELLINT_2_HPP Chris@16: #define BOOST_MATH_ELLINT_2_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@101: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // Elliptic integrals (complete and incomplete) of the second kind Chris@16: // Carlson, Numerische Mathematik, vol 33, 1 (1979) Chris@16: Chris@16: namespace boost { namespace math { Chris@16: Chris@16: template Chris@16: typename tools::promote_args::type ellint_2(T1 k, T2 phi, const Policy& pol); Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: template Chris@16: T ellint_e_imp(T k, const Policy& pol); Chris@16: Chris@16: // Elliptic integral (Legendre form) of the second kind Chris@16: template Chris@16: T ellint_e_imp(T phi, T k, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: using namespace boost::math::tools; Chris@16: using namespace boost::math::constants; Chris@16: Chris@16: bool invert = false; Chris@16: if(phi < 0) Chris@16: { Chris@16: phi = fabs(phi); Chris@16: invert = true; Chris@16: } Chris@16: Chris@16: T result; Chris@16: Chris@16: if(phi >= tools::max_value()) Chris@16: { Chris@16: // Need to handle infinity as a special case: Chris@16: result = policies::raise_overflow_error("boost::math::ellint_e<%1%>(%1%,%1%)", 0, pol); Chris@16: } Chris@16: else if(phi > 1 / tools::epsilon()) Chris@16: { Chris@16: // Phi is so large that phi%pi is necessarily zero (or garbage), Chris@16: // just return the second part of the duplication formula: Chris@16: result = 2 * phi * ellint_e_imp(k, pol) / constants::pi(); Chris@16: } Chris@101: else if(k == 0) Chris@101: { Chris@101: return invert ? T(-phi) : phi; Chris@101: } Chris@101: else if(fabs(k) == 1) Chris@101: { Chris@101: return invert ? T(-sin(phi)) : sin(phi); Chris@101: } Chris@16: else Chris@16: { Chris@16: // Carlson's algorithm works only for |phi| <= pi/2, Chris@16: // use the integrand's periodicity to normalize phi Chris@16: // Chris@16: // Xiaogang's original code used a cast to long long here Chris@16: // but that fails if T has more digits than a long long, Chris@16: // so rewritten to use fmod instead: Chris@16: // Chris@16: T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi())); Chris@16: T m = boost::math::round((phi - rphi) / constants::half_pi()); Chris@16: int s = 1; Chris@16: if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5) Chris@16: { Chris@16: m += 1; Chris@16: s = -1; Chris@16: rphi = constants::half_pi() - rphi; Chris@16: } Chris@16: T sinp = sin(rphi); Chris@16: T cosp = cos(rphi); Chris@101: T c = 1 / (sinp * sinp); Chris@101: T cm1 = cosp * cosp / (sinp * sinp); // c - 1 Chris@101: T k2 = k * k; Chris@101: if(k2 > 1) Chris@101: { Chris@101: return policies::raise_domain_error("boost::math::ellint_2<%1%>(%1%, %1%)", "The parameter k is out of range, got k = %1%", k, pol); Chris@101: } Chris@101: else if(rphi == 0) Chris@101: { Chris@101: result = 0; Chris@101: } Chris@101: else if(sinp * sinp < tools::min_value()) Chris@101: { Chris@101: T x = cosp * cosp; Chris@101: T t = k * k * sinp * sinp; Chris@101: T y = 1 - t; Chris@101: T z = 1; Chris@101: result = s * sinp * (ellint_rf_imp(x, y, z, pol) - t * ellint_rd_imp(x, y, z, pol) / 3); Chris@101: } Chris@101: else Chris@101: { Chris@101: // http://dlmf.nist.gov/19.25#E10 Chris@101: result = s * ((1 - k2) * ellint_rf_imp(cm1, T(c - k2), c, pol) + k2 * (1 - k2) * ellint_rd(cm1, c, T(c - k2), pol) / 3 + k2 * sqrt(cm1 / (c * (c - k2)))); Chris@101: } Chris@16: if(m != 0) Chris@16: result += m * ellint_e_imp(k, pol); Chris@16: } Chris@16: return invert ? T(-result) : result; Chris@16: } Chris@16: Chris@16: // Complete elliptic integral (Legendre form) of the second kind Chris@16: template Chris@16: T ellint_e_imp(T k, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: using namespace boost::math::tools; Chris@16: Chris@16: if (abs(k) > 1) Chris@16: { Chris@16: return policies::raise_domain_error("boost::math::ellint_e<%1%>(%1%)", Chris@16: "Got k = %1%, function requires |k| <= 1", k, pol); Chris@16: } Chris@16: if (abs(k) == 1) Chris@16: { Chris@16: return static_cast(1); Chris@16: } Chris@16: Chris@16: T x = 0; Chris@16: T t = k * k; Chris@16: T y = 1 - t; Chris@16: T z = 1; Chris@101: T value = 2 * ellint_rg_imp(x, y, z, pol); Chris@16: Chris@16: return value; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_2(T k, const Policy& pol, const mpl::true_&) 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(detail::ellint_e_imp(static_cast(k), pol), "boost::math::ellint_2<%1%>(%1%)"); Chris@16: } Chris@16: Chris@16: // Elliptic integral (Legendre form) of the second kind Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_2(T1 k, T2 phi, const mpl::false_&) Chris@16: { Chris@16: return boost::math::ellint_2(k, phi, policies::policy<>()); Chris@16: } Chris@16: Chris@16: } // detail Chris@16: Chris@16: // Complete elliptic integral (Legendre form) of the second kind Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_2(T k) Chris@16: { Chris@16: return ellint_2(k, policies::policy<>()); Chris@16: } Chris@16: Chris@16: // Elliptic integral (Legendre form) of the second kind Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_2(T1 k, T2 phi) Chris@16: { Chris@16: typedef typename policies::is_policy::type tag_type; Chris@16: return detail::ellint_2(k, phi, tag_type()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_2(T1 k, T2 phi, 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(detail::ellint_e_imp(static_cast(phi), static_cast(k), pol), "boost::math::ellint_2<%1%>(%1%,%1%)"); Chris@16: } Chris@16: Chris@16: }} // namespaces Chris@16: Chris@16: #endif // BOOST_MATH_ELLINT_2_HPP Chris@16: