]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/math/special_functions/ellint_2.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / math / special_functions / ellint_2.hpp
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_2_HPP
15 #define BOOST_MATH_ELLINT_2_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_2(T1 k, T2 phi, const Policy& pol);
37
38 namespace detail{
39
40 template <typename T, typename Policy>
41 T ellint_e_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_e_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 return 0;
54
55 if(phi < 0)
56 {
57 phi = fabs(phi);
58 invert = true;
59 }
60
61 T result;
62
63 if(phi >= tools::max_value<T>())
64 {
65 // Need to handle infinity as a special case:
66 result = policies::raise_overflow_error<T>("boost::math::ellint_e<%1%>(%1%,%1%)", 0, pol);
67 }
68 else if(phi > 1 / tools::epsilon<T>())
69 {
70 // Phi is so large that phi%pi is necessarily zero (or garbage),
71 // just return the second part of the duplication formula:
72 result = 2 * phi * ellint_e_imp(k, pol) / constants::pi<T>();
73 }
74 else if(k == 0)
75 {
76 return invert ? T(-phi) : phi;
77 }
78 else if(fabs(k) == 1)
79 {
80 //
81 // For k = 1 ellipse actually turns to a line and every pi/2 in phi is exactly 1 in arc length
82 // Periodicity though is in pi, curve follows sin(pi) for 0 <= phi <= pi/2 and then
83 // 2 - sin(pi- phi) = 2 + sin(phi - pi) for pi/2 <= phi <= pi, so general form is:
84 //
85 // 2n + sin(phi - n * pi) ; |phi - n * pi| <= pi / 2
86 //
87 T m = boost::math::round(phi / boost::math::constants::pi<T>());
88 T remains = phi - m * boost::math::constants::pi<T>();
89 T value = 2 * m + sin(remains);
90
91 // negative arc length for negative phi
92 return invert ? -value : value;
93 }
94 else
95 {
96 // Carlson's algorithm works only for |phi| <= pi/2,
97 // use the integrand's periodicity to normalize phi
98 //
99 // Xiaogang's original code used a cast to long long here
100 // but that fails if T has more digits than a long long,
101 // so rewritten to use fmod instead:
102 //
103 T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
104 T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
105 int s = 1;
106 if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
107 {
108 m += 1;
109 s = -1;
110 rphi = constants::half_pi<T>() - rphi;
111 }
112 T k2 = k * k;
113 if(boost::math::pow<3>(rphi) * k2 / 6 < tools::epsilon<T>() * fabs(rphi))
114 {
115 // See http://functions.wolfram.com/EllipticIntegrals/EllipticE2/06/01/03/0001/
116 result = s * rphi;
117 }
118 else
119 {
120 // http://dlmf.nist.gov/19.25#E10
121 T sinp = sin(rphi);
122 if (k2 * sinp * sinp >= 1)
123 {
124 return policies::raise_domain_error<T>("boost::math::ellint_2<%1%>(%1%, %1%)", "The parameter k is out of range, got k = %1%", k, pol);
125 }
126 T cosp = cos(rphi);
127 T c = 1 / (sinp * sinp);
128 T cm1 = cosp * cosp / (sinp * sinp); // c - 1
129 result = s * ((1 - k2) * ellint_rf_imp(cm1, T(c - k2), c, pol) + k2 * (1 - k2) * ellint_rd(cm1, c, T(c - k2), pol) / 3 + k2 * sqrt(cm1 / (c * (c - k2))));
130 }
131 if(m != 0)
132 result += m * ellint_e_imp(k, pol);
133 }
134 return invert ? T(-result) : result;
135 }
136
137 // Complete elliptic integral (Legendre form) of the second kind
138 template <typename T, typename Policy>
139 T ellint_e_imp(T k, const Policy& pol)
140 {
141 BOOST_MATH_STD_USING
142 using namespace boost::math::tools;
143
144 if (abs(k) > 1)
145 {
146 return policies::raise_domain_error<T>("boost::math::ellint_e<%1%>(%1%)",
147 "Got k = %1%, function requires |k| <= 1", k, pol);
148 }
149 if (abs(k) == 1)
150 {
151 return static_cast<T>(1);
152 }
153
154 T x = 0;
155 T t = k * k;
156 T y = 1 - t;
157 T z = 1;
158 T value = 2 * ellint_rg_imp(x, y, z, pol);
159
160 return value;
161 }
162
163 template <typename T, typename Policy>
164 inline typename tools::promote_args<T>::type ellint_2(T k, const Policy& pol, const std::true_type&)
165 {
166 typedef typename tools::promote_args<T>::type result_type;
167 typedef typename policies::evaluation<result_type, Policy>::type value_type;
168 return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_e_imp(static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%)");
169 }
170
171 // Elliptic integral (Legendre form) of the second kind
172 template <class T1, class T2>
173 inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const std::false_type&)
174 {
175 return boost::math::ellint_2(k, phi, policies::policy<>());
176 }
177
178 } // detail
179
180 // Complete elliptic integral (Legendre form) of the second kind
181 template <typename T>
182 inline typename tools::promote_args<T>::type ellint_2(T k)
183 {
184 return ellint_2(k, policies::policy<>());
185 }
186
187 // Elliptic integral (Legendre form) of the second kind
188 template <class T1, class T2>
189 inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi)
190 {
191 typedef typename policies::is_policy<T2>::type tag_type;
192 return detail::ellint_2(k, phi, tag_type());
193 }
194
195 template <class T1, class T2, class Policy>
196 inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const Policy& pol)
197 {
198 typedef typename tools::promote_args<T1, T2>::type result_type;
199 typedef typename policies::evaluation<result_type, Policy>::type value_type;
200 return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_e_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%,%1%)");
201 }
202
203 }} // namespaces
204
205 #endif // BOOST_MATH_ELLINT_2_HPP
206