ellint_rc.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2006 Xiaogang Zhang, 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. // 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. // Updated 2015 to use Carlson's latest methods.
  12. //
  13. #ifndef BOOST_MATH_ELLINT_RC_HPP
  14. #define BOOST_MATH_ELLINT_RC_HPP
  15. #ifdef _MSC_VER
  16. #pragma once
  17. #endif
  18. #include <boost/math/policies/error_handling.hpp>
  19. #include <boost/math/tools/config.hpp>
  20. #include <boost/math/special_functions/math_fwd.hpp>
  21. #include <boost/math/special_functions/log1p.hpp>
  22. #include <boost/math/constants/constants.hpp>
  23. #include <iostream>
  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. T ellint_rc_imp(T x, T y, const Policy& pol)
  30. {
  31. BOOST_MATH_STD_USING
  32. static const char* 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. 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. 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