ellint_1.hpp 6.0 KB

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