ellint_rg.hpp 4.0 KB

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