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