ellint_3.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Copyright (c) 2006 Xiaogang Zhang
  2. // Copyright (c) 2006 John Maddock
  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 various corner cases.
  12. //
  13. #ifndef BOOST_MATH_ELLINT_3_HPP
  14. #define BOOST_MATH_ELLINT_3_HPP
  15. #ifdef _MSC_VER
  16. #pragma once
  17. #endif
  18. #include <boost/math/special_functions/math_fwd.hpp>
  19. #include <boost/math/special_functions/ellint_rf.hpp>
  20. #include <boost/math/special_functions/ellint_rj.hpp>
  21. #include <boost/math/special_functions/ellint_1.hpp>
  22. #include <boost/math/special_functions/ellint_2.hpp>
  23. #include <boost/math/special_functions/log1p.hpp>
  24. #include <boost/math/special_functions/atanh.hpp>
  25. #include <boost/math/constants/constants.hpp>
  26. #include <boost/math/policies/error_handling.hpp>
  27. #include <boost/math/tools/workaround.hpp>
  28. #include <boost/math/special_functions/round.hpp>
  29. // Elliptic integrals (complete and incomplete) of the third kind
  30. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  31. namespace boost { namespace math {
  32. namespace detail{
  33. template <typename T, typename Policy>
  34. T ellint_pi_imp(T v, T k, T vc, const Policy& pol);
  35. // Elliptic integral (Legendre form) of the third kind
  36. template <typename T, typename Policy>
  37. T ellint_pi_imp(T v, T phi, T k, T vc, const Policy& pol)
  38. {
  39. // Note vc = 1-v presumably without cancellation error.
  40. BOOST_MATH_STD_USING
  41. static const char* function = "boost::math::ellint_3<%1%>(%1%,%1%,%1%)";
  42. if(abs(k) > 1)
  43. {
  44. return policies::raise_domain_error<T>(function,
  45. "Got k = %1%, function requires |k| <= 1", k, pol);
  46. }
  47. T sphi = sin(fabs(phi));
  48. T result = 0;
  49. // Special cases first:
  50. if(v == 0)
  51. {
  52. // A&S 17.7.18 & 19
  53. return (k == 0) ? phi : ellint_f_imp(phi, k, pol);
  54. }
  55. if((v > 0) && (1 / v < (sphi * sphi)))
  56. {
  57. // Complex result is a domain error:
  58. return policies::raise_domain_error<T>(function,
  59. "Got v = %1%, but result is complex for v > 1 / sin^2(phi)", v, pol);
  60. }
  61. if(v == 1)
  62. {
  63. // http://functions.wolfram.com/08.06.03.0008.01
  64. T m = k * k;
  65. result = sqrt(1 - m * sphi * sphi) * tan(phi) - ellint_e_imp(phi, k, pol);
  66. result /= 1 - m;
  67. result += ellint_f_imp(phi, k, pol);
  68. return result;
  69. }
  70. if(phi == constants::half_pi<T>())
  71. {
  72. // Have to filter this case out before the next
  73. // special case, otherwise we might get an infinity from
  74. // tan(phi).
  75. // Also note that since we can't represent PI/2 exactly
  76. // in a T, this is a bit of a guess as to the users true
  77. // intent...
  78. //
  79. return ellint_pi_imp(v, k, vc, pol);
  80. }
  81. if((phi > constants::half_pi<T>()) || (phi < 0))
  82. {
  83. // Carlson's algorithm works only for |phi| <= pi/2,
  84. // use the integrand's periodicity to normalize phi
  85. //
  86. // Xiaogang's original code used a cast to long long here
  87. // but that fails if T has more digits than a long long,
  88. // so rewritten to use fmod instead:
  89. //
  90. // See http://functions.wolfram.com/08.06.16.0002.01
  91. //
  92. if(fabs(phi) > 1 / tools::epsilon<T>())
  93. {
  94. if(v > 1)
  95. return policies::raise_domain_error<T>(
  96. function,
  97. "Got v = %1%, but this is only supported for 0 <= phi <= pi/2", v, pol);
  98. //
  99. // Phi is so large that phi%pi is necessarily zero (or garbage),
  100. // just return the second part of the duplication formula:
  101. //
  102. result = 2 * fabs(phi) * ellint_pi_imp(v, k, vc, pol) / constants::pi<T>();
  103. }
  104. else
  105. {
  106. T rphi = boost::math::tools::fmod_workaround(T(fabs(phi)), T(constants::half_pi<T>()));
  107. T m = boost::math::round((fabs(phi) - rphi) / constants::half_pi<T>());
  108. int sign = 1;
  109. if((m != 0) && (k >= 1))
  110. {
  111. return policies::raise_domain_error<T>(function, "Got k=1 and phi=%1% but the result is complex in that domain", phi, pol);
  112. }
  113. if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
  114. {
  115. m += 1;
  116. sign = -1;
  117. rphi = constants::half_pi<T>() - rphi;
  118. }
  119. result = sign * ellint_pi_imp(v, rphi, k, vc, pol);
  120. if((m > 0) && (vc > 0))
  121. result += m * ellint_pi_imp(v, k, vc, pol);
  122. }
  123. return phi < 0 ? T(-result) : result;
  124. }
  125. if(k == 0)
  126. {
  127. // A&S 17.7.20:
  128. if(v < 1)
  129. {
  130. T vcr = sqrt(vc);
  131. return atan(vcr * tan(phi)) / vcr;
  132. }
  133. else if(v == 1)
  134. {
  135. return tan(phi);
  136. }
  137. else
  138. {
  139. // v > 1:
  140. T vcr = sqrt(-vc);
  141. T arg = vcr * tan(phi);
  142. return (boost::math::log1p(arg, pol) - boost::math::log1p(-arg, pol)) / (2 * vcr);
  143. }
  144. }
  145. if(v < 0)
  146. {
  147. //
  148. // If we don't shift to 0 <= v <= 1 we get
  149. // cancellation errors later on. Use
  150. // A&S 17.7.15/16 to shift to v > 0.
  151. //
  152. // Mathematica simplifies the expressions
  153. // given in A&S as follows (with thanks to
  154. // Rocco Romeo for figuring these out!):
  155. //
  156. // V = (k2 - n)/(1 - n)
  157. // Assuming[(k2 >= 0 && k2 <= 1) && n < 0, FullSimplify[Sqrt[(1 - V)*(1 - k2 / V)] / Sqrt[((1 - n)*(1 - k2 / n))]]]
  158. // Result: ((-1 + k2) n) / ((-1 + n) (-k2 + n))
  159. //
  160. // Assuming[(k2 >= 0 && k2 <= 1) && n < 0, FullSimplify[k2 / (Sqrt[-n*(k2 - n) / (1 - n)] * Sqrt[(1 - n)*(1 - k2 / n)])]]
  161. // Result : k2 / (k2 - n)
  162. //
  163. // Assuming[(k2 >= 0 && k2 <= 1) && n < 0, FullSimplify[Sqrt[1 / ((1 - n)*(1 - k2 / n))]]]
  164. // Result : Sqrt[n / ((k2 - n) (-1 + n))]
  165. //
  166. T k2 = k * k;
  167. T N = (k2 - v) / (1 - v);
  168. T Nm1 = (1 - k2) / (1 - v);
  169. T p2 = -v * N;
  170. T t;
  171. if(p2 <= tools::min_value<T>())
  172. p2 = sqrt(-v) * sqrt(N);
  173. else
  174. p2 = sqrt(p2);
  175. T delta = sqrt(1 - k2 * sphi * sphi);
  176. if(N > k2)
  177. {
  178. result = ellint_pi_imp(N, phi, k, Nm1, pol);
  179. result *= v / (v - 1);
  180. result *= (k2 - 1) / (v - k2);
  181. }
  182. if(k != 0)
  183. {
  184. t = ellint_f_imp(phi, k, pol);
  185. t *= k2 / (k2 - v);
  186. result += t;
  187. }
  188. t = v / ((k2 - v) * (v - 1));
  189. if(t > tools::min_value<T>())
  190. {
  191. result += atan((p2 / 2) * sin(2 * phi) / delta) * sqrt(t);
  192. }
  193. else
  194. {
  195. result += atan((p2 / 2) * sin(2 * phi) / delta) * sqrt(fabs(1 / (k2 - v))) * sqrt(fabs(v / (v - 1)));
  196. }
  197. return result;
  198. }
  199. if(k == 1)
  200. {
  201. // See http://functions.wolfram.com/08.06.03.0013.01
  202. result = sqrt(v) * atanh(sqrt(v) * sin(phi)) - log(1 / cos(phi) + tan(phi));
  203. result /= v - 1;
  204. return result;
  205. }
  206. #if 0 // disabled but retained for future reference: see below.
  207. if(v > 1)
  208. {
  209. //
  210. // If v > 1 we can use the identity in A&S 17.7.7/8
  211. // to shift to 0 <= v <= 1. In contrast to previous
  212. // revisions of this header, this identity does now work
  213. // but appears not to produce better error rates in
  214. // practice. Archived here for future reference...
  215. //
  216. T k2 = k * k;
  217. T N = k2 / v;
  218. T Nm1 = (v - k2) / v;
  219. T p1 = sqrt((-vc) * (1 - k2 / v));
  220. T delta = sqrt(1 - k2 * sphi * sphi);
  221. //
  222. // These next two terms have a large amount of cancellation
  223. // so it's not clear if this relation is useable even if
  224. // the issues with phi > pi/2 can be fixed:
  225. //
  226. result = -ellint_pi_imp(N, phi, k, Nm1, pol);
  227. result += ellint_f_imp(phi, k, pol);
  228. //
  229. // This log term gives the complex result when
  230. // n > 1/sin^2(phi)
  231. // However that case is dealt with as an error above,
  232. // so we should always get a real result here:
  233. //
  234. result += log((delta + p1 * tan(phi)) / (delta - p1 * tan(phi))) / (2 * p1);
  235. return result;
  236. }
  237. #endif
  238. //
  239. // Carlson's algorithm works only for |phi| <= pi/2,
  240. // by the time we get here phi should already have been
  241. // normalised above.
  242. //
  243. BOOST_ASSERT(fabs(phi) < constants::half_pi<T>());
  244. BOOST_ASSERT(phi >= 0);
  245. T x, y, z, p, t;
  246. T cosp = cos(phi);
  247. x = cosp * cosp;
  248. t = sphi * sphi;
  249. y = 1 - k * k * t;
  250. z = 1;
  251. if(v * t < 0.5)
  252. p = 1 - v * t;
  253. else
  254. p = x + vc * t;
  255. result = sphi * (ellint_rf_imp(x, y, z, pol) + v * t * ellint_rj_imp(x, y, z, p, pol) / 3);
  256. return result;
  257. }
  258. // Complete elliptic integral (Legendre form) of the third kind
  259. template <typename T, typename Policy>
  260. T ellint_pi_imp(T v, T k, T vc, const Policy& pol)
  261. {
  262. // Note arg vc = 1-v, possibly without cancellation errors
  263. BOOST_MATH_STD_USING
  264. using namespace boost::math::tools;
  265. static const char* function = "boost::math::ellint_pi<%1%>(%1%,%1%)";
  266. if (abs(k) >= 1)
  267. {
  268. return policies::raise_domain_error<T>(function,
  269. "Got k = %1%, function requires |k| <= 1", k, pol);
  270. }
  271. if(vc <= 0)
  272. {
  273. // Result is complex:
  274. return policies::raise_domain_error<T>(function,
  275. "Got v = %1%, function requires v < 1", v, pol);
  276. }
  277. if(v == 0)
  278. {
  279. return (k == 0) ? boost::math::constants::pi<T>() / 2 : ellint_k_imp(k, pol);
  280. }
  281. if(v < 0)
  282. {
  283. // Apply A&S 17.7.17:
  284. T k2 = k * k;
  285. T N = (k2 - v) / (1 - v);
  286. T Nm1 = (1 - k2) / (1 - v);
  287. T result = 0;
  288. result = boost::math::detail::ellint_pi_imp(N, k, Nm1, pol);
  289. // This next part is split in two to avoid spurious over/underflow:
  290. result *= -v / (1 - v);
  291. result *= (1 - k2) / (k2 - v);
  292. result += ellint_k_imp(k, pol) * k2 / (k2 - v);
  293. return result;
  294. }
  295. T x = 0;
  296. T y = 1 - k * k;
  297. T z = 1;
  298. T p = vc;
  299. T value = ellint_rf_imp(x, y, z, pol) + v * ellint_rj_imp(x, y, z, p, pol) / 3;
  300. return value;
  301. }
  302. template <class T1, class T2, class T3>
  303. inline typename tools::promote_args<T1, T2, T3>::type ellint_3(T1 k, T2 v, T3 phi, const mpl::false_&)
  304. {
  305. return boost::math::ellint_3(k, v, phi, policies::policy<>());
  306. }
  307. template <class T1, class T2, class Policy>
  308. inline typename tools::promote_args<T1, T2>::type ellint_3(T1 k, T2 v, const Policy& pol, const mpl::true_&)
  309. {
  310. typedef typename tools::promote_args<T1, T2>::type result_type;
  311. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  312. return policies::checked_narrowing_cast<result_type, Policy>(
  313. detail::ellint_pi_imp(
  314. static_cast<value_type>(v),
  315. static_cast<value_type>(k),
  316. static_cast<value_type>(1-v),
  317. pol), "boost::math::ellint_3<%1%>(%1%,%1%)");
  318. }
  319. } // namespace detail
  320. template <class T1, class T2, class T3, class Policy>
  321. inline typename tools::promote_args<T1, T2, T3>::type ellint_3(T1 k, T2 v, T3 phi, const Policy& pol)
  322. {
  323. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  324. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  325. return policies::checked_narrowing_cast<result_type, Policy>(
  326. detail::ellint_pi_imp(
  327. static_cast<value_type>(v),
  328. static_cast<value_type>(phi),
  329. static_cast<value_type>(k),
  330. static_cast<value_type>(1-v),
  331. pol), "boost::math::ellint_3<%1%>(%1%,%1%,%1%)");
  332. }
  333. template <class T1, class T2, class T3>
  334. typename detail::ellint_3_result<T1, T2, T3>::type ellint_3(T1 k, T2 v, T3 phi)
  335. {
  336. typedef typename policies::is_policy<T3>::type tag_type;
  337. return detail::ellint_3(k, v, phi, tag_type());
  338. }
  339. template <class T1, class T2>
  340. inline typename tools::promote_args<T1, T2>::type ellint_3(T1 k, T2 v)
  341. {
  342. return ellint_3(k, v, policies::policy<>());
  343. }
  344. }} // namespaces
  345. #endif // BOOST_MATH_ELLINT_3_HPP