ellint_1.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. if (abs(k) > 1)
  44. {
  45. return policies::raise_domain_error<T>(function,
  46. "Got k = %1%, function requires |k| <= 1", k, pol);
  47. }
  48. bool invert = false;
  49. if(phi < 0)
  50. {
  51. BOOST_MATH_INSTRUMENT_VARIABLE(phi);
  52. phi = fabs(phi);
  53. invert = true;
  54. }
  55. T result;
  56. if(phi >= tools::max_value<T>())
  57. {
  58. // Need to handle infinity as a special case:
  59. result = policies::raise_overflow_error<T>(function, 0, pol);
  60. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  61. }
  62. else if(phi > 1 / tools::epsilon<T>())
  63. {
  64. // Phi is so large that phi%pi is necessarily zero (or garbage),
  65. // just return the second part of the duplication formula:
  66. result = 2 * phi * ellint_k_imp(k, pol) / constants::pi<T>();
  67. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  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. BOOST_MATH_INSTRUMENT_CODE("pi/2 = " << constants::pi<T>() / 2);
  79. T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
  80. BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
  81. T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
  82. BOOST_MATH_INSTRUMENT_VARIABLE(m);
  83. int s = 1;
  84. if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
  85. {
  86. m += 1;
  87. s = -1;
  88. rphi = constants::half_pi<T>() - rphi;
  89. BOOST_MATH_INSTRUMENT_VARIABLE(rphi);
  90. }
  91. T sinp = sin(rphi);
  92. sinp *= sinp;
  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. //
  100. // Use http://dlmf.nist.gov/19.25#E5, note that
  101. // c-1 simplifies to cot^2(rphi) which avoid cancellation:
  102. //
  103. T c = 1 / sinp;
  104. result = rphi == 0 ? static_cast<T>(0) : static_cast<T>(s * ellint_rf_imp(T(cosp / sinp), T(c - k * k), c, pol));
  105. }
  106. else
  107. result = s * sin(rphi);
  108. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  109. if(m != 0)
  110. {
  111. result += m * ellint_k_imp(k, pol);
  112. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  113. }
  114. }
  115. return invert ? T(-result) : result;
  116. }
  117. // Complete elliptic integral (Legendre form) of the first kind
  118. template <typename T, typename Policy>
  119. T ellint_k_imp(T k, const Policy& pol)
  120. {
  121. BOOST_MATH_STD_USING
  122. using namespace boost::math::tools;
  123. static const char* function = "boost::math::ellint_k<%1%>(%1%)";
  124. if (abs(k) > 1)
  125. {
  126. return policies::raise_domain_error<T>(function,
  127. "Got k = %1%, function requires |k| <= 1", k, pol);
  128. }
  129. if (abs(k) == 1)
  130. {
  131. return policies::raise_overflow_error<T>(function, 0, pol);
  132. }
  133. T x = 0;
  134. T y = 1 - k * k;
  135. T z = 1;
  136. T value = ellint_rf_imp(x, y, z, pol);
  137. return value;
  138. }
  139. template <typename T, typename Policy>
  140. inline typename tools::promote_args<T>::type ellint_1(T k, const Policy& pol, const mpl::true_&)
  141. {
  142. typedef typename tools::promote_args<T>::type result_type;
  143. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  144. return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_k_imp(static_cast<value_type>(k), pol), "boost::math::ellint_1<%1%>(%1%)");
  145. }
  146. template <class T1, class T2>
  147. inline typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const mpl::false_&)
  148. {
  149. return boost::math::ellint_1(k, phi, policies::policy<>());
  150. }
  151. }
  152. // Complete elliptic integral (Legendre form) of the first kind
  153. template <typename T>
  154. inline typename tools::promote_args<T>::type ellint_1(T k)
  155. {
  156. return ellint_1(k, policies::policy<>());
  157. }
  158. // Elliptic integral (Legendre form) of the first kind
  159. template <class T1, class T2, class Policy>
  160. inline typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi, const Policy& pol)
  161. {
  162. typedef typename tools::promote_args<T1, T2>::type result_type;
  163. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  164. 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%)");
  165. }
  166. template <class T1, class T2>
  167. inline typename tools::promote_args<T1, T2>::type ellint_1(T1 k, T2 phi)
  168. {
  169. typedef typename policies::is_policy<T2>::type tag_type;
  170. return detail::ellint_1(k, phi, tag_type());
  171. }
  172. }} // namespaces
  173. #endif // BOOST_MATH_ELLINT_1_HPP