ellint_rf.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2006 Xiaogang Zhang, 2015 John Maddock
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // History:
  7. // XZ wrote the original of this file as part of the Google
  8. // Summer of Code 2006. JM modified it to fit into the
  9. // Boost.Math conceptual framework better, and to handle
  10. // types longer than 80-bit reals.
  11. // Updated 2015 to use Carlson's latest methods.
  12. //
  13. #ifndef BOOST_MATH_ELLINT_RF_HPP
  14. #define BOOST_MATH_ELLINT_RF_HPP
  15. #ifdef _MSC_VER
  16. #pragma once
  17. #endif
  18. #include <boost/math/special_functions/math_fwd.hpp>
  19. #include <boost/math/tools/config.hpp>
  20. #include <boost/math/constants/constants.hpp>
  21. #include <boost/math/policies/error_handling.hpp>
  22. #include <boost/math/special_functions/ellint_rc.hpp>
  23. // Carlson's elliptic integral of the first kind
  24. // R_F(x, y, z) = 0.5 * \int_{0}^{\infty} [(t+x)(t+y)(t+z)]^{-1/2} dt
  25. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  26. namespace boost { namespace math { namespace detail{
  27. template <typename T, typename Policy>
  28. T ellint_rf_imp(T x, T y, T z, const Policy& pol)
  29. {
  30. BOOST_MATH_STD_USING
  31. using namespace boost::math;
  32. using std::swap;
  33. static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
  34. if(x < 0 || y < 0 || z < 0)
  35. {
  36. return policies::raise_domain_error<T>(function,
  37. "domain error, all arguments must be non-negative, "
  38. "only sensible result is %1%.",
  39. std::numeric_limits<T>::quiet_NaN(), pol);
  40. }
  41. if(x + y == 0 || y + z == 0 || z + x == 0)
  42. {
  43. return policies::raise_domain_error<T>(function,
  44. "domain error, at most one argument can be zero, "
  45. "only sensible result is %1%.",
  46. std::numeric_limits<T>::quiet_NaN(), pol);
  47. }
  48. //
  49. // Special cases from http://dlmf.nist.gov/19.20#i
  50. //
  51. if(x == y)
  52. {
  53. if(x == z)
  54. {
  55. // x, y, z equal:
  56. return 1 / sqrt(x);
  57. }
  58. else
  59. {
  60. // 2 equal, x and y:
  61. if(z == 0)
  62. return constants::pi<T>() / (2 * sqrt(x));
  63. else
  64. return ellint_rc_imp(z, x, pol);
  65. }
  66. }
  67. if(x == z)
  68. {
  69. if(y == 0)
  70. return constants::pi<T>() / (2 * sqrt(x));
  71. else
  72. return ellint_rc_imp(y, x, pol);
  73. }
  74. if(y == z)
  75. {
  76. if(x == 0)
  77. return constants::pi<T>() / (2 * sqrt(y));
  78. else
  79. return ellint_rc_imp(x, y, pol);
  80. }
  81. if(x == 0)
  82. swap(x, z);
  83. else if(y == 0)
  84. swap(y, z);
  85. if(z == 0)
  86. {
  87. //
  88. // Special case for one value zero:
  89. //
  90. T xn = sqrt(x);
  91. T yn = sqrt(y);
  92. while(fabs(xn - yn) >= 2.7 * tools::root_epsilon<T>() * fabs(xn))
  93. {
  94. T t = sqrt(xn * yn);
  95. xn = (xn + yn) / 2;
  96. yn = t;
  97. }
  98. return constants::pi<T>() / (xn + yn);
  99. }
  100. T xn = x;
  101. T yn = y;
  102. T zn = z;
  103. T An = (x + y + z) / 3;
  104. T A0 = An;
  105. T Q = pow(3 * boost::math::tools::epsilon<T>(), T(-1) / 8) * (std::max)((std::max)(fabs(An - xn), fabs(An - yn)), fabs(An - zn));
  106. T fn = 1;
  107. // duplication
  108. unsigned k = 1;
  109. for(; k < boost::math::policies::get_max_series_iterations<Policy>(); ++k)
  110. {
  111. T root_x = sqrt(xn);
  112. T root_y = sqrt(yn);
  113. T root_z = sqrt(zn);
  114. T lambda = root_x * root_y + root_x * root_z + root_y * root_z;
  115. An = (An + lambda) / 4;
  116. xn = (xn + lambda) / 4;
  117. yn = (yn + lambda) / 4;
  118. zn = (zn + lambda) / 4;
  119. Q /= 4;
  120. fn *= 4;
  121. if(Q < fabs(An))
  122. break;
  123. }
  124. // Check to see if we gave up too soon:
  125. policies::check_series_iterations<T>(function, k, pol);
  126. BOOST_MATH_INSTRUMENT_VARIABLE(k);
  127. T X = (A0 - x) / (An * fn);
  128. T Y = (A0 - y) / (An * fn);
  129. T Z = -X - Y;
  130. // Taylor series expansion to the 7th order
  131. T E2 = X * Y - Z * Z;
  132. T E3 = X * Y * Z;
  133. return (1 + E3 * (T(1) / 14 + 3 * E3 / 104) + E2 * (T(-1) / 10 + E2 / 24 - (3 * E3) / 44 - 5 * E2 * E2 / 208 + E2 * E3 / 16)) / sqrt(An);
  134. }
  135. } // namespace detail
  136. template <class T1, class T2, class T3, class Policy>
  137. inline typename tools::promote_args<T1, T2, T3>::type
  138. ellint_rf(T1 x, T2 y, T3 z, const Policy& pol)
  139. {
  140. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  141. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  142. return policies::checked_narrowing_cast<result_type, Policy>(
  143. detail::ellint_rf_imp(
  144. static_cast<value_type>(x),
  145. static_cast<value_type>(y),
  146. static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
  147. }
  148. template <class T1, class T2, class T3>
  149. inline typename tools::promote_args<T1, T2, T3>::type
  150. ellint_rf(T1 x, T2 y, T3 z)
  151. {
  152. return ellint_rf(x, y, z, policies::policy<>());
  153. }
  154. }} // namespaces
  155. #endif // BOOST_MATH_ELLINT_RF_HPP