-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyfit.hpp
197 lines (185 loc) · 6 KB
/
polyfit.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#pragma once
#ifndef SIMPLE_UNIFORM_NOISE_POLYFIT
#define SIMPLE_UNIFORM_NOISE_POLYFIT
#include <cmath>
#include <array>
#include <vector>
namespace utils {
// https://github.com/splicer/polyfit
// polyfit uses recursive least squares to perform a polynomial regression
// (i.e. it fits a polynomial to a set of data points without requiring a large buffer).
// For details on the math behind this recursive least squares implementation, see
// Gentlemen & Kung's famous 1981 paper "Matrix triangularization by systolic arrays".
template <typename value_t, uint8_t degree>
class Polyfit {
static constexpr value_t s_smallValue = 1.0E-32;
static constexpr value_t s_forgettingFactor = 1.0 - 1.0E-11;
static constexpr size_t s_rows = degree + 1;
static constexpr size_t s_cols = degree + 2;
public:
Polyfit() {
reset();
}
void reset() {
std::fill(m_cells.begin(), m_cells.end(), s_smallValue);
}
void add(const value_t x, const value_t y) {
std::array<value_t, s_cols> in;
in[0] = 1.0f;
for (size_t j = 1; j < s_cols - 1; ++j) {
in[j] = in[j - 1] * x;
}
in[s_cols - 1] = y;
for (size_t i = 0; i < s_rows; ++i) {
value_t c;
value_t s;
boundaryCell(m_cells[i * s_cols + i], c, s, in[i]);
for (size_t j = i + 1; j < s_cols; ++j) {
value_t out = internalCell(m_cells[i * s_cols + j], c, s, in[j]);
if (i < s_rows - 1) {
in[j] = out;
}
}
}
m_weightsNeedUpdate = true;
}
const std::array<value_t, s_rows>& weights() const {
computeWeights();
return m_weights;
}
value_t y(const value_t x) const {
computeWeights();
intptr_t i = s_rows - 1;
value_t y = m_weights[i];
while (i-- > 0) {
y = y * x + m_weights[i];
}
return y;
}
private:
// givens generation
static void boundaryCell(value_t& cell, value_t& c, value_t& s, value_t in) {
if (std::abs(in) < s_smallValue) {
// close enough to zero
c = 1.0f;
s = 0.0f;
}
else {
value_t norm = std::sqrt(cell * cell + in * in);
c = cell / norm;
s = in / norm;
cell = s_forgettingFactor * norm;
}
}
// givens rotation
static value_t internalCell(value_t& cell, value_t c, value_t s, value_t in) {
value_t out = c * in - s_forgettingFactor * s * cell;
cell = s * in + s_forgettingFactor * c * cell;
return out;
}
void computeWeights() const {
if (!m_weightsNeedUpdate) {
return;
}
for (intptr_t i = s_rows - 1; i >= 0; --i) {
m_weights[i] = m_cells[i * s_cols + s_cols - 1];
for (size_t j = i + 1; j < s_cols - 1; j++) {
m_weights[i] -= m_cells[i * s_cols + j] * m_weights[j];
}
m_weights[i] /= m_cells[i * s_cols + i];
}
m_weightsNeedUpdate = false;
}
std::array<value_t, s_rows * s_cols> m_cells;
mutable std::array<value_t, s_rows> m_weights;
mutable bool m_weightsNeedUpdate = false;
};
class PolyfitD {
static constexpr double m_smallValue = 1.0E-32;
static constexpr double m_forgettingFactor = 1.0 - 1.0E-11;
public:
using value_t = double;
void create(const uint8_t degree) {
m_rows = degree + 1;
m_cols = degree + 2;
m_cells.clear();
m_cells.resize(m_rows * m_cols, m_smallValue);
m_weights.clear();
m_weights.resize(m_rows);
m_in.resize(m_cols);
}
void add(const value_t x, const value_t y) {
m_in[0] = 1.0f;
for (size_t j = 1; j < m_cols - 1; ++j) {
m_in[j] = m_in[j - 1] * x;
}
m_in[m_cols - 1] = y;
for (size_t i = 0; i < m_rows; ++i) {
value_t c;
value_t s;
boundaryCell(m_cells[i * m_cols + i], c, s, m_in[i]);
for (size_t j = i + 1; j < m_cols; ++j) {
value_t out = internalCell(m_cells[i * m_cols + j], c, s, m_in[j]);
if (i < m_rows - 1) {
m_in[j] = out;
}
}
}
m_weightsNeedUpdate = true;
}
const std::vector<value_t>& weights() const {
computeWeights();
return m_weights;
}
value_t y(const value_t x) const {
computeWeights();
intptr_t i = m_rows - 1;
value_t y = m_weights[i];
while (i-- > 0) {
y = y * x + m_weights[i];
}
return y;
}
private:
// givens generation
static void boundaryCell(value_t& cell, value_t& c, value_t& s, value_t in) {
if (std::abs(in) < m_smallValue) {
// close enough to zero
c = 1.0f;
s = 0.0f;
}
else {
value_t norm = std::sqrt(cell * cell + in * in);
c = cell / norm;
s = in / norm;
cell = m_forgettingFactor * norm;
}
}
// givens rotation
static value_t internalCell(value_t& cell, value_t c, value_t s, value_t in) {
value_t out = c * in - m_forgettingFactor * s * cell;
cell = s * in + m_forgettingFactor * c * cell;
return out;
}
void computeWeights() const {
if (!m_weightsNeedUpdate) {
return;
}
for (intptr_t i = m_rows - 1; i >= 0; --i) {
m_weights[i] = m_cells[i * m_cols + m_cols - 1];
for (size_t j = i + 1; j < m_cols - 1; j++) {
m_weights[i] -= m_cells[i * m_cols + j] * m_weights[j];
}
m_weights[i] /= m_cells[i * m_cols + i];
}
m_weightsNeedUpdate = false;
}
std::vector<value_t> m_cells;
mutable std::vector<value_t> m_weights;
std::vector<value_t> m_in;
size_t m_rows = 0;
size_t m_cols = 0;
mutable bool m_weightsNeedUpdate = false;
};
} // namespace utils
#endif // SIMPLE_UNIFORM_NOISE_POLYFIT