ellint_1.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_1_HPP
  14. #define BOOST_MATH_ELLINT_1_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/constants/constants.hpp>
  21. #include <boost/math/policies/error_handling.hpp>
  22. #include <boost/math/tools/workaround.hpp>
  23. #include <boost/math/special_functions/round.hpp>
  24. // Elliptic integrals (complete and incomplete) of the first kind
  25. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  26. namespace boost { namespace math {
  27. template <class T1, class T2, class Policy>
  28. typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const Policy& pol);
  29. namespace detail{
  30. template <typename T, typename Policy>
  31. T ellint_k_imp(T k, const Policy& pol);
  32. // Elliptic integral (Legendre form) of the first kind
  33. template <typename T, typename Policy>
  34. T ellint_f_imp(T phi, T k, const Policy& pol)
  35. {
  36. BOOST_MATH_STD_USING
  37. using namespace boost::math::tools;
  38. using namespace boost::math::constants;
  39. static const char* function = "boost::math::ellint_f<%1%>(%1%,%1%)";
  40. BOOST_MATH_INSTRUMENT_VARIABLE(phi);
  41. BOOST_MATH_INSTRUMENT_VARIABLE(k);
  42. BOOST_MATH_INSTRUMENT_VARIABLE(function);
  43. bool invert = false;
  44. if(phi < 0)
  45. {
  46. BOOST_MATH_INSTRUMENT_VARIABLE(phi);
  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>(function, 0, pol);
  55. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  56. }
  57. else if(phi > 1 / tools::epsilon<T>())
  58. {
  59. // Phi is so large that phi%pi is necessarily zero (or garbage),
  60. // just return the second part of the duplication formula:
  61. result = 2 * phi * ellint_k_imp(k, pol) / constants::pi<T>();
  62. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  63. }
  64. else
  65. {
  66. // Carlson's algorithm works only for |phi| <= pi/2,
  67. // use the integrand's periodicity to normalize phi
  68. //
  69. // Xiaogang's original code used a cast to long long here
  70. // but that fails if T has more digits than a long long,
  71. // so rewritten to use fmod instead:
  72. //
  73. BOOST_MATH_INSTRUMENT_CODE("pi/2 = " << constants::pi<T>() / 2);
  74. T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
  75. BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
  76. T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
  77. BOOST_MATH_INSTRUMENT_VARIABLE(m);
  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. BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
  85. }
  86. T sinp = sin(rphi);
  87. sinp *= sinp;
  88. if (sinp * k * k >= 1)
  89. {
  90. return policies::raise_domain_error<T>(function,
  91. "Got k^2 * sin^2(phi) = %1%, but the function requires this < 1", sinp * k * k, pol);
  92. }
  93. T cosp = cos(rphi);
  94. cosp *= cosp;
  95. BOOST_MATH_INSTRUMENT_VARIABLE(sinp);
  96. BOOST_MATH_INSTRUMENT_VARIABLE(cosp);
  97. if(sinp > tools::min_value<T>())
  98. {
  99. BOOST_ASSERT(rphi != 0); // precondition, can't be true if sin(rphi) != 0.
  100. //
  101. // Use http://dlmf.nist.gov/19.25#E5, note that
  102. // c-1 simplifies to cot^2(rphi) which avoid cancellation:
  103. //
  104. T c = 1 / sinp;
  105. result = static_cast<T>(s * ellint_rf_imp(T(cosp / sinp), T(c - k * k), c, pol));
  106. }
  107. else
  108. result = s * sin(rphi);
  109. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  110. if(m != 0)
  111. {
  112. result += m * ellint_k_imp(k, pol);
  113. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  114. }
  115. }
  116. return invert ? T(-result) : result;
  117. }
  118. // Complete elliptic integral (Legendre form) of the first kind
  119. template <typename T, typename Policy>
  120. T ellint_k_imp(T k, const Policy& pol)
  121. {
  122. BOOST_MATH_STD_USING
  123. using namespace boost::math::tools;
  124. static const char* function = "boost::math::ellint_k<%1%>(%1%)";
  125. if (abs(k) > 1)
  126. {
  127. return policies::raise_domain_error<T>(function,
  128. "Got k = %1%, function requires |k| <= 1", k, pol);
  129. }
  130. if (abs(k) == 1)
  131. {
  132. return policies::raise_overflow_error<T>(function, 0, pol);
  133. }
  134. T x = 0;
  135. T y = 1 - k * k;
  136. T z = 1;
  137. T value = ellint_rf_imp(x, y, z, pol);
  138. return value;
  139. }
  140. template <typename T, typename Policy>
  141. inline typename tools::promote_args<T>::type ellint_1(T k, const Policy& pol, const std::true_type&)
  142. {
  143. typedef typename tools::promote_args<T>::type result_type;
  144. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  145. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_k_imp(static_cast<value_type>(k), pol), "boost::math::ellint_1<%1%>(%1%)");
  146. }
  147. template <class T1, class T2>
  148. inline typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const std::false_type&)
  149. {
  150. return boost::math::ellint_1(k, phi, policies::policy<>());
  151. }
  152. }
  153. // Complete elliptic integral (Legendre form) of the first kind
  154. template <typename T>
  155. inline typename tools::promote_args<T>::type ellint_1(T k)
  156. {
  157. return ellint_1(k, policies::policy<>());
  158. }
  159. // Elliptic integral (Legendre form) of the first kind
  160. template <class T1, class T2, class Policy>
  161. inline typename tools::promote_args<T1, T2>::type ellint_1(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_f_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_1<%1%>(%1%,%1%)");
  166. }
  167. template <class T1, class T2>
  168. inline typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi)
  169. {
  170. typedef typename policies::is_policy<T2>::type tag_type;
  171. return detail::ellint_1(k, phi, tag_type());
  172. }
  173. }} // namespaces
  174. #endif // BOOST_MATH_ELLINT_1_HPP