File indexing completed on 2026-08-18 08:56:05
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
0080
0081
0082 if(x == y)
0083 {
0084 if(x == z)
0085 {
0086 if(x == p)
0087 {
0088
0089 return 1 / (x * sqrt(x));
0090 }
0091 else
0092 {
0093
0094 return 3 * (ellint_rc_imp(x, p, pol) - 1 / sqrt(x)) / (x - p);
0095 }
0096 }
0097 else
0098 {
0099
0100 BOOST_MATH_GPU_SAFE_SWAP(x, z);
0101 if(y == p)
0102 {
0103 return ellint_rd_imp(x, y, y, pol);
0104 }
0105 else if(BOOST_MATH_GPU_SAFE_MAX(y, p) / BOOST_MATH_GPU_SAFE_MIN(y, p) > T(1.2))
0106 {
0107 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
0108 }
0109
0110 }
0111 }
0112 if(y == z)
0113 {
0114 if(y == p)
0115 {
0116
0117 return ellint_rd_imp(x, y, y, pol);
0118 }
0119 else if(BOOST_MATH_GPU_SAFE_MAX(y, p) / BOOST_MATH_GPU_SAFE_MIN(y, p) > T(1.2))
0120 {
0121
0122 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
0123 }
0124
0125 }
0126 if(z == p)
0127 {
0128 return ellint_rd_imp(x, y, z, pol);
0129 }
0130
0131 T xn = x;
0132 T yn = y;
0133 T zn = z;
0134 T pn = p;
0135 T An = (x + y + z + 2 * p) / 5;
0136 T A0 = An;
0137 T delta = (p - x) * (p - y) * (p - z);
0138 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)));
0139
0140 unsigned n;
0141 T lambda;
0142 T Dn;
0143 T En;
0144 T rx, ry, rz, rp;
0145 T fmn = 1;
0146 T RC_sum = 0;
0147
0148 for(n = 0; n < policies::get_max_series_iterations<Policy>(); ++n)
0149 {
0150 rx = sqrt(xn);
0151 ry = sqrt(yn);
0152 rz = sqrt(zn);
0153 rp = sqrt(pn);
0154 Dn = (rp + rx) * (rp + ry) * (rp + rz);
0155 En = delta / Dn;
0156 En /= Dn;
0157 if((En < T(-0.5)) && (En > T(-1.5)))
0158 {
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 T b = 2 * rp * (pn + rx * (ry + rz) + ry * rz) / Dn;
0173 RC_sum += fmn / Dn * detail::ellint_rc_imp(T(1), b, pol);
0174 }
0175 else
0176 {
0177 RC_sum += fmn / Dn * ellint_rc1p_imp(En, pol);
0178 }
0179 lambda = rx * ry + rx * rz + ry * rz;
0180
0181
0182 An = (An + lambda) / 4;
0183 fmn /= 4;
0184
0185 if(fmn * Q < An)
0186 break;
0187
0188 xn = (xn + lambda) / 4;
0189 yn = (yn + lambda) / 4;
0190 zn = (zn + lambda) / 4;
0191 pn = (pn + lambda) / 4;
0192 delta /= 64;
0193 }
0194
0195 T X = fmn * (A0 - x) / An;
0196 T Y = fmn * (A0 - y) / An;
0197 T Z = fmn * (A0 - z) / An;
0198 T P = (-X - Y - Z) / 2;
0199 T E2 = X * Y + X * Z + Y * Z - 3 * P * P;
0200 T E3 = X * Y * Z + 2 * E2 * P + 4 * P * P * P;
0201 T E4 = (2 * X * Y * Z + E2 * P + 3 * P * P * P) * P;
0202 T E5 = X * Y * Z * P * P;
0203 T result = fmn * pow(An, T(-3) / 2) *
0204 (1 - 3 * E2 / 14 + E3 / 6 + 9 * E2 * E2 / 88 - 3 * E4 / 22 - 9 * E2 * E3 / 52 + 3 * E5 / 26 - E2 * E2 * E2 / 16
0205 + 3 * E3 * E3 / 40 + 3 * E2 * E4 / 20 + 45 * E2 * E2 * E3 / 272 - 9 * (E3 * E4 + E2 * E5) / 68);
0206
0207 result += 6 * RC_sum;
0208 return result;
0209 }
0210
0211 template <typename T, typename Policy>
0212 BOOST_MATH_GPU_ENABLED T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
0213 {
0214 BOOST_MATH_STD_USING
0215
0216 constexpr auto function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
0217
0218 if(x < 0)
0219 {
0220 return policies::raise_domain_error<T>(function, "Argument x must be non-negative, but got x = %1%", x, pol);
0221 }
0222 if(y < 0)
0223 {
0224 return policies::raise_domain_error<T>(function, "Argument y must be non-negative, but got y = %1%", y, pol);
0225 }
0226 if(z < 0)
0227 {
0228 return policies::raise_domain_error<T>(function, "Argument z must be non-negative, but got z = %1%", z, pol);
0229 }
0230 if(p == 0)
0231 {
0232 return policies::raise_domain_error<T>(function, "Argument p must not be zero, but got p = %1%", p, pol);
0233 }
0234 if(x + y == 0 || y + z == 0 || z + x == 0)
0235 {
0236 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);
0237 }
0238
0239
0240 if(p < 0)
0241 {
0242
0243
0244
0245
0246
0247 if(x > y)
0248 BOOST_MATH_GPU_SAFE_SWAP(x, y);
0249 if(y > z)
0250 BOOST_MATH_GPU_SAFE_SWAP(y, z);
0251 if(x > y)
0252 BOOST_MATH_GPU_SAFE_SWAP(x, y);
0253
0254 BOOST_MATH_ASSERT(x <= y);
0255 BOOST_MATH_ASSERT(y <= z);
0256
0257 T q = -p;
0258 p = (z * (x + y + q) - x * y) / (z + q);
0259
0260 BOOST_MATH_ASSERT(p >= 0);
0261
0262 T value = (p - z) * ellint_rj_imp_final(x, y, z, p, pol);
0263 value -= 3 * ellint_rf_imp(x, y, z, pol);
0264 value += 3 * sqrt((x * y * z) / (x * y + p * q)) * ellint_rc_imp(T(x * y + p * q), T(p * q), pol);
0265 value /= (z + q);
0266 return value;
0267 }
0268
0269 return ellint_rj_imp_final(x, y, z, p, pol);
0270 }
0271
0272 }
0273
0274 template <class T1, class T2, class T3, class T4, class Policy>
0275 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3, T4>::type
0276 ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
0277 {
0278 typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
0279 typedef typename policies::evaluation<result_type, Policy>::type value_type;
0280 return policies::checked_narrowing_cast<result_type, Policy>(
0281 detail::ellint_rj_imp(
0282 static_cast<value_type>(x),
0283 static_cast<value_type>(y),
0284 static_cast<value_type>(z),
0285 static_cast<value_type>(p),
0286 pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
0287 }
0288
0289 template <class T1, class T2, class T3, class T4>
0290 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3, T4>::type
0291 ellint_rj(T1 x, T2 y, T3 z, T4 p)
0292 {
0293 return ellint_rj(x, y, z, p, policies::policy<>());
0294 }
0295
0296 }}
0297
0298 #endif
0299