Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/math/special_functions/ellint_rd.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 slightly to fit into the
0010 //  Boost.Math conceptual framework better.
0011 //  Updated 2015 to use Carlson's latest methods.
0012 
0013 #ifndef BOOST_MATH_ELLINT_RD_HPP
0014 #define BOOST_MATH_ELLINT_RD_HPP
0015 
0016 #ifdef _MSC_VER
0017 #pragma once
0018 #endif
0019 
0020 #include <boost/math/tools/config.hpp>
0021 #include <boost/math/tools/promotion.hpp>
0022 #include <boost/math/special_functions/math_fwd.hpp>
0023 #include <boost/math/special_functions/ellint_rc.hpp>
0024 #include <boost/math/policies/error_handling.hpp>
0025 
0026 // Carlson's elliptic integral of the second kind
0027 // R_D(x, y, z) = R_J(x, y, z, z) = 1.5 * \int_{0}^{\infty} [(t+x)(t+y)]^{-1/2} (t+z)^{-3/2} dt
0028 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
0029 
0030 namespace boost { namespace math { namespace detail{
0031 
0032 template <typename T, typename Policy>
0033 BOOST_MATH_GPU_ENABLED T ellint_rd_imp(T x, T y, T z, const Policy& pol)
0034 {
0035    BOOST_MATH_STD_USING
0036 
0037    constexpr auto function = "boost::math::ellint_rd<%1%>(%1%,%1%,%1%)";
0038 
0039    if(x < 0)
0040    {
0041       return policies::raise_domain_error<T>(function, "Argument x must be >= 0, but got %1%", x, pol);
0042    }
0043    if(y < 0)
0044    {
0045       return policies::raise_domain_error<T>(function, "Argument y must be >= 0, but got %1%", y, pol);
0046    }
0047    if(z <= 0)
0048    {
0049       return policies::raise_domain_error<T>(function, "Argument z must be > 0, but got %1%", z, pol);
0050    }
0051    if(x + y == 0)
0052    {
0053       return policies::raise_domain_error<T>(function, "At most one argument can be zero, but got, x + y = %1%", x + y, pol);
0054    }
0055    //
0056    // Special cases from http://dlmf.nist.gov/19.20#iv
0057    //
0058 
0059    if(x == z)
0060    {
0061       BOOST_MATH_GPU_SAFE_SWAP(x, y);
0062    }
0063    if(y == z)
0064    {
0065       if(x == y)
0066       {
0067          return 1 / (x * sqrt(x));
0068       }
0069       else if(x == 0)
0070       {
0071          return 3 * constants::pi<T>() / (4 * y * sqrt(y));
0072       }
0073       else
0074       {
0075          if(BOOST_MATH_GPU_SAFE_MAX(x, y) / BOOST_MATH_GPU_SAFE_MIN(x, y) > T(1.3))
0076             return 3 * (ellint_rc_imp(x, y, pol) - sqrt(x) / y) / (2 * (y - x));
0077          // Otherwise fall through to avoid cancellation in the above (RC(x,y) -> 1/x^0.5 as x -> y)
0078       }
0079    }
0080    if(x == y)
0081    {
0082       if(BOOST_MATH_GPU_SAFE_MAX(x, z) / BOOST_MATH_GPU_SAFE_MIN(x, z) > T(1.3))
0083          return 3 * (ellint_rc_imp(z, x, pol) - 1 / sqrt(z)) / (z - x);
0084       // Otherwise fall through to avoid cancellation in the above (RC(x,y) -> 1/x^0.5 as x -> y)
0085    }
0086    if(y == 0)
0087    {
0088       BOOST_MATH_GPU_SAFE_SWAP(x, y);
0089    }
0090    if(x == 0)
0091    {
0092       //
0093       // Special handling for common case, from
0094       // Numerical Computation of Real or Complex Elliptic Integrals, eq.47
0095       //
0096       T xn = sqrt(y);
0097       T yn = sqrt(z);
0098       T x0 = xn;
0099       T y0 = yn;
0100       T sum = 0;
0101       T sum_pow = 0.25f;
0102 
0103       while(fabs(xn - yn) >= T(2.7) * tools::root_epsilon<T>() * fabs(xn))
0104       {
0105          T t = sqrt(xn * yn);
0106          xn = (xn + yn) / 2;
0107          yn = t;
0108          sum_pow *= 2;
0109          const auto temp = (xn - yn);
0110          sum += sum_pow * temp * temp;
0111       }
0112       T RF = constants::pi<T>() / (xn + yn);
0113       //
0114       // This following calculation suffers from serious cancellation when y ~ z
0115       // unless we combine terms.  We have:
0116       //
0117       // ( ((x0 + y0)/2)^2 - z ) / (z(y-z))
0118       //
0119       // Substituting y = x0^2 and z = y0^2 and simplifying we get the following:
0120       //
0121       T pt = (x0 + 3 * y0) / (4 * z * (x0 + y0));
0122       //
0123       // Since we've moved the denominator from eq.47 inside the expression, we
0124       // need to also scale "sum" by the same value:
0125       //
0126       pt -= sum / (z * (y - z));
0127       return pt * RF * 3;
0128    }
0129 
0130    T xn = x;
0131    T yn = y;
0132    T zn = z;
0133    T An = (x + y + 3 * z) / 5;
0134    T A0 = An;
0135    // This has an extra 1.2 fudge factor which is really only needed when x, y and z are close in magnitude:
0136    T Q = pow(tools::epsilon<T>() / 4, -T(1) / 8) * BOOST_MATH_GPU_SAFE_MAX(BOOST_MATH_GPU_SAFE_MAX(An - x, An - y), An - z) * 1.2f;
0137    BOOST_MATH_INSTRUMENT_VARIABLE(Q);
0138    T lambda, rx, ry, rz;
0139    unsigned k = 0;
0140    T fn = 1;
0141    T RD_sum = 0;
0142 
0143    for(; k < policies::get_max_series_iterations<Policy>(); ++k)
0144    {
0145       rx = sqrt(xn);
0146       ry = sqrt(yn);
0147       rz = sqrt(zn);
0148       lambda = rx * ry + rx * rz + ry * rz;
0149       RD_sum += fn / (rz * (zn + lambda));
0150       An = (An + lambda) / 4;
0151       xn = (xn + lambda) / 4;
0152       yn = (yn + lambda) / 4;
0153       zn = (zn + lambda) / 4;
0154       fn /= 4;
0155       Q /= 4;
0156       BOOST_MATH_INSTRUMENT_VARIABLE(k);
0157       BOOST_MATH_INSTRUMENT_VARIABLE(RD_sum);
0158       BOOST_MATH_INSTRUMENT_VARIABLE(Q);
0159       if(Q < An)
0160          break;
0161    }
0162 
0163    policies::check_series_iterations<T, Policy>(function, k, pol);
0164 
0165    T X = fn * (A0 - x) / An;
0166    T Y = fn * (A0 - y) / An;
0167    T Z = -(X + Y) / 3;
0168    T E2 = X * Y - 6 * Z * Z;
0169    T E3 = (3 * X * Y - 8 * Z * Z) * Z;
0170    T E4 = 3 * (X * Y - Z * Z) * Z * Z;
0171    T E5 = X * Y * Z * Z * Z;
0172 
0173    T result = fn * pow(An, T(-3) / 2) *
0174       (1 - 3 * E2 / 14 + E3 / 6 + 9 * E2 * E2 / 88 - 3 * E4 / 22 - 9 * E2 * E3 / 52 + 3 * E5 / 26 - E2 * E2 * E2 / 16
0175       + 3 * E3 * E3 / 40 + 3 * E2 * E4 / 20 + 45 * E2 * E2 * E3 / 272 - 9 * (E3 * E4 + E2 * E5) / 68);
0176    BOOST_MATH_INSTRUMENT_VARIABLE(result);
0177    result += 3 * RD_sum;
0178 
0179    return result;
0180 }
0181 
0182 } // namespace detail
0183 
0184 template <class T1, class T2, class T3, class Policy>
0185 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3>::type 
0186    ellint_rd(T1 x, T2 y, T3 z, const Policy& pol)
0187 {
0188    typedef typename tools::promote_args<T1, T2, T3>::type result_type;
0189    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0190    return policies::checked_narrowing_cast<result_type, Policy>(
0191       detail::ellint_rd_imp(
0192          static_cast<value_type>(x),
0193          static_cast<value_type>(y),
0194          static_cast<value_type>(z), pol), "boost::math::ellint_rd<%1%>(%1%,%1%,%1%)");
0195 }
0196 
0197 template <class T1, class T2, class T3>
0198 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3>::type 
0199    ellint_rd(T1 x, T2 y, T3 z)
0200 {
0201    return ellint_rd(x, y, z, policies::policy<>());
0202 }
0203 
0204 }} // namespaces
0205 
0206 #endif // BOOST_MATH_ELLINT_RD_HPP
0207