ellint_2.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 ensure
  11. // that the code continues to work no matter how many digits
  12. // type T has.
  13. #ifndef BOOST_MATH_ELLINT_2_HPP
  14. #define BOOST_MATH_ELLINT_2_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_rd.hpp>
  21. #include <boost/math/special_functions/ellint_rg.hpp>
  22. #include <boost/math/constants/constants.hpp>
  23. #include <boost/math/policies/error_handling.hpp>
  24. #include <boost/math/tools/workaround.hpp>
  25. #include <boost/math/special_functions/round.hpp>
  26. // Elliptic integrals (complete and incomplete) of the second kind
  27. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  28. namespace boost { namespace math {
  29. template <class T1, class T2, class Policy>
  30. typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const Policy& pol);
  31. namespace detail{
  32. template <typename T, typename Policy>
  33. T ellint_e_imp(T k, const Policy& pol);
  34. // Elliptic integral (Legendre form) of the second kind
  35. template <typename T, typename Policy>
  36. T ellint_e_imp(T phi, T k, const Policy& pol)
  37. {
  38. BOOST_MATH_STD_USING
  39. using namespace boost::math::tools;
  40. using namespace boost::math::constants;
  41. bool invert = false;
  42. if (phi == 0)
  43. return 0;
  44. if(phi < 0)
  45. {
  46. phi = fabs(phi);
  47. invert = true;
  48. }
  49. T result;
  50. if(phi >= tools::max_value<T>())
  51. {
  52. // Need to handle infinity as a special case:
  53. result = policies::raise_overflow_error<T>("boost::math::ellint_e<%1%>(%1%,%1%)", 0, pol);
  54. }
  55. else if(phi > 1 / tools::epsilon<T>())
  56. {
  57. // Phi is so large that phi%pi is necessarily zero (or garbage),
  58. // just return the second part of the duplication formula:
  59. result = 2 * phi * ellint_e_imp(k, pol) / constants::pi<T>();
  60. }
  61. else if(k == 0)
  62. {
  63. return invert ? T(-phi) : phi;
  64. }
  65. else if(fabs(k) == 1)
  66. {
  67. //
  68. // For k = 1 ellipse actually turns to a line and every pi/2 in phi is exactly 1 in arc length
  69. // Periodicity though is in pi, curve follows sin(pi) for 0 <= phi <= pi/2 and then
  70. // 2 - sin(pi- phi) = 2 + sin(phi - pi) for pi/2 <= phi <= pi, so general form is:
  71. //
  72. // 2n + sin(phi - n * pi) ; |phi - n * pi| <= pi / 2
  73. //
  74. T m = boost::math::round(phi / boost::math::constants::pi<T>());
  75. T remains = phi - m * boost::math::constants::pi<T>();
  76. T value = 2 * m + sin(remains);
  77. // negative arc length for negative phi
  78. return invert ? -value : value;
  79. }
  80. else
  81. {
  82. // Carlson's algorithm works only for |phi| <= pi/2,
  83. // use the integrand's periodicity to normalize phi
  84. //
  85. // Xiaogang's original code used a cast to long long here
  86. // but that fails if T has more digits than a long long,
  87. // so rewritten to use fmod instead:
  88. //
  89. T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
  90. T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
  91. int s = 1;
  92. if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
  93. {
  94. m += 1;
  95. s = -1;
  96. rphi = constants::half_pi<T>() - rphi;
  97. }
  98. T k2 = k * k;
  99. if(boost::math::pow<3>(rphi) * k2 / 6 < tools::epsilon<T>() * fabs(rphi))
  100. {
  101. // See http://functions.wolfram.com/EllipticIntegrals/EllipticE2/06/01/03/0001/
  102. result = s * rphi;
  103. }
  104. else
  105. {
  106. // http://dlmf.nist.gov/19.25#E10
  107. T sinp = sin(rphi);
  108. if (k2 * sinp * sinp >= 1)
  109. {
  110. return policies::raise_domain_error<T>("boost::math::ellint_2<%1%>(%1%, %1%)", "The parameter k is out of range, got k = %1%", k, pol);
  111. }
  112. T cosp = cos(rphi);
  113. T c = 1 / (sinp * sinp);
  114. T cm1 = cosp * cosp / (sinp * sinp); // c - 1
  115. result = s * ((1 - k2) * ellint_rf_imp(cm1, T(c - k2), c, pol) + k2 * (1 - k2) * ellint_rd(cm1, c, T(c - k2), pol) / 3 + k2 * sqrt(cm1 / (c * (c - k2))));
  116. }
  117. if(m != 0)
  118. result += m * ellint_e_imp(k, pol);
  119. }
  120. return invert ? T(-result) : result;
  121. }
  122. // Complete elliptic integral (Legendre form) of the second kind
  123. template <typename T, typename Policy>
  124. T ellint_e_imp(T k, const Policy& pol)
  125. {
  126. BOOST_MATH_STD_USING
  127. using namespace boost::math::tools;
  128. if (abs(k) > 1)
  129. {
  130. return policies::raise_domain_error<T>("boost::math::ellint_e<%1%>(%1%)",
  131. "Got k = %1%, function requires |k| <= 1", k, pol);
  132. }
  133. if (abs(k) == 1)
  134. {
  135. return static_cast<T>(1);
  136. }
  137. T x = 0;
  138. T t = k * k;
  139. T y = 1 - t;
  140. T z = 1;
  141. T value = 2 * ellint_rg_imp(x, y, z, pol);
  142. return value;
  143. }
  144. template <typename T, typename Policy>
  145. inline typename tools::promote_args<T>::type ellint_2(T k, const Policy& pol, const std::true_type&)
  146. {
  147. typedef typename tools::promote_args<T>::type result_type;
  148. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  149. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_e_imp(static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%)");
  150. }
  151. // Elliptic integral (Legendre form) of the second kind
  152. template <class T1, class T2>
  153. inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const std::false_type&)
  154. {
  155. return boost::math::ellint_2(k, phi, policies::policy<>());
  156. }
  157. } // detail
  158. // Complete elliptic integral (Legendre form) of the second kind
  159. template <typename T>
  160. inline typename tools::promote_args<T>::type ellint_2(T k)
  161. {
  162. return ellint_2(k, policies::policy<>());
  163. }
  164. // Elliptic integral (Legendre form) of the second kind
  165. template <class T1, class T2>
  166. inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi)
  167. {
  168. typedef typename policies::is_policy<T2>::type tag_type;
  169. return detail::ellint_2(k, phi, tag_type());
  170. }
  171. template <class T1, class T2, class Policy>
  172. inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const Policy& pol)
  173. {
  174. typedef typename tools::promote_args<T1, T2>::type result_type;
  175. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  176. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_e_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%,%1%)");
  177. }
  178. }} // namespaces
  179. #endif // BOOST_MATH_ELLINT_2_HPP