ellint_rc.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2006 Xiaogang Zhang, 2015 John Maddock
  2. // Copyright (c) 2024 Matt Borland
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // History:
  8. // XZ wrote the original of this file as part of the Google
  9. // Summer of Code 2006. JM modified it to fit into the
  10. // Boost.Math conceptual framework better, and to correctly
  11. // handle the y < 0 case.
  12. // Updated 2015 to use Carlson's latest methods.
  13. //
  14. #ifndef BOOST_MATH_ELLINT_RC_HPP
  15. #define BOOST_MATH_ELLINT_RC_HPP
  16. #ifdef _MSC_VER
  17. #pragma once
  18. #endif
  19. #include <boost/math/tools/config.hpp>
  20. #include <boost/math/policies/error_handling.hpp>
  21. #include <boost/math/special_functions/math_fwd.hpp>
  22. #include <boost/math/special_functions/log1p.hpp>
  23. #include <boost/math/constants/constants.hpp>
  24. // Carlson's degenerate elliptic integral
  25. // R_C(x, y) = R_F(x, y, y) = 0.5 * \int_{0}^{\infty} (t+x)^{-1/2} (t+y)^{-1} dt
  26. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  27. namespace boost { namespace math { namespace detail{
  28. template <typename T, typename Policy>
  29. BOOST_MATH_GPU_ENABLED T ellint_rc_imp(T x, T y, const Policy& pol)
  30. {
  31. BOOST_MATH_STD_USING
  32. constexpr auto function = "boost::math::ellint_rc<%1%>(%1%,%1%)";
  33. if(x < 0)
  34. {
  35. return policies::raise_domain_error<T>(function, "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, "Argument y must not be zero but got %1%", y, pol);
  40. }
  41. // for y < 0, the integral is singular, return Cauchy principal value
  42. T prefix, result;
  43. if(y < 0)
  44. {
  45. prefix = sqrt(x / (x - y));
  46. x = x - y;
  47. y = -y;
  48. }
  49. else
  50. prefix = 1;
  51. if(x == 0)
  52. {
  53. result = constants::half_pi<T>() / sqrt(y);
  54. }
  55. else if(x == y)
  56. {
  57. result = 1 / sqrt(x);
  58. }
  59. else if(y > x)
  60. {
  61. result = atan(sqrt((y - x) / x)) / sqrt(y - x);
  62. }
  63. else
  64. {
  65. if(y / x > T(0.5))
  66. {
  67. T arg = sqrt((x - y) / x);
  68. result = (boost::math::log1p(arg, pol) - boost::math::log1p(-arg, pol)) / (2 * sqrt(x - y));
  69. }
  70. else
  71. {
  72. result = log((sqrt(x) + sqrt(x - y)) / sqrt(y)) / sqrt(x - y);
  73. }
  74. }
  75. return prefix * result;
  76. }
  77. } // namespace detail
  78. template <class T1, class T2, class Policy>
  79. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
  80. ellint_rc(T1 x, T2 y, const Policy& pol)
  81. {
  82. typedef typename tools::promote_args<T1, T2>::type result_type;
  83. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  84. return policies::checked_narrowing_cast<result_type, Policy>(
  85. detail::ellint_rc_imp(
  86. static_cast<value_type>(x),
  87. static_cast<value_type>(y), pol), "boost::math::ellint_rc<%1%>(%1%,%1%)");
  88. }
  89. template <class T1, class T2>
  90. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
  91. ellint_rc(T1 x, T2 y)
  92. {
  93. return ellint_rc(x, y, policies::policy<>());
  94. }
  95. }} // namespaces
  96. #endif // BOOST_MATH_ELLINT_RC_HPP