ellint_2.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. {
  44. phi = fabs(phi);
  45. invert = true;
  46. }
  47. T result;
  48. if(phi >= tools::max_value<T>())
  49. {
  50. // Need to handle infinity as a special case:
  51. result = policies::raise_overflow_error<T>("boost::math::ellint_e<%1%>(%1%,%1%)", 0, pol);
  52. }
  53. else if(phi > 1 / tools::epsilon<T>())
  54. {
  55. // Phi is so large that phi%pi is necessarily zero (or garbage),
  56. // just return the second part of the duplication formula:
  57. result = 2 * phi * ellint_e_imp(k, pol) / constants::pi<T>();
  58. }
  59. else if(k == 0)
  60. {
  61. return invert ? T(-phi) : phi;
  62. }
  63. else if(fabs(k) == 1)
  64. {
  65. return invert ? T(-sin(phi)) : T(sin(phi));
  66. }
  67. else
  68. {
  69. // Carlson's algorithm works only for |phi| <= pi/2,
  70. // use the integrand's periodicity to normalize phi
  71. //
  72. // Xiaogang's original code used a cast to long long here
  73. // but that fails if T has more digits than a long long,
  74. // so rewritten to use fmod instead:
  75. //
  76. T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
  77. T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
  78. int s = 1;
  79. if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
  80. {
  81. m += 1;
  82. s = -1;
  83. rphi = constants::half_pi<T>() - rphi;
  84. }
  85. T k2 = k * k;
  86. if(k2 > 1)
  87. {
  88. 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);
  89. }
  90. else if(rphi < tools::root_epsilon<T>())
  91. {
  92. // See http://functions.wolfram.com/EllipticIntegrals/EllipticE2/06/01/03/0001/
  93. result = s * rphi;
  94. }
  95. else
  96. {
  97. // http://dlmf.nist.gov/19.25#E10
  98. T sinp = sin(rphi);
  99. T cosp = cos(rphi);
  100. T c = 1 / (sinp * sinp);
  101. T cm1 = cosp * cosp / (sinp * sinp); // c - 1
  102. 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))));
  103. }
  104. if(m != 0)
  105. result += m * ellint_e_imp(k, pol);
  106. }
  107. return invert ? T(-result) : result;
  108. }
  109. // Complete elliptic integral (Legendre form) of the second kind
  110. template <typename T, typename Policy>
  111. T ellint_e_imp(T k, const Policy& pol)
  112. {
  113. BOOST_MATH_STD_USING
  114. using namespace boost::math::tools;
  115. if (abs(k) > 1)
  116. {
  117. return policies::raise_domain_error<T>("boost::math::ellint_e<%1%>(%1%)",
  118. "Got k = %1%, function requires |k| <= 1", k, pol);
  119. }
  120. if (abs(k) == 1)
  121. {
  122. return static_cast<T>(1);
  123. }
  124. T x = 0;
  125. T t = k * k;
  126. T y = 1 - t;
  127. T z = 1;
  128. T value = 2 * ellint_rg_imp(x, y, z, pol);
  129. return value;
  130. }
  131. template <typename T, typename Policy>
  132. inline typename tools::promote_args<T>::type ellint_2(T k, const Policy& pol, const mpl::true_&)
  133. {
  134. typedef typename tools::promote_args<T>::type result_type;
  135. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  136. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_e_imp(static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%)");
  137. }
  138. // Elliptic integral (Legendre form) of the second kind
  139. template <class T1, class T2>
  140. inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const mpl::false_&)
  141. {
  142. return boost::math::ellint_2(k, phi, policies::policy<>());
  143. }
  144. } // detail
  145. // Complete elliptic integral (Legendre form) of the second kind
  146. template <typename T>
  147. inline typename tools::promote_args<T>::type ellint_2(T k)
  148. {
  149. return ellint_2(k, policies::policy<>());
  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)
  154. {
  155. typedef typename policies::is_policy<T2>::type tag_type;
  156. return detail::ellint_2(k, phi, tag_type());
  157. }
  158. template <class T1, class T2, class Policy>
  159. inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const Policy& pol)
  160. {
  161. typedef typename tools::promote_args<T1, T2>::type result_type;
  162. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  163. 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%)");
  164. }
  165. }} // namespaces
  166. #endif // BOOST_MATH_ELLINT_2_HPP