bernoulli.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 mpl::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)", 0, 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 mpl::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. return out;
  45. return get_bernoulli_numbers_cache<T, Policy>().copy_bernoulli_numbers(out, start, n, pol);
  46. }
  47. } // namespace detail
  48. template <class T, class Policy>
  49. inline T bernoulli_b2n(const int i, const Policy &pol)
  50. {
  51. typedef mpl::int_<detail::bernoulli_imp_variant<T>::value> tag_type;
  52. if(i < 0)
  53. return policies::raise_domain_error<T>("boost::math::bernoulli_b2n<%1%>", "Index should be >= 0 but got %1%", T(i), pol);
  54. T result = static_cast<T>(0); // The = 0 is just to silence compiler warnings :-(
  55. boost::math::detail::bernoulli_number_imp<T>(&result, static_cast<std::size_t>(i), 1u, pol, tag_type());
  56. return result;
  57. }
  58. template <class T>
  59. inline T bernoulli_b2n(const int i)
  60. {
  61. return boost::math::bernoulli_b2n<T>(i, policies::policy<>());
  62. }
  63. template <class T, class OutputIterator, class Policy>
  64. inline OutputIterator bernoulli_b2n(const int start_index,
  65. const unsigned number_of_bernoullis_b2n,
  66. OutputIterator out_it,
  67. const Policy& pol)
  68. {
  69. typedef mpl::int_<detail::bernoulli_imp_variant<T>::value> tag_type;
  70. if(start_index < 0)
  71. {
  72. *out_it = policies::raise_domain_error<T>("boost::math::bernoulli_b2n<%1%>", "Index should be >= 0 but got %1%", T(start_index), pol);
  73. return ++out_it;
  74. }
  75. return boost::math::detail::bernoulli_number_imp<T>(out_it, start_index, number_of_bernoullis_b2n, pol, tag_type());
  76. }
  77. template <class T, class OutputIterator>
  78. inline OutputIterator bernoulli_b2n(const int start_index,
  79. const unsigned number_of_bernoullis_b2n,
  80. OutputIterator out_it)
  81. {
  82. return boost::math::bernoulli_b2n<T, OutputIterator>(start_index, number_of_bernoullis_b2n, out_it, policies::policy<>());
  83. }
  84. template <class T, class Policy>
  85. inline T tangent_t2n(const int i, const Policy &pol)
  86. {
  87. if(i < 0)
  88. return policies::raise_domain_error<T>("boost::math::tangent_t2n<%1%>", "Index should be >= 0 but got %1%", T(i), pol);
  89. T result;
  90. boost::math::detail::get_bernoulli_numbers_cache<T, Policy>().copy_tangent_numbers(&result, i, 1, pol);
  91. return result;
  92. }
  93. template <class T>
  94. inline T tangent_t2n(const int i)
  95. {
  96. return boost::math::tangent_t2n<T>(i, policies::policy<>());
  97. }
  98. template <class T, class OutputIterator, class Policy>
  99. inline OutputIterator tangent_t2n(const int start_index,
  100. const unsigned number_of_tangent_t2n,
  101. OutputIterator out_it,
  102. const Policy& pol)
  103. {
  104. if(start_index < 0)
  105. {
  106. *out_it = policies::raise_domain_error<T>("boost::math::tangent_t2n<%1%>", "Index should be >= 0 but got %1%", T(start_index), pol);
  107. return ++out_it;
  108. }
  109. return boost::math::detail::get_bernoulli_numbers_cache<T, Policy>().copy_tangent_numbers(out_it, start_index, number_of_tangent_t2n, pol);
  110. }
  111. template <class T, class OutputIterator>
  112. inline OutputIterator tangent_t2n(const int start_index,
  113. const unsigned number_of_tangent_t2n,
  114. OutputIterator out_it)
  115. {
  116. return boost::math::tangent_t2n<T, OutputIterator>(start_index, number_of_tangent_t2n, out_it, policies::policy<>());
  117. }
  118. } } // namespace boost::math
  119. #endif // _BOOST_BERNOULLI_B2N_2013_05_30_HPP_