Debian Codegraph @

third-party/boost-math/include/boost/math/special_functions/ellint_rj.hpp

raw
1 // Copyright (c) 2006 Xiaogang Zhang, 2015 John Maddock
2 // Copyright (c) 2024 Matt Borland
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 correctly
11 // handle the p < 0 case.
12 // Updated 2015 to use Carlson's latest methods.
13 //
14
15 #ifndef BOOST_MATH_ELLINT_RJ_HPP
16 #define BOOST_MATH_ELLINT_RJ_HPP
17
18 #ifdef _MSC_VER
19 #pragma once
20 #endif
21
22 #include <boost/math/tools/config.hpp>
23 #include <boost/math/tools/numeric_limits.hpp>
24 #include <boost/math/special_functions/math_fwd.hpp>
25 #include <boost/math/policies/error_handling.hpp>
26 #include <boost/math/special_functions/ellint_rc.hpp>
27 #include <boost/math/special_functions/ellint_rf.hpp>
28 #include <boost/math/special_functions/ellint_rd.hpp>
29
30 // Carlson's elliptic integral of the third kind
31 // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt
32 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
33
34 namespace boost { namespace math { namespace detail{
35
36 template <typename T, typename Policy>
37 BOOST_MATH_GPU_ENABLED T ellint_rc1p_imp(T y, const Policy& pol)
38 {
39 using namespace boost::math;
40 // Calculate RC(1, 1 + x)
41 BOOST_MATH_STD_USING
42
43 BOOST_MATH_ASSERT(y != -1);
44
45 // for 1 + y < 0, the integral is singular, return Cauchy principal value
46 T result;
47 if(y < -1)
48 {
49 result = sqrt(1 / -y) * detail::ellint_rc_imp(T(-y), T(-1 - y), pol);
50 }
51 else if(y == 0)
52 {
53 result = 1;
54 }
55 else if(y > 0)
56 {
57 result = atan(sqrt(y)) / sqrt(y);
58 }
59 else
60 {
61 if(y > T(-0.5))
62 {
63 T arg = sqrt(-y);
64 result = (boost::math::log1p(arg, pol) - boost::math::log1p(-arg, pol)) / (2 * sqrt(-y));
65 }
66 else
67 {
68 result = log((1 + sqrt(-y)) / sqrt(1 + y)) / sqrt(-y);
69 }
70 }
71 return result;
72 }
73
74 template <typename T, typename Policy>
75 BOOST_MATH_GPU_ENABLED T ellint_rj_imp_final(T x, T y, T z, T p, const Policy& pol)
76 {
77 BOOST_MATH_STD_USING
78
79 //
80 // Special cases from http://dlmf.nist.gov/19.20#iii
81 //
82 if(x == y)
83 {
84 if(x == z)
85 {
86 if(x == p)
87 {
88 // All values equal:
89 return 1 / (x * sqrt(x));
90 }
91 else
92 {
93 // x = y = z:
94 return 3 * (ellint_rc_imp(x, p, pol) - 1 / sqrt(x)) / (x - p);
95 }
96 }
97 else
98 {
99 // x = y only, permute so y = z:
100 BOOST_MATH_GPU_SAFE_SWAP(x, z);
101 if(y == p)
102 {
103 return ellint_rd_imp(x, y, y, pol);
104 }
105 else if(BOOST_MATH_GPU_SAFE_MAX(y, p) / BOOST_MATH_GPU_SAFE_MIN(y, p) > T(1.2))
106 {
107 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
108 }
109 // Otherwise fall through to normal method, special case above will suffer too much cancellation...
110 }
111 }
112 if(y == z)
113 {
114 if(y == p)
115 {
116 // y = z = p:
117 return ellint_rd_imp(x, y, y, pol);
118 }
119 else if(BOOST_MATH_GPU_SAFE_MAX(y, p) / BOOST_MATH_GPU_SAFE_MIN(y, p) > T(1.2))
120 {
121 // y = z:
122 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
123 }
124 // Otherwise fall through to normal method, special case above will suffer too much cancellation...
125 }
126 if(z == p)
127 {
128 return ellint_rd_imp(x, y, z, pol);
129 }
130
131 T xn = x;
132 T yn = y;
133 T zn = z;
134 T pn = p;
135 T An = (x + y + z + 2 * p) / 5;
136 T A0 = An;
137 T delta = (p - x) * (p - y) * (p - z);
138 T Q = pow(tools::epsilon<T>() / 5, -T(1) / 8) * BOOST_MATH_GPU_SAFE_MAX(BOOST_MATH_GPU_SAFE_MAX(fabs(An - x), fabs(An - y)), BOOST_MATH_GPU_SAFE_MAX(fabs(An - z), fabs(An - p)));
139
140 unsigned n;
141 T lambda;
142 T Dn;
143 T En;
144 T rx, ry, rz, rp;
145 T fmn = 1; // 4^-n
146 T RC_sum = 0;
147
148 for(n = 0; n < policies::get_max_series_iterations<Policy>(); ++n)
149 {
150 rx = sqrt(xn);
151 ry = sqrt(yn);
152 rz = sqrt(zn);
153 rp = sqrt(pn);
154 Dn = (rp + rx) * (rp + ry) * (rp + rz);
155 En = delta / Dn;
156 En /= Dn;
157 if((En < T(-0.5)) && (En > T(-1.5)))
158 {
159 //
160 // Occasionally En ~ -1, we then have no means of calculating
161 // RC(1, 1+En) without terrible cancellation error, so we
162 // need to get to 1+En directly. By substitution we have
163 //
164 // 1+E_0 = 1 + (p-x)*(p-y)*(p-z)/((sqrt(p) + sqrt(x))*(sqrt(p)+sqrt(y))*(sqrt(p)+sqrt(z)))^2
165 // = 2*sqrt(p)*(p+sqrt(x) * (sqrt(y)+sqrt(z)) + sqrt(y)*sqrt(z)) / ((sqrt(p) + sqrt(x))*(sqrt(p) + sqrt(y)*(sqrt(p)+sqrt(z))))
166 //
167 // And since this is just an application of the duplication formula for RJ, the same
168 // expression works for 1+En if we use x,y,z,p_n etc.
169 // This branch is taken only once or twice at the start of iteration,
170 // after than En reverts to it's usual very small values.
171 //
172 T b = 2 * rp * (pn + rx * (ry + rz) + ry * rz) / Dn;
173 RC_sum += fmn / Dn * detail::ellint_rc_imp(T(1), b, pol);
174 }
175 else
176 {
177 RC_sum += fmn / Dn * ellint_rc1p_imp(En, pol);
178 }
179 lambda = rx * ry + rx * rz + ry * rz;
180
181 // From here on we move to n+1:
182 An = (An + lambda) / 4;
183 fmn /= 4;
184
185 if(fmn * Q < An)
186 break;
187
188 xn = (xn + lambda) / 4;
189 yn = (yn + lambda) / 4;
190 zn = (zn + lambda) / 4;
191 pn = (pn + lambda) / 4;
192 delta /= 64;
193 }
194
195 T X = fmn * (A0 - x) / An;
196 T Y = fmn * (A0 - y) / An;
197 T Z = fmn * (A0 - z) / An;
198 T P = (-X - Y - Z) / 2;
199 T E2 = X * Y + X * Z + Y * Z - 3 * P * P;
200 T E3 = X * Y * Z + 2 * E2 * P + 4 * P * P * P;
201 T E4 = (2 * X * Y * Z + E2 * P + 3 * P * P * P) * P;
202 T E5 = X * Y * Z * P * P;
203 T result = fmn * pow(An, T(-3) / 2) *
204 (1 - 3 * E2 / 14 + E3 / 6 + 9 * E2 * E2 / 88 - 3 * E4 / 22 - 9 * E2 * E3 / 52 + 3 * E5 / 26 - E2 * E2 * E2 / 16
205 + 3 * E3 * E3 / 40 + 3 * E2 * E4 / 20 + 45 * E2 * E2 * E3 / 272 - 9 * (E3 * E4 + E2 * E5) / 68);
206
207 result += 6 * RC_sum;
208 return result;
209 }
210
211 template <typename T, typename Policy>
212 BOOST_MATH_GPU_ENABLED T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
213 {
214 BOOST_MATH_STD_USING
215
216 constexpr auto function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
217
218 if(x < 0)
219 {
220 return policies::raise_domain_error<T>(function, "Argument x must be non-negative, but got x = %1%", x, pol);
221 }
222 if(y < 0)
223 {
224 return policies::raise_domain_error<T>(function, "Argument y must be non-negative, but got y = %1%", y, pol);
225 }
226 if(z < 0)
227 {
228 return policies::raise_domain_error<T>(function, "Argument z must be non-negative, but got z = %1%", z, pol);
229 }
230 if(p == 0)
231 {
232 return policies::raise_domain_error<T>(function, "Argument p must not be zero, but got p = %1%", p, pol);
233 }
234 if(x + y == 0 || y + z == 0 || z + x == 0)
235 {
236 return policies::raise_domain_error<T>(function, "At most one argument can be zero, only possible result is %1%.", boost::math::numeric_limits<T>::quiet_NaN(), pol);
237 }
238
239 // for p < 0, the integral is singular, return Cauchy principal value
240 if(p < 0)
241 {
242 //
243 // We must ensure that x < y < z.
244 // Since the integral is symmetrical in x, y and z
245 // we can just permute the values:
246 //
247 if(x > y)
248 BOOST_MATH_GPU_SAFE_SWAP(x, y);
249 if(y > z)
250 BOOST_MATH_GPU_SAFE_SWAP(y, z);
251 if(x > y)
252 BOOST_MATH_GPU_SAFE_SWAP(x, y);
253
254 BOOST_MATH_ASSERT(x <= y);
255 BOOST_MATH_ASSERT(y <= z);
256
257 T q = -p;
258 p = (z * (x + y + q) - x * y) / (z + q);
259
260 BOOST_MATH_ASSERT(p >= 0);
261
262 T value = (p - z) * ellint_rj_imp_final(x, y, z, p, pol);
263 value -= 3 * ellint_rf_imp(x, y, z, pol);
264 value += 3 * sqrt((x * y * z) / (x * y + p * q)) * ellint_rc_imp(T(x * y + p * q), T(p * q), pol);
265 value /= (z + q);
266 return value;
267 }
268
269 return ellint_rj_imp_final(x, y, z, p, pol);
270 }
271
272 } // namespace detail
273
274 template <class T1, class T2, class T3, class T4, class Policy>
275 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3, T4>::type
276 ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
277 {
278 typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
279 typedef typename policies::evaluation<result_type, Policy>::type value_type;
280 return policies::checked_narrowing_cast<result_type, Policy>(
281 detail::ellint_rj_imp(
282 static_cast<value_type>(x),
283 static_cast<value_type>(y),
284 static_cast<value_type>(z),
285 static_cast<value_type>(p),
286 pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
287 }
288
289 template <class T1, class T2, class T3, class T4>
290 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3, T4>::type
291 ellint_rj(T1 x, T2 y, T3 z, T4 p)
292 {
293 return ellint_rj(x, y, z, p, policies::policy<>());
294 }
295
296 }} // namespaces
297
298 #endif // BOOST_MATH_ELLINT_RJ_HPP
299