Chris@101
|
1 // Copyright (c) 2006 Xiaogang Zhang, 2015 John Maddock
|
Chris@16
|
2 // Use, modification and distribution are subject to the
|
Chris@16
|
3 // Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 //
|
Chris@16
|
6 // History:
|
Chris@16
|
7 // XZ wrote the original of this file as part of the Google
|
Chris@16
|
8 // Summer of Code 2006. JM modified it to fit into the
|
Chris@16
|
9 // Boost.Math conceptual framework better, and to handle
|
Chris@16
|
10 // types longer than 80-bit reals.
|
Chris@101
|
11 // Updated 2015 to use Carlson's latest methods.
|
Chris@16
|
12 //
|
Chris@16
|
13 #ifndef BOOST_MATH_ELLINT_RF_HPP
|
Chris@16
|
14 #define BOOST_MATH_ELLINT_RF_HPP
|
Chris@16
|
15
|
Chris@16
|
16 #ifdef _MSC_VER
|
Chris@16
|
17 #pragma once
|
Chris@16
|
18 #endif
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/math/special_functions/math_fwd.hpp>
|
Chris@16
|
21 #include <boost/math/tools/config.hpp>
|
Chris@101
|
22 #include <boost/math/constants/constants.hpp>
|
Chris@16
|
23 #include <boost/math/policies/error_handling.hpp>
|
Chris@101
|
24 #include <boost/math/special_functions/ellint_rc.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 // Carlson's elliptic integral of the first kind
|
Chris@16
|
27 // R_F(x, y, z) = 0.5 * \int_{0}^{\infty} [(t+x)(t+y)(t+z)]^{-1/2} dt
|
Chris@16
|
28 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost { namespace math { namespace detail{
|
Chris@16
|
31
|
Chris@101
|
32 template <typename T, typename Policy>
|
Chris@101
|
33 T ellint_rf_imp(T x, T y, T z, const Policy& pol)
|
Chris@101
|
34 {
|
Chris@101
|
35 BOOST_MATH_STD_USING
|
Chris@101
|
36 using namespace boost::math;
|
Chris@101
|
37 using std::swap;
|
Chris@16
|
38
|
Chris@101
|
39 static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
|
Chris@16
|
40
|
Chris@101
|
41 if(x < 0 || y < 0 || z < 0)
|
Chris@101
|
42 {
|
Chris@101
|
43 return policies::raise_domain_error<T>(function,
|
Chris@16
|
44 "domain error, all arguments must be non-negative, "
|
Chris@16
|
45 "only sensible result is %1%.",
|
Chris@16
|
46 std::numeric_limits<T>::quiet_NaN(), pol);
|
Chris@101
|
47 }
|
Chris@101
|
48 if(x + y == 0 || y + z == 0 || z + x == 0)
|
Chris@101
|
49 {
|
Chris@101
|
50 return policies::raise_domain_error<T>(function,
|
Chris@16
|
51 "domain error, at most one argument can be zero, "
|
Chris@16
|
52 "only sensible result is %1%.",
|
Chris@16
|
53 std::numeric_limits<T>::quiet_NaN(), pol);
|
Chris@101
|
54 }
|
Chris@101
|
55 //
|
Chris@101
|
56 // Special cases from http://dlmf.nist.gov/19.20#i
|
Chris@101
|
57 //
|
Chris@101
|
58 if(x == y)
|
Chris@101
|
59 {
|
Chris@101
|
60 if(x == z)
|
Chris@101
|
61 {
|
Chris@101
|
62 // x, y, z equal:
|
Chris@101
|
63 return 1 / sqrt(x);
|
Chris@101
|
64 }
|
Chris@101
|
65 else
|
Chris@101
|
66 {
|
Chris@101
|
67 // 2 equal, x and y:
|
Chris@101
|
68 if(z == 0)
|
Chris@101
|
69 return constants::pi<T>() / (2 * sqrt(x));
|
Chris@101
|
70 else
|
Chris@101
|
71 return ellint_rc_imp(z, x, pol);
|
Chris@101
|
72 }
|
Chris@101
|
73 }
|
Chris@101
|
74 if(x == z)
|
Chris@101
|
75 {
|
Chris@101
|
76 if(y == 0)
|
Chris@101
|
77 return constants::pi<T>() / (2 * sqrt(x));
|
Chris@101
|
78 else
|
Chris@101
|
79 return ellint_rc_imp(y, x, pol);
|
Chris@101
|
80 }
|
Chris@101
|
81 if(y == z)
|
Chris@101
|
82 {
|
Chris@101
|
83 if(x == 0)
|
Chris@101
|
84 return constants::pi<T>() / (2 * sqrt(y));
|
Chris@101
|
85 else
|
Chris@101
|
86 return ellint_rc_imp(x, y, pol);
|
Chris@101
|
87 }
|
Chris@101
|
88 if(x == 0)
|
Chris@101
|
89 swap(x, z);
|
Chris@101
|
90 else if(y == 0)
|
Chris@101
|
91 swap(y, z);
|
Chris@101
|
92 if(z == 0)
|
Chris@101
|
93 {
|
Chris@101
|
94 //
|
Chris@101
|
95 // Special case for one value zero:
|
Chris@101
|
96 //
|
Chris@101
|
97 T xn = sqrt(x);
|
Chris@101
|
98 T yn = sqrt(y);
|
Chris@16
|
99
|
Chris@101
|
100 while(fabs(xn - yn) >= 2.7 * tools::root_epsilon<T>() * fabs(xn))
|
Chris@101
|
101 {
|
Chris@101
|
102 T t = sqrt(xn * yn);
|
Chris@101
|
103 xn = (xn + yn) / 2;
|
Chris@101
|
104 yn = t;
|
Chris@101
|
105 }
|
Chris@101
|
106 return constants::pi<T>() / (xn + yn);
|
Chris@101
|
107 }
|
Chris@16
|
108
|
Chris@101
|
109 T xn = x;
|
Chris@101
|
110 T yn = y;
|
Chris@101
|
111 T zn = z;
|
Chris@101
|
112 T An = (x + y + z) / 3;
|
Chris@101
|
113 T A0 = An;
|
Chris@101
|
114 T Q = pow(3 * boost::math::tools::epsilon<T>(), T(-1) / 8) * (std::max)((std::max)(fabs(An - xn), fabs(An - yn)), fabs(An - zn));
|
Chris@101
|
115 T fn = 1;
|
Chris@16
|
116
|
Chris@16
|
117
|
Chris@101
|
118 // duplication
|
Chris@101
|
119 unsigned k = 1;
|
Chris@101
|
120 for(; k < boost::math::policies::get_max_series_iterations<Policy>(); ++k)
|
Chris@101
|
121 {
|
Chris@101
|
122 T root_x = sqrt(xn);
|
Chris@101
|
123 T root_y = sqrt(yn);
|
Chris@101
|
124 T root_z = sqrt(zn);
|
Chris@101
|
125 T lambda = root_x * root_y + root_x * root_z + root_y * root_z;
|
Chris@101
|
126 An = (An + lambda) / 4;
|
Chris@101
|
127 xn = (xn + lambda) / 4;
|
Chris@101
|
128 yn = (yn + lambda) / 4;
|
Chris@101
|
129 zn = (zn + lambda) / 4;
|
Chris@101
|
130 Q /= 4;
|
Chris@101
|
131 fn *= 4;
|
Chris@101
|
132 if(Q < fabs(An))
|
Chris@101
|
133 break;
|
Chris@101
|
134 }
|
Chris@101
|
135 // Check to see if we gave up too soon:
|
Chris@101
|
136 policies::check_series_iterations<T>(function, k, pol);
|
Chris@101
|
137 BOOST_MATH_INSTRUMENT_VARIABLE(k);
|
Chris@16
|
138
|
Chris@101
|
139 T X = (A0 - x) / (An * fn);
|
Chris@101
|
140 T Y = (A0 - y) / (An * fn);
|
Chris@101
|
141 T Z = -X - Y;
|
Chris@16
|
142
|
Chris@101
|
143 // Taylor series expansion to the 7th order
|
Chris@101
|
144 T E2 = X * Y - Z * Z;
|
Chris@101
|
145 T E3 = X * Y * Z;
|
Chris@101
|
146 return (1 + E3 * (T(1) / 14 + 3 * E3 / 104) + E2 * (T(-1) / 10 + E2 / 24 - (3 * E3) / 44 - 5 * E2 * E2 / 208 + E2 * E3 / 16)) / sqrt(An);
|
Chris@101
|
147 }
|
Chris@16
|
148
|
Chris@16
|
149 } // namespace detail
|
Chris@16
|
150
|
Chris@16
|
151 template <class T1, class T2, class T3, class Policy>
|
Chris@16
|
152 inline typename tools::promote_args<T1, T2, T3>::type
|
Chris@16
|
153 ellint_rf(T1 x, T2 y, T3 z, const Policy& pol)
|
Chris@16
|
154 {
|
Chris@16
|
155 typedef typename tools::promote_args<T1, T2, T3>::type result_type;
|
Chris@16
|
156 typedef typename policies::evaluation<result_type, Policy>::type value_type;
|
Chris@16
|
157 return policies::checked_narrowing_cast<result_type, Policy>(
|
Chris@16
|
158 detail::ellint_rf_imp(
|
Chris@16
|
159 static_cast<value_type>(x),
|
Chris@16
|
160 static_cast<value_type>(y),
|
Chris@16
|
161 static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
|
Chris@16
|
162 }
|
Chris@16
|
163
|
Chris@16
|
164 template <class T1, class T2, class T3>
|
Chris@16
|
165 inline typename tools::promote_args<T1, T2, T3>::type
|
Chris@16
|
166 ellint_rf(T1 x, T2 y, T3 z)
|
Chris@16
|
167 {
|
Chris@16
|
168 return ellint_rf(x, y, z, policies::policy<>());
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 }} // namespaces
|
Chris@16
|
172
|
Chris@16
|
173 #endif // BOOST_MATH_ELLINT_RF_HPP
|
Chris@16
|
174
|