Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 08:17: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 to fit into the
0009 //  Boost.Math conceptual framework better, and to handle
0010 //  types longer than 80-bit reals.
0011 //  Updated 2015 to use Carlson's latest methods.
0012 //
0013 #ifndef BOOST_MATH_ELLINT_RF_HPP
0014 #define BOOST_MATH_ELLINT_RF_HPP
0015 
0016 #ifdef _MSC_VER
0017 #pragma once
0018 #endif
0019 
0020 #include <boost/math/special_functions/math_fwd.hpp>
0021 #include <boost/math/tools/config.hpp>
0022 #include <boost/math/constants/constants.hpp>
0023 #include <boost/math/policies/error_handling.hpp>
0024 #include <boost/math/special_functions/ellint_rc.hpp>
0025 
0026 // Carlson's elliptic integral of the first kind
0027 // R_F(x, y, z) = 0.5 * \int_{0}^{\infty} [(t+x)(t+y)(t+z)]^{-1/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    T ellint_rf_imp(T x, T y, T z, const Policy& pol)
0034    {
0035       BOOST_MATH_STD_USING
0036       using namespace boost::math;
0037       using std::swap;
0038 
0039       static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
0040 
0041       if(x < 0 || y < 0 || z < 0)
0042       {
0043          return policies::raise_domain_error<T>(function, "domain error, all arguments must be non-negative, only sensible result is %1%.", std::numeric_limits<T>::quiet_NaN(), pol);
0044       }
0045       if(x + y == 0 || y + z == 0 || z + x == 0)
0046       {
0047          return policies::raise_domain_error<T>(function, "domain error, at most one argument can be zero, only sensible result is %1%.", std::numeric_limits<T>::quiet_NaN(), pol);
0048       }
0049       //
0050       // Special cases from http://dlmf.nist.gov/19.20#i
0051       //
0052       if(x == y)
0053       {
0054          if(x == z)
0055          {
0056             // x, y, z equal:
0057             return 1 / sqrt(x);
0058          }
0059          else
0060          {
0061             // 2 equal, x and y:
0062             if(z == 0)
0063                return constants::pi<T>() / (2 * sqrt(x));
0064             else
0065                return ellint_rc_imp(z, x, pol);
0066          }
0067       }
0068       if(x == z)
0069       {
0070          if(y == 0)
0071             return constants::pi<T>() / (2 * sqrt(x));
0072          else
0073             return ellint_rc_imp(y, x, pol);
0074       }
0075       if(y == z)
0076       {
0077          if(x == 0)
0078             return constants::pi<T>() / (2 * sqrt(y));
0079          else
0080             return ellint_rc_imp(x, y, pol);
0081       }
0082       if(x == 0)
0083          swap(x, z);
0084       else if(y == 0)
0085          swap(y, z);
0086       if(z == 0)
0087       {
0088          //
0089          // Special case for one value zero:
0090          //
0091          T xn = sqrt(x);
0092          T yn = sqrt(y);
0093 
0094          while(fabs(xn - yn) >= T(2.7) * tools::root_epsilon<T>() * fabs(xn))
0095          {
0096             T t = sqrt(xn * yn);
0097             xn = (xn + yn) / 2;
0098             yn = t;
0099          }
0100          return constants::pi<T>() / (xn + yn);
0101       }
0102 
0103       T xn = x;
0104       T yn = y;
0105       T zn = z;
0106       T An = (x + y + z) / 3;
0107       T A0 = An;
0108       T Q = pow(3 * boost::math::tools::epsilon<T>(), T(-1) / 8) * (std::max)((std::max)(fabs(An - xn), fabs(An - yn)), fabs(An - zn));
0109       T fn = 1;
0110 
0111 
0112       // duplication
0113       unsigned k = 1;
0114       for(; k < boost::math::policies::get_max_series_iterations<Policy>(); ++k)
0115       {
0116          T root_x = sqrt(xn);
0117          T root_y = sqrt(yn);
0118          T root_z = sqrt(zn);
0119          T lambda = root_x * root_y + root_x * root_z + root_y * root_z;
0120          An = (An + lambda) / 4;
0121          xn = (xn + lambda) / 4;
0122          yn = (yn + lambda) / 4;
0123          zn = (zn + lambda) / 4;
0124          Q /= 4;
0125          fn *= 4;
0126          if(Q < fabs(An))
0127             break;
0128       }
0129       // Check to see if we gave up too soon:
0130       policies::check_series_iterations<T>(function, k, pol);
0131       BOOST_MATH_INSTRUMENT_VARIABLE(k);
0132 
0133       T X = (A0 - x) / (An * fn);
0134       T Y = (A0 - y) / (An * fn);
0135       T Z = -X - Y;
0136 
0137       // Taylor series expansion to the 7th order
0138       T E2 = X * Y - Z * Z;
0139       T E3 = X * Y * Z;
0140       return (1 + E3 * (T(1) / 14 + 3 * E3 / 104) + E2 * (T(-1) / 10 + E2 / 24 - (3 * E3) / 44 - 5 * E2 * E2 / 208 + E2 * E3 / 16)) / sqrt(An);
0141    }
0142 
0143 } // namespace detail
0144 
0145 template <class T1, class T2, class T3, class Policy>
0146 inline typename tools::promote_args<T1, T2, T3>::type 
0147    ellint_rf(T1 x, T2 y, T3 z, const Policy& pol)
0148 {
0149    typedef typename tools::promote_args<T1, T2, T3>::type result_type;
0150    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0151    return policies::checked_narrowing_cast<result_type, Policy>(
0152       detail::ellint_rf_imp(
0153          static_cast<value_type>(x),
0154          static_cast<value_type>(y),
0155          static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
0156 }
0157 
0158 template <class T1, class T2, class T3>
0159 inline typename tools::promote_args<T1, T2, T3>::type 
0160    ellint_rf(T1 x, T2 y, T3 z)
0161 {
0162    return ellint_rf(x, y, z, policies::policy<>());
0163 }
0164 
0165 }} // namespaces
0166 
0167 #endif // BOOST_MATH_ELLINT_RF_HPP
0168