ellint_2.hpp 6.2 KB

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