Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
exact_bernoulli.hpp
Go to the documentation of this file.
1#pragma once
2#include "../exact_rational.hpp"
3#include <cstddef>
4#include <stdexcept>
5#include <vector>
6
7namespace uni20::detail
8{
9// Akiyama--Tanigawa recurrence in exact rationals. The internal row has
10// B1=+1/2; next() exposes the conventional B1=-1/2 without changing that row.
11class exact_bernoulli_sequence {
12 public:
13 explicit exact_bernoulli_sequence(std::size_t maximum_order)
14 {
15 if (maximum_order >= row_.max_size()) throw std::length_error("Bernoulli order exceeds storage capacity");
16 row_.reserve(maximum_order + 1);
17 }
18 exact_rational next()
19 {
20 auto const m = row_.size();
21 row_.push_back(exact_rational{1} / exact_rational{m + 1});
22 for (std::size_t j = m; j > 0; --j)
23 row_[j - 1] = exact_rational{j} * (row_[j - 1] - row_[j]);
24 return m == 1 ? -row_[0] : row_[0];
25 }
26
27 private:
28 std::vector<exact_rational> row_;
29};
30
31inline exact_rational exact_bernoulli(std::size_t n)
32{
33 exact_bernoulli_sequence sequence(n);
34 exact_rational value;
35 for (std::size_t i = 0; i <= n; ++i)
36 value = sequence.next();
37 return value;
38}
39} // namespace uni20::detail
locale_t value
Definition data_table.cpp:49