ellint_rj.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 p < 0 case.
  11. //
  12. #ifndef BOOST_MATH_ELLINT_RJ_HPP
  13. #define BOOST_MATH_ELLINT_RJ_HPP
  14. #ifdef _MSC_VER
  15. #pragma once
  16. #endif
  17. #include <boost/math/special_functions/math_fwd.hpp>
  18. #include <boost/math/tools/config.hpp>
  19. #include <boost/math/policies/error_handling.hpp>
  20. #include <boost/math/special_functions/ellint_rc.hpp>
  21. #include <boost/math/special_functions/ellint_rf.hpp>
  22. // Carlson's elliptic integral of the third kind
  23. // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt
  24. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  25. namespace boost { namespace math { namespace detail{
  26. template <typename T, typename Policy>
  27. T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
  28. {
  29. T value, u, lambda, alpha, beta, sigma, factor, tolerance;
  30. T X, Y, Z, P, EA, EB, EC, E2, E3, S1, S2, S3;
  31. unsigned long k;
  32. BOOST_MATH_STD_USING
  33. using namespace boost::math::tools;
  34. static const char* function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
  35. if (x < 0)
  36. {
  37. return policies::raise_domain_error<T>(function,
  38. "Argument x must be non-negative, but got x = %1%", x, pol);
  39. }
  40. if(y < 0)
  41. {
  42. return policies::raise_domain_error<T>(function,
  43. "Argument y must be non-negative, but got y = %1%", y, pol);
  44. }
  45. if(z < 0)
  46. {
  47. return policies::raise_domain_error<T>(function,
  48. "Argument z must be non-negative, but got z = %1%", z, pol);
  49. }
  50. if(p == 0)
  51. {
  52. return policies::raise_domain_error<T>(function,
  53. "Argument p must not be zero, but got p = %1%", p, pol);
  54. }
  55. if (x + y == 0 || y + z == 0 || z + x == 0)
  56. {
  57. return policies::raise_domain_error<T>(function,
  58. "At most one argument can be zero, "
  59. "only possible result is %1%.", std::numeric_limits<T>::quiet_NaN(), pol);
  60. }
  61. // error scales as the 6th power of tolerance
  62. tolerance = pow(T(1) * tools::epsilon<T>() / 3, T(1) / 6);
  63. // for p < 0, the integral is singular, return Cauchy principal value
  64. if (p < 0)
  65. {
  66. //
  67. // We must ensure that (z - y) * (y - x) is positive.
  68. // Since the integral is symmetrical in x, y and z
  69. // we can just permute the values:
  70. //
  71. if(x > y)
  72. std::swap(x, y);
  73. if(y > z)
  74. std::swap(y, z);
  75. if(x > y)
  76. std::swap(x, y);
  77. T q = -p;
  78. T pmy = (z - y) * (y - x) / (y + q); // p - y
  79. BOOST_ASSERT(pmy >= 0);
  80. p = pmy + y;
  81. value = boost::math::ellint_rj(x, y, z, p, pol);
  82. value *= pmy;
  83. value -= 3 * boost::math::ellint_rf(x, y, z, pol);
  84. value += 3 * sqrt((x * y * z) / (x * z + p * q)) * boost::math::ellint_rc(x * z + p * q, p * q, pol);
  85. value /= (y + q);
  86. return value;
  87. }
  88. // duplication
  89. sigma = 0;
  90. factor = 1;
  91. k = 1;
  92. do
  93. {
  94. u = (x + y + z + p + p) / 5;
  95. X = (u - x) / u;
  96. Y = (u - y) / u;
  97. Z = (u - z) / u;
  98. P = (u - p) / u;
  99. if ((tools::max)(abs(X), abs(Y), abs(Z), abs(P)) < tolerance)
  100. break;
  101. T sx = sqrt(x);
  102. T sy = sqrt(y);
  103. T sz = sqrt(z);
  104. lambda = sy * (sx + sz) + sz * sx;
  105. alpha = p * (sx + sy + sz) + sx * sy * sz;
  106. alpha *= alpha;
  107. beta = p * (p + lambda) * (p + lambda);
  108. sigma += factor * boost::math::ellint_rc(alpha, beta, pol);
  109. factor /= 4;
  110. x = (x + lambda) / 4;
  111. y = (y + lambda) / 4;
  112. z = (z + lambda) / 4;
  113. p = (p + lambda) / 4;
  114. ++k;
  115. }
  116. while(k < policies::get_max_series_iterations<Policy>());
  117. // Check to see if we gave up too soon:
  118. policies::check_series_iterations<T>(function, k, pol);
  119. // Taylor series expansion to the 5th order
  120. EA = X * Y + Y * Z + Z * X;
  121. EB = X * Y * Z;
  122. EC = P * P;
  123. E2 = EA - 3 * EC;
  124. E3 = EB + 2 * P * (EA - EC);
  125. S1 = 1 + E2 * (E2 * T(9) / 88 - E3 * T(9) / 52 - T(3) / 14);
  126. S2 = EB * (T(1) / 6 + P * (T(-6) / 22 + P * T(3) / 26));
  127. S3 = P * ((EA - EC) / 3 - P * EA * T(3) / 22);
  128. value = 3 * sigma + factor * (S1 + S2 + S3) / (u * sqrt(u));
  129. return value;
  130. }
  131. } // namespace detail
  132. template <class T1, class T2, class T3, class T4, class Policy>
  133. inline typename tools::promote_args<T1, T2, T3, T4>::type
  134. ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
  135. {
  136. typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
  137. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  138. return policies::checked_narrowing_cast<result_type, Policy>(
  139. detail::ellint_rj_imp(
  140. static_cast<value_type>(x),
  141. static_cast<value_type>(y),
  142. static_cast<value_type>(z),
  143. static_cast<value_type>(p),
  144. pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
  145. }
  146. template <class T1, class T2, class T3, class T4>
  147. inline typename tools::promote_args<T1, T2, T3, T4>::type
  148. ellint_rj(T1 x, T2 y, T3 z, T4 p)
  149. {
  150. return ellint_rj(x, y, z, p, policies::policy<>());
  151. }
  152. }} // namespaces
  153. #endif // BOOST_MATH_ELLINT_RJ_HPP