ellint_rj.hpp 8.6 KB

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