]> git.donarmstrong.com Git - rsem.git/blob - boost/math/special_functions/ellint_rj.hpp
Updated boost to v1.55.0
[rsem.git] / boost / math / special_functions / ellint_rj.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 p < 0 case.
11 //
12
13 #ifndef BOOST_MATH_ELLINT_RJ_HPP
14 #define BOOST_MATH_ELLINT_RJ_HPP
15
16 #ifdef _MSC_VER
17 #pragma once
18 #endif
19
20 #include <boost/math/special_functions/math_fwd.hpp>
21 #include <boost/math/tools/config.hpp>
22 #include <boost/math/policies/error_handling.hpp>
23 #include <boost/math/special_functions/ellint_rc.hpp>
24 #include <boost/math/special_functions/ellint_rf.hpp>
25
26 // Carlson's elliptic integral of the third kind
27 // R_J(x, y, z, p) = 1.5 * \int_{0}^{\infty} (t+p)^{-1} [(t+x)(t+y)(t+z)]^{-1/2} dt
28 // Carlson, Numerische Mathematik, vol 33, 1 (1979)
29
30 namespace boost { namespace math { namespace detail{
31
32 template <typename T, typename Policy>
33 T ellint_rj_imp(T x, T y, T z, T p, const Policy& pol)
34 {
35     T value, u, lambda, alpha, beta, sigma, factor, tolerance;
36     T X, Y, Z, P, EA, EB, EC, E2, E3, S1, S2, S3;
37     unsigned long k;
38
39     BOOST_MATH_STD_USING
40     using namespace boost::math::tools;
41
42     static const char* function = "boost::math::ellint_rj<%1%>(%1%,%1%,%1%)";
43
44     if (x < 0)
45     {
46        return policies::raise_domain_error<T>(function,
47             "Argument x must be non-negative, but got x = %1%", x, pol);
48     }
49     if(y < 0)
50     {
51        return policies::raise_domain_error<T>(function,
52             "Argument y must be non-negative, but got y = %1%", y, pol);
53     }
54     if(z < 0)
55     {
56        return policies::raise_domain_error<T>(function,
57             "Argument z must be non-negative, but got z = %1%", z, pol);
58     }
59     if(p == 0)
60     {
61        return policies::raise_domain_error<T>(function,
62             "Argument p must not be zero, but got p = %1%", p, pol);
63     }
64     if (x + y == 0 || y + z == 0 || z + x == 0)
65     {
66        return policies::raise_domain_error<T>(function,
67             "At most one argument can be zero, "
68             "only possible result is %1%.", std::numeric_limits<T>::quiet_NaN(), pol);
69     }
70
71     // error scales as the 6th power of tolerance
72     tolerance = pow(T(1) * tools::epsilon<T>() / 3, T(1) / 6);
73
74     // for p < 0, the integral is singular, return Cauchy principal value
75     if (p < 0)
76     {
77        //
78        // We must ensure that (z - y) * (y - x) is positive.
79        // Since the integral is symmetrical in x, y and z
80        // we can just permute the values:
81        //
82        if(x > y)
83           std::swap(x, y);
84        if(y > z)
85           std::swap(y, z);
86        if(x > y)
87           std::swap(x, y);
88
89        T q = -p;
90        T pmy = (z - y) * (y - x) / (y + q);  // p - y
91
92        BOOST_ASSERT(pmy >= 0);
93
94        p = pmy + y;
95        value = boost::math::ellint_rj(x, y, z, p, pol);
96        value *= pmy;
97        value -= 3 * boost::math::ellint_rf(x, y, z, pol);
98        value += 3 * sqrt((x * y * z) / (x * z + p * q)) * boost::math::ellint_rc(x * z + p * q, p * q, pol);
99        value /= (y + q);
100        return value;
101     }
102
103     // duplication
104     sigma = 0;
105     factor = 1;
106     k = 1;
107     do
108     {
109         u = (x + y + z + p + p) / 5;
110         X = (u - x) / u;
111         Y = (u - y) / u;
112         Z = (u - z) / u;
113         P = (u - p) / u;
114         
115         if ((tools::max)(abs(X), abs(Y), abs(Z), abs(P)) < tolerance) 
116            break;
117
118         T sx = sqrt(x);
119         T sy = sqrt(y);
120         T sz = sqrt(z);
121         
122         lambda = sy * (sx + sz) + sz * sx;
123         alpha = p * (sx + sy + sz) + sx * sy * sz;
124         alpha *= alpha;
125         beta = p * (p + lambda) * (p + lambda);
126         sigma += factor * boost::math::ellint_rc(alpha, beta, pol);
127         factor /= 4;
128         x = (x + lambda) / 4;
129         y = (y + lambda) / 4;
130         z = (z + lambda) / 4;
131         p = (p + lambda) / 4;
132         ++k;
133     }
134     while(k < policies::get_max_series_iterations<Policy>());
135
136     // Check to see if we gave up too soon:
137     policies::check_series_iterations<T>(function, k, pol);
138
139     // Taylor series expansion to the 5th order
140     EA = X * Y + Y * Z + Z * X;
141     EB = X * Y * Z;
142     EC = P * P;
143     E2 = EA - 3 * EC;
144     E3 = EB + 2 * P * (EA - EC);
145     S1 = 1 + E2 * (E2 * T(9) / 88 - E3 * T(9) / 52 - T(3) / 14);
146     S2 = EB * (T(1) / 6 + P * (T(-6) / 22 + P * T(3) / 26));
147     S3 = P * ((EA - EC) / 3 - P * EA * T(3) / 22);
148     value = 3 * sigma + factor * (S1 + S2 + S3) / (u * sqrt(u));
149
150     return value;
151 }
152
153 } // namespace detail
154
155 template <class T1, class T2, class T3, class T4, class Policy>
156 inline typename tools::promote_args<T1, T2, T3, T4>::type 
157    ellint_rj(T1 x, T2 y, T3 z, T4 p, const Policy& pol)
158 {
159    typedef typename tools::promote_args<T1, T2, T3, T4>::type result_type;
160    typedef typename policies::evaluation<result_type, Policy>::type value_type;
161    return policies::checked_narrowing_cast<result_type, Policy>(
162       detail::ellint_rj_imp(
163          static_cast<value_type>(x),
164          static_cast<value_type>(y),
165          static_cast<value_type>(z),
166          static_cast<value_type>(p),
167          pol), "boost::math::ellint_rj<%1%>(%1%,%1%,%1%,%1%)");
168 }
169
170 template <class T1, class T2, class T3, class T4>
171 inline typename tools::promote_args<T1, T2, T3, T4>::type 
172    ellint_rj(T1 x, T2 y, T3 z, T4 p)
173 {
174    return ellint_rj(x, y, z, p, policies::policy<>());
175 }
176
177 }} // namespaces
178
179 #endif // BOOST_MATH_ELLINT_RJ_HPP
180