Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-18 08:56:05

0001 //  Copyright (c) 2006 Xiaogang Zhang, 2015 John Maddock
0002 //  Copyright (c) 2024 Matt Borland
0003 //  Use, modification and distribution are subject to the
0004 //  Boost Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 //  History:
0008 //  XZ wrote the original of this file as part of the Google
0009 //  Summer of Code 2006.  JM modified it to fit into the
0010 //  Boost.Math conceptual framework better, and to correctly
0011 //  handle the p < 0 case.
0012 //  Updated 2015 to use Carlson's latest methods.
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 // Carlson's elliptic integral of the third kind
0031 // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt
0032 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
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    // Calculate RC(1, 1 + x)
0041    BOOST_MATH_STD_USING
0042 
0043    BOOST_MATH_ASSERT(y != -1);
0044 
0045    // for 1 + y < 0, the integral is singular, return Cauchy principal value
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    // Special cases from http://dlmf.nist.gov/19.20#iii
0081    //
0082    if(x == y)
0083    {
0084       if(x == z)
0085       {
0086          if(x == p)
0087          {
0088             // All values equal:
0089             return 1 / (x * sqrt(x));
0090          }
0091          else
0092          {
0093             // x = y = z:
0094             return 3 * (ellint_rc_imp(x, p, pol) - 1 / sqrt(x)) / (x - p);
0095          }
0096       }
0097       else
0098       {
0099          // x = y only, permute so y = z:
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          // Otherwise fall through to normal method, special case above will suffer too much cancellation...
0110       }
0111    }
0112    if(y == z)
0113    {
0114       if(y == p)
0115       {
0116          // y = z = p:
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          // y = z:
0122          return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
0123       }
0124       // Otherwise fall through to normal method, special case above will suffer too much cancellation...
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; // 4^-n
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          // Occasionally En ~ -1, we then have no means of calculating
0161          // RC(1, 1+En) without terrible cancellation error, so we
0162          // need to get to 1+En directly.  By substitution we have
0163          //
0164          // 1+E_0 = 1 + (p-x)*(p-y)*(p-z)/((sqrt(p) + sqrt(x))*(sqrt(p)+sqrt(y))*(sqrt(p)+sqrt(z)))^2
0165          //       = 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))))
0166          //
0167          // And since this is just an application of the duplication formula for RJ, the same
0168          // expression works for 1+En if we use x,y,z,p_n etc.
0169          // This branch is taken only once or twice at the start of iteration,
0170          // after than En reverts to it's usual very small values.
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       // From here on we move to n+1:
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    // for p < 0, the integral is singular, return Cauchy principal value
0240    if(p < 0)
0241    {
0242       //
0243       // We must ensure that x < y < z.
0244       // Since the integral is symmetrical in x, y and z
0245       // we can just permute the values:
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 } // namespace detail
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 }} // namespaces
0297 
0298 #endif // BOOST_MATH_ELLINT_RJ_HPP
0299