Debian Codegraph @

third-party/boost-math/include/boost/math/special_functions/bernoulli.hpp

raw
1
2 ///////////////////////////////////////////////////////////////////////////////
3 // Copyright 2013 Nikhar Agrawal
4 // Copyright 2013 Christopher Kormanyos
5 // Copyright 2013 John Maddock
6 // Copyright 2013 Paul Bristow
7 // Distributed under the Boost
8 // Software License, Version 1.0. (See accompanying file
9 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10
11 #ifndef _BOOST_BERNOULLI_B2N_2013_05_30_HPP_
12 #define _BOOST_BERNOULLI_B2N_2013_05_30_HPP_
13
14 #include <boost/math/special_functions/math_fwd.hpp>
15 #include <boost/math/special_functions/detail/unchecked_bernoulli.hpp>
16 #include <boost/math/special_functions/detail/bernoulli_details.hpp>
17
18 namespace boost { namespace math {
19
20 namespace detail {
21
22 template <class T, class OutputIterator, class Policy, int N>
23 OutputIterator bernoulli_number_imp(OutputIterator out, std::size_t start, std::size_t n, const Policy& pol, const std::integral_constant<int, N>& tag)
24 {
25 for(std::size_t i = start; (i <= max_bernoulli_b2n<T>::value) && (i < start + n); ++i)
26 {
27 *out = unchecked_bernoulli_imp<T>(i, tag);
28 ++out;
29 }
30
31 for(std::size_t i = (std::max)(static_cast<std::size_t>(max_bernoulli_b2n<T>::value + 1), start); i < start + n; ++i)
32 {
33 // We must overflow:
34 *out = (i & 1 ? 1 : -1) * policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(n)", nullptr, T(i), pol);
35 ++out;
36 }
37 return out;
38 }
39
40 template <class T, class OutputIterator, class Policy>
41 OutputIterator bernoulli_number_imp(OutputIterator out, std::size_t start, std::size_t n, const Policy& pol, const std::integral_constant<int, 0>& tag)
42 {
43 for(std::size_t i = start; (i <= max_bernoulli_b2n<T>::value) && (i < start + n); ++i)
44 {
45 *out = unchecked_bernoulli_imp<T>(i, tag);
46 ++out;
47 }
48 //
49 // Short circuit return so we don't grab the mutex below unless we have to:
50 //
51 if(start + n <= max_bernoulli_b2n<T>::value)
52 {
53 return out;
54 }
55
56 return get_bernoulli_numbers_cache<T, Policy>().copy_bernoulli_numbers(out, start, n, pol);
57 }
58
59 } // namespace detail
60
61 template <class T, class Policy>
62 inline T bernoulli_b2n(const int i, const Policy &pol)
63 {
64 using tag_type = std::integral_constant<int, detail::bernoulli_imp_variant<T>::value>;
65 if(i < 0)
66 {
67 return policies::raise_domain_error<T>("boost::math::bernoulli_b2n<%1%>", "Index should be >= 0 but got %1%", T(i), pol);
68 }
69
70 T result {};
71 boost::math::detail::bernoulli_number_imp<T>(&result, static_cast<std::size_t>(i), 1u, pol, tag_type());
72 return result;
73 }
74
75 template <class T>
76 inline T bernoulli_b2n(const int i)
77 {
78 return boost::math::bernoulli_b2n<T>(i, policies::policy<>());
79 }
80
81 template <class T, class OutputIterator, class Policy>
82 inline OutputIterator bernoulli_b2n(const int start_index,
83 const unsigned number_of_bernoullis_b2n,
84 OutputIterator out_it,
85 const Policy& pol)
86 {
87 using tag_type = std::integral_constant<int, detail::bernoulli_imp_variant<T>::value>;
88 if(start_index < 0)
89 {
90 *out_it = policies::raise_domain_error<T>("boost::math::bernoulli_b2n<%1%>", "Index should be >= 0 but got %1%", T(start_index), pol);
91 return ++out_it; // LCOV_EXCL_LINE we don't reach here, previous line throws.
92 }
93
94 return boost::math::detail::bernoulli_number_imp<T>(out_it, start_index, number_of_bernoullis_b2n, pol, tag_type());
95 }
96
97 template <class T, class OutputIterator>
98 inline OutputIterator bernoulli_b2n(const int start_index,
99 const unsigned number_of_bernoullis_b2n,
100 OutputIterator out_it)
101 {
102 return boost::math::bernoulli_b2n<T, OutputIterator>(start_index, number_of_bernoullis_b2n, out_it, policies::policy<>());
103 }
104
105 template <class T, class Policy>
106 inline T tangent_t2n(const int i, const Policy &pol)
107 {
108 if(i < 0)
109 {
110 return policies::raise_domain_error<T>("boost::math::tangent_t2n<%1%>", "Index should be >= 0 but got %1%", T(i), pol);
111 }
112
113 T result {};
114 boost::math::detail::get_bernoulli_numbers_cache<T, Policy>().copy_tangent_numbers(&result, i, 1, pol);
115 return result;
116 }
117
118 template <class T>
119 inline T tangent_t2n(const int i)
120 {
121 return boost::math::tangent_t2n<T>(i, policies::policy<>());
122 }
123
124 template <class T, class OutputIterator, class Policy>
125 inline OutputIterator tangent_t2n(const int start_index,
126 const unsigned number_of_tangent_t2n,
127 OutputIterator out_it,
128 const Policy& pol)
129 {
130 if(start_index < 0)
131 {
132 *out_it = policies::raise_domain_error<T>("boost::math::tangent_t2n<%1%>", "Index should be >= 0 but got %1%", T(start_index), pol);
133 return ++out_it; // LCOV_EXCL_LINE we don't reach here, previous line throws.
134 }
135
136 return boost::math::detail::get_bernoulli_numbers_cache<T, Policy>().copy_tangent_numbers(out_it, start_index, number_of_tangent_t2n, pol);
137 }
138
139 template <class T, class OutputIterator>
140 inline OutputIterator tangent_t2n(const int start_index,
141 const unsigned number_of_tangent_t2n,
142 OutputIterator out_it)
143 {
144 return boost::math::tangent_t2n<T, OutputIterator>(start_index, number_of_tangent_t2n, out_it, policies::policy<>());
145 }
146
147 } } // namespace boost::math
148
149 #endif // _BOOST_BERNOULLI_B2N_2013_05_30_HPP_