13 std::function<
double(
double)> f)
14 : x_min_(x_min), x_max_(x_min + range), inv_dx_(num / range) {
16 throw std::runtime_error(
"Tabulation needs at least two values");
19 const double dx = range / num;
20 for (
size_t i = 0; i <= num; i++) {
51 std::min(
static_cast<size_t>(index_double),
values_.size() - 2);
52 const double r = index_double -
n;
62 static void swrite(std::ofstream& stream,
double x) {
63 stream.write(
reinterpret_cast<const char*
>(&x),
sizeof(x));
74 stream.read(
reinterpret_cast<char*
>(&x),
sizeof(x));
84 static void swrite(std::ofstream& stream,
size_t x) {
87 const auto const_size_x =
static_cast<uint64_t
>(x);
88 stream.write(
reinterpret_cast<const char*
>(&const_size_x),
89 sizeof(const_size_x));
100 stream.read(
reinterpret_cast<char*
>(&x),
sizeof(x));
101 if (x > std::numeric_limits<size_t>::max()) {
102 throw std::runtime_error(
"trying to read vector larger than supported");
113 static void swrite(std::ofstream& stream,
const std::vector<double> x) {
116 stream.write(
reinterpret_cast<const char*
>(x.data()),
117 sizeof(x[0]) * x.size());
129 std::vector<double> x;
131 stream.read(
reinterpret_cast<char*
>(x.data()),
sizeof(
double) *
n);
143 stream.write(
reinterpret_cast<const char*
>(x.data()),
144 sizeof(x[0]) * x.size());
155 stream.read(
reinterpret_cast<char*
>(x.data()), x.size());
170 if (hash != hash_from_stream) {
A class for storing a one-dimensional lookup table of floating-point values.
double x_min_
lower bound for tabulation
double inv_dx_
inverse step size 1/dx
double x_max_
upper bound for tabulation
std::vector< double > values_
vector for storing tabulated values
static Tabulation from_file(std::ifstream &stream, sha256::Hash hash)
Construct a tabulation object by reading binary data from a stream.
Tabulation()
Construct an empty tabulation object.
void write(std::ofstream &stream, sha256::Hash hash) const
Write a binary representation of the tabulation to a stream.
double get_value_step(double x) const
Look up a value from the tabulation (without any interpolation, simply using the closest tabulated va...
double get_value_linear(double x, Extrapolation extrapolation=Extrapolation::Linear) const
Look up a value from the tabulation using linear interpolation.
std::array< uint8_t, HASH_SIZE > Hash
A SHA256 hash.
static std::vector< double > sread_vector(std::ifstream &stream)
Read binary representation of a vector of doubles.
static double sread_double(std::ifstream &stream)
Read binary representation of a double.
static sha256::Hash sread_hash(std::ifstream &stream)
Read binary representation of a SHA256 hash.
Extrapolation
The kind of extrapolation used by the tabulation.
static void swrite(std::ofstream &stream, double x)
Write binary representation to stream.
static size_t sread_size(std::ifstream &stream)
Read binary representation of a size_t.