Chris@102: // Copyright (c) 2006 Xiaogang Zhang Chris@102: // Copyright (c) 2006 John Maddock Chris@102: // Use, modification and distribution are subject to the Chris@102: // Boost Software License, Version 1.0. (See accompanying file Chris@102: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@102: // Chris@102: // History: Chris@102: // XZ wrote the original of this file as part of the Google Chris@102: // Summer of Code 2006. JM modified it to fit into the Chris@102: // Boost.Math conceptual framework better, and to ensure Chris@102: // that the code continues to work no matter how many digits Chris@102: // type T has. Chris@102: Chris@102: #ifndef BOOST_MATH_ELLINT_D_HPP Chris@102: #define BOOST_MATH_ELLINT_D_HPP Chris@102: Chris@102: #ifdef _MSC_VER Chris@102: #pragma once Chris@102: #endif Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: // Elliptic integrals (complete and incomplete) of the second kind Chris@102: // Carlson, Numerische Mathematik, vol 33, 1 (1979) Chris@102: Chris@102: namespace boost { namespace math { Chris@102: Chris@102: template Chris@102: typename tools::promote_args::type ellint_d(T1 k, T2 phi, const Policy& pol); Chris@102: Chris@102: namespace detail{ Chris@102: Chris@102: template Chris@102: T ellint_d_imp(T k, const Policy& pol); Chris@102: Chris@102: // Elliptic integral (Legendre form) of the second kind Chris@102: template Chris@102: T ellint_d_imp(T phi, T k, const Policy& pol) Chris@102: { Chris@102: BOOST_MATH_STD_USING Chris@102: using namespace boost::math::tools; Chris@102: using namespace boost::math::constants; Chris@102: Chris@102: bool invert = false; Chris@102: if(phi < 0) Chris@102: { Chris@102: phi = fabs(phi); Chris@102: invert = true; Chris@102: } Chris@102: Chris@102: T result; Chris@102: Chris@102: if(phi >= tools::max_value()) Chris@102: { Chris@102: // Need to handle infinity as a special case: Chris@102: result = policies::raise_overflow_error("boost::math::ellint_e<%1%>(%1%,%1%)", 0, pol); Chris@102: } Chris@102: else if(phi > 1 / tools::epsilon()) Chris@102: { Chris@102: // Phi is so large that phi%pi is necessarily zero (or garbage), Chris@102: // just return the second part of the duplication formula: Chris@102: result = 2 * phi * ellint_d_imp(k, pol) / constants::pi(); Chris@102: } Chris@102: else Chris@102: { Chris@102: // Carlson's algorithm works only for |phi| <= pi/2, Chris@102: // use the integrand's periodicity to normalize phi Chris@102: // Chris@102: T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi())); Chris@102: T m = boost::math::round((phi - rphi) / constants::half_pi()); Chris@102: int s = 1; Chris@102: if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5) Chris@102: { Chris@102: m += 1; Chris@102: s = -1; Chris@102: rphi = constants::half_pi() - rphi; Chris@102: } Chris@102: T sinp = sin(rphi); Chris@102: T cosp = cos(rphi); Chris@102: T c = 1 / (sinp * sinp); Chris@102: T cm1 = cosp * cosp / (sinp * sinp); // c - 1 Chris@102: T k2 = k * k; Chris@102: if(k2 > 1) Chris@102: { Chris@102: return policies::raise_domain_error("boost::math::ellint_d<%1%>(%1%, %1%)", "The parameter k is out of range, got k = %1%", k, pol); Chris@102: } Chris@102: else if(rphi == 0) Chris@102: { Chris@102: result = 0; Chris@102: } Chris@102: else Chris@102: { Chris@102: // http://dlmf.nist.gov/19.25#E10 Chris@102: result = s * ellint_rd_imp(cm1, T(c - k2), c, pol) / 3; Chris@102: } Chris@102: if(m != 0) Chris@102: result += m * ellint_d_imp(k, pol); Chris@102: } Chris@102: return invert ? T(-result) : result; Chris@102: } Chris@102: Chris@102: // Complete elliptic integral (Legendre form) of the second kind Chris@102: template Chris@102: T ellint_d_imp(T k, const Policy& pol) Chris@102: { Chris@102: BOOST_MATH_STD_USING Chris@102: using namespace boost::math::tools; Chris@102: Chris@102: if (abs(k) > 1) Chris@102: { Chris@102: return policies::raise_domain_error("boost::math::ellint_e<%1%>(%1%)", Chris@102: "Got k = %1%, function requires |k| <= 1", k, pol); Chris@102: } Chris@102: if (abs(k) == 1) Chris@102: { Chris@102: return static_cast(1); Chris@102: } Chris@102: if(fabs(k) <= tools::root_epsilon()) Chris@102: return constants::pi() / 4; Chris@102: Chris@102: T x = 0; Chris@102: T t = k * k; Chris@102: T y = 1 - t; Chris@102: T z = 1; Chris@102: T value = ellint_rd_imp(x, y, z, pol) / 3; Chris@102: Chris@102: return value; Chris@102: } Chris@102: Chris@102: template Chris@102: inline typename tools::promote_args::type ellint_d(T k, const Policy& pol, const mpl::true_&) Chris@102: { Chris@102: typedef typename tools::promote_args::type result_type; Chris@102: typedef typename policies::evaluation::type value_type; Chris@102: return policies::checked_narrowing_cast(detail::ellint_d_imp(static_cast(k), pol), "boost::math::ellint_d<%1%>(%1%)"); Chris@102: } Chris@102: Chris@102: // Elliptic integral (Legendre form) of the second kind Chris@102: template Chris@102: inline typename tools::promote_args::type ellint_d(T1 k, T2 phi, const mpl::false_&) Chris@102: { Chris@102: return boost::math::ellint_d(k, phi, policies::policy<>()); Chris@102: } Chris@102: Chris@102: } // detail Chris@102: Chris@102: // Complete elliptic integral (Legendre form) of the second kind Chris@102: template Chris@102: inline typename tools::promote_args::type ellint_d(T k) Chris@102: { Chris@102: return ellint_d(k, policies::policy<>()); Chris@102: } Chris@102: Chris@102: // Elliptic integral (Legendre form) of the second kind Chris@102: template Chris@102: inline typename tools::promote_args::type ellint_d(T1 k, T2 phi) Chris@102: { Chris@102: typedef typename policies::is_policy::type tag_type; Chris@102: return detail::ellint_d(k, phi, tag_type()); Chris@102: } Chris@102: Chris@102: template Chris@102: inline typename tools::promote_args::type ellint_d(T1 k, T2 phi, const Policy& pol) Chris@102: { Chris@102: typedef typename tools::promote_args::type result_type; Chris@102: typedef typename policies::evaluation::type value_type; Chris@102: return policies::checked_narrowing_cast(detail::ellint_d_imp(static_cast(phi), static_cast(k), pol), "boost::math::ellint_2<%1%>(%1%,%1%)"); Chris@102: } Chris@102: Chris@102: }} // namespaces Chris@102: Chris@102: #endif // BOOST_MATH_ELLINT_D_HPP Chris@102: