]> git.donarmstrong.com Git - rsem.git/blob - boost/math/special_functions/ellint_rf.hpp
Updated boost to v1.55.0
[rsem.git] / boost / math / special_functions / ellint_rf.hpp
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
15 #ifdef _MSC_VER
16 #pragma once
17 #endif
18
19 #include <boost/math/special_functions/math_fwd.hpp>
20 #include <boost/math/tools/config.hpp>
21
22 #include <boost/math/policies/error_handling.hpp>
23
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)
27
28 namespace boost { namespace math { namespace detail{
29
30 template <typename T, typename Policy>
31 T ellint_rf_imp(T x, T y, T z, const Policy& pol)
32 {
33     T value, X, Y, Z, E2, E3, u, lambda, tolerance;
34     unsigned long k;
35
36     BOOST_MATH_STD_USING
37     using namespace boost::math::tools;
38
39     static const char* function = "boost::math::ellint_rf<%1%>(%1%,%1%,%1%)";
40
41     if (x < 0 || y < 0 || z < 0)
42     {
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);
47     }
48     if (x + y == 0 || y + z == 0 || z + x == 0)
49     {
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);
54     }
55
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)
60     {
61       tolerance = pow(tools::epsilon<T>(), T(1)/4.25f);
62       BOOST_MATH_INSTRUMENT_VARIABLE(tolerance);
63     }
64     else
65     {
66       tolerance = pow(4*tools::epsilon<T>(), T(1)/6);
67       BOOST_MATH_INSTRUMENT_VARIABLE(tolerance);
68     }
69
70     // duplication
71     k = 1;
72     do
73     {
74         u = (x + y + z) / 3;
75         X = (u - x) / u;
76         Y = (u - y) / u;
77         Z = (u - z) / u;
78
79         // Termination condition: 
80         if ((tools::max)(abs(X), abs(Y), abs(Z)) < tolerance) 
81            break; 
82
83         T sx = sqrt(x);
84         T sy = sqrt(y);
85         T sz = sqrt(z);
86         lambda = sy * (sx + sz) + sz * sx;
87         x = (x + lambda) / 4;
88         y = (y + lambda) / 4;
89         z = (z + lambda) / 4;
90         ++k;
91     }
92     while(k < policies::get_max_series_iterations<Policy>());
93
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);
97
98     // Taylor series expansion to the 5th order
99     E2 = X * Y - Z * Z;
100     E3 = X * Y * Z;
101     value = (1 + E2*(E2/24 - E3*T(3)/44 - T(0.1)) + E3/14) / sqrt(u);
102     BOOST_MATH_INSTRUMENT_VARIABLE(value);
103
104     return value;
105 }
106
107 } // namespace detail
108
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)
112 {
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%)");
120 }
121
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)
125 {
126    return ellint_rf(x, y, z, policies::policy<>());
127 }
128
129 }} // namespaces
130
131 #endif // BOOST_MATH_ELLINT_RF_HPP
132