1 //  Copyright (c) 2006 Xiaogang Zhang
2 //  Copyright (c) 2006 John Maddock
3 //  Use, modification and distribution are subject to the
4 //  Boost Software License, Version 1.0. (See accompanying file
5 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 //  History:
8 //  XZ wrote the original of this file as part of the Google
9 //  Summer of Code 2006.  JM modified it to fit into the
10 //  Boost.Math conceptual framework better, and to ensure
11 //  that the code continues to work no matter how many digits
12 //  type T has.
13 
14 #ifndef BOOST_MATH_ELLINT_D_HPP
15 #define BOOST_MATH_ELLINT_D_HPP
16 
17 #ifdef _MSC_VER
18 #pragma once
19 #endif
20 
21 #include <boost/math/special_functions/math_fwd.hpp>
22 #include <boost/math/special_functions/ellint_rf.hpp>
23 #include <boost/math/special_functions/ellint_rd.hpp>
24 #include <boost/math/special_functions/ellint_rg.hpp>
25 #include <boost/math/constants/constants.hpp>
26 #include <boost/math/policies/error_handling.hpp>
27 #include <boost/math/tools/workaround.hpp>
28 #include <boost/math/special_functions/round.hpp>
29 
30 // Elliptic integrals (complete and incomplete) of the second kind
31 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
32 
33 namespace boost { namespace math {
34 
35 template <class T1, class T2, class Policy>
36 typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol);
37 
38 namespace detail{
39 
40 template <typename T, typename Policy>
41 T ellint_d_imp(T k, const Policy& pol);
42 
43 // Elliptic integral (Legendre form) of the second kind
44 template <typename T, typename Policy>
45 T ellint_d_imp(T phi, T k, const Policy& pol)
46 {
47     BOOST_MATH_STD_USING
48     using namespace boost::math::tools;
49     using namespace boost::math::constants;
50 
51     bool invert = false;
52     if(phi < 0)
53     {
54        phi = fabs(phi);
55        invert = true;
56     }
57 
58     T result;
59 
60     if(phi >= tools::max_value<T>())
61     {
62        // Need to handle infinity as a special case:
63        result = policies::raise_overflow_error<T>("boost::math::ellint_d<%1%>(%1%,%1%)", 0, pol);
64     }
65     else if(phi > 1 / tools::epsilon<T>())
66     {
67        // Phi is so large that phi%pi is necessarily zero (or garbage),
68        // just return the second part of the duplication formula:
69        result = 2 * phi * ellint_d_imp(k, pol) / constants::pi<T>();
70     }
71     else
72     {
73        // Carlson's algorithm works only for |phi| <= pi/2,
74        // use the integrand's periodicity to normalize phi
75        //
76        T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
77        T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
78        int s = 1;
79        if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
80        {
81           m += 1;
82           s = -1;
83           rphi = constants::half_pi<T>() - rphi;
84        }
85        BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
86        BOOST_MATH_INSTRUMENT_VARIABLE(m);
87        T sinp = sin(rphi);
88        T cosp = cos(rphi);
89        BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
90        BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
91        T c = 1 / (sinp * sinp);
92        T cm1 = cosp * cosp / (sinp * sinp);  // c - 1
93        T k2 = k * k;
94        if(k2 * sinp * sinp > 1)
95        {
96           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);
97        }
98        else if(rphi == 0)
99        {
100           result = 0;
101        }
102        else
103        {
104           // http://dlmf.nist.gov/19.25#E10
105           result = s * ellint_rd_imp(cm1, T(c - k2), c, pol) / 3;
106           BOOST_MATH_INSTRUMENT_VARIABLE(result);
107        }
108        if(m != 0)
109           result += m * ellint_d_imp(k, pol);
110     }
111     return invert ? T(-result) : result;
112 }
113 
114 // Complete elliptic integral (Legendre form) of the second kind
115 template <typename T, typename Policy>
116 T ellint_d_imp(T k, const Policy& pol)
117 {
118     BOOST_MATH_STD_USING
119     using namespace boost::math::tools;
120 
121     if (abs(k) >= 1)
122     {
123        return policies::raise_domain_error<T>("boost::math::ellint_d<%1%>(%1%)",
124             "Got k = %1%, function requires |k| <= 1", k, pol);
125     }
126     if(fabs(k) <= tools::root_epsilon<T>())
127        return constants::pi<T>() / 4;
128 
129     T x = 0;
130     T t = k * k;
131     T y = 1 - t;
132     T z = 1;
133     T value = ellint_rd_imp(x, y, z, pol) / 3;
134 
135     return value;
136 }
137 
138 template <typename T, typename Policy>
ellint_d(T k,const Policy & pol,const std::true_type &)139 inline typename tools::promote_args<T>::type ellint_d(T k, const Policy& pol, const std::true_type&)
140 {
141    typedef typename tools::promote_args<T>::type result_type;
142    typedef typename policies::evaluation<result_type, Policy>::type value_type;
143    return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_d_imp(static_cast<value_type>(k), pol), "boost::math::ellint_d<%1%>(%1%)");
144 }
145 
146 // Elliptic integral (Legendre form) of the second kind
147 template <class T1, class T2>
ellint_d(T1 k,T2 phi,const std::false_type &)148 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const std::false_type&)
149 {
150    return boost::math::ellint_d(k, phi, policies::policy<>());
151 }
152 
153 } // detail
154 
155 // Complete elliptic integral (Legendre form) of the second kind
156 template <typename T>
ellint_d(T k)157 inline typename tools::promote_args<T>::type ellint_d(T k)
158 {
159    return ellint_d(k, policies::policy<>());
160 }
161 
162 // Elliptic integral (Legendre form) of the second kind
163 template <class T1, class T2>
ellint_d(T1 k,T2 phi)164 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi)
165 {
166    typedef typename policies::is_policy<T2>::type tag_type;
167    return detail::ellint_d(k, phi, tag_type());
168 }
169 
170 template <class T1, class T2, class Policy>
ellint_d(T1 k,T2 phi,const Policy & pol)171 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol)
172 {
173    typedef typename tools::promote_args<T1, T2>::type result_type;
174    typedef typename policies::evaluation<result_type, Policy>::type value_type;
175    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%)");
176 }
177 
178 }} // namespaces
179 
180 #endif // BOOST_MATH_ELLINT_D_HPP
181 
182