ellint_rc.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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,
  36. "Argument x must be non-negative but got %1%", x, pol);
  37. }
  38. if(y == 0)
  39. {
  40. return policies::raise_domain_error<T>(function,
  41. "Argument y must not be zero but got %1%", y, pol);
  42. }
  43. // for y < 0, the integral is singular, return Cauchy principal value
  44. T prefix, result;
  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. if(x == 0)
  54. {
  55. result = constants::half_pi<T>() / sqrt(y);
  56. }
  57. else if(x == y)
  58. {
  59. result = 1 / sqrt(x);
  60. }
  61. else if(y > x)
  62. {
  63. result = atan(sqrt((y - x) / x)) / sqrt(y - x);
  64. }
  65. else
  66. {
  67. if(y / x > 0.5)
  68. {
  69. T arg = sqrt((x - y) / x);
  70. result = (boost::math::log1p(arg) - boost::math::log1p(-arg)) / (2 * sqrt(x - y));
  71. }
  72. else
  73. {
  74. result = log((sqrt(x) + sqrt(x - y)) / sqrt(y)) / sqrt(x - y);
  75. }
  76. }
  77. return prefix * result;
  78. }
  79. } // namespace detail
  80. template <class T1, class T2, class Policy>
  81. inline typename tools::promote_args<T1, T2>::type
  82. ellint_rc(T1 x, T2 y, const Policy& pol)
  83. {
  84. typedef typename tools::promote_args<T1, T2>::type result_type;
  85. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  86. return policies::checked_narrowing_cast<result_type, Policy>(
  87. detail::ellint_rc_imp(
  88. static_cast<value_type>(x),
  89. static_cast<value_type>(y), pol), "boost::math::ellint_rc<%1%>(%1%,%1%)");
  90. }
  91. template <class T1, class T2>
  92. inline typename tools::promote_args<T1, T2>::type
  93. ellint_rc(T1 x, T2 y)
  94. {
  95. return ellint_rc(x, y, policies::policy<>());
  96. }
  97. }} // namespaces
  98. #endif // BOOST_MATH_ELLINT_RC_HPP