ellint_rd.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 2006 Xiaogang Zhang
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // History:
  7. // XZ wrote the original of this file as part of the Google
  8. // Summer of Code 2006. JM modified it slightly to fit into the
  9. // Boost.Math conceptual framework better.
  10. #ifndef BOOST_MATH_ELLINT_RD_HPP
  11. #define BOOST_MATH_ELLINT_RD_HPP
  12. #ifdef _MSC_VER
  13. #pragma once
  14. #endif
  15. #include <boost/math/special_functions/math_fwd.hpp>
  16. #include <boost/math/tools/config.hpp>
  17. #include <boost/math/policies/error_handling.hpp>
  18. // Carlson's elliptic integral of the second kind
  19. // R_D(x, y, z) = R_J(x, y, z, z) = 1.5 * \int_{0}^{\infty} [(t+x)(t+y)]^{-1/2} (t+z)^{-3/2} dt
  20. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  21. namespace boost { namespace math { namespace detail{
  22. template <typename T, typename Policy>
  23. T ellint_rd_imp(T x, T y, T z, const Policy& pol)
  24. {
  25. T value, u, lambda, sigma, factor, tolerance;
  26. T X, Y, Z, EA, EB, EC, ED, EE, S1, S2;
  27. unsigned long k;
  28. BOOST_MATH_STD_USING
  29. using namespace boost::math::tools;
  30. static const char* function = "boost::math::ellint_rd<%1%>(%1%,%1%,%1%)";
  31. if (x < 0)
  32. {
  33. return policies::raise_domain_error<T>(function,
  34. "Argument x must be >= 0, but got %1%", x, pol);
  35. }
  36. if (y < 0)
  37. {
  38. return policies::raise_domain_error<T>(function,
  39. "Argument y must be >= 0, but got %1%", y, pol);
  40. }
  41. if (z <= 0)
  42. {
  43. return policies::raise_domain_error<T>(function,
  44. "Argument z must be > 0, but got %1%", z, pol);
  45. }
  46. if (x + y == 0)
  47. {
  48. return policies::raise_domain_error<T>(function,
  49. "At most one argument can be zero, but got, x + y = %1%", x+y, pol);
  50. }
  51. // error scales as the 6th power of tolerance
  52. tolerance = pow(tools::epsilon<T>() / 3, T(1)/6);
  53. // duplication
  54. sigma = 0;
  55. factor = 1;
  56. k = 1;
  57. do
  58. {
  59. u = (x + y + z + z + z) / 5;
  60. X = (u - x) / u;
  61. Y = (u - y) / u;
  62. Z = (u - z) / u;
  63. if ((tools::max)(abs(X), abs(Y), abs(Z)) < tolerance)
  64. break;
  65. T sx = sqrt(x);
  66. T sy = sqrt(y);
  67. T sz = sqrt(z);
  68. lambda = sy * (sx + sz) + sz * sx; //sqrt(x * y) + sqrt(y * z) + sqrt(z * x);
  69. sigma += factor / (sz * (z + lambda));
  70. factor /= 4;
  71. x = (x + lambda) / 4;
  72. y = (y + lambda) / 4;
  73. z = (z + lambda) / 4;
  74. ++k;
  75. }
  76. while(k < policies::get_max_series_iterations<Policy>());
  77. // Check to see if we gave up too soon:
  78. policies::check_series_iterations<T>(function, k, pol);
  79. // Taylor series expansion to the 5th order
  80. EA = X * Y;
  81. EB = Z * Z;
  82. EC = EA - EB;
  83. ED = EA - 6 * EB;
  84. EE = ED + EC + EC;
  85. S1 = ED * (ED * T(9) / 88 - Z * EE * T(9) / 52 - T(3) / 14);
  86. S2 = Z * (EE / 6 + Z * (-EC * T(9) / 22 + Z * EA * T(3) / 26));
  87. value = 3 * sigma + factor * (1 + S1 + S2) / (u * sqrt(u));
  88. return value;
  89. }
  90. } // namespace detail
  91. template <class T1, class T2, class T3, class Policy>
  92. inline typename tools::promote_args<T1, T2, T3>::type
  93. ellint_rd(T1 x, T2 y, T3 z, const Policy& pol)
  94. {
  95. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  96. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  97. return policies::checked_narrowing_cast<result_type, Policy>(
  98. detail::ellint_rd_imp(
  99. static_cast<value_type>(x),
  100. static_cast<value_type>(y),
  101. static_cast<value_type>(z), pol), "boost::math::ellint_rd<%1%>(%1%,%1%,%1%)");
  102. }
  103. template <class T1, class T2, class T3>
  104. inline typename tools::promote_args<T1, T2, T3>::type
  105. ellint_rd(T1 x, T2 y, T3 z)
  106. {
  107. return ellint_rd(x, y, z, policies::policy<>());
  108. }
  109. }} // namespaces
  110. #endif // BOOST_MATH_ELLINT_RD_HPP