ellint_3.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. T sphi = sin(fabs(phi));
  43. T result = 0;
  44. if (k * k * sphi * sphi > 1)
  45. {
  46. return policies::raise_domain_error<T>(function, "Got k = %1%, function requires |k| <= 1", k, pol);
  47. }
  48. // Special cases first:
  49. if(v == 0)
  50. {
  51. // A&S 17.7.18 & 19
  52. return (k == 0) ? phi : ellint_f_imp(phi, k, pol);
  53. }
  54. if((v > 0) && (1 / v < (sphi * sphi)))
  55. {
  56. // Complex result is a domain error:
  57. return policies::raise_domain_error<T>(function, "Got v = %1%, but result is complex for v > 1 / sin^2(phi)", v, pol);
  58. }
  59. if(v == 1)
  60. {
  61. if (k == 0)
  62. return tan(phi);
  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. // Invalid for v > 1, this case is caught above since v > 1 implies 1/v < sin^2(phi)
  95. BOOST_MATH_ASSERT(v <= 1);
  96. //
  97. // Phi is so large that phi%pi is necessarily zero (or garbage),
  98. // just return the second part of the duplication formula:
  99. //
  100. result = 2 * fabs(phi) * ellint_pi_imp(v, k, vc, pol) / constants::pi<T>();
  101. }
  102. else
  103. {
  104. T rphi = boost::math::tools::fmod_workaround(T(fabs(phi)), T(constants::half_pi<T>()));
  105. T m = boost::math::round((fabs(phi) - rphi) / constants::half_pi<T>());
  106. int sign = 1;
  107. if((m != 0) && (k >= 1))
  108. {
  109. return policies::raise_domain_error<T>(function, "Got k=1 and phi=%1% but the result is complex in that domain", phi, pol);
  110. }
  111. if(boost::math::tools::fmod_workaround(m, T(2)) > T(0.5))
  112. {
  113. m += 1;
  114. sign = -1;
  115. rphi = constants::half_pi<T>() - rphi;
  116. }
  117. result = sign * ellint_pi_imp(v, rphi, k, vc, pol);
  118. if((m > 0) && (vc > 0))
  119. result += m * ellint_pi_imp(v, k, vc, pol);
  120. }
  121. return phi < 0 ? T(-result) : result;
  122. }
  123. if(k == 0)
  124. {
  125. // A&S 17.7.20:
  126. if(v < 1)
  127. {
  128. T vcr = sqrt(vc);
  129. return atan(vcr * tan(phi)) / vcr;
  130. }
  131. else
  132. {
  133. // v > 1:
  134. T vcr = sqrt(-vc);
  135. T arg = vcr * tan(phi);
  136. return (boost::math::log1p(arg, pol) - boost::math::log1p(-arg, pol)) / (2 * vcr);
  137. }
  138. }
  139. if((v < 0) && fabs(k) <= 1)
  140. {
  141. //
  142. // If we don't shift to 0 <= v <= 1 we get
  143. // cancellation errors later on. Use
  144. // A&S 17.7.15/16 to shift to v > 0.
  145. //
  146. // Mathematica simplifies the expressions
  147. // given in A&S as follows (with thanks to
  148. // Rocco Romeo for figuring these out!):
  149. //
  150. // V = (k2 - n)/(1 - n)
  151. // Assuming[(k2 >= 0 && k2 <= 1) && n < 0, FullSimplify[Sqrt[(1 - V)*(1 - k2 / V)] / Sqrt[((1 - n)*(1 - k2 / n))]]]
  152. // Result: ((-1 + k2) n) / ((-1 + n) (-k2 + n))
  153. //
  154. // Assuming[(k2 >= 0 && k2 <= 1) && n < 0, FullSimplify[k2 / (Sqrt[-n*(k2 - n) / (1 - n)] * Sqrt[(1 - n)*(1 - k2 / n)])]]
  155. // Result : k2 / (k2 - n)
  156. //
  157. // Assuming[(k2 >= 0 && k2 <= 1) && n < 0, FullSimplify[Sqrt[1 / ((1 - n)*(1 - k2 / n))]]]
  158. // Result : Sqrt[n / ((k2 - n) (-1 + n))]
  159. //
  160. T k2 = k * k;
  161. T N = (k2 - v) / (1 - v);
  162. T Nm1 = (1 - k2) / (1 - v);
  163. T p2 = -v * N;
  164. T t;
  165. if (p2 <= tools::min_value<T>())
  166. {
  167. p2 = sqrt(-v) * sqrt(N);
  168. }
  169. else
  170. p2 = sqrt(p2);
  171. T delta = sqrt(1 - k2 * sphi * sphi);
  172. if(N > k2)
  173. {
  174. result = ellint_pi_imp(N, phi, k, Nm1, pol);
  175. result *= v / (v - 1);
  176. result *= (k2 - 1) / (v - k2);
  177. }
  178. if(k != 0)
  179. {
  180. t = ellint_f_imp(phi, k, pol);
  181. t *= k2 / (k2 - v);
  182. result += t;
  183. }
  184. t = v / ((k2 - v) * (v - 1));
  185. if(t > tools::min_value<T>())
  186. {
  187. result += atan((p2 / 2) * sin(2 * phi) / delta) * sqrt(t);
  188. }
  189. else
  190. {
  191. result += atan((p2 / 2) * sin(2 * phi) / delta) * sqrt(fabs(1 / (k2 - v))) * sqrt(fabs(v / (v - 1)));
  192. }
  193. return result;
  194. }
  195. if(k == 1)
  196. {
  197. // See http://functions.wolfram.com/08.06.03.0013.01
  198. result = sqrt(v) * atanh(sqrt(v) * sin(phi), pol) - log(1 / cos(phi) + tan(phi));
  199. result /= v - 1;
  200. return result;
  201. }
  202. #if 0 // disabled but retained for future reference: see below.
  203. if(v > 1)
  204. {
  205. //
  206. // If v > 1 we can use the identity in A&S 17.7.7/8
  207. // to shift to 0 <= v <= 1. In contrast to previous
  208. // revisions of this header, this identity does now work
  209. // but appears not to produce better error rates in
  210. // practice. Archived here for future reference...
  211. //
  212. T k2 = k * k;
  213. T N = k2 / v;
  214. T Nm1 = (v - k2) / v;
  215. T p1 = sqrt((-vc) * (1 - k2 / v));
  216. T delta = sqrt(1 - k2 * sphi * sphi);
  217. //
  218. // These next two terms have a large amount of cancellation
  219. // so it's not clear if this relation is useable even if
  220. // the issues with phi > pi/2 can be fixed:
  221. //
  222. result = -ellint_pi_imp(N, phi, k, Nm1, pol);
  223. result += ellint_f_imp(phi, k, pol);
  224. //
  225. // This log term gives the complex result when
  226. // n > 1/sin^2(phi)
  227. // However that case is dealt with as an error above,
  228. // so we should always get a real result here:
  229. //
  230. result += log((delta + p1 * tan(phi)) / (delta - p1 * tan(phi))) / (2 * p1);
  231. return result;
  232. }
  233. #endif
  234. //
  235. // Carlson's algorithm works only for |phi| <= pi/2,
  236. // by the time we get here phi should already have been
  237. // normalised above.
  238. //
  239. BOOST_MATH_ASSERT(fabs(phi) < constants::half_pi<T>());
  240. BOOST_MATH_ASSERT(phi >= 0);
  241. T x, y, z, p, t;
  242. T cosp = cos(phi);
  243. x = cosp * cosp;
  244. t = sphi * sphi;
  245. y = 1 - k * k * t;
  246. z = 1;
  247. if(v * t < T(0.5))
  248. p = 1 - v * t;
  249. else
  250. p = x + vc * t;
  251. result = sphi * (ellint_rf_imp(x, y, z, pol) + v * t * ellint_rj_imp(x, y, z, p, pol) / 3);
  252. return result;
  253. }
  254. // Complete elliptic integral (Legendre form) of the third kind
  255. template <typename T, typename Policy>
  256. T ellint_pi_imp(T v, T k, T vc, const Policy& pol)
  257. {
  258. // Note arg vc = 1-v, possibly without cancellation errors
  259. BOOST_MATH_STD_USING
  260. using namespace boost::math::tools;
  261. static const char* function = "boost::math::ellint_pi<%1%>(%1%,%1%)";
  262. if (abs(k) >= 1)
  263. {
  264. return policies::raise_domain_error<T>(function, "Got k = %1%, function requires |k| <= 1", k, pol);
  265. }
  266. if(vc <= 0)
  267. {
  268. // Result is complex:
  269. return policies::raise_domain_error<T>(function, "Got v = %1%, function requires v < 1", v, pol);
  270. }
  271. if(v == 0)
  272. {
  273. return (k == 0) ? boost::math::constants::pi<T>() / 2 : boost::math::ellint_1(k, pol);
  274. }
  275. if(v < 0)
  276. {
  277. // Apply A&S 17.7.17:
  278. T k2 = k * k;
  279. T N = (k2 - v) / (1 - v);
  280. T Nm1 = (1 - k2) / (1 - v);
  281. T result = 0;
  282. result = boost::math::detail::ellint_pi_imp(N, k, Nm1, pol);
  283. // This next part is split in two to avoid spurious over/underflow:
  284. result *= -v / (1 - v);
  285. result *= (1 - k2) / (k2 - v);
  286. result += boost::math::ellint_1(k, pol) * k2 / (k2 - v);
  287. return result;
  288. }
  289. T x = 0;
  290. T y = 1 - k * k;
  291. T z = 1;
  292. T p = vc;
  293. T value = ellint_rf_imp(x, y, z, pol) + v * ellint_rj_imp(x, y, z, p, pol) / 3;
  294. return value;
  295. }
  296. template <class T1, class T2, class T3>
  297. inline typename tools::promote_args<T1, T2, T3>::type ellint_3(T1 k, T2 v, T3 phi, const std::false_type&)
  298. {
  299. return boost::math::ellint_3(k, v, phi, policies::policy<>());
  300. }
  301. template <class T1, class T2, class Policy>
  302. inline typename tools::promote_args<T1, T2>::type ellint_3(T1 k, T2 v, const Policy& pol, const std::true_type&)
  303. {
  304. typedef typename tools::promote_args<T1, T2>::type result_type;
  305. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  306. return policies::checked_narrowing_cast<result_type, Policy>(
  307. detail::ellint_pi_imp(
  308. static_cast<value_type>(v),
  309. static_cast<value_type>(k),
  310. static_cast<value_type>(1-v),
  311. pol), "boost::math::ellint_3<%1%>(%1%,%1%)");
  312. }
  313. } // namespace detail
  314. template <class T1, class T2, class T3, class Policy>
  315. inline typename tools::promote_args<T1, T2, T3>::type ellint_3(T1 k, T2 v, T3 phi, const Policy&)
  316. {
  317. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  318. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  319. typedef typename policies::normalise<Policy, policies::promote_float<false>, policies::promote_double<false> >::type forwarding_policy;
  320. return policies::checked_narrowing_cast<result_type, Policy>(
  321. detail::ellint_pi_imp(
  322. static_cast<value_type>(v),
  323. static_cast<value_type>(phi),
  324. static_cast<value_type>(k),
  325. static_cast<value_type>(1-v),
  326. forwarding_policy()), "boost::math::ellint_3<%1%>(%1%,%1%,%1%)");
  327. }
  328. template <class T1, class T2, class T3>
  329. typename detail::ellint_3_result<T1, T2, T3>::type ellint_3(T1 k, T2 v, T3 phi)
  330. {
  331. typedef typename policies::is_policy<T3>::type tag_type;
  332. return detail::ellint_3(k, v, phi, tag_type());
  333. }
  334. template <class T1, class T2>
  335. inline typename tools::promote_args<T1, T2>::type ellint_3(T1 k, T2 v)
  336. {
  337. return ellint_3(k, v, policies::policy<>());
  338. }
  339. }} // namespaces
  340. #endif // BOOST_MATH_ELLINT_3_HPP