ellint_rf.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2006 Xiaogang Zhang
  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. //
  12. #ifndef BOOST_MATH_ELLINT_RF_HPP
  13. #define BOOST_MATH_ELLINT_RF_HPP
  14. #ifdef _MSC_VER
  15. #pragma once
  16. #endif
  17. #include <boost/math/special_functions/math_fwd.hpp>
  18. #include <boost/math/tools/config.hpp>
  19. #include <boost/math/policies/error_handling.hpp>
  20. // Carlson's elliptic integral of the first kind
  21. // R_F(x, y, z) = 0.5 * \int_{0}^{\infty} [(t+x)(t+y)(t+z)]^{-1/2} dt
  22. // Carlson, Numerische Mathematik, vol 33, 1 (1979)
  23. namespace boost { namespace math { namespace detail{
  24. template <typename T, typename Policy>
  25. T ellint_rf_imp(T x, T y, T z, const Policy& pol)
  26. {
  27. T value, X, Y, Z, E2, E3, u, lambda, tolerance;
  28. unsigned long k;
  29. BOOST_MATH_STD_USING
  30. using namespace boost::math::tools;
  31. static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
  32. if (x < 0 || y < 0 || z < 0)
  33. {
  34. return policies::raise_domain_error<T>(function,
  35. "domain error, all arguments must be non-negative, "
  36. "only sensible result is %1%.",
  37. std::numeric_limits<T>::quiet_NaN(), pol);
  38. }
  39. if (x + y == 0 || y + z == 0 || z + x == 0)
  40. {
  41. return policies::raise_domain_error<T>(function,
  42. "domain error, at most one argument can be zero, "
  43. "only sensible result is %1%.",
  44. std::numeric_limits<T>::quiet_NaN(), pol);
  45. }
  46. // Carlson scales error as the 6th power of tolerance,
  47. // but this seems not to work for types larger than
  48. // 80-bit reals, this heuristic seems to work OK:
  49. if(policies::digits<T, Policy>() > 64)
  50. {
  51. tolerance = pow(tools::epsilon<T>(), T(1)/4.25f);
  52. BOOST_MATH_INSTRUMENT_VARIABLE(tolerance);
  53. }
  54. else
  55. {
  56. tolerance = pow(4*tools::epsilon<T>(), T(1)/6);
  57. BOOST_MATH_INSTRUMENT_VARIABLE(tolerance);
  58. }
  59. // duplication
  60. k = 1;
  61. do
  62. {
  63. u = (x + y + z) / 3;
  64. X = (u - x) / u;
  65. Y = (u - y) / u;
  66. Z = (u - z) / u;
  67. // Termination condition:
  68. if ((tools::max)(abs(X), abs(Y), abs(Z)) < tolerance)
  69. break;
  70. T sx = sqrt(x);
  71. T sy = sqrt(y);
  72. T sz = sqrt(z);
  73. lambda = sy * (sx + sz) + sz * sx;
  74. x = (x + lambda) / 4;
  75. y = (y + lambda) / 4;
  76. z = (z + lambda) / 4;
  77. ++k;
  78. }
  79. while(k < policies::get_max_series_iterations<Policy>());
  80. // Check to see if we gave up too soon:
  81. policies::check_series_iterations<T>(function, k, pol);
  82. BOOST_MATH_INSTRUMENT_VARIABLE(k);
  83. // Taylor series expansion to the 5th order
  84. E2 = X * Y - Z * Z;
  85. E3 = X * Y * Z;
  86. value = (1 + E2*(E2/24 - E3*T(3)/44 - T(0.1)) + E3/14) / sqrt(u);
  87. BOOST_MATH_INSTRUMENT_VARIABLE(value);
  88. return value;
  89. }
  90. } // namespace detail
  91. template <class T1, class T2, class T3, class Policy>
  92. inline typename tools::promote_args<T1, T2, T3>::type
  93. ellint_rf(T1 x, T2 y, T3 z, const Policy& pol)
  94. {
  95. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  96. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  97. return policies::checked_narrowing_cast<result_type, Policy>(
  98. detail::ellint_rf_imp(
  99. static_cast<value_type>(x),
  100. static_cast<value_type>(y),
  101. static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
  102. }
  103. template <class T1, class T2, class T3>
  104. inline typename tools::promote_args<T1, T2, T3>::type
  105. ellint_rf(T1 x, T2 y, T3 z)
  106. {
  107. return ellint_rf(x, y, z, policies::policy<>());
  108. }
  109. }} // namespaces
  110. #endif // BOOST_MATH_ELLINT_RF_HPP