]> git.donarmstrong.com Git - rsem.git/blob - boost/math/special_functions/ellint_rc.hpp
Added error detection for cases such as a read's two mates having different names...
[rsem.git] / boost / math / special_functions / ellint_rc.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 correctly
10 //  handle the y < 0 case.
11 //
12
13 #ifndef BOOST_MATH_ELLINT_RC_HPP
14 #define BOOST_MATH_ELLINT_RC_HPP
15
16 #ifdef _MSC_VER
17 #pragma once
18 #endif
19
20 #include <boost/math/policies/error_handling.hpp>
21 #include <boost/math/tools/config.hpp>
22 #include <boost/math/special_functions/math_fwd.hpp>
23
24 // Carlson's degenerate elliptic integral
25 // R_C(x, y) = R_F(x, y, y) = 0.5 * \int_{0}^{\infty} (t+x)^{-1/2} (t+y)^{-1} 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_rc_imp(T x, T y, const Policy& pol)
32 {
33     T value, S, u, lambda, tolerance, prefix;
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_rc<%1%>(%1%,%1%)";
40
41     if(x < 0)
42     {
43        return policies::raise_domain_error<T>(function,
44             "Argument x must be non-negative but got %1%", x, pol);
45     }
46     if(y == 0)
47     {
48        return policies::raise_domain_error<T>(function,
49             "Argument y must not be zero but got %1%", y, pol);
50     }
51
52     // error scales as the 6th power of tolerance
53     tolerance = pow(4 * tools::epsilon<T>(), T(1) / 6);
54
55     // for y < 0, the integral is singular, return Cauchy principal value
56     if (y < 0)
57     {
58         prefix = sqrt(x / (x - y));
59         x = x - y;
60         y = -y;
61     }
62     else
63        prefix = 1;
64
65     // duplication:
66     k = 1;
67     do
68     {
69         u = (x + y + y) / 3;
70         S = y / u - 1;               // 1 - x / u = 2 * S
71
72         if (2 * abs(S) < tolerance) 
73            break;
74
75         T sx = sqrt(x);
76         T sy = sqrt(y);
77         lambda = 2 * sx * sy + y;
78         x = (x + lambda) / 4;
79         y = (y + lambda) / 4;
80         ++k;
81     }while(k < policies::get_max_series_iterations<Policy>());
82     // Check to see if we gave up too soon:
83     policies::check_series_iterations<T>(function, k, pol);
84
85     // Taylor series expansion to the 5th order
86     value = (1 + S * S * (T(3) / 10 + S * (T(1) / 7 + S * (T(3) / 8 + S * T(9) / 22)))) / sqrt(u);
87
88     return value * prefix;
89 }
90
91 } // namespace detail
92
93 template <class T1, class T2, class Policy>
94 inline typename tools::promote_args<T1, T2>::type 
95    ellint_rc(T1 x, T2 y, const Policy& pol)
96 {
97    typedef typename tools::promote_args<T1, T2>::type result_type;
98    typedef typename policies::evaluation<result_type, Policy>::type value_type;
99    return policies::checked_narrowing_cast<result_type, Policy>(
100       detail::ellint_rc_imp(
101          static_cast<value_type>(x),
102          static_cast<value_type>(y), pol), "boost::math::ellint_rc<%1%>(%1%,%1%)");
103 }
104
105 template <class T1, class T2>
106 inline typename tools::promote_args<T1, T2>::type 
107    ellint_rc(T1 x, T2 y)
108 {
109    return ellint_rc(x, y, policies::policy<>());
110 }
111
112 }} // namespaces
113
114 #endif // BOOST_MATH_ELLINT_RC_HPP
115