]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/math/special_functions/ellint_1.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / math / special_functions / ellint_1.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_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 bool invert = false;
55 if(phi < 0)
56 {
57 BOOST_MATH_INSTRUMENT_VARIABLE(phi);
58 phi = fabs(phi);
59 invert = true;
60 }
61
62 T result;
63
64 if(phi >= tools::max_value<T>())
65 {
66 // Need to handle infinity as a special case:
67 result = policies::raise_overflow_error<T>(function, 0, pol);
68 BOOST_MATH_INSTRUMENT_VARIABLE(result);
69 }
70 else if(phi > 1 / tools::epsilon<T>())
71 {
72 // Phi is so large that phi%pi is necessarily zero (or garbage),
73 // just return the second part of the duplication formula:
74 result = 2 * phi * ellint_k_imp(k, pol) / constants::pi<T>();
75 BOOST_MATH_INSTRUMENT_VARIABLE(result);
76 }
77 else
78 {
79 // Carlson's algorithm works only for |phi| <= pi/2,
80 // use the integrand's periodicity to normalize phi
81 //
82 // Xiaogang's original code used a cast to long long here
83 // but that fails if T has more digits than a long long,
84 // so rewritten to use fmod instead:
85 //
86 BOOST_MATH_INSTRUMENT_CODE("pi/2 = " << constants::pi<T>() / 2);
87 T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
88 BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
89 T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
90 BOOST_MATH_INSTRUMENT_VARIABLE(m);
91 int s = 1;
92 if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
93 {
94 m += 1;
95 s = -1;
96 rphi = constants::half_pi<T>() - rphi;
97 BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
98 }
99 T sinp = sin(rphi);
100 sinp *= sinp;
101 if (sinp * k * k >= 1)
102 {
103 return policies::raise_domain_error<T>(function,
104 "Got k^2 * sin^2(phi) = %1%, but the function requires this < 1", sinp * k * k, pol);
105 }
106 T cosp = cos(rphi);
107 cosp *= cosp;
108 BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
109 BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
110 if(sinp > tools::min_value<T>())
111 {
112 BOOST_ASSERT(rphi != 0); // precondition, can't be true if sin(rphi) != 0.
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 = 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>
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>
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>
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>
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>
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