Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //  Copyright (c) 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 #ifndef BOOST_MATH_ELLINT_RG_HPP
0007 #define BOOST_MATH_ELLINT_RG_HPP
0008 
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #endif
0012 
0013 #include <boost/math/special_functions/math_fwd.hpp>
0014 #include <boost/math/tools/config.hpp>
0015 #include <boost/math/constants/constants.hpp>
0016 #include <boost/math/policies/error_handling.hpp>
0017 #include <boost/math/special_functions/ellint_rd.hpp>
0018 #include <boost/math/special_functions/ellint_rf.hpp>
0019 #include <boost/math/special_functions/pow.hpp>
0020 
0021 namespace boost { namespace math { namespace detail{
0022 
0023    template <typename T, typename Policy>
0024    T ellint_rg_imp(T x, T y, T z, const Policy& pol)
0025    {
0026       BOOST_MATH_STD_USING
0027       static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
0028 
0029       if(x < 0 || y < 0 || z < 0)
0030       {
0031          return policies::raise_domain_error<T>(function,
0032             "domain error, all arguments must be non-negative, "
0033             "only sensible result is %1%.",
0034             std::numeric_limits<T>::quiet_NaN(), pol);
0035       }
0036       //
0037       // Function is symmetric in x, y and z, but we require
0038       // (x - z)(y - z) >= 0 to avoid cancellation error in the result
0039       // which implies (for example) x >= z >= y
0040       //
0041       using std::swap;
0042       if(x < y)
0043          swap(x, y);
0044       if(x < z)
0045          swap(x, z);
0046       if(y > z)
0047          swap(y, z);
0048       
0049       BOOST_MATH_ASSERT(x >= z);
0050       BOOST_MATH_ASSERT(z >= y);
0051       //
0052       // Special cases from http://dlmf.nist.gov/19.20#ii
0053       //
0054       if(x == z)
0055       {
0056          if(y == z)
0057          {
0058             // x = y = z
0059             // This also works for x = y = z = 0 presumably.
0060             return sqrt(x);
0061          }
0062          else if(y == 0)
0063          {
0064             // x = y, z = 0
0065             return constants::pi<T>() * sqrt(x) / 4;
0066          }
0067          else
0068          {
0069             // x = z, y != 0
0070             swap(x, y);
0071             return (x == 0) ? T(sqrt(z) / 2) : T((z * ellint_rc_imp(x, z, pol) + sqrt(x)) / 2);
0072          }
0073       }
0074       else if(y == z)
0075       {
0076          if(x == 0)
0077             return constants::pi<T>() * sqrt(y) / 4;
0078          else
0079             return (y == 0) ? T(sqrt(x) / 2) : T((y * ellint_rc_imp(x, y, pol) + sqrt(x)) / 2);
0080       }
0081       else if(y == 0)
0082       {
0083          swap(y, z);
0084          //
0085          // Special handling for common case, from
0086          // Numerical Computation of Real or Complex Elliptic Integrals, eq.46
0087          //
0088          T xn = sqrt(x);
0089          T yn = sqrt(y);
0090          T x0 = xn;
0091          T y0 = yn;
0092          T sum = 0;
0093          T sum_pow = 0.25f;
0094 
0095          while(fabs(xn - yn) >= T(2.7) * tools::root_epsilon<T>() * fabs(xn))
0096          {
0097             T t = sqrt(xn * yn);
0098             xn = (xn + yn) / 2;
0099             yn = t;
0100             sum_pow *= 2;
0101             sum += sum_pow * boost::math::pow<2>(xn - yn);
0102          }
0103          T RF = constants::pi<T>() / (xn + yn);
0104          return ((boost::math::pow<2>((x0 + y0) / 2) - sum) * RF) / 2;
0105       }
0106       return (z * ellint_rf_imp(x, y, z, pol)
0107          - (x - z) * (y - z) * ellint_rd_imp(x, y, z, pol) / 3
0108          + sqrt(x * y / z)) / 2;
0109    }
0110 
0111 } // namespace detail
0112 
0113 template <class T1, class T2, class T3, class Policy>
0114 inline typename tools::promote_args<T1, T2, T3>::type 
0115    ellint_rg(T1 x, T2 y, T3 z, const Policy& pol)
0116 {
0117    typedef typename tools::promote_args<T1, T2, T3>::type result_type;
0118    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0119    return policies::checked_narrowing_cast<result_type, Policy>(
0120       detail::ellint_rg_imp(
0121          static_cast<value_type>(x),
0122          static_cast<value_type>(y),
0123          static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
0124 }
0125 
0126 template <class T1, class T2, class T3>
0127 inline typename tools::promote_args<T1, T2, T3>::type 
0128    ellint_rg(T1 x, T2 y, T3 z)
0129 {
0130    return ellint_rg(x, y, z, policies::policy<>());
0131 }
0132 
0133 }} // namespaces
0134 
0135 #endif // BOOST_MATH_ELLINT_RG_HPP
0136