Back to home page

EIC code displayed by LXR

 
 

    


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

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,
0044             "domain error, all arguments must be non-negative, "
0045             "only sensible result is %1%.",
0046             std::numeric_limits<T>::quiet_NaN(), pol);
0047       }
0048       if(x + y == 0 || y + z == 0 || z + x == 0)
0049       {
0050          return policies::raise_domain_error<T>(function,
0051             "domain error, at most one argument can be zero, "
0052             "only sensible result is %1%.",
0053             std::numeric_limits<T>::quiet_NaN(), pol);
0054       }
0055       //
0056       // Special cases from http://dlmf.nist.gov/19.20#i
0057       //
0058       if(x == y)
0059       {
0060          if(x == z)
0061          {
0062             // x, y, z equal:
0063             return 1 / sqrt(x);
0064          }
0065          else
0066          {
0067             // 2 equal, x and y:
0068             if(z == 0)
0069                return constants::pi<T>() / (2 * sqrt(x));
0070             else
0071                return ellint_rc_imp(z, x, pol);
0072          }
0073       }
0074       if(x == z)
0075       {
0076          if(y == 0)
0077             return constants::pi<T>() / (2 * sqrt(x));
0078          else
0079             return ellint_rc_imp(y, x, pol);
0080       }
0081       if(y == z)
0082       {
0083          if(x == 0)
0084             return constants::pi<T>() / (2 * sqrt(y));
0085          else
0086             return ellint_rc_imp(x, y, pol);
0087       }
0088       if(x == 0)
0089          swap(x, z);
0090       else if(y == 0)
0091          swap(y, z);
0092       if(z == 0)
0093       {
0094          //
0095          // Special case for one value zero:
0096          //
0097          T xn = sqrt(x);
0098          T yn = sqrt(y);
0099 
0100          while(fabs(xn - yn) >= T(2.7) * tools::root_epsilon<T>() * fabs(xn))
0101          {
0102             T t = sqrt(xn * yn);
0103             xn = (xn + yn) / 2;
0104             yn = t;
0105          }
0106          return constants::pi<T>() / (xn + yn);
0107       }
0108 
0109       T xn = x;
0110       T yn = y;
0111       T zn = z;
0112       T An = (x + y + z) / 3;
0113       T A0 = An;
0114       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));
0115       T fn = 1;
0116 
0117 
0118       // duplication
0119       unsigned k = 1;
0120       for(; k < boost::math::policies::get_max_series_iterations<Policy>(); ++k)
0121       {
0122          T root_x = sqrt(xn);
0123          T root_y = sqrt(yn);
0124          T root_z = sqrt(zn);
0125          T lambda = root_x * root_y + root_x * root_z + root_y * root_z;
0126          An = (An + lambda) / 4;
0127          xn = (xn + lambda) / 4;
0128          yn = (yn + lambda) / 4;
0129          zn = (zn + lambda) / 4;
0130          Q /= 4;
0131          fn *= 4;
0132          if(Q < fabs(An))
0133             break;
0134       }
0135       // Check to see if we gave up too soon:
0136       policies::check_series_iterations<T>(function, k, pol);
0137       BOOST_MATH_INSTRUMENT_VARIABLE(k);
0138 
0139       T X = (A0 - x) / (An * fn);
0140       T Y = (A0 - y) / (An * fn);
0141       T Z = -X - Y;
0142 
0143       // Taylor series expansion to the 7th order
0144       T E2 = X * Y - Z * Z;
0145       T E3 = X * Y * Z;
0146       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);
0147    }
0148 
0149 } // namespace detail
0150 
0151 template <class T1, class T2, class T3, class Policy>
0152 inline typename tools::promote_args<T1, T2, T3>::type 
0153    ellint_rf(T1 x, T2 y, T3 z, const Policy& pol)
0154 {
0155    typedef typename tools::promote_args<T1, T2, T3>::type result_type;
0156    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0157    return policies::checked_narrowing_cast<result_type, Policy>(
0158       detail::ellint_rf_imp(
0159          static_cast<value_type>(x),
0160          static_cast<value_type>(y),
0161          static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
0162 }
0163 
0164 template <class T1, class T2, class T3>
0165 inline typename tools::promote_args<T1, T2, T3>::type 
0166    ellint_rf(T1 x, T2 y, T3 z)
0167 {
0168    return ellint_rf(x, y, z, policies::policy<>());
0169 }
0170 
0171 }} // namespaces
0172 
0173 #endif // BOOST_MATH_ELLINT_RF_HPP
0174