File indexing completed on 2025-01-18 09:40:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef BOOST_MATH_ELLINT_D_HPP
0015 #define BOOST_MATH_ELLINT_D_HPP
0016
0017 #ifdef _MSC_VER
0018 #pragma once
0019 #endif
0020
0021 #include <boost/math/special_functions/math_fwd.hpp>
0022 #include <boost/math/special_functions/ellint_rf.hpp>
0023 #include <boost/math/special_functions/ellint_rd.hpp>
0024 #include <boost/math/special_functions/ellint_rg.hpp>
0025 #include <boost/math/constants/constants.hpp>
0026 #include <boost/math/policies/error_handling.hpp>
0027 #include <boost/math/tools/workaround.hpp>
0028 #include <boost/math/special_functions/round.hpp>
0029
0030
0031
0032
0033 namespace boost { namespace math {
0034
0035 template <class T1, class T2, class Policy>
0036 typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol);
0037
0038 namespace detail{
0039
0040 template <typename T, typename Policy>
0041 T ellint_d_imp(T k, const Policy& pol);
0042
0043
0044 template <typename T, typename Policy>
0045 T ellint_d_imp(T phi, T k, const Policy& pol)
0046 {
0047 BOOST_MATH_STD_USING
0048 using namespace boost::math::tools;
0049 using namespace boost::math::constants;
0050
0051 bool invert = false;
0052 if(phi < 0)
0053 {
0054 phi = fabs(phi);
0055 invert = true;
0056 }
0057
0058 T result;
0059
0060 if(phi >= tools::max_value<T>())
0061 {
0062
0063 result = policies::raise_overflow_error<T>("boost::math::ellint_d<%1%>(%1%,%1%)", nullptr, pol);
0064 }
0065 else if(phi > 1 / tools::epsilon<T>())
0066 {
0067
0068
0069 result = 2 * phi * ellint_d_imp(k, pol) / constants::pi<T>();
0070 }
0071 else
0072 {
0073
0074
0075
0076 T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
0077 T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
0078 int s = 1;
0079 if(boost::math::tools::fmod_workaround(m, T(2)) > T(0.5))
0080 {
0081 m += 1;
0082 s = -1;
0083 rphi = constants::half_pi<T>() - rphi;
0084 }
0085 BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
0086 BOOST_MATH_INSTRUMENT_VARIABLE(m);
0087 T sinp = sin(rphi);
0088 T cosp = cos(rphi);
0089 BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
0090 BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
0091 T c = 1 / (sinp * sinp);
0092 T cm1 = cosp * cosp / (sinp * sinp);
0093 T k2 = k * k;
0094 if(k2 * sinp * sinp > 1)
0095 {
0096 return policies::raise_domain_error<T>("boost::math::ellint_d<%1%>(%1%, %1%)", "The parameter k is out of range, got k = %1%", k, pol);
0097 }
0098 else if(rphi == 0)
0099 {
0100 result = 0;
0101 }
0102 else
0103 {
0104
0105 result = s * ellint_rd_imp(cm1, T(c - k2), c, pol) / 3;
0106 BOOST_MATH_INSTRUMENT_VARIABLE(result);
0107 }
0108 if(m != 0)
0109 result += m * ellint_d_imp(k, pol);
0110 }
0111 return invert ? T(-result) : result;
0112 }
0113
0114
0115 template <typename T, typename Policy>
0116 T ellint_d_imp(T k, const Policy& pol)
0117 {
0118 BOOST_MATH_STD_USING
0119 using namespace boost::math::tools;
0120
0121 if (abs(k) >= 1)
0122 {
0123 return policies::raise_domain_error<T>("boost::math::ellint_d<%1%>(%1%)",
0124 "Got k = %1%, function requires |k| <= 1", k, pol);
0125 }
0126 if(fabs(k) <= tools::root_epsilon<T>())
0127 return constants::pi<T>() / 4;
0128
0129 T x = 0;
0130 T t = k * k;
0131 T y = 1 - t;
0132 T z = 1;
0133 T value = ellint_rd_imp(x, y, z, pol) / 3;
0134
0135 return value;
0136 }
0137
0138 template <typename T, typename Policy>
0139 inline typename tools::promote_args<T>::type ellint_d(T k, const Policy& pol, const std::true_type&)
0140 {
0141 typedef typename tools::promote_args<T>::type result_type;
0142 typedef typename policies::evaluation<result_type, Policy>::type value_type;
0143 return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_d_imp(static_cast<value_type>(k), pol), "boost::math::ellint_d<%1%>(%1%)");
0144 }
0145
0146
0147 template <class T1, class T2>
0148 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const std::false_type&)
0149 {
0150 return boost::math::ellint_d(k, phi, policies::policy<>());
0151 }
0152
0153 }
0154
0155
0156 template <typename T>
0157 inline typename tools::promote_args<T>::type ellint_d(T k)
0158 {
0159 return ellint_d(k, policies::policy<>());
0160 }
0161
0162
0163 template <class T1, class T2>
0164 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi)
0165 {
0166 typedef typename policies::is_policy<T2>::type tag_type;
0167 return detail::ellint_d(k, phi, tag_type());
0168 }
0169
0170 template <class T1, class T2, class Policy>
0171 inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol)
0172 {
0173 typedef typename tools::promote_args<T1, T2>::type result_type;
0174 typedef typename policies::evaluation<result_type, Policy>::type value_type;
0175 return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_d_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%,%1%)");
0176 }
0177
0178 }}
0179
0180 #endif
0181