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)
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.
12 #ifndef BOOST_MATH_ELLINT_RF_HPP
13 #define BOOST_MATH_ELLINT_RF_HPP
19 #include <boost/math/special_functions/math_fwd.hpp>
20 #include <boost/math/tools/config.hpp>
22 #include <boost/math/policies/error_handling.hpp>
24 // Carlson's elliptic integral of the first kind
25 // R_F(x, y, z) = 0.5 * \int_{0}^{\infty} [(t+x)(t+y)(t+z)]^{-1/2} dt
26 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
28 namespace boost { namespace math { namespace detail{
30 template <typename T, typename Policy>
31 T ellint_rf_imp(T x, T y, T z, const Policy& pol)
33 T value, X, Y, Z, E2, E3, u, lambda, tolerance;
37 using namespace boost::math::tools;
39 static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
41 if (x < 0 || y < 0 || z < 0)
43 return policies::raise_domain_error<T>(function,
44 "domain error, all arguments must be non-negative, "
45 "only sensible result is %1%.",
46 std::numeric_limits<T>::quiet_NaN(), pol);
48 if (x + y == 0 || y + z == 0 || z + x == 0)
50 return policies::raise_domain_error<T>(function,
51 "domain error, at most one argument can be zero, "
52 "only sensible result is %1%.",
53 std::numeric_limits<T>::quiet_NaN(), pol);
56 // Carlson scales error as the 6th power of tolerance,
57 // but this seems not to work for types larger than
58 // 80-bit reals, this heuristic seems to work OK:
59 if(policies::digits<T, Policy>() > 64)
61 tolerance = pow(tools::epsilon<T>(), T(1)/4.25f);
62 BOOST_MATH_INSTRUMENT_VARIABLE(tolerance);
66 tolerance = pow(4*tools::epsilon<T>(), T(1)/6);
67 BOOST_MATH_INSTRUMENT_VARIABLE(tolerance);
79 // Termination condition:
80 if ((tools::max)(abs(X), abs(Y), abs(Z)) < tolerance)
86 lambda = sy * (sx + sz) + sz * sx;
92 while(k < policies::get_max_series_iterations<Policy>());
94 // Check to see if we gave up too soon:
95 policies::check_series_iterations<T>(function, k, pol);
96 BOOST_MATH_INSTRUMENT_VARIABLE(k);
98 // Taylor series expansion to the 5th order
101 value = (1 + E2*(E2/24 - E3*T(3)/44 - T(0.1)) + E3/14) / sqrt(u);
102 BOOST_MATH_INSTRUMENT_VARIABLE(value);
107 } // namespace detail
109 template <class T1, class T2, class T3, class Policy>
110 inline typename tools::promote_args<T1, T2, T3>::type
111 ellint_rf(T1 x, T2 y, T3 z, const Policy& pol)
113 typedef typename tools::promote_args<T1, T2, T3>::type result_type;
114 typedef typename policies::evaluation<result_type, Policy>::type value_type;
115 return policies::checked_narrowing_cast<result_type, Policy>(
116 detail::ellint_rf_imp(
117 static_cast<value_type>(x),
118 static_cast<value_type>(y),
119 static_cast<value_type>(z), pol), "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)");
122 template <class T1, class T2, class T3>
123 inline typename tools::promote_args<T1, T2, T3>::type
124 ellint_rf(T1 x, T2 y, T3 z)
126 return ellint_rf(x, y, z, policies::policy<>());
131 #endif // BOOST_MATH_ELLINT_RF_HPP