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_1_HPP
15 #define BOOST_MATH_ELLINT_1_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/constants/constants.hpp>
24 #include <boost/math/policies/error_handling.hpp>
25 #include <boost/math/tools/workaround.hpp>
26 #include <boost/math/special_functions/round.hpp>
27 
28 // Elliptic integrals (complete and incomplete) of the first kind
29 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
30 
31 namespace boost { namespace math {
32 
33 template <class T1, class T2, class Policy>
34 typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const Policy& pol);
35 
36 namespace detail{
37 
38 template <typename T, typename Policy>
39 T ellint_k_imp(T k, const Policy& pol);
40 
41 // Elliptic integral (Legendre form) of the first kind
42 template <typename T, typename Policy>
43 T ellint_f_imp(T phi, T k, const Policy& pol)
44 {
45     BOOST_MATH_STD_USING
46     using namespace boost::math::tools;
47     using namespace boost::math::constants;
48 
49     static const char* function = "boost::math::ellint_f<%1%>(%1%,%1%)";
50     BOOST_MATH_INSTRUMENT_VARIABLE(phi);
51     BOOST_MATH_INSTRUMENT_VARIABLE(k);
52     BOOST_MATH_INSTRUMENT_VARIABLE(function);
53 
54     if (abs(k) > 1)
55     {
56        return policies::raise_domain_error<T>(function,
57             "Got k = %1%, function requires |k| <= 1", k, pol);
58     }
59 
60     bool invert = false;
61     if(phi < 0)
62     {
63        BOOST_MATH_INSTRUMENT_VARIABLE(phi);
64        phi = fabs(phi);
65        invert = true;
66     }
67 
68     T result;
69 
70     if(phi >= tools::max_value<T>())
71     {
72        // Need to handle infinity as a special case:
73        result = policies::raise_overflow_error<T>(function, 0, pol);
74        BOOST_MATH_INSTRUMENT_VARIABLE(result);
75     }
76     else if(phi > 1 / tools::epsilon<T>())
77     {
78        // Phi is so large that phi%pi is necessarily zero (or garbage),
79        // just return the second part of the duplication formula:
80        result = 2 * phi * ellint_k_imp(k, pol) / constants::pi<T>();
81        BOOST_MATH_INSTRUMENT_VARIABLE(result);
82     }
83     else
84     {
85        // Carlson's algorithm works only for |phi| <= pi/2,
86        // use the integrand's periodicity to normalize phi
87        //
88        // Xiaogang's original code used a cast to long long here
89        // but that fails if T has more digits than a long long,
90        // so rewritten to use fmod instead:
91        //
92        BOOST_MATH_INSTRUMENT_CODE("pi/2 = " << constants::pi<T>() / 2);
93        T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
94        BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
95        T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
96        BOOST_MATH_INSTRUMENT_VARIABLE(m);
97        int s = 1;
98        if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
99        {
100           m += 1;
101           s = -1;
102           rphi = constants::half_pi<T>() - rphi;
103           BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
104        }
105        T sinp = sin(rphi);
106        sinp *= sinp;
107        T cosp = cos(rphi);
108        cosp *= cosp;
109        BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
110        BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
111        if(sinp > tools::min_value<T>())
112        {
113           //
114           // Use http://dlmf.nist.gov/19.25#E5, note that
115           // c-1 simplifies to cot^2(rphi) which avoid cancellation:
116           //
117           T c = 1 / sinp;
118           result = rphi == 0 ? static_cast<T>(0) : static_cast<T>(s * ellint_rf_imp(T(cosp / sinp), T(c - k * k), c, pol));
119        }
120        else
121           result = s * sin(rphi);
122        BOOST_MATH_INSTRUMENT_VARIABLE(result);
123        if(m != 0)
124        {
125           result += m * ellint_k_imp(k, pol);
126           BOOST_MATH_INSTRUMENT_VARIABLE(result);
127        }
128     }
129     return invert ? T(-result) : result;
130 }
131 
132 // Complete elliptic integral (Legendre form) of the first kind
133 template <typename T, typename Policy>
134 T ellint_k_imp(T k, const Policy& pol)
135 {
136     BOOST_MATH_STD_USING
137     using namespace boost::math::tools;
138 
139     static const char* function = "boost::math::ellint_k<%1%>(%1%)";
140 
141     if (abs(k) > 1)
142     {
143        return policies::raise_domain_error<T>(function,
144             "Got k = %1%, function requires |k| <= 1", k, pol);
145     }
146     if (abs(k) == 1)
147     {
148        return policies::raise_overflow_error<T>(function, 0, pol);
149     }
150 
151     T x = 0;
152     T y = 1 - k * k;
153     T z = 1;
154     T value = ellint_rf_imp(x, y, z, pol);
155 
156     return value;
157 }
158 
159 template <typename T, typename Policy>
ellint_1(T k,const Policy & pol,const mpl::true_ &)160 inline typename tools::promote_args<T>::type ellint_1(T k, const Policy& pol, const mpl::true_&)
161 {
162    typedef typename tools::promote_args<T>::type result_type;
163    typedef typename policies::evaluation<result_type, Policy>::type value_type;
164    return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_k_imp(static_cast<value_type>(k), pol), "boost::math::ellint_1<%1%>(%1%)");
165 }
166 
167 template <class T1, class T2>
ellint_1(T1 k,T2 phi,const mpl::false_ &)168 inline typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const mpl::false_&)
169 {
170    return boost::math::ellint_1(k, phi, policies::policy<>());
171 }
172 
173 }
174 
175 // Complete elliptic integral (Legendre form) of the first kind
176 template <typename T>
ellint_1(T k)177 inline typename tools::promote_args<T>::type ellint_1(T k)
178 {
179    return ellint_1(k, policies::policy<>());
180 }
181 
182 // Elliptic integral (Legendre form) of the first kind
183 template <class T1, class T2, class Policy>
ellint_1(T1 k,T2 phi,const Policy & pol)184 inline typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const Policy& pol)
185 {
186    typedef typename tools::promote_args<T1, T2>::type result_type;
187    typedef typename policies::evaluation<result_type, Policy>::type value_type;
188    return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_f_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_1<%1%>(%1%,%1%)");
189 }
190 
191 template <class T1, class T2>
ellint_1(T1 k,T2 phi)192 inline typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi)
193 {
194    typedef typename policies::is_policy<T2>::type tag_type;
195    return detail::ellint_1(k, phi, tag_type());
196 }
197 
198 }} // namespaces
199 
200 #endif // BOOST_MATH_ELLINT_1_HPP
201 
202