Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:11

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