ellint_d.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_D_HPP
  14. #define BOOST_MATH_ELLINT_D_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_d(T1 k, T2 phi, const Policy& pol);
  31. namespace detail{
  32. template <typename T, typename Policy>
  33. T ellint_d_imp(T k, const Policy& pol);
  34. // Elliptic integral (Legendre form) of the second kind
  35. template <typename T, typename Policy>
  36. T ellint_d_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_d<%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_d_imp(k, pol) / constants::pi<T>();
  58. }
  59. else
  60. {
  61. // Carlson's algorithm works only for |phi| <= pi/2,
  62. // use the integrand's periodicity to normalize phi
  63. //
  64. T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
  65. T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
  66. int s = 1;
  67. if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
  68. {
  69. m += 1;
  70. s = -1;
  71. rphi = constants::half_pi<T>() - rphi;
  72. }
  73. BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
  74. BOOST_MATH_INSTRUMENT_VARIABLE(m);
  75. T sinp = sin(rphi);
  76. T cosp = cos(rphi);
  77. BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
  78. BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
  79. T c = 1 / (sinp * sinp);
  80. T cm1 = cosp * cosp / (sinp * sinp); // c - 1
  81. T k2 = k * k;
  82. if(k2 * sinp * sinp > 1)
  83. {
  84. return policies::raise_domain_error<T>("boost::math::ellint_d<%1%>(%1%, %1%)", "The parameter k is out of range, got k = %1%", k, pol);
  85. }
  86. else if(rphi == 0)
  87. {
  88. result = 0;
  89. }
  90. else
  91. {
  92. // http://dlmf.nist.gov/19.25#E10
  93. result = s * ellint_rd_imp(cm1, T(c - k2), c, pol) / 3;
  94. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  95. }
  96. if(m != 0)
  97. result += m * ellint_d_imp(k, pol);
  98. }
  99. return invert ? T(-result) : result;
  100. }
  101. // Complete elliptic integral (Legendre form) of the second kind
  102. template <typename T, typename Policy>
  103. T ellint_d_imp(T k, const Policy& pol)
  104. {
  105. BOOST_MATH_STD_USING
  106. using namespace boost::math::tools;
  107. if (abs(k) >= 1)
  108. {
  109. return policies::raise_domain_error<T>("boost::math::ellint_d<%1%>(%1%)",
  110. "Got k = %1%, function requires |k| <= 1", k, pol);
  111. }
  112. if(fabs(k) <= tools::root_epsilon<T>())
  113. return constants::pi<T>() / 4;
  114. T x = 0;
  115. T t = k * k;
  116. T y = 1 - t;
  117. T z = 1;
  118. T value = ellint_rd_imp(x, y, z, pol) / 3;
  119. return value;
  120. }
  121. template <typename T, typename Policy>
  122. inline typename tools::promote_args<T>::type ellint_d(T k, const Policy& pol, const std::true_type&)
  123. {
  124. typedef typename tools::promote_args<T>::type result_type;
  125. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  126. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_d_imp(static_cast<value_type>(k), pol), "boost::math::ellint_d<%1%>(%1%)");
  127. }
  128. // Elliptic integral (Legendre form) of the second kind
  129. template <class T1, class T2>
  130. inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const std::false_type&)
  131. {
  132. return boost::math::ellint_d(k, phi, policies::policy<>());
  133. }
  134. } // detail
  135. // Complete elliptic integral (Legendre form) of the second kind
  136. template <typename T>
  137. inline typename tools::promote_args<T>::type ellint_d(T k)
  138. {
  139. return ellint_d(k, policies::policy<>());
  140. }
  141. // Elliptic integral (Legendre form) of the second kind
  142. template <class T1, class T2>
  143. inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi)
  144. {
  145. typedef typename policies::is_policy<T2>::type tag_type;
  146. return detail::ellint_d(k, phi, tag_type());
  147. }
  148. template <class T1, class T2, class Policy>
  149. inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol)
  150. {
  151. typedef typename tools::promote_args<T1, T2>::type result_type;
  152. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  153. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_d_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%,%1%)");
  154. }
  155. }} // namespaces
  156. #endif // BOOST_MATH_ELLINT_D_HPP