ellint_rj.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 p < 0 case.
  12. // Updated 2015 to use Carlson's latest methods.
  13. //
  14. #ifndef BOOST_MATH_ELLINT_RJ_HPP
  15. #define BOOST_MATH_ELLINT_RJ_HPP
  16. #ifdef _MSC_VER
  17. #pragma once
  18. #endif
  19. #include <boost/math/tools/config.hpp>
  20. #include <boost/math/tools/numeric_limits.hpp>
  21. #include <boost/math/special_functions/math_fwd.hpp>
  22. #include <boost/math/policies/error_handling.hpp>
  23. #include <boost/math/special_functions/ellint_rc.hpp>
  24. #include <boost/math/special_functions/ellint_rf.hpp>
  25. #include <boost/math/special_functions/ellint_rd.hpp>
  26. // Carlson's elliptic integral of the third kind
  27. // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt
  28. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  29. namespace boost { namespace math { namespace detail{
  30. template <typename T, typename Policy>
  31. BOOST_MATH_GPU_ENABLED T ellint_rc1p_imp(T y, const Policy& pol)
  32. {
  33. using namespace boost::math;
  34. // Calculate RC(1, 1 + x)
  35. BOOST_MATH_STD_USING
  36. BOOST_MATH_ASSERT(y != -1);
  37. // for 1 + y < 0, the integral is singular, return Cauchy principal value
  38. T result;
  39. if(y < -1)
  40. {
  41. result = sqrt(1 / -y) * detail::ellint_rc_imp(T(-y), T(-1 - y), pol);
  42. }
  43. else if(y == 0)
  44. {
  45. result = 1;
  46. }
  47. else if(y > 0)
  48. {
  49. result = atan(sqrt(y)) / sqrt(y);
  50. }
  51. else
  52. {
  53. if(y > T(-0.5))
  54. {
  55. T arg = sqrt(-y);
  56. result = (boost::math::log1p(arg, pol) - boost::math::log1p(-arg, pol)) / (2 * sqrt(-y));
  57. }
  58. else
  59. {
  60. result = log((1 + sqrt(-y)) / sqrt(1 + y)) / sqrt(-y);
  61. }
  62. }
  63. return result;
  64. }
  65. template <typename T, typename Policy>
  66. BOOST_MATH_GPU_ENABLED T ellint_rj_imp_final(T x, T y, T z, T p, const Policy& pol)
  67. {
  68. BOOST_MATH_STD_USING
  69. //
  70. // Special cases from http://dlmf.nist.gov/19.20#iii
  71. //
  72. if(x == y)
  73. {
  74. if(x == z)
  75. {
  76. if(x == p)
  77. {
  78. // All values equal:
  79. return 1 / (x * sqrt(x));
  80. }
  81. else
  82. {
  83. // x = y = z:
  84. return 3 * (ellint_rc_imp(x, p, pol) - 1 / sqrt(x)) / (x - p);
  85. }
  86. }
  87. else
  88. {
  89. // x = y only, permute so y = z:
  90. BOOST_MATH_GPU_SAFE_SWAP(x, z);
  91. if(y == p)
  92. {
  93. return ellint_rd_imp(x, y, y, pol);
  94. }
  95. else if(BOOST_MATH_GPU_SAFE_MAX(y, p) / BOOST_MATH_GPU_SAFE_MIN(y, p) > T(1.2))
  96. {
  97. return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
  98. }
  99. // Otherwise fall through to normal method, special case above will suffer too much cancellation...
  100. }
  101. }
  102. if(y == z)
  103. {
  104. if(y == p)
  105. {
  106. // y = z = p:
  107. return ellint_rd_imp(x, y, y, pol);
  108. }
  109. else if(BOOST_MATH_GPU_SAFE_MAX(y, p) / BOOST_MATH_GPU_SAFE_MIN(y, p) > T(1.2))
  110. {
  111. // y = z:
  112. return 3 * (ellint_rc_imp(x, y, pol) - ellint_rc_imp(x, p, pol)) / (p - y);
  113. }
  114. // Otherwise fall through to normal method, special case above will suffer too much cancellation...
  115. }
  116. if(z == p)
  117. {
  118. return ellint_rd_imp(x, y, z, pol);
  119. }
  120. T xn = x;
  121. T yn = y;
  122. T zn = z;
  123. T pn = p;
  124. T An = (x + y + z + 2 * p) / 5;
  125. T A0 = An;
  126. T delta = (p - x) * (p - y) * (p - z);
  127. T Q = pow(tools::epsilon<T>() / 5, -T(1) / 8) * BOOST_MATH_GPU_SAFE_MAX(BOOST_MATH_GPU_SAFE_MAX(fabs(An - x), fabs(An - y)), BOOST_MATH_GPU_SAFE_MAX(fabs(An - z), fabs(An - p)));
  128. unsigned n;
  129. T lambda;
  130. T Dn;
  131. T En;
  132. T rx, ry, rz, rp;
  133. T fmn = 1; // 4^-n
  134. T RC_sum = 0;
  135. for(n = 0; n < policies::get_max_series_iterations<Policy>(); ++n)
  136. {
  137. rx = sqrt(xn);
  138. ry = sqrt(yn);
  139. rz = sqrt(zn);
  140. rp = sqrt(pn);
  141. Dn = (rp + rx) * (rp + ry) * (rp + rz);
  142. En = delta / Dn;
  143. En /= Dn;
  144. if((En < T(-0.5)) && (En > T(-1.5)))
  145. {
  146. //
  147. // Occasionally En ~ -1, we then have no means of calculating
  148. // RC(1, 1+En) without terrible cancellation error, so we
  149. // need to get to 1+En directly. By substitution we have
  150. //
  151. // 1+E_0 = 1 + (p-x)*(p-y)*(p-z)/((sqrt(p) + sqrt(x))*(sqrt(p)+sqrt(y))*(sqrt(p)+sqrt(z)))^2
  152. // = 2*sqrt(p)*(p+sqrt(x) * (sqrt(y)+sqrt(z)) + sqrt(y)*sqrt(z)) / ((sqrt(p) + sqrt(x))*(sqrt(p) + sqrt(y)*(sqrt(p)+sqrt(z))))
  153. //
  154. // And since this is just an application of the duplication formula for RJ, the same
  155. // expression works for 1+En if we use x,y,z,p_n etc.
  156. // This branch is taken only once or twice at the start of iteration,
  157. // after than En reverts to it's usual very small values.
  158. //
  159. T b = 2 * rp * (pn + rx * (ry + rz) + ry * rz) / Dn;
  160. RC_sum += fmn / Dn * detail::ellint_rc_imp(T(1), b, pol);
  161. }
  162. else
  163. {
  164. RC_sum += fmn / Dn * ellint_rc1p_imp(En, pol);
  165. }
  166. lambda = rx * ry + rx * rz + ry * rz;
  167. // From here on we move to n+1:
  168. An = (An + lambda) / 4;
  169. fmn /= 4;
  170. if(fmn * Q < An)
  171. break;
  172. xn = (xn + lambda) / 4;
  173. yn = (yn + lambda) / 4;
  174. zn = (zn + lambda) / 4;
  175. pn = (pn + lambda) / 4;
  176. delta /= 64;
  177. }
  178. T X = fmn * (A0 - x) / An;
  179. T Y = fmn * (A0 - y) / An;
  180. T Z = fmn * (A0 - z) / An;
  181. T P = (-X - Y - Z) / 2;
  182. T E2 = X * Y + X * Z + Y * Z - 3 * P * P;
  183. T E3 = X * Y * Z + 2 * E2 * P + 4 * P * P * P;
  184. T E4 = (2 * X * Y * Z + E2 * P + 3 * P * P * P) * P;
  185. T E5 = X * Y * Z * P * P;
  186. T result = fmn * pow(An, T(-3) / 2) *
  187. (1 - 3 * E2 / 14 + E3 / 6 + 9 * E2 * E2 / 88 - 3 * E4 / 22 - 9 * E2 * E3 / 52 + 3 * E5 / 26 - E2 * E2 * E2 / 16
  188. + 3 * E3 * E3 / 40 + 3 * E2 * E4 / 20 + 45 * E2 * E2 * E3 / 272 - 9 * (E3 * E4 + E2 * E5) / 68);
  189. result += 6 * RC_sum;
  190. return result;
  191. }
  192. template <typename T, typename Policy>
  193. BOOST_MATH_GPU_ENABLED T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
  194. {
  195. BOOST_MATH_STD_USING
  196. constexpr auto function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
  197. if(x < 0)
  198. {
  199. return policies::raise_domain_error<T>(function, "Argument x must be non-negative, but got x = %1%", x, pol);
  200. }
  201. if(y < 0)
  202. {
  203. return policies::raise_domain_error<T>(function, "Argument y must be non-negative, but got y = %1%", y, pol);
  204. }
  205. if(z < 0)
  206. {
  207. return policies::raise_domain_error<T>(function, "Argument z must be non-negative, but got z = %1%", z, pol);
  208. }
  209. if(p == 0)
  210. {
  211. return policies::raise_domain_error<T>(function, "Argument p must not be zero, but got p = %1%", p, pol);
  212. }
  213. if(x + y == 0 || y + z == 0 || z + x == 0)
  214. {
  215. return policies::raise_domain_error<T>(function, "At most one argument can be zero, only possible result is %1%.", boost::math::numeric_limits<T>::quiet_NaN(), pol);
  216. }
  217. // for p < 0, the integral is singular, return Cauchy principal value
  218. if(p < 0)
  219. {
  220. //
  221. // We must ensure that x < y < z.
  222. // Since the integral is symmetrical in x, y and z
  223. // we can just permute the values:
  224. //
  225. if(x > y)
  226. BOOST_MATH_GPU_SAFE_SWAP(x, y);
  227. if(y > z)
  228. BOOST_MATH_GPU_SAFE_SWAP(y, z);
  229. if(x > y)
  230. BOOST_MATH_GPU_SAFE_SWAP(x, y);
  231. BOOST_MATH_ASSERT(x <= y);
  232. BOOST_MATH_ASSERT(y <= z);
  233. T q = -p;
  234. p = (z * (x + y + q) - x * y) / (z + q);
  235. BOOST_MATH_ASSERT(p >= 0);
  236. T value = (p - z) * ellint_rj_imp_final(x, y, z, p, pol);
  237. value -= 3 * ellint_rf_imp(x, y, z, pol);
  238. value += 3 * sqrt((x * y * z) / (x * y + p * q)) * ellint_rc_imp(T(x * y + p * q), T(p * q), pol);
  239. value /= (z + q);
  240. return value;
  241. }
  242. return ellint_rj_imp_final(x, y, z, p, pol);
  243. }
  244. } // namespace detail
  245. template <class T1, class T2, class T3, class T4, class Policy>
  246. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3, T4>::type
  247. ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
  248. {
  249. typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
  250. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  251. return policies::checked_narrowing_cast<result_type, Policy>(
  252. detail::ellint_rj_imp(
  253. static_cast<value_type>(x),
  254. static_cast<value_type>(y),
  255. static_cast<value_type>(z),
  256. static_cast<value_type>(p),
  257. pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
  258. }
  259. template <class T1, class T2, class T3, class T4>
  260. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3, T4>::type
  261. ellint_rj(T1 x, T2 y, T3 z, T4 p)
  262. {
  263. return ellint_rj(x, y, z, p, policies::policy<>());
  264. }
  265. }} // namespaces
  266. #endif // BOOST_MATH_ELLINT_RJ_HPP