1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright 2013 John Maddock
3 // Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_MATH_BERNOULLI_DETAIL_HPP
8 #define BOOST_MATH_BERNOULLI_DETAIL_HPP
10 #include <boost/config.hpp>
11 #include <boost/detail/lightweight_mutex.hpp>
12 #include <boost/math/tools/atomic.hpp>
13 #include <boost/utility/enable_if.hpp>
14 #include <boost/math/tools/toms748_solve.hpp>
17 namespace boost{ namespace math{ namespace detail{
19 // Asymptotic expansion for B2n due to
20 // Luschny LogB3 formula (http://www.luschny.de/math/primes/bernincl.html)
22 template <class T, class Policy>
23 T b2n_asymptotic(int n)
26 const T nx = static_cast<T>(n);
29 const T approximate_log_of_bernoulli_bn =
30 ((boost::math::constants::half<T>() + nx) * log(nx))
31 + ((boost::math::constants::half<T>() - nx) * log(boost::math::constants::pi<T>()))
32 + (((T(3) / 2) - nx) * boost::math::constants::ln_two<T>())
33 + ((nx * (T(2) - (nx2 * 7) * (1 + ((nx2 * 30) * ((nx2 * 12) - 1))))) / (((nx2 * nx2) * nx2) * 2520));
34 return ((n / 2) & 1 ? 1 : -1) * (approximate_log_of_bernoulli_bn > tools::log_max_value<T>()
35 ? policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, nx, Policy())
36 : static_cast<T>(exp(approximate_log_of_bernoulli_bn)));
39 template <class T, class Policy>
40 T t2n_asymptotic(int n)
43 // Just get B2n and convert to a Tangent number:
44 T t2n = fabs(b2n_asymptotic<T, Policy>(2 * n)) / (2 * n);
45 T p2 = ldexp(T(1), n);
46 if(tools::max_value<T>() / p2 < t2n)
47 return policies::raise_overflow_error<T>("boost::math::tangent_t2n<%1%>(std::size_t)", 0, T(n), Policy());
50 if(tools::max_value<T>() / p2 < t2n)
51 return policies::raise_overflow_error<T>("boost::math::tangent_t2n<%1%>(std::size_t)", 0, Policy());
56 // We need to know the approximate value of /n/ which will
57 // cause bernoulli_b2n<T>(n) to return infinity - this allows
58 // us to elude a great deal of runtime checking for values below
59 // n, and only perform the full overflow checks when we know that we're
60 // getting close to the point where our calculations will overflow.
61 // We use Luschny's LogB3 formula (http://www.luschny.de/math/primes/bernincl.html)
62 // to find the limit, and since we're dealing with the log of the Bernoulli numbers
63 // we need only perform the calculation at double precision and not with T
64 // (which may be a multiprecision type). The limit returned is within 1 of the true
65 // limit for all the types tested. Note that although the code below is basically
66 // the same as b2n_asymptotic above, it has been recast as a continuous real-valued
67 // function as this makes the root finding go smoother/faster. It also omits the
68 // sign of the Bernoulli number.
70 struct max_bernoulli_root_functor
72 max_bernoulli_root_functor(long long t) : target(static_cast<double>(t)) {}
73 double operator()(double n)
77 // Luschny LogB3(n) formula.
79 const double nx2(n * n);
81 const double approximate_log_of_bernoulli_bn
82 = ((boost::math::constants::half<double>() + n) * log(n))
83 + ((boost::math::constants::half<double>() - n) * log(boost::math::constants::pi<double>()))
84 + (((double(3) / 2) - n) * boost::math::constants::ln_two<double>())
85 + ((n * (2 - (nx2 * 7) * (1 + ((nx2 * 30) * ((nx2 * 12) - 1))))) / (((nx2 * nx2) * nx2) * 2520));
87 return approximate_log_of_bernoulli_bn - target;
93 template <class T, class Policy>
94 inline std::size_t find_bernoulli_overflow_limit(const mpl::false_&)
96 long long t = lltrunc(boost::math::tools::log_max_value<T>());
97 max_bernoulli_root_functor fun(t);
98 boost::math::tools::equal_floor tol;
99 boost::uintmax_t max_iter = boost::math::policies::get_max_root_iterations<Policy>();
100 return static_cast<std::size_t>(boost::math::tools::toms748_solve(fun, sqrt(double(t)), double(t), tol, max_iter).first) / 2;
103 template <class T, class Policy>
104 inline std::size_t find_bernoulli_overflow_limit(const mpl::true_&)
106 return max_bernoulli_index<bernoulli_imp_variant<T>::value>::value;
109 template <class T, class Policy>
110 std::size_t b2n_overflow_limit()
112 // This routine is called at program startup if it's called at all:
113 // that guarantees safe initialization of the static variable.
114 typedef mpl::bool_<(bernoulli_imp_variant<T>::value >= 1) && (bernoulli_imp_variant<T>::value <= 3)> tag_type;
115 static const std::size_t lim = find_bernoulli_overflow_limit<T, Policy>(tag_type());
120 // The tangent numbers grow larger much more rapidly than the Bernoulli numbers do....
121 // so to compute the Bernoulli numbers from the tangent numbers, we need to avoid spurious
122 // overflow in the calculation, we can do this by scaling all the tangent number by some scale factor:
125 inline typename enable_if_c<std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::radix == 2), T>::type tangent_scale_factor()
128 return ldexp(T(1), std::numeric_limits<T>::min_exponent + 5);
131 inline typename disable_if_c<std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::radix == 2), T>::type tangent_scale_factor()
133 return tools::min_value<T>() * 16;
136 // Initializer: ensure all our constants are initialized prior to the first call of main:
138 template <class T, class Policy>
139 struct bernoulli_initializer
146 // We call twice, once to initialize our static table, and once to
147 // initialize our dymanic table:
149 boost::math::bernoulli_b2n<T>(2, Policy());
150 #ifndef BOOST_NO_EXCEPTIONS
153 boost::math::bernoulli_b2n<T>(max_bernoulli_b2n<T>::value + 1, Policy());
154 #ifndef BOOST_NO_EXCEPTIONS
155 } catch(const std::overflow_error&){}
157 boost::math::tangent_t2n<T>(2, Policy());
159 void force_instantiate()const{}
161 static const init initializer;
162 static void force_instantiate()
164 initializer.force_instantiate();
168 template <class T, class Policy>
169 const typename bernoulli_initializer<T, Policy>::init bernoulli_initializer<T, Policy>::initializer;
172 // We need something to act as a cache for our calculated Bernoulli numbers. In order to
173 // ensure both fast access and thread safety, we need a stable table which may be extended
174 // in size, but which never reallocates: that way values already calculated may be accessed
175 // concurrently with another thread extending the table with new values.
177 // Very very simple vector class that will never allocate more than once, we could use
178 // boost::container::static_vector here, but that allocates on the stack, which may well
179 // cause issues for the amount of memory we want in the extreme case...
182 struct fixed_vector : private std::allocator<T>
184 typedef unsigned size_type;
186 typedef const T* const_iterator;
187 fixed_vector() : m_used(0)
189 std::size_t overflow_limit = 5 + b2n_overflow_limit<T, policies::policy<> >();
190 m_capacity = static_cast<unsigned>((std::min)(overflow_limit, static_cast<std::size_t>(100000u)));
191 m_data = this->allocate(m_capacity);
195 for(unsigned i = 0; i < m_used; ++i)
196 this->destroy(&m_data[i]);
197 this->deallocate(m_data, m_capacity);
199 T& operator[](unsigned n) { BOOST_ASSERT(n < m_used); return m_data[n]; }
200 const T& operator[](unsigned n)const { BOOST_ASSERT(n < m_used); return m_data[n]; }
201 unsigned size()const { return m_used; }
202 unsigned size() { return m_used; }
203 void resize(unsigned n, const T& val)
207 BOOST_THROW_EXCEPTION(std::runtime_error("Exhausted storage for Bernoulli numbers."));
209 for(unsigned i = m_used; i < n; ++i)
210 new (m_data + i) T(val);
213 void resize(unsigned n) { resize(n, T()); }
214 T* begin() { return m_data; }
215 T* end() { return m_data + m_used; }
216 T* begin()const { return m_data; }
217 T* end()const { return m_data + m_used; }
218 unsigned capacity()const { return m_capacity; }
219 void clear() { m_used = 0; }
222 unsigned m_used, m_capacity;
225 template <class T, class Policy>
226 class bernoulli_numbers_cache
229 bernoulli_numbers_cache() : m_overflow_limit((std::numeric_limits<std::size_t>::max)())
230 #if defined(BOOST_HAS_THREADS) && !defined(BOOST_MATH_NO_ATOMIC_INT)
233 , m_current_precision(boost::math::tools::digits<T>())
236 typedef fixed_vector<T> container_type;
238 void tangent(std::size_t m)
240 static const std::size_t min_overflow_index = b2n_overflow_limit<T, Policy>() - 1;
241 tn.resize(static_cast<typename container_type::size_type>(m), T(0U));
243 BOOST_MATH_INSTRUMENT_VARIABLE(min_overflow_index);
245 std::size_t prev_size = m_intermediates.size();
246 m_intermediates.resize(m, T(0U));
250 m_intermediates[1] = tangent_scale_factor<T>() /*T(1U)*/;
252 tn[1U] = tangent_scale_factor<T>()/* T(1U)*/;
253 BOOST_MATH_INSTRUMENT_VARIABLE(tn[0]);
254 BOOST_MATH_INSTRUMENT_VARIABLE(tn[1]);
257 for(std::size_t i = std::max<size_t>(2, prev_size); i < m; i++)
259 bool overflow_check = false;
260 if(i >= min_overflow_index && (boost::math::tools::max_value<T>() / (i-1) < m_intermediates[1]) )
262 std::fill(tn.begin() + i, tn.end(), boost::math::tools::max_value<T>());
265 m_intermediates[1] = m_intermediates[1] * (i-1);
266 for(std::size_t j = 2; j <= i; j++)
269 (i >= min_overflow_index) && (
270 (boost::math::tools::max_value<T>() / (i - j) < m_intermediates[j])
271 || (boost::math::tools::max_value<T>() / (i - j + 2) < m_intermediates[j-1])
272 || (boost::math::tools::max_value<T>() - m_intermediates[j] * (i - j) < m_intermediates[j-1] * (i - j + 2))
273 || ((boost::math::isinf)(m_intermediates[j]))
278 std::fill(tn.begin() + i, tn.end(), boost::math::tools::max_value<T>());
281 m_intermediates[j] = m_intermediates[j] * (i - j) + m_intermediates[j-1] * (i - j + 2);
284 break; // already filled the tn...
285 tn[static_cast<typename container_type::size_type>(i)] = m_intermediates[i];
286 BOOST_MATH_INSTRUMENT_VARIABLE(i);
287 BOOST_MATH_INSTRUMENT_VARIABLE(tn[static_cast<typename container_type::size_type>(i)]);
291 void tangent_numbers_series(const std::size_t m)
294 static const std::size_t min_overflow_index = b2n_overflow_limit<T, Policy>() - 1;
296 typename container_type::size_type old_size = bn.size();
299 bn.resize(static_cast<typename container_type::size_type>(m));
307 T power_two(ldexp(T(1), static_cast<int>(2 * old_size)));
309 for(std::size_t i = old_size; i < m; i++)
311 T b(static_cast<T>(i * 2));
313 // Not only do we need to take care to avoid spurious over/under flow in
314 // the calculation, but we also need to avoid overflow altogether in case
315 // we're calculating with a type where "bad things" happen in that case:
317 b = b / (power_two * tangent_scale_factor<T>());
318 b /= (power_two - 1);
319 bool overflow_check = (i >= min_overflow_index) && (tools::max_value<T>() / tn[static_cast<typename container_type::size_type>(i)] < b);
322 m_overflow_limit = i;
325 b = std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : tools::max_value<T>();
326 bn[static_cast<typename container_type::size_type>(i)] = ((i % 2U) ? b : T(-b));
333 b *= tn[static_cast<typename container_type::size_type>(i)];
336 power_two = ldexp(power_two, 2);
338 const bool b_neg = i % 2 == 0;
340 bn[static_cast<typename container_type::size_type>(i)] = ((!b_neg) ? b : T(-b));
344 template <class OutputIterator>
345 OutputIterator copy_bernoulli_numbers(OutputIterator out, std::size_t start, std::size_t n, const Policy& pol)
348 // There are basically 3 thread safety options:
350 // 1) There are no threads (BOOST_HAS_THREADS is not defined).
351 // 2) There are threads, but we do not have a true atomic integer type,
352 // in this case we just use a mutex to guard against race conditions.
353 // 3) There are threads, and we have an atomic integer: in this case we can
354 // use the double-checked locking pattern to avoid thread synchronisation
355 // when accessing values already in the cache.
357 // First off handle the common case for overflow and/or asymptotic expansion:
359 if(start + n > bn.capacity())
361 if(start < bn.capacity())
363 out = copy_bernoulli_numbers(out, start, bn.capacity() - start, pol);
364 n -= bn.capacity() - start;
365 start = static_cast<std::size_t>(bn.capacity());
367 if(start < b2n_overflow_limit<T, Policy>() + 2u)
369 for(; n; ++start, --n)
371 *out = b2n_asymptotic<T, Policy>(static_cast<typename container_type::size_type>(start * 2U));
375 for(; n; ++start, --n)
377 *out = policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(start), pol);
382 #if !defined(BOOST_HAS_THREADS)
384 // Single threaded code, very simple:
386 if(m_current_precision < boost::math::tools::digits<T>())
390 m_intermediates.clear();
391 m_current_precision = boost::math::tools::digits<T>();
393 if(start + n >= bn.size())
395 std::size_t new_size = (std::min)((std::max)((std::max)(std::size_t(start + n), std::size_t(bn.size() + 20)), std::size_t(50)), std::size_t(bn.capacity()));
396 tangent_numbers_series(new_size);
399 for(std::size_t i = (std::max)(std::size_t(max_bernoulli_b2n<T>::value + 1), start); i < start + n; ++i)
401 *out = (i >= m_overflow_limit) ? policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol) : bn[i];
404 #elif defined(BOOST_MATH_NO_ATOMIC_INT)
406 // We need to grab a mutex every time we get here, for both readers and writers:
408 boost::detail::lightweight_mutex::scoped_lock l(m_mutex);
409 if(m_current_precision < boost::math::tools::digits<T>())
413 m_intermediates.clear();
414 m_current_precision = boost::math::tools::digits<T>();
416 if(start + n >= bn.size())
418 std::size_t new_size = (std::min)((std::max)((std::max)(std::size_t(start + n), std::size_t(bn.size() + 20)), std::size_t(50)), std::size_t(bn.capacity()));
419 tangent_numbers_series(new_size);
422 for(std::size_t i = (std::max)(std::size_t(max_bernoulli_b2n<T>::value + 1), start); i < start + n; ++i)
424 *out = (i >= m_overflow_limit) ? policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol) : bn[i];
430 // Double-checked locking pattern, lets us access cached already cached values
433 // Get the counter and see if we need to calculate more constants:
435 if((static_cast<std::size_t>(m_counter.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < start + n)
436 || (static_cast<int>(m_current_precision.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < boost::math::tools::digits<T>()))
438 boost::detail::lightweight_mutex::scoped_lock l(m_mutex);
440 if((static_cast<std::size_t>(m_counter.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < start + n)
441 || (static_cast<int>(m_current_precision.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < boost::math::tools::digits<T>()))
443 if(static_cast<int>(m_current_precision.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < boost::math::tools::digits<T>())
447 m_intermediates.clear();
448 m_counter.store(0, BOOST_MATH_ATOMIC_NS::memory_order_release);
449 m_current_precision = boost::math::tools::digits<T>();
451 if(start + n >= bn.size())
453 std::size_t new_size = (std::min)((std::max)((std::max)(std::size_t(start + n), std::size_t(bn.size() + 20)), std::size_t(50)), std::size_t(bn.capacity()));
454 tangent_numbers_series(new_size);
456 m_counter.store(static_cast<atomic_integer_type>(bn.size()), BOOST_MATH_ATOMIC_NS::memory_order_release);
460 for(std::size_t i = (std::max)(static_cast<std::size_t>(max_bernoulli_b2n<T>::value + 1), start); i < start + n; ++i)
462 *out = (i >= m_overflow_limit) ? policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol) : bn[static_cast<typename container_type::size_type>(i)];
470 template <class OutputIterator>
471 OutputIterator copy_tangent_numbers(OutputIterator out, std::size_t start, std::size_t n, const Policy& pol)
474 // There are basically 3 thread safety options:
476 // 1) There are no threads (BOOST_HAS_THREADS is not defined).
477 // 2) There are threads, but we do not have a true atomic integer type,
478 // in this case we just use a mutex to guard against race conditions.
479 // 3) There are threads, and we have an atomic integer: in this case we can
480 // use the double-checked locking pattern to avoid thread synchronisation
481 // when accessing values already in the cache.
484 // First off handle the common case for overflow and/or asymptotic expansion:
486 if(start + n > bn.capacity())
488 if(start < bn.capacity())
490 out = copy_tangent_numbers(out, start, bn.capacity() - start, pol);
491 n -= bn.capacity() - start;
492 start = static_cast<std::size_t>(bn.capacity());
494 if(start < b2n_overflow_limit<T, Policy>() + 2u)
496 for(; n; ++start, --n)
498 *out = t2n_asymptotic<T, Policy>(static_cast<typename container_type::size_type>(start));
502 for(; n; ++start, --n)
504 *out = policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(start), pol);
509 #if !defined(BOOST_HAS_THREADS)
511 // Single threaded code, very simple:
513 if(m_current_precision < boost::math::tools::digits<T>())
517 m_intermediates.clear();
518 m_current_precision = boost::math::tools::digits<T>();
520 if(start + n >= bn.size())
522 std::size_t new_size = (std::min)((std::max)((std::max)(start + n, std::size_t(bn.size() + 20)), std::size_t(50)), std::size_t(bn.capacity()));
523 tangent_numbers_series(new_size);
526 for(std::size_t i = start; i < start + n; ++i)
528 if(i >= m_overflow_limit)
529 *out = policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol);
532 if(tools::max_value<T>() * tangent_scale_factor<T>() < tn[static_cast<typename container_type::size_type>(i)])
533 *out = policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol);
535 *out = tn[static_cast<typename container_type::size_type>(i)] / tangent_scale_factor<T>();
539 #elif defined(BOOST_MATH_NO_ATOMIC_INT)
541 // We need to grab a mutex every time we get here, for both readers and writers:
543 boost::detail::lightweight_mutex::scoped_lock l(m_mutex);
544 if(m_current_precision < boost::math::tools::digits<T>())
548 m_intermediates.clear();
549 m_current_precision = boost::math::tools::digits<T>();
551 if(start + n >= bn.size())
553 std::size_t new_size = (std::min)((std::max)((std::max)(start + n, std::size_t(bn.size() + 20)), std::size_t(50)), std::size_t(bn.capacity()));
554 tangent_numbers_series(new_size);
557 for(std::size_t i = start; i < start + n; ++i)
559 if(i >= m_overflow_limit)
560 *out = policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol);
563 if(tools::max_value<T>() * tangent_scale_factor<T>() < tn[static_cast<typename container_type::size_type>(i)])
564 *out = policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol);
566 *out = tn[static_cast<typename container_type::size_type>(i)] / tangent_scale_factor<T>();
573 // Double-checked locking pattern, lets us access cached already cached values
576 // Get the counter and see if we need to calculate more constants:
578 if((static_cast<std::size_t>(m_counter.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < start + n)
579 || (static_cast<int>(m_current_precision.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < boost::math::tools::digits<T>()))
581 boost::detail::lightweight_mutex::scoped_lock l(m_mutex);
583 if((static_cast<std::size_t>(m_counter.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < start + n)
584 || (static_cast<int>(m_current_precision.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < boost::math::tools::digits<T>()))
586 if(static_cast<int>(m_current_precision.load(BOOST_MATH_ATOMIC_NS::memory_order_consume)) < boost::math::tools::digits<T>())
590 m_intermediates.clear();
591 m_counter.store(0, BOOST_MATH_ATOMIC_NS::memory_order_release);
592 m_current_precision = boost::math::tools::digits<T>();
594 if(start + n >= bn.size())
596 std::size_t new_size = (std::min)((std::max)((std::max)(start + n, std::size_t(bn.size() + 20)), std::size_t(50)), std::size_t(bn.capacity()));
597 tangent_numbers_series(new_size);
599 m_counter.store(static_cast<atomic_integer_type>(bn.size()), BOOST_MATH_ATOMIC_NS::memory_order_release);
603 for(std::size_t i = start; i < start + n; ++i)
605 if(i >= m_overflow_limit)
606 *out = policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol);
609 if(tools::max_value<T>() * tangent_scale_factor<T>() < tn[static_cast<typename container_type::size_type>(i)])
610 *out = policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol);
612 *out = tn[static_cast<typename container_type::size_type>(i)] / tangent_scale_factor<T>();
623 // The caches for Bernoulli and tangent numbers, once allocated,
624 // these must NEVER EVER reallocate as it breaks our thread
625 // safety guarantees:
627 fixed_vector<T> bn, tn;
628 std::vector<T> m_intermediates;
629 // The value at which we know overflow has already occurred for the Bn:
630 std::size_t m_overflow_limit;
631 #if !defined(BOOST_HAS_THREADS)
632 int m_current_precision;
633 #elif defined(BOOST_MATH_NO_ATOMIC_INT)
634 boost::detail::lightweight_mutex m_mutex;
635 int m_current_precision;
637 boost::detail::lightweight_mutex m_mutex;
638 atomic_counter_type m_counter, m_current_precision;
642 template <class T, class Policy>
643 inline bernoulli_numbers_cache<T, Policy>& get_bernoulli_numbers_cache()
646 // Force this function to be called at program startup so all the static variables
647 // get initailzed then (thread safety).
649 bernoulli_initializer<T, Policy>::force_instantiate();
650 static bernoulli_numbers_cache<T, Policy> data;
656 #endif // BOOST_MATH_BERNOULLI_DETAIL_HPP