15 std::function<
double(
double)> f)
16 : x_min_(x_min), x_max_(x_min + range), inv_dx_(num / range) {
18 throw std::runtime_error(
"Tabulation needs at least two values");
21 const double dx = range / num;
22 for (
size_t i = 0; i <= num; i++) {
31 const unsigned int n =
32 numeric_cast<unsigned int>(std::floor((x -
x_min_) *
inv_dx_ + 0.5));
45 switch (extrapolation_type_for_x_beyond_xmax) {
59 throw std::invalid_argument(
60 "The provided extrapolation type is not supported. Valid types are "
61 "'Zero', 'Constant', and 'Linear'.");
71 static void swrite(std::ofstream& stream,
double x) {
72 stream.write(
reinterpret_cast<const char*
>(&x),
sizeof(x));
83 stream.read(
reinterpret_cast<char*
>(&x),
sizeof(x));
93 static void swrite(std::ofstream& stream,
size_t x) {
96 const auto const_size_x =
static_cast<uint64_t
>(x);
97 stream.write(
reinterpret_cast<const char*
>(&const_size_x),
98 sizeof(const_size_x));
109 stream.read(
reinterpret_cast<char*
>(&x),
sizeof(x));
110 if (x > std::numeric_limits<size_t>::max()) {
111 throw std::runtime_error(
"trying to read vector larger than supported");
122 static void swrite(std::ofstream& stream,
const std::vector<double> x) {
125 stream.write(
reinterpret_cast<const char*
>(x.data()),
126 sizeof(x[0]) * x.size());
138 std::vector<double> x;
140 stream.read(
reinterpret_cast<char*
>(x.data()),
sizeof(
double) *
n);
152 stream.write(
reinterpret_cast<const char*
>(x.data()),
153 sizeof(x[0]) * x.size());
164 stream.read(
reinterpret_cast<char*
>(x.data()), x.size());
179 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
double get_value_linear(double x, ExtrapolationType extrapolation=ExtrapolationType::Linear) const
Look up a value from the tabulation using linear interpolation.
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 linear_approximation_(double x) const
Linear approximation to interpolate and extrapolate tabulations.
double get_value_step(double x) const
Look up a value from the tabulation (without any interpolation, simply using the closest tabulated va...
ExtrapolationType
Allows to specify the desired extrapolation type.
@ Linear
Extrapolate using a linear approach.
@ Constant
Extrapolate using a constant value.
@ Zero
Extrapolate with zero.
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.
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.