src/tools/tabulated_pow.h
Utility to perform fast pow interpolation from tabulated data. More…
Namespaces
Name |
---|
allpix Helper class to hold support layers for a detector model. |
Classes
Name | |
---|---|
class | allpix::TabulatedPow Class to pre-calculate powers of a fixed exponent within a defined range. |
Detailed Description
Utility to perform fast pow interpolation from tabulated data.
Copyright: Copyright (c) 2023-2024 CERN and the Allpix Squared authors. This software is distributed under the terms of the MIT License, copied verbatim in the file “LICENSE.md”. In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction. SPDX-License-Identifier: MIT
Source code
#ifndef ALLPIX_TABULATED_POW_H
#define ALLPIX_TABULATED_POW_H
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
namespace allpix {
template <size_t S> class TabulatedPow {
private:
// Tabulated pow values
std::array<double, S> table_;
double x_min_;
double dx_;
public:
TabulatedPow(double min, double max, double y) : x_min_(min), dx_((max - min) / static_cast<double>(S - 1)) {
static_assert(S >= 3, "Lookup table needs at least three bins");
assert(min < max);
// Generate lookup table:
for(size_t idx = 0; idx < S; ++idx) {
double x = dx_ * static_cast<double>(idx) + x_min_;
table_[idx] = std::pow(x, y);
}
}
inline double operator()(double x) const noexcept {
// Calculate position on pre-calculate table
double pos = (x - x_min_) / dx_;
// Calculate left index by truncation to integer, clamping to pre-calculated range
size_t idx = std::clamp(static_cast<size_t>(pos), 0ul, S - 2);
// Linear interpolation between left and right bin
double tmp = pos - static_cast<double>(idx);
return table_[idx] * (1 - tmp) + tmp * table_[idx + 1];
};
};
} // namespace allpix
#endif /* ALLPIX_TABULATED_POW_H */
Updated on 2024-12-13 at 08:31:37 +0000