ellint_rj.hpp 8.4 KB

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