| 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 boost::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)(a: static_cast<std::size_t>(max_bernoulli_b2n<T>::value + 1), b: 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)" , 0, 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 boost::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 | return out; |
| 53 | |
| 54 | return get_bernoulli_numbers_cache<T, Policy>().copy_bernoulli_numbers(out, start, n, pol); |
| 55 | } |
| 56 | |
| 57 | } // namespace detail |
| 58 | |
| 59 | template <class T, class Policy> |
| 60 | inline T bernoulli_b2n(const int i, const Policy &pol) |
| 61 | { |
| 62 | typedef boost::integral_constant<int, detail::bernoulli_imp_variant<T>::value> tag_type; |
| 63 | if(i < 0) |
| 64 | return policies::raise_domain_error<T>("boost::math::bernoulli_b2n<%1%>" , "Index should be >= 0 but got %1%" , T(i), pol); |
| 65 | |
| 66 | T result = static_cast<T>(0); // The = 0 is just to silence compiler warnings :-( |
| 67 | boost::math::detail::bernoulli_number_imp<T>(&result, static_cast<std::size_t>(i), 1u, pol, tag_type()); |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | template <class T> |
| 72 | inline T bernoulli_b2n(const int i) |
| 73 | { |
| 74 | return boost::math::bernoulli_b2n<T>(i, policies::policy<>()); |
| 75 | } |
| 76 | |
| 77 | template <class T, class OutputIterator, class Policy> |
| 78 | inline OutputIterator bernoulli_b2n(const int start_index, |
| 79 | const unsigned number_of_bernoullis_b2n, |
| 80 | OutputIterator out_it, |
| 81 | const Policy& pol) |
| 82 | { |
| 83 | typedef boost::integral_constant<int, detail::bernoulli_imp_variant<T>::value> tag_type; |
| 84 | if(start_index < 0) |
| 85 | { |
| 86 | *out_it = policies::raise_domain_error<T>("boost::math::bernoulli_b2n<%1%>" , "Index should be >= 0 but got %1%" , T(start_index), pol); |
| 87 | return ++out_it; |
| 88 | } |
| 89 | |
| 90 | return boost::math::detail::bernoulli_number_imp<T>(out_it, start_index, number_of_bernoullis_b2n, pol, tag_type()); |
| 91 | } |
| 92 | |
| 93 | template <class T, class OutputIterator> |
| 94 | inline OutputIterator bernoulli_b2n(const int start_index, |
| 95 | const unsigned number_of_bernoullis_b2n, |
| 96 | OutputIterator out_it) |
| 97 | { |
| 98 | return boost::math::bernoulli_b2n<T, OutputIterator>(start_index, number_of_bernoullis_b2n, out_it, policies::policy<>()); |
| 99 | } |
| 100 | |
| 101 | template <class T, class Policy> |
| 102 | inline T tangent_t2n(const int i, const Policy &pol) |
| 103 | { |
| 104 | if(i < 0) |
| 105 | return policies::raise_domain_error<T>("boost::math::tangent_t2n<%1%>" , "Index should be >= 0 but got %1%" , T(i), pol); |
| 106 | |
| 107 | T result; |
| 108 | boost::math::detail::get_bernoulli_numbers_cache<T, Policy>().copy_tangent_numbers(&result, i, 1, pol); |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | template <class T> |
| 113 | inline T tangent_t2n(const int i) |
| 114 | { |
| 115 | return boost::math::tangent_t2n<T>(i, policies::policy<>()); |
| 116 | } |
| 117 | |
| 118 | template <class T, class OutputIterator, class Policy> |
| 119 | inline OutputIterator tangent_t2n(const int start_index, |
| 120 | const unsigned number_of_tangent_t2n, |
| 121 | OutputIterator out_it, |
| 122 | const Policy& pol) |
| 123 | { |
| 124 | if(start_index < 0) |
| 125 | { |
| 126 | *out_it = policies::raise_domain_error<T>("boost::math::tangent_t2n<%1%>" , "Index should be >= 0 but got %1%" , T(start_index), pol); |
| 127 | return ++out_it; |
| 128 | } |
| 129 | |
| 130 | return boost::math::detail::get_bernoulli_numbers_cache<T, Policy>().copy_tangent_numbers(out_it, start_index, number_of_tangent_t2n, pol); |
| 131 | } |
| 132 | |
| 133 | template <class T, class OutputIterator> |
| 134 | inline OutputIterator tangent_t2n(const int start_index, |
| 135 | const unsigned number_of_tangent_t2n, |
| 136 | OutputIterator out_it) |
| 137 | { |
| 138 | return boost::math::tangent_t2n<T, OutputIterator>(start_index, number_of_tangent_t2n, out_it, policies::policy<>()); |
| 139 | } |
| 140 | |
| 141 | } } // namespace boost::math |
| 142 | |
| 143 | #endif // _BOOST_BERNOULLI_B2N_2013_05_30_HPP_ |
| 144 | |