File indexing completed on 2025-09-18 08:49:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef BOOST_MATH_ELLINT_RJ_HPP
0016 #define BOOST_MATH_ELLINT_RJ_HPP
0017
0018 #ifdef _MSC_VER
0019 #pragma once
0020 #endif
0021
0022 #include <boost/math/tools/config.hpp>
0023 #include <boost/math/tools/numeric_limits.hpp>
0024 #include <boost/math/special_functions/math_fwd.hpp>
0025 #include <boost/math/policies/error_handling.hpp>
0026 #include <boost/math/special_functions/ellint_rc.hpp>
0027 #include <boost/math/special_functions/ellint_rf.hpp>
0028 #include <boost/math/special_functions/ellint_rd.hpp>
0029
0030
0031
0032
0033
0034 namespace boost { namespace math { namespace detail{
0035
0036 template <typename T, typename Policy>
0037 BOOST_MATH_GPU_ENABLED T ellint_rc1p_imp(T y, const Policy& pol)
0038 {
0039 using namespace boost::math;
0040
0041 BOOST_MATH_STD_USING
0042
0043 BOOST_MATH_ASSERT(y != -1);
0044
0045
0046 T result;
0047 if(y < -1)
0048 {
0049 result = sqrt(1 / -y) * detail::ellint_rc_imp(T(-y), T(-1 - y), pol);
0050 }
0051 else if(y == 0)
0052 {
0053 result = 1;
0054 }
0055 else if(y > 0)
0056 {
0057 result = atan(sqrt(y)) / sqrt(y);
0058 }
0059 else
0060 {
0061 if(y > T(-0.5))
0062 {
0063 T arg = sqrt(-y);
0064 result = (boost::math::log1p(arg, pol) - boost::math::log1p(-arg, pol)) / (2 * sqrt(-y));
0065 }
0066 else
0067 {
0068 result = log((1 + sqrt(-y)) / sqrt(1 + y)) / sqrt(-y);
0069 }
0070 }
0071 return result;
0072 }
0073
0074 template <typename T, typename Policy>
0075 BOOST_MATH_GPU_ENABLED T ellint_rj_imp_final(T x, T y, T z, T p, const Policy& pol)
0076 {
0077 BOOST_MATH_STD_USING
0078
0079 constexpr auto function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
0080
0081 if(x < 0)
0082 {
0083 return policies::raise_domain_error<T>(function, "Argument x must be non-negative, but got x = %1%", x, pol);
0084 }
0085 if(y < 0)
0086 {
0087 return policies::raise_domain_error<T>(function, "Argument y must be non-negative, but got y = %1%", y, pol);
0088 }
0089 if(z < 0)
0090 {
0091 return policies::raise_domain_error<T>(function, "Argument z must be non-negative, but got z = %1%", z, pol);
0092 }
0093 if(p == 0)
0094 {
0095 return policies::raise_domain_error<T>(function, "Argument p must not be zero, but got p = %1%", p, pol);
0096 }
0097 if(x + y == 0 || y + z == 0 || z + x == 0)
0098 {
0099 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);
0100 }
0101
0102
0103
0104
0105 if(x == y)
0106 {
0107 if(x == z)
0108 {
0109 if(x == p)
0110 {
0111
0112 return 1 / (x * sqrt(x));
0113 }
0114 else
0115 {
0116
0117 return 3 * (ellint_rc_imp(x, p, pol) - 1 / sqrt(x)) / (x - p);
0118 }
0119 }
0120 else
0121 {
0122
0123 BOOST_MATH_GPU_SAFE_SWAP(x, z);
0124 if(y == p)
0125 {
0126 return ellint_rd_imp(x, y, y, pol);
0127 }
0128 else if(BOOST_MATH_GPU_SAFE_MAX(y, p) / BOOST_MATH_GPU_SAFE_MIN(y, p) > T(1.2))
0129 {
0130 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
0131 }
0132
0133 }
0134 }
0135 if(y == z)
0136 {
0137 if(y == p)
0138 {
0139
0140 return ellint_rd_imp(x, y, y, pol);
0141 }
0142 else if(BOOST_MATH_GPU_SAFE_MAX(y, p) / BOOST_MATH_GPU_SAFE_MIN(y, p) > T(1.2))
0143 {
0144
0145 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
0146 }
0147
0148 }
0149 if(z == p)
0150 {
0151 return ellint_rd_imp(x, y, z, pol);
0152 }
0153
0154 T xn = x;
0155 T yn = y;
0156 T zn = z;
0157 T pn = p;
0158 T An = (x + y + z + 2 * p) / 5;
0159 T A0 = An;
0160 T delta = (p - x) * (p - y) * (p - z);
0161 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)));
0162
0163 unsigned n;
0164 T lambda;
0165 T Dn;
0166 T En;
0167 T rx, ry, rz, rp;
0168 T fmn = 1;
0169 T RC_sum = 0;
0170
0171 for(n = 0; n < policies::get_max_series_iterations<Policy>(); ++n)
0172 {
0173 rx = sqrt(xn);
0174 ry = sqrt(yn);
0175 rz = sqrt(zn);
0176 rp = sqrt(pn);
0177 Dn = (rp + rx) * (rp + ry) * (rp + rz);
0178 En = delta / Dn;
0179 En /= Dn;
0180 if((En < T(-0.5)) && (En > T(-1.5)))
0181 {
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 T b = 2 * rp * (pn + rx * (ry + rz) + ry * rz) / Dn;
0196 RC_sum += fmn / Dn * detail::ellint_rc_imp(T(1), b, pol);
0197 }
0198 else
0199 {
0200 RC_sum += fmn / Dn * ellint_rc1p_imp(En, pol);
0201 }
0202 lambda = rx * ry + rx * rz + ry * rz;
0203
0204
0205 An = (An + lambda) / 4;
0206 fmn /= 4;
0207
0208 if(fmn * Q < An)
0209 break;
0210
0211 xn = (xn + lambda) / 4;
0212 yn = (yn + lambda) / 4;
0213 zn = (zn + lambda) / 4;
0214 pn = (pn + lambda) / 4;
0215 delta /= 64;
0216 }
0217
0218 T X = fmn * (A0 - x) / An;
0219 T Y = fmn * (A0 - y) / An;
0220 T Z = fmn * (A0 - z) / An;
0221 T P = (-X - Y - Z) / 2;
0222 T E2 = X * Y + X * Z + Y * Z - 3 * P * P;
0223 T E3 = X * Y * Z + 2 * E2 * P + 4 * P * P * P;
0224 T E4 = (2 * X * Y * Z + E2 * P + 3 * P * P * P) * P;
0225 T E5 = X * Y * Z * P * P;
0226 T result = fmn * pow(An, T(-3) / 2) *
0227 (1 - 3 * E2 / 14 + E3 / 6 + 9 * E2 * E2 / 88 - 3 * E4 / 22 - 9 * E2 * E3 / 52 + 3 * E5 / 26 - E2 * E2 * E2 / 16
0228 + 3 * E3 * E3 / 40 + 3 * E2 * E4 / 20 + 45 * E2 * E2 * E3 / 272 - 9 * (E3 * E4 + E2 * E5) / 68);
0229
0230 result += 6 * RC_sum;
0231 return result;
0232 }
0233
0234 template <typename T, typename Policy>
0235 BOOST_MATH_GPU_ENABLED T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
0236 {
0237 BOOST_MATH_STD_USING
0238
0239 constexpr auto function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
0240
0241 if(x < 0)
0242 {
0243 return policies::raise_domain_error<T>(function, "Argument x must be non-negative, but got x = %1%", x, pol);
0244 }
0245 if(y < 0)
0246 {
0247 return policies::raise_domain_error<T>(function, "Argument y must be non-negative, but got y = %1%", y, pol);
0248 }
0249 if(z < 0)
0250 {
0251 return policies::raise_domain_error<T>(function, "Argument z must be non-negative, but got z = %1%", z, pol);
0252 }
0253 if(p == 0)
0254 {
0255 return policies::raise_domain_error<T>(function, "Argument p must not be zero, but got p = %1%", p, pol);
0256 }
0257 if(x + y == 0 || y + z == 0 || z + x == 0)
0258 {
0259 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);
0260 }
0261
0262
0263 if(p < 0)
0264 {
0265
0266
0267
0268
0269
0270 if(x > y)
0271 BOOST_MATH_GPU_SAFE_SWAP(x, y);
0272 if(y > z)
0273 BOOST_MATH_GPU_SAFE_SWAP(y, z);
0274 if(x > y)
0275 BOOST_MATH_GPU_SAFE_SWAP(x, y);
0276
0277 BOOST_MATH_ASSERT(x <= y);
0278 BOOST_MATH_ASSERT(y <= z);
0279
0280 T q = -p;
0281 p = (z * (x + y + q) - x * y) / (z + q);
0282
0283 BOOST_MATH_ASSERT(p >= 0);
0284
0285 T value = (p - z) * ellint_rj_imp_final(x, y, z, p, pol);
0286 value -= 3 * ellint_rf_imp(x, y, z, pol);
0287 value += 3 * sqrt((x * y * z) / (x * y + p * q)) * ellint_rc_imp(T(x * y + p * q), T(p * q), pol);
0288 value /= (z + q);
0289 return value;
0290 }
0291
0292 return ellint_rj_imp_final(x, y, z, p, pol);
0293 }
0294
0295 }
0296
0297 template <class T1, class T2, class T3, class T4, class Policy>
0298 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3, T4>::type
0299 ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
0300 {
0301 typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
0302 typedef typename policies::evaluation<result_type, Policy>::type value_type;
0303 return policies::checked_narrowing_cast<result_type, Policy>(
0304 detail::ellint_rj_imp(
0305 static_cast<value_type>(x),
0306 static_cast<value_type>(y),
0307 static_cast<value_type>(z),
0308 static_cast<value_type>(p),
0309 pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
0310 }
0311
0312 template <class T1, class T2, class T3, class T4>
0313 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3, T4>::type
0314 ellint_rj(T1 x, T2 y, T3 z, T4 p)
0315 {
0316 return ellint_rj(x, y, z, p, policies::policy<>());
0317 }
0318
0319 }}
0320
0321 #endif
0322