ellint_rc.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 to fit into the
  9. // Boost.Math conceptual framework better, and to correctly
  10. // handle the y < 0 case.
  11. //
  12. #ifndef BOOST_MATH_ELLINT_RC_HPP
  13. #define BOOST_MATH_ELLINT_RC_HPP
  14. #ifdef _MSC_VER
  15. #pragma once
  16. #endif
  17. #include <boost/math/policies/error_handling.hpp>
  18. #include <boost/math/tools/config.hpp>
  19. #include <boost/math/special_functions/math_fwd.hpp>
  20. // Carlson's degenerate elliptic integral
  21. // R_C(x, y) = R_F(x, y, y) = 0.5 * \int_{0}^{\infty} (t+x)^{-1/2} (t+y)^{-1} dt
  22. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  23. namespace boost { namespace math { namespace detail{
  24. template <typename T, typename Policy>
  25. T ellint_rc_imp(T x, T y, const Policy& pol)
  26. {
  27. T value, S, u, lambda, tolerance, prefix;
  28. unsigned long k;
  29. BOOST_MATH_STD_USING
  30. using namespace boost::math::tools;
  31. static const char* function = "boost::math::ellint_rc<%1%>(%1%,%1%)";
  32. if(x < 0)
  33. {
  34. return policies::raise_domain_error<T>(function,
  35. "Argument x must be non-negative but got %1%", x, pol);
  36. }
  37. if(y == 0)
  38. {
  39. return policies::raise_domain_error<T>(function,
  40. "Argument y must not be zero but got %1%", y, pol);
  41. }
  42. // error scales as the 6th power of tolerance
  43. tolerance = pow(4 * tools::epsilon<T>(), T(1) / 6);
  44. // for y < 0, the integral is singular, return Cauchy principal value
  45. if (y < 0)
  46. {
  47. prefix = sqrt(x / (x - y));
  48. x = x - y;
  49. y = -y;
  50. }
  51. else
  52. prefix = 1;
  53. // duplication:
  54. k = 1;
  55. do
  56. {
  57. u = (x + y + y) / 3;
  58. S = y / u - 1; // 1 - x / u = 2 * S
  59. if (2 * abs(S) < tolerance)
  60. break;
  61. T sx = sqrt(x);
  62. T sy = sqrt(y);
  63. lambda = 2 * sx * sy + y;
  64. x = (x + lambda) / 4;
  65. y = (y + lambda) / 4;
  66. ++k;
  67. }while(k < policies::get_max_series_iterations<Policy>());
  68. // Check to see if we gave up too soon:
  69. policies::check_series_iterations<T>(function, k, pol);
  70. // Taylor series expansion to the 5th order
  71. value = (1 + S * S * (T(3) / 10 + S * (T(1) / 7 + S * (T(3) / 8 + S * T(9) / 22)))) / sqrt(u);
  72. return value * prefix;
  73. }
  74. } // namespace detail
  75. template <class T1, class T2, class Policy>
  76. inline typename tools::promote_args<T1, T2>::type
  77. ellint_rc(T1 x, T2 y, const Policy& pol)
  78. {
  79. typedef typename tools::promote_args<T1, T2>::type result_type;
  80. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  81. return policies::checked_narrowing_cast<result_type, Policy>(
  82. detail::ellint_rc_imp(
  83. static_cast<value_type>(x),
  84. static_cast<value_type>(y), pol), "boost::math::ellint_rc<%1%>(%1%,%1%)");
  85. }
  86. template <class T1, class T2>
  87. inline typename tools::promote_args<T1, T2>::type
  88. ellint_rc(T1 x, T2 y)
  89. {
  90. return ellint_rc(x, y, policies::policy<>());
  91. }
  92. }} // namespaces
  93. #endif // BOOST_MATH_ELLINT_RC_HPP