bernoulli.hpp 5.3 KB

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