annotate DEPENDENCIES/generic/include/boost/math/special_functions/ellint_rj.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@101 1 // Copyright (c) 2006 Xiaogang Zhang, 2015 John Maddock
Chris@16 2 // Use, modification and distribution are subject to the
Chris@16 3 // Boost Software License, Version 1.0. (See accompanying file
Chris@16 4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 5 //
Chris@16 6 // History:
Chris@16 7 // XZ wrote the original of this file as part of the Google
Chris@16 8 // Summer of Code 2006. JM modified it to fit into the
Chris@16 9 // Boost.Math conceptual framework better, and to correctly
Chris@16 10 // handle the p < 0 case.
Chris@101 11 // Updated 2015 to use Carlson's latest methods.
Chris@16 12 //
Chris@16 13
Chris@16 14 #ifndef BOOST_MATH_ELLINT_RJ_HPP
Chris@16 15 #define BOOST_MATH_ELLINT_RJ_HPP
Chris@16 16
Chris@16 17 #ifdef _MSC_VER
Chris@16 18 #pragma once
Chris@16 19 #endif
Chris@16 20
Chris@16 21 #include <boost/math/special_functions/math_fwd.hpp>
Chris@16 22 #include <boost/math/tools/config.hpp>
Chris@16 23 #include <boost/math/policies/error_handling.hpp>
Chris@16 24 #include <boost/math/special_functions/ellint_rc.hpp>
Chris@16 25 #include <boost/math/special_functions/ellint_rf.hpp>
Chris@101 26 #include <boost/math/special_functions/ellint_rd.hpp>
Chris@16 27
Chris@16 28 // Carlson's elliptic integral of the third kind
Chris@16 29 // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt
Chris@16 30 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
Chris@16 31
Chris@16 32 namespace boost { namespace math { namespace detail{
Chris@16 33
Chris@16 34 template <typename T, typename Policy>
Chris@101 35 T ellint_rc1p_imp(T y, const Policy& pol)
Chris@101 36 {
Chris@101 37 using namespace boost::math;
Chris@101 38 // Calculate RC(1, 1 + x)
Chris@101 39 BOOST_MATH_STD_USING
Chris@101 40
Chris@101 41 static const char* function = "boost::math::ellint_rc<%1%>(%1%,%1%)";
Chris@101 42
Chris@101 43 if(y == -1)
Chris@101 44 {
Chris@101 45 return policies::raise_domain_error<T>(function,
Chris@101 46 "Argument y must not be zero but got %1%", y, pol);
Chris@101 47 }
Chris@101 48
Chris@101 49 // for 1 + y < 0, the integral is singular, return Cauchy principal value
Chris@101 50 T result;
Chris@101 51 if(y < -1)
Chris@101 52 {
Chris@101 53 result = sqrt(1 / -y) * detail::ellint_rc_imp(T(-y), T(-1 - y), pol);
Chris@101 54 }
Chris@101 55 else if(y == 0)
Chris@101 56 {
Chris@101 57 result = 1;
Chris@101 58 }
Chris@101 59 else if(y > 0)
Chris@101 60 {
Chris@101 61 result = atan(sqrt(y)) / sqrt(y);
Chris@101 62 }
Chris@101 63 else
Chris@101 64 {
Chris@101 65 if(y > -0.5)
Chris@101 66 {
Chris@101 67 T arg = sqrt(-y);
Chris@101 68 result = (boost::math::log1p(arg) - boost::math::log1p(-arg)) / (2 * sqrt(-y));
Chris@101 69 }
Chris@101 70 else
Chris@101 71 {
Chris@101 72 result = log((1 + sqrt(-y)) / sqrt(1 + y)) / sqrt(-y);
Chris@101 73 }
Chris@101 74 }
Chris@101 75 return result;
Chris@101 76 }
Chris@101 77
Chris@101 78 template <typename T, typename Policy>
Chris@16 79 T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
Chris@16 80 {
Chris@101 81 BOOST_MATH_STD_USING
Chris@16 82
Chris@101 83 static const char* function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
Chris@16 84
Chris@101 85 if(x < 0)
Chris@101 86 {
Chris@101 87 return policies::raise_domain_error<T>(function,
Chris@101 88 "Argument x must be non-negative, but got x = %1%", x, pol);
Chris@101 89 }
Chris@101 90 if(y < 0)
Chris@101 91 {
Chris@101 92 return policies::raise_domain_error<T>(function,
Chris@101 93 "Argument y must be non-negative, but got y = %1%", y, pol);
Chris@101 94 }
Chris@101 95 if(z < 0)
Chris@101 96 {
Chris@101 97 return policies::raise_domain_error<T>(function,
Chris@101 98 "Argument z must be non-negative, but got z = %1%", z, pol);
Chris@101 99 }
Chris@101 100 if(p == 0)
Chris@101 101 {
Chris@101 102 return policies::raise_domain_error<T>(function,
Chris@101 103 "Argument p must not be zero, but got p = %1%", p, pol);
Chris@101 104 }
Chris@101 105 if(x + y == 0 || y + z == 0 || z + x == 0)
Chris@101 106 {
Chris@101 107 return policies::raise_domain_error<T>(function,
Chris@101 108 "At most one argument can be zero, "
Chris@101 109 "only possible result is %1%.", std::numeric_limits<T>::quiet_NaN(), pol);
Chris@101 110 }
Chris@16 111
Chris@101 112 // for p < 0, the integral is singular, return Cauchy principal value
Chris@101 113 if(p < 0)
Chris@101 114 {
Chris@101 115 //
Chris@101 116 // We must ensure that x < y < z.
Chris@101 117 // Since the integral is symmetrical in x, y and z
Chris@101 118 // we can just permute the values:
Chris@101 119 //
Chris@101 120 if(x > y)
Chris@101 121 std::swap(x, y);
Chris@101 122 if(y > z)
Chris@101 123 std::swap(y, z);
Chris@101 124 if(x > y)
Chris@101 125 std::swap(x, y);
Chris@16 126
Chris@101 127 BOOST_ASSERT(x <= y);
Chris@101 128 BOOST_ASSERT(y <= z);
Chris@16 129
Chris@101 130 T q = -p;
Chris@101 131 p = (z * (x + y + q) - x * y) / (z + q);
Chris@16 132
Chris@101 133 BOOST_ASSERT(p >= 0);
Chris@16 134
Chris@101 135 T value = (p - z) * ellint_rj_imp(x, y, z, p, pol);
Chris@101 136 value -= 3 * ellint_rf_imp(x, y, z, pol);
Chris@101 137 value += 3 * sqrt((x * y * z) / (x * y + p * q)) * ellint_rc_imp(T(x * y + p * q), T(p * q), pol);
Chris@101 138 value /= (z + q);
Chris@101 139 return value;
Chris@101 140 }
Chris@16 141
Chris@101 142 //
Chris@101 143 // Special cases from http://dlmf.nist.gov/19.20#iii
Chris@101 144 //
Chris@101 145 if(x == y)
Chris@101 146 {
Chris@101 147 if(x == z)
Chris@101 148 {
Chris@101 149 if(x == p)
Chris@101 150 {
Chris@101 151 // All values equal:
Chris@101 152 return 1 / (x * sqrt(x));
Chris@101 153 }
Chris@101 154 else
Chris@101 155 {
Chris@101 156 // x = y = z:
Chris@101 157 return 3 * (ellint_rc_imp(x, p, pol) - 1 / sqrt(x)) / (x - p);
Chris@101 158 }
Chris@101 159 }
Chris@101 160 else
Chris@101 161 {
Chris@101 162 // x = y only, permute so y = z:
Chris@101 163 using std::swap;
Chris@101 164 swap(x, z);
Chris@101 165 if(y == p)
Chris@101 166 {
Chris@101 167 return ellint_rd_imp(x, y, y, pol);
Chris@101 168 }
Chris@101 169 else if((std::max)(y, p) / (std::min)(y, p) > 1.2)
Chris@101 170 {
Chris@101 171 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
Chris@101 172 }
Chris@101 173 // Otherwise fall through to normal method, special case above will suffer too much cancellation...
Chris@101 174 }
Chris@101 175 }
Chris@101 176 if(y == z)
Chris@101 177 {
Chris@101 178 if(y == p)
Chris@101 179 {
Chris@101 180 // y = z = p:
Chris@101 181 return ellint_rd_imp(x, y, y, pol);
Chris@101 182 }
Chris@101 183 else if((std::max)(y, p) / (std::min)(y, p) > 1.2)
Chris@101 184 {
Chris@101 185 // y = z:
Chris@101 186 return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
Chris@101 187 }
Chris@101 188 // Otherwise fall through to normal method, special case above will suffer too much cancellation...
Chris@101 189 }
Chris@101 190 if(z == p)
Chris@101 191 {
Chris@101 192 return ellint_rd_imp(x, y, z, pol);
Chris@101 193 }
Chris@16 194
Chris@101 195 T xn = x;
Chris@101 196 T yn = y;
Chris@101 197 T zn = z;
Chris@101 198 T pn = p;
Chris@101 199 T An = (x + y + z + 2 * p) / 5;
Chris@101 200 T A0 = An;
Chris@101 201 T delta = (p - x) * (p - y) * (p - z);
Chris@101 202 T Q = pow(tools::epsilon<T>() / 5, -T(1) / 8) * (std::max)((std::max)(fabs(An - x), fabs(An - y)), (std::max)(fabs(An - z), fabs(An - p)));
Chris@16 203
Chris@101 204 unsigned n;
Chris@101 205 T lambda;
Chris@101 206 T Dn;
Chris@101 207 T En;
Chris@101 208 T rx, ry, rz, rp;
Chris@101 209 T fmn = 1; // 4^-n
Chris@101 210 T RC_sum = 0;
Chris@16 211
Chris@101 212 for(n = 0; n < policies::get_max_series_iterations<Policy>(); ++n)
Chris@101 213 {
Chris@101 214 rx = sqrt(xn);
Chris@101 215 ry = sqrt(yn);
Chris@101 216 rz = sqrt(zn);
Chris@101 217 rp = sqrt(pn);
Chris@101 218 Dn = (rp + rx) * (rp + ry) * (rp + rz);
Chris@101 219 En = delta / Dn;
Chris@101 220 En /= Dn;
Chris@101 221 if((En < -0.5) && (En > -1.5))
Chris@101 222 {
Chris@101 223 //
Chris@101 224 // Occationally En ~ -1, we then have no means of calculating
Chris@101 225 // RC(1, 1+En) without terrible cancellation error, so we
Chris@101 226 // need to get to 1+En directly. By substitution we have
Chris@101 227 //
Chris@101 228 // 1+E_0 = 1 + (p-x)*(p-y)*(p-z)/((sqrt(p) + sqrt(x))*(sqrt(p)+sqrt(y))*(sqrt(p)+sqrt(z)))^2
Chris@101 229 // = 2*sqrt(p)*(p+sqrt(x) * (sqrt(y)+sqrt(z)) + sqrt(y)*sqrt(z)) / ((sqrt(p) + sqrt(x))*(sqrt(p) + sqrt(y)*(sqrt(p)+sqrt(z))))
Chris@101 230 //
Chris@101 231 // And since this is just an application of the duplication formula for RJ, the same
Chris@101 232 // expression works for 1+En if we use x,y,z,p_n etc.
Chris@101 233 // This branch is taken only once or twice at the start of iteration,
Chris@101 234 // after than En reverts to it's usual very small values.
Chris@101 235 //
Chris@101 236 T b = 2 * rp * (pn + rx * (ry + rz) + ry * rz) / Dn;
Chris@101 237 RC_sum += fmn / Dn * detail::ellint_rc_imp(T(1), b, pol);
Chris@101 238 }
Chris@101 239 else
Chris@101 240 {
Chris@101 241 RC_sum += fmn / Dn * ellint_rc1p_imp(En, pol);
Chris@101 242 }
Chris@101 243 lambda = rx * ry + rx * rz + ry * rz;
Chris@16 244
Chris@101 245 // From here on we move to n+1:
Chris@101 246 An = (An + lambda) / 4;
Chris@101 247 fmn /= 4;
Chris@16 248
Chris@101 249 if(fmn * Q < An)
Chris@101 250 break;
Chris@101 251
Chris@101 252 xn = (xn + lambda) / 4;
Chris@101 253 yn = (yn + lambda) / 4;
Chris@101 254 zn = (zn + lambda) / 4;
Chris@101 255 pn = (pn + lambda) / 4;
Chris@101 256 delta /= 64;
Chris@101 257 }
Chris@101 258
Chris@101 259 T X = fmn * (A0 - x) / An;
Chris@101 260 T Y = fmn * (A0 - y) / An;
Chris@101 261 T Z = fmn * (A0 - z) / An;
Chris@101 262 T P = (-X - Y - Z) / 2;
Chris@101 263 T E2 = X * Y + X * Z + Y * Z - 3 * P * P;
Chris@101 264 T E3 = X * Y * Z + 2 * E2 * P + 4 * P * P * P;
Chris@101 265 T E4 = (2 * X * Y * Z + E2 * P + 3 * P * P * P) * P;
Chris@101 266 T E5 = X * Y * Z * P * P;
Chris@101 267 T result = fmn * pow(An, T(-3) / 2) *
Chris@101 268 (1 - 3 * E2 / 14 + E3 / 6 + 9 * E2 * E2 / 88 - 3 * E4 / 22 - 9 * E2 * E3 / 52 + 3 * E5 / 26 - E2 * E2 * E2 / 16
Chris@101 269 + 3 * E3 * E3 / 40 + 3 * E2 * E4 / 20 + 45 * E2 * E2 * E3 / 272 - 9 * (E3 * E4 + E2 * E5) / 68);
Chris@101 270
Chris@101 271 result += 6 * RC_sum;
Chris@101 272 return result;
Chris@16 273 }
Chris@16 274
Chris@16 275 } // namespace detail
Chris@16 276
Chris@16 277 template <class T1, class T2, class T3, class T4, class Policy>
Chris@16 278 inline typename tools::promote_args<T1, T2, T3, T4>::type
Chris@16 279 ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
Chris@16 280 {
Chris@16 281 typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
Chris@16 282 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 283 return policies::checked_narrowing_cast<result_type, Policy>(
Chris@16 284 detail::ellint_rj_imp(
Chris@16 285 static_cast<value_type>(x),
Chris@16 286 static_cast<value_type>(y),
Chris@16 287 static_cast<value_type>(z),
Chris@16 288 static_cast<value_type>(p),
Chris@16 289 pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
Chris@16 290 }
Chris@16 291
Chris@16 292 template <class T1, class T2, class T3, class T4>
Chris@16 293 inline typename tools::promote_args<T1, T2, T3, T4>::type
Chris@16 294 ellint_rj(T1 x, T2 y, T3 z, T4 p)
Chris@16 295 {
Chris@16 296 return ellint_rj(x, y, z, p, policies::policy<>());
Chris@16 297 }
Chris@16 298
Chris@16 299 }} // namespaces
Chris@16 300
Chris@16 301 #endif // BOOST_MATH_ELLINT_RJ_HPP
Chris@16 302