Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:11

0001 //  Copyright (c) 2006 Xiaogang Zhang
0002 //  Copyright (c) 2006 John Maddock
0003 //  Use, modification and distribution are subject to the
0004 //  Boost Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 //  History:
0008 //  XZ wrote the original of this file as part of the Google
0009 //  Summer of Code 2006.  JM modified it to fit into the
0010 //  Boost.Math conceptual framework better, and to ensure
0011 //  that the code continues to work no matter how many digits
0012 //  type T has.
0013 
0014 #ifndef BOOST_MATH_ELLINT_D_HPP
0015 #define BOOST_MATH_ELLINT_D_HPP
0016 
0017 #ifdef _MSC_VER
0018 #pragma once
0019 #endif
0020 
0021 #include <boost/math/special_functions/math_fwd.hpp>
0022 #include <boost/math/special_functions/ellint_rf.hpp>
0023 #include <boost/math/special_functions/ellint_rd.hpp>
0024 #include <boost/math/special_functions/ellint_rg.hpp>
0025 #include <boost/math/constants/constants.hpp>
0026 #include <boost/math/policies/error_handling.hpp>
0027 #include <boost/math/tools/workaround.hpp>
0028 #include <boost/math/special_functions/round.hpp>
0029 
0030 // Elliptic integrals (complete and incomplete) of the second kind
0031 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
0032 
0033 namespace boost { namespace math {
0034 
0035 template <class T1, class T2, class Policy>
0036 typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol);
0037 
0038 namespace detail{
0039 
0040 template <typename T, typename Policy>
0041 T ellint_d_imp(T k, const Policy& pol);
0042 
0043 // Elliptic integral (Legendre form) of the second kind
0044 template <typename T, typename Policy>
0045 T ellint_d_imp(T phi, T k, const Policy& pol)
0046 {
0047     BOOST_MATH_STD_USING
0048     using namespace boost::math::tools;
0049     using namespace boost::math::constants;
0050 
0051     bool invert = false;
0052     if(phi < 0)
0053     {
0054        phi = fabs(phi);
0055        invert = true;
0056     }
0057 
0058     T result;
0059 
0060     if(phi >= tools::max_value<T>())
0061     {
0062        // Need to handle infinity as a special case:
0063        result = policies::raise_overflow_error<T>("boost::math::ellint_d<%1%>(%1%,%1%)", nullptr, pol);
0064     }
0065     else if(phi > 1 / tools::epsilon<T>())
0066     {
0067        // Phi is so large that phi%pi is necessarily zero (or garbage),
0068        // just return the second part of the duplication formula:
0069        result = 2 * phi * ellint_d_imp(k, pol) / constants::pi<T>();
0070     }
0071     else
0072     {
0073        // Carlson's algorithm works only for |phi| <= pi/2,
0074        // use the integrand's periodicity to normalize phi
0075        //
0076        T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
0077        T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
0078        int s = 1;
0079        if(boost::math::tools::fmod_workaround(m, T(2)) > T(0.5))
0080        {
0081           m += 1;
0082           s = -1;
0083           rphi = constants::half_pi<T>() - rphi;
0084        }
0085        BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
0086        BOOST_MATH_INSTRUMENT_VARIABLE(m);
0087        T sinp = sin(rphi);
0088        T cosp = cos(rphi);
0089        BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
0090        BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
0091        T c = 1 / (sinp * sinp);
0092        T cm1 = cosp * cosp / (sinp * sinp);  // c - 1
0093        T k2 = k * k;
0094        if(k2 * sinp * sinp > 1)
0095        {
0096           return policies::raise_domain_error<T>("boost::math::ellint_d<%1%>(%1%, %1%)", "The parameter k is out of range, got k = %1%", k, pol);
0097        }
0098        else if(rphi == 0)
0099        {
0100           result = 0;
0101        }
0102        else
0103        {
0104           // http://dlmf.nist.gov/19.25#E10
0105           result = s * ellint_rd_imp(cm1, T(c - k2), c, pol) / 3;
0106           BOOST_MATH_INSTRUMENT_VARIABLE(result);
0107        }
0108        if(m != 0)
0109           result += m * ellint_d_imp(k, pol);
0110     }
0111     return invert ? T(-result) : result;
0112 }
0113 
0114 // Complete elliptic integral (Legendre form) of the second kind
0115 template <typename T, typename Policy>
0116 T ellint_d_imp(T k, const Policy& pol)
0117 {
0118     BOOST_MATH_STD_USING
0119     using namespace boost::math::tools;
0120 
0121     if (abs(k) >= 1)
0122     {
0123        return policies::raise_domain_error<T>("boost::math::ellint_d<%1%>(%1%)",
0124             "Got k = %1%, function requires |k| <= 1", k, pol);
0125     }
0126     if(fabs(k) <= tools::root_epsilon<T>())
0127        return constants::pi<T>() / 4;
0128 
0129     T x = 0;
0130     T t = k * k;
0131     T y = 1 - t;
0132     T z = 1;
0133     T value = ellint_rd_imp(x, y, z, pol) / 3;
0134 
0135     return value;
0136 }
0137 
0138 template <typename T, typename Policy>
0139 inline typename tools::promote_args<T>::type ellint_d(T k, const Policy& pol, const std::true_type&)
0140 {
0141    typedef typename tools::promote_args<T>::type result_type;
0142    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0143    return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_d_imp(static_cast<value_type>(k), pol), "boost::math::ellint_d<%1%>(%1%)");
0144 }
0145 
0146 // Elliptic integral (Legendre form) of the second kind
0147 template <class T1, class T2>
0148 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const std::false_type&)
0149 {
0150    return boost::math::ellint_d(k, phi, policies::policy<>());
0151 }
0152 
0153 } // detail
0154 
0155 // Complete elliptic integral (Legendre form) of the second kind
0156 template <typename T>
0157 inline typename tools::promote_args<T>::type ellint_d(T k)
0158 {
0159    return ellint_d(k, policies::policy<>());
0160 }
0161 
0162 // Elliptic integral (Legendre form) of the second kind
0163 template <class T1, class T2>
0164 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi)
0165 {
0166    typedef typename policies::is_policy<T2>::type tag_type;
0167    return detail::ellint_d(k, phi, tag_type());
0168 }
0169 
0170 template <class T1, class T2, class Policy>
0171 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol)
0172 {
0173    typedef typename tools::promote_args<T1, T2>::type result_type;
0174    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0175    return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_d_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%,%1%)");
0176 }
0177 
0178 }} // namespaces
0179 
0180 #endif // BOOST_MATH_ELLINT_D_HPP
0181