Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:36:10

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