File indexing completed on 2025-01-18 09:40:12
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,
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
0057
0058 if(x == y)
0059 {
0060 if(x == z)
0061 {
0062
0063 return 1 / sqrt(x);
0064 }
0065 else
0066 {
0067
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
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
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
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
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 }
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 }}
0172
0173 #endif
0174