]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/math/include/boost/math/special_functions/ellint_2.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / math / include / boost / math / special_functions / ellint_2.hpp
CommitLineData
7c673cae
FG
1// Copyright (c) 2006 Xiaogang Zhang
2// Copyright (c) 2006 John Maddock
3// Use, modification and distribution are subject to the
4// Boost Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// History:
8// XZ wrote the original of this file as part of the Google
9// Summer of Code 2006. JM modified it to fit into the
10// Boost.Math conceptual framework better, and to ensure
11// that the code continues to work no matter how many digits
12// type T has.
13
14#ifndef BOOST_MATH_ELLINT_2_HPP
15#define BOOST_MATH_ELLINT_2_HPP
16
17#ifdef _MSC_VER
18#pragma once
19#endif
20
21#include <boost/math/special_functions/math_fwd.hpp>
22#include <boost/math/special_functions/ellint_rf.hpp>
23#include <boost/math/special_functions/ellint_rd.hpp>
24#include <boost/math/special_functions/ellint_rg.hpp>
25#include <boost/math/constants/constants.hpp>
26#include <boost/math/policies/error_handling.hpp>
27#include <boost/math/tools/workaround.hpp>
28#include <boost/math/special_functions/round.hpp>
29
30// Elliptic integrals (complete and incomplete) of the second kind
31// Carlson, Numerische Mathematik, vol 33, 1 (1979)
32
33namespace boost { namespace math {
34
35template <class T1, class T2, class Policy>
36typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const Policy& pol);
37
38namespace detail{
39
40template <typename T, typename Policy>
41T ellint_e_imp(T k, const Policy& pol);
42
43// Elliptic integral (Legendre form) of the second kind
44template <typename T, typename Policy>
45T ellint_e_imp(T phi, T k, const Policy& pol)
46{
47 BOOST_MATH_STD_USING
48 using namespace boost::math::tools;
49 using namespace boost::math::constants;
50
51 bool invert = false;
52 if(phi < 0)
53 {
54 phi = fabs(phi);
55 invert = true;
56 }
57
58 T result;
59
60 if(phi >= tools::max_value<T>())
61 {
62 // Need to handle infinity as a special case:
63 result = policies::raise_overflow_error<T>("boost::math::ellint_e<%1%>(%1%,%1%)", 0, pol);
64 }
65 else if(phi > 1 / tools::epsilon<T>())
66 {
67 // Phi is so large that phi%pi is necessarily zero (or garbage),
68 // just return the second part of the duplication formula:
69 result = 2 * phi * ellint_e_imp(k, pol) / constants::pi<T>();
70 }
71 else if(k == 0)
72 {
73 return invert ? T(-phi) : phi;
74 }
75 else if(fabs(k) == 1)
76 {
77 return invert ? T(-sin(phi)) : T(sin(phi));
78 }
79 else
80 {
81 // Carlson's algorithm works only for |phi| <= pi/2,
82 // use the integrand's periodicity to normalize phi
83 //
84 // Xiaogang's original code used a cast to long long here
85 // but that fails if T has more digits than a long long,
86 // so rewritten to use fmod instead:
87 //
88 T rphi = boost::math::tools::fmod_workaround(phi, T(constants::half_pi<T>()));
89 T m = boost::math::round((phi - rphi) / constants::half_pi<T>());
90 int s = 1;
91 if(boost::math::tools::fmod_workaround(m, T(2)) > 0.5)
92 {
93 m += 1;
94 s = -1;
95 rphi = constants::half_pi<T>() - rphi;
96 }
97 T k2 = k * k;
98 if(k2 > 1)
99 {
100 return policies::raise_domain_error<T>("boost::math::ellint_2<%1%>(%1%, %1%)", "The parameter k is out of range, got k = %1%", k, pol);
101 }
102 else if(rphi < tools::root_epsilon<T>())
103 {
104 // See http://functions.wolfram.com/EllipticIntegrals/EllipticE2/06/01/03/0001/
105 result = s * rphi;
106 }
107 else
108 {
109 // http://dlmf.nist.gov/19.25#E10
110 T sinp = sin(rphi);
111 T cosp = cos(rphi);
112 T c = 1 / (sinp * sinp);
113 T cm1 = cosp * cosp / (sinp * sinp); // c - 1
114 result = s * ((1 - k2) * ellint_rf_imp(cm1, T(c - k2), c, pol) + k2 * (1 - k2) * ellint_rd(cm1, c, T(c - k2), pol) / 3 + k2 * sqrt(cm1 / (c * (c - k2))));
115 }
116 if(m != 0)
117 result += m * ellint_e_imp(k, pol);
118 }
119 return invert ? T(-result) : result;
120}
121
122// Complete elliptic integral (Legendre form) of the second kind
123template <typename T, typename Policy>
124T ellint_e_imp(T k, const Policy& pol)
125{
126 BOOST_MATH_STD_USING
127 using namespace boost::math::tools;
128
129 if (abs(k) > 1)
130 {
131 return policies::raise_domain_error<T>("boost::math::ellint_e<%1%>(%1%)",
132 "Got k = %1%, function requires |k| <= 1", k, pol);
133 }
134 if (abs(k) == 1)
135 {
136 return static_cast<T>(1);
137 }
138
139 T x = 0;
140 T t = k * k;
141 T y = 1 - t;
142 T z = 1;
143 T value = 2 * ellint_rg_imp(x, y, z, pol);
144
145 return value;
146}
147
148template <typename T, typename Policy>
149inline typename tools::promote_args<T>::type ellint_2(T k, const Policy& pol, const mpl::true_&)
150{
151 typedef typename tools::promote_args<T>::type result_type;
152 typedef typename policies::evaluation<result_type, Policy>::type value_type;
153 return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_e_imp(static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%)");
154}
155
156// Elliptic integral (Legendre form) of the second kind
157template <class T1, class T2>
158inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const mpl::false_&)
159{
160 return boost::math::ellint_2(k, phi, policies::policy<>());
161}
162
163} // detail
164
165// Complete elliptic integral (Legendre form) of the second kind
166template <typename T>
167inline typename tools::promote_args<T>::type ellint_2(T k)
168{
169 return ellint_2(k, policies::policy<>());
170}
171
172// Elliptic integral (Legendre form) of the second kind
173template <class T1, class T2>
174inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi)
175{
176 typedef typename policies::is_policy<T2>::type tag_type;
177 return detail::ellint_2(k, phi, tag_type());
178}
179
180template <class T1, class T2, class Policy>
181inline typename tools::promote_args<T1, T2>::type ellint_2(T1 k, T2 phi, const Policy& pol)
182{
183 typedef typename tools::promote_args<T1, T2>::type result_type;
184 typedef typename policies::evaluation<result_type, Policy>::type value_type;
185 return policies::checked_narrowing_cast<result_type, Policy>(detail::ellint_e_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::ellint_2<%1%>(%1%,%1%)");
186}
187
188}} // namespaces
189
190#endif // BOOST_MATH_ELLINT_2_HPP
191