HighMap library (C++)
Loading...
Searching...
No Matches
chebyshev.hpp
Go to the documentation of this file.
1/* Copyright (c) 2026 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
10#pragma once
11#include <cmath>
12#include <vector>
13
14#include <cassert>
15
16namespace hmap
17{
18
31{
32public:
33 using value_type = float;
34
38 ChebyshevEvaluator() = default;
39
44 explicit ChebyshevEvaluator(std::vector<value_type> coefficients)
45 : m_coefficients(std::move(coefficients))
46 {
47 }
48
53 void set_coefficients(std::vector<value_type> coefficients)
54 {
55 m_coefficients = std::move(coefficients);
56 }
57
66 [[nodiscard]]
67 inline value_type evaluate(value_type x) const noexcept
68 {
69 const int n = static_cast<int>(m_coefficients.size());
70
71 if (n == 0) return value_type(0);
72
73 if (n == 1) return m_coefficients[0];
74
75 value_type b_kplus1 = 0.0;
76 value_type b_kplus2 = 0.0;
77
78 const value_type two_x = 2.0 * x;
79
80 for (int k = n - 1; k >= 1; --k)
81 {
82 const value_type b_k = m_coefficients[k] + two_x * b_kplus1 - b_kplus2;
83
84 b_kplus2 = b_kplus1;
85 b_kplus1 = b_k;
86 }
87
88 return m_coefficients[0] + x * b_kplus1 - b_kplus2;
89 }
90
101 template <typename InputIt, typename OutputIt>
102 inline void evaluate_batch(InputIt begin,
103 InputIt end,
104 OutputIt out) const noexcept
105 {
106 for (; begin != end; ++begin, ++out)
107 {
108 *out = evaluate(*begin);
109 }
110 }
111
116 [[nodiscard]]
117 inline int degree() const noexcept
118 {
119 return static_cast<int>(m_coefficients.size()) - 1;
120 }
121
126 [[nodiscard]]
127 inline const std::vector<value_type> &coefficients() const noexcept
128 {
129 return m_coefficients;
130 }
131
132private:
133 std::vector<value_type> m_coefficients;
134};
135
136} // namespace hmap
Evaluates a Chebyshev polynomial series using Clenshaw recurrence.
Definition chebyshev.hpp:31
value_type evaluate(value_type x) const noexcept
Evaluate the polynomial series at x.
Definition chebyshev.hpp:67
ChebyshevEvaluator()=default
Default constructor.
void set_coefficients(std::vector< value_type > coefficients)
Set Chebyshev coefficients.
Definition chebyshev.hpp:53
const std::vector< value_type > & coefficients() const noexcept
Get series coefficients.
Definition chebyshev.hpp:127
void evaluate_batch(InputIt begin, InputIt end, OutputIt out) const noexcept
Evaluate the series on a range of input values.
Definition chebyshev.hpp:102
ChebyshevEvaluator(std::vector< value_type > coefficients)
Construct from Chebyshev coefficients.
Definition chebyshev.hpp:44
int degree() const noexcept
Get polynomial degree.
Definition chebyshev.hpp:117
float value_type
Definition chebyshev.hpp:33
Definition algebra.hpp:23