ellint_d.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2006 Xiaogang Zhang
  2. // Copyright (c) 2006 John Maddock
  3. // Copyright (c) 2024 Matt Borland
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // History:
  9. // XZ wrote the original of this file as part of the Google
  10. // Summer of Code 2006. JM modified it to fit into the
  11. // Boost.Math conceptual framework better, and to ensure
  12. // that the code continues to work no matter how many digits
  13. // type T has.
  14. #ifndef BOOST_MATH_ELLINT_D_HPP
  15. #define BOOST_MATH_ELLINT_D_HPP
  16. #ifdef _MSC_VER
  17. #pragma once
  18. #endif
  19. #include <boost/math/tools/config.hpp>
  20. #include <boost/math/tools/type_traits.hpp>
  21. #include <boost/math/special_functions/math_fwd.hpp>
  22. #include <boost/math/special_functions/ellint_rf.hpp>
  23. #include <boost/math/special_functions/ellint_rd.hpp>
  24. #include <boost/math/special_functions/ellint_rg.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 second kind
  30. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  31. namespace boost { namespace math {
  32. template <class T1, class T2, class Policy>
  33. BOOST_MATH_GPU_ENABLED typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol);
  34. namespace detail{
  35. template <typename T, typename Policy>
  36. BOOST_MATH_GPU_ENABLED T ellint_d_imp(T k, const Policy& pol);
  37. // Elliptic integral (Legendre form) of the second kind
  38. template <typename T, typename Policy>
  39. BOOST_MATH_GPU_ENABLED T ellint_d_imp(T phi, T k, const Policy& pol)
  40. {
  41. BOOST_MATH_STD_USING
  42. using namespace boost::math::tools;
  43. using namespace boost::math::constants;
  44. bool invert = false;
  45. if(phi < 0)
  46. {
  47. phi = fabs(phi);
  48. invert = true;
  49. }
  50. T result;
  51. if(phi >= tools::max_value<T>())
  52. {
  53. // Need to handle infinity as a special case:
  54. result = policies::raise_overflow_error<T>("boost::math::ellint_d<%1%>(%1%,%1%)", nullptr, pol);
  55. }
  56. else if(phi > 1 / tools::epsilon<T>())
  57. {
  58. // Phi is so large that phi%pi is necessarily zero (or garbage),
  59. // just return the second part of the duplication formula:
  60. result = 2 * phi * ellint_d_imp(k, pol) / constants::pi<T>();
  61. }
  62. else
  63. {
  64. // Carlson's algorithm works only for |phi| <= pi/2,
  65. // use the integrand's periodicity to normalize phi
  66. //
  67. T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
  68. T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
  69. int s = 1;
  70. if(boost::math::tools::fmod_workaround(m, T(2)) > T(0.5))
  71. {
  72. m += 1;
  73. s = -1;
  74. rphi = constants::half_pi<T>() - rphi;
  75. }
  76. BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
  77. BOOST_MATH_INSTRUMENT_VARIABLE(m);
  78. T sinp = sin(rphi);
  79. T cosp = cos(rphi);
  80. BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
  81. BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
  82. T c = 1 / (sinp * sinp);
  83. T cm1 = cosp * cosp / (sinp * sinp); // c - 1
  84. T k2 = k * k;
  85. if(k2 * sinp * sinp > 1)
  86. {
  87. 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);
  88. }
  89. else if(rphi == 0)
  90. {
  91. result = 0;
  92. }
  93. else
  94. {
  95. // http://dlmf.nist.gov/19.25#E10
  96. result = s * ellint_rd_imp(cm1, T(c - k2), c, pol) / 3;
  97. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  98. }
  99. if(m != 0)
  100. result += m * ellint_d_imp(k, pol);
  101. }
  102. return invert ? T(-result) : result;
  103. }
  104. // Complete elliptic integral (Legendre form) of the second kind
  105. template <typename T, typename Policy>
  106. BOOST_MATH_GPU_ENABLED T ellint_d_imp(T k, const Policy& pol)
  107. {
  108. BOOST_MATH_STD_USING
  109. using namespace boost::math::tools;
  110. if (abs(k) >= 1)
  111. {
  112. return policies::raise_domain_error<T>("boost::math::ellint_d<%1%>(%1%)", "Got k = %1%, function requires |k| <= 1", k, pol);
  113. }
  114. if(fabs(k) <= tools::root_epsilon<T>())
  115. return constants::pi<T>() / 4;
  116. T x = 0;
  117. T t = k * k;
  118. T y = 1 - t;
  119. T z = 1;
  120. T value = ellint_rd_imp(x, y, z, pol) / 3;
  121. return value;
  122. }
  123. template <typename T, typename Policy>
  124. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type ellint_d(T k, const Policy& pol, const boost::math::true_type&)
  125. {
  126. typedef typename tools::promote_args<T>::type result_type;
  127. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  128. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_d_imp(static_cast<value_type>(k), pol), "boost::math::ellint_d<%1%>(%1%)");
  129. }
  130. // Elliptic integral (Legendre form) of the second kind
  131. template <class T1, class T2>
  132. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const boost::math::false_type&)
  133. {
  134. return boost::math::ellint_d(k, phi, policies::policy<>());
  135. }
  136. } // detail
  137. // Complete elliptic integral (Legendre form) of the second kind
  138. template <typename T>
  139. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type ellint_d(T k)
  140. {
  141. return ellint_d(k, policies::policy<>());
  142. }
  143. // Elliptic integral (Legendre form) of the second kind
  144. template <class T1, class T2>
  145. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi)
  146. {
  147. typedef typename policies::is_policy<T2>::type tag_type;
  148. return detail::ellint_d(k, phi, tag_type());
  149. }
  150. template <class T1, class T2, class Policy>
  151. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type ellint_d(T1 k, T2 phi, const Policy& pol)
  152. {
  153. typedef typename tools::promote_args<T1, T2>::type result_type;
  154. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  155. 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%)");
  156. }
  157. }} // namespaces
  158. #endif // BOOST_MATH_ELLINT_D_HPP