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