File indexing completed on 2025-07-02 08:17:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
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
0027
0028
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
0051
0052 if(x == y)
0053 {
0054 if(x == z)
0055 {
0056
0057 return 1 / sqrt(x);
0058 }
0059 else
0060 {
0061
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
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
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
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
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 }
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 }}
0166
0167 #endif
0168