/// @file grotto/piecewise.hpp /// @author Ryan Henry /// @brief /// @details /// @copyright Copyright (c) 2019-2023 Ryan Henry and others /// @license Released under a GNU General Public v2.0 (GPLv2) license; /// see [LICENSE.md](@ref GPLv2) for details. #ifndef LIBDPF_INCLUDE_GROTTO_PIECEWISE_HPP__ #define LIBDPF_INCLUDE_GROTTO_PIECEWISE_HPP__ #include #include #include namespace grotto { namespace polynomials { template using poly_constant = std::array; template using poly_linear = std::array; template using poly_quadratic = std::array; template using poly_cubic = std::array; template constexpr auto eval_horner(const poly_constant & f, T x) { return f[0]; } template constexpr auto eval_horner(const poly_linear & f, T x) { return f[1] * x + f[0]; } template constexpr auto eval_horner(const poly_quadratic & f, T x) { return (f[2] * x + f[1]) * x + f[0]; } template constexpr auto eval_horner(const poly_cubic & f, T x) { return ((f[3] * x + f[2]) * x + f[1]) * x + f[0]; } template auto piecewise_eval(const std::array, N1> & polys, const std::array & bounds, T x) { auto it = std::upper_bound(std::cbegin(bounds), std::cend(bounds), x, [](const T & lhs, const T & rhs){ return lhs < rhs; }); auto i = std::distance(std::cbegin(bounds), it); return eval_horner(polys[i], x); } } // namespace polynomials } // namespace grotto #endif // LIBDPF_INCLUDE_GROTTO_PIECEWISE_HPP__