ellint_rg.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2015 John Maddock
  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. #ifndef BOOST_MATH_ELLINT_RG_HPP
  7. #define BOOST_MATH_ELLINT_RG_HPP
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #endif
  11. #include <boost/math/special_functions/math_fwd.hpp>
  12. #include <boost/math/tools/config.hpp>
  13. #include <boost/math/constants/constants.hpp>
  14. #include <boost/math/policies/error_handling.hpp>
  15. #include <boost/math/special_functions/ellint_rd.hpp>
  16. #include <boost/math/special_functions/ellint_rf.hpp>
  17. #include <boost/math/special_functions/pow.hpp>
  18. namespace boost { namespace math { namespace detail{
  19. template <typename T, typename Policy>
  20. T ellint_rg_imp(T x, T y, T z, const Policy& pol)
  21. {
  22. BOOST_MATH_STD_USING
  23. static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
  24. if(x < 0 || y < 0 || z < 0)
  25. {
  26. return policies::raise_domain_error<T>(function,
  27. "domain error, all arguments must be non-negative, "
  28. "only sensible result is %1%.",
  29. std::numeric_limits<T>::quiet_NaN(), pol);
  30. }
  31. //
  32. // Function is symmetric in x, y and z, but we require
  33. // (x - z)(y - z) >= 0 to avoid cancellation error in the result
  34. // which implies (for example) x >= z >= y
  35. //
  36. using std::swap;
  37. if(x < y)
  38. swap(x, y);
  39. if(x < z)
  40. swap(x, z);
  41. if(y > z)
  42. swap(y, z);
  43. BOOST_ASSERT(x >= z);
  44. BOOST_ASSERT(z >= y);
  45. //
  46. // Special cases from http://dlmf.nist.gov/19.20#ii
  47. //
  48. if(x == z)
  49. {
  50. if(y == z)
  51. {
  52. // x = y = z
  53. // This also works for x = y = z = 0 presumably.
  54. return sqrt(x);
  55. }
  56. else if(y == 0)
  57. {
  58. // x = y, z = 0
  59. return constants::pi<T>() * sqrt(x) / 4;
  60. }
  61. else
  62. {
  63. // x = z, y != 0
  64. swap(x, y);
  65. return (x == 0) ? T(sqrt(z) / 2) : T((z * ellint_rc_imp(x, z, pol) + sqrt(x)) / 2);
  66. }
  67. }
  68. else if(y == z)
  69. {
  70. if(x == 0)
  71. return constants::pi<T>() * sqrt(y) / 4;
  72. else
  73. return (y == 0) ? T(sqrt(x) / 2) : T((y * ellint_rc_imp(x, y, pol) + sqrt(x)) / 2);
  74. }
  75. else if(y == 0)
  76. {
  77. swap(y, z);
  78. //
  79. // Special handling for common case, from
  80. // Numerical Computation of Real or Complex Elliptic Integrals, eq.46
  81. //
  82. T xn = sqrt(x);
  83. T yn = sqrt(y);
  84. T x0 = xn;
  85. T y0 = yn;
  86. T sum = 0;
  87. T sum_pow = 0.25f;
  88. while(fabs(xn - yn) >= 2.7 * tools::root_epsilon<T>() * fabs(xn))
  89. {
  90. T t = sqrt(xn * yn);
  91. xn = (xn + yn) / 2;
  92. yn = t;
  93. sum_pow *= 2;
  94. sum += sum_pow * boost::math::pow<2>(xn - yn);
  95. }
  96. T RF = constants::pi<T>() / (xn + yn);
  97. return ((boost::math::pow<2>((x0 + y0) / 2) - sum) * RF) / 2;
  98. }
  99. return (z * ellint_rf_imp(x, y, z, pol)
  100. - (x - z) * (y - z) * ellint_rd_imp(x, y, z, pol) / 3
  101. + sqrt(x * y / z)) / 2;
  102. }
  103. } // namespace detail
  104. template <class T1, class T2, class T3, class Policy>
  105. inline typename tools::promote_args<T1, T2, T3>::type
  106. ellint_rg(T1 x, T2 y, T3 z, const Policy& pol)
  107. {
  108. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  109. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  110. return policies::checked_narrowing_cast<result_type, Policy>(
  111. detail::ellint_rg_imp(
  112. static_cast<value_type>(x),
  113. static_cast<value_type>(y),
  114. static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
  115. }
  116. template <class T1, class T2, class T3>
  117. inline typename tools::promote_args<T1, T2, T3>::type
  118. ellint_rg(T1 x, T2 y, T3 z)
  119. {
  120. return ellint_rg(x, y, z, policies::policy<>());
  121. }
  122. }} // namespaces
  123. #endif // BOOST_MATH_ELLINT_RG_HPP