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_1_HPP Chris@16: #define BOOST_MATH_ELLINT_1_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@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // Elliptic integrals (complete and incomplete) of the first 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_1(T1 k, T2 phi, const Policy& pol); Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: template Chris@16: T ellint_k_imp(T k, const Policy& pol); Chris@16: Chris@16: // Elliptic integral (Legendre form) of the first kind Chris@16: template Chris@16: T ellint_f_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: static const char* function = "boost::math::ellint_f<%1%>(%1%,%1%)"; Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(phi); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(k); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(function); Chris@16: Chris@16: if (abs(k) > 1) Chris@16: { Chris@16: return policies::raise_domain_error(function, Chris@16: "Got k = %1%, function requires |k| <= 1", k, pol); Chris@16: } Chris@16: Chris@16: bool invert = false; Chris@16: if(phi < 0) Chris@16: { Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(phi); 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(function, 0, pol); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(result); 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_k_imp(k, pol) / constants::pi(); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(result); Chris@16: } 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: BOOST_MATH_INSTRUMENT_CODE("pi/2 = " << constants::pi() / 2); Chris@16: T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi())); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(rphi); Chris@16: T m = boost::math::round((phi - rphi) / constants::half_pi()); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(m); 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: BOOST_MATH_INSTRUMENT_VARIABLE(rphi); Chris@16: } Chris@16: T sinp = sin(rphi); Chris@101: sinp *= sinp; Chris@16: T cosp = cos(rphi); Chris@101: cosp *= cosp; Chris@101: T c = 1 / sinp; Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(sinp); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(cosp); Chris@101: if(sinp > tools::min_value()) Chris@101: { Chris@101: // Chris@101: // Use http://dlmf.nist.gov/19.25#E5, note that Chris@101: // c-1 simplifies to cot^2(rphi) which avoid cancellation: Chris@101: // Chris@101: result = rphi == 0 ? static_cast(0) : static_cast(s * ellint_rf_imp(T(cosp / sinp), T(c - k * k), c, pol)); Chris@101: } Chris@101: else Chris@101: result = s * sin(rphi); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(result); Chris@16: if(m != 0) Chris@16: { Chris@16: result += m * ellint_k_imp(k, pol); Chris@16: BOOST_MATH_INSTRUMENT_VARIABLE(result); Chris@16: } Chris@16: } Chris@16: return invert ? T(-result) : result; Chris@16: } Chris@16: Chris@16: // Complete elliptic integral (Legendre form) of the first kind Chris@16: template Chris@16: T ellint_k_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: static const char* function = "boost::math::ellint_k<%1%>(%1%)"; Chris@16: Chris@16: if (abs(k) > 1) Chris@16: { Chris@16: return policies::raise_domain_error(function, Chris@16: "Got k = %1%, function requires |k| <= 1", k, pol); Chris@16: } Chris@16: if (abs(k) == 1) Chris@16: { Chris@16: return policies::raise_overflow_error(function, 0, pol); Chris@16: } Chris@16: Chris@16: T x = 0; Chris@16: T y = 1 - k * k; Chris@16: T z = 1; Chris@16: T value = ellint_rf_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_1(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_k_imp(static_cast(k), pol), "boost::math::ellint_1<%1%>(%1%)"); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_1(T1 k, T2 phi, const mpl::false_&) Chris@16: { Chris@16: return boost::math::ellint_1(k, phi, policies::policy<>()); Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: // Complete elliptic integral (Legendre form) of the first kind Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_1(T k) Chris@16: { Chris@16: return ellint_1(k, policies::policy<>()); Chris@16: } Chris@16: Chris@16: // Elliptic integral (Legendre form) of the first kind Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_1(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_f_imp(static_cast(phi), static_cast(k), pol), "boost::math::ellint_1<%1%>(%1%,%1%)"); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type ellint_1(T1 k, T2 phi) Chris@16: { Chris@16: typedef typename policies::is_policy::type tag_type; Chris@16: return detail::ellint_1(k, phi, tag_type()); Chris@16: } Chris@16: Chris@16: }} // namespaces Chris@16: Chris@16: #endif // BOOST_MATH_ELLINT_1_HPP Chris@16: