-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcsaps.cpp
More file actions
333 lines (247 loc) · 8.26 KB
/
Copy pathcsaps.cpp
File metadata and controls
333 lines (247 loc) · 8.26 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <cmath>
#include <algorithm>
#include <stdexcept>
#include "csaps.h"
namespace csaps
{
DoubleArray Diff(const DoubleArray &vec)
{
const Size n = vec.size() - 1;
return vec.tail(n) - vec.head(n);
}
IndexArray Digitize(const DoubleArray &arr, const DoubleArray &bins)
{
// This code works if `arr` and `bins` are monotonically increasing
IndexArray indexes = IndexArray::Ones(arr.size());
auto IsInsideBin = [arr, bins](Index item, Index index)
{
const double prc = 1.e-8;
double a = arr(item);
double bl = bins(index - 1);
double br = bins(index);
// bins[i-1] <= a < bins[i]
return (a > bl || std::abs(a - bl) < std::abs(std::min(a, bl)) * prc) && a < br;
};
Index kstart = 1;
for (Index i = 0; i < arr.size(); ++i) {
for (Index k = kstart; k < bins.size(); ++k) {
if (IsInsideBin(i, k)) {
indexes(i) = k;
kstart = k;
break;
}
}
}
return indexes;
}
DoubleSparseMatrix MakeSparseDiagMatrix(const DoubleArray2D& diags, const IndexArray& offsets, Size rows, Size cols)
{
auto GetNumElemsAndIndex = [rows, cols](Index offset, Index &i, Index &j)
{
if (offset < 0) {
i = -offset;
j = 0;
}
else {
i = 0;
j = offset;
}
return std::min(rows - i, cols - j);
};
DoubleSparseMatrix m(rows, cols);
for (Index k = 0; k < offsets.size(); ++k) {
Index offset = offsets(k);
Index i, j;
Index n = GetNumElemsAndIndex(offset, i, j);
// When rows == cols or rows > cols, the function takes elements of the
// super-diagonal from the lower part of the corresponding diag array, and
// elements of the sub-diagonal from the upper part of the corresponding diag array.
//
// When rows < cols, the function does the opposite, taking elements of the
// super-diagonal from the upper part of the corresponding diag array, and
// elements of the sub-diagonal from the lower part of the corresponding diag array.
DoubleArray diag(n);
if (offset < 0) {
if (rows >= cols) {
diag = diags.row(k).head(n);
}
else {
diag = diags.row(k).tail(n);
}
}
else {
if (rows >= cols) {
diag = diags.row(k).tail(n);
}
else {
diag = diags.row(k).head(n);
}
}
for (Index l = 0; l < n; ++l) {
m.insert(i+l, j+l) = diag(l);
}
}
return m;
}
csaps::DoubleArray SolveLinearSystem(const DoubleSparseMatrix &A, const DoubleArray &b)
{
Eigen::SparseLU<DoubleSparseMatrix> solver;
// Compute the ordering permutation vector from the structural pattern of A
solver.analyzePattern(A);
// Compute the numerical factorization
solver.factorize(A);
// Use the factors to solve the linear system
DoubleArray x = solver.solve(b.matrix()).array();
return x;
}
UnivariateCubicSmoothingSpline::UnivariateCubicSmoothingSpline(const DoubleArray &xdata, const DoubleArray &ydata)
: UnivariateCubicSmoothingSpline(xdata, ydata, DoubleArray(), -1.0)
{
}
UnivariateCubicSmoothingSpline::UnivariateCubicSmoothingSpline(const DoubleArray &xdata, const DoubleArray &ydata, const DoubleArray &weights)
: UnivariateCubicSmoothingSpline(xdata, ydata, weights, -1.0)
{
}
UnivariateCubicSmoothingSpline::UnivariateCubicSmoothingSpline(const DoubleArray &xdata, const DoubleArray &ydata, double smooth)
: UnivariateCubicSmoothingSpline(xdata, ydata, DoubleArray(), smooth)
{
}
UnivariateCubicSmoothingSpline::UnivariateCubicSmoothingSpline(const DoubleArray &xdata, const DoubleArray &ydata, const DoubleArray &weights, double smooth)
: m_xdata(xdata)
, m_ydata(ydata)
, m_weights(weights)
, m_smooth(smooth)
{
if (m_xdata.size() < 2) {
throw std::runtime_error("There must be at least 2 data points");
}
if (m_weights.size() == 0) {
m_weights = DoubleArray::Constant(m_xdata.size(), 1.0);
}
if (m_smooth > 1.0) {
throw std::runtime_error("Smoothing parameter must be less than or equal 1.0");
}
if (m_xdata.size() != m_ydata.size() || m_xdata.size() != m_weights.size()) {
throw std::runtime_error("Lenghts of the input data vectors are not equal");
}
MakeSpline();
}
DoubleArray UnivariateCubicSmoothingSpline::operator()(const DoubleArray &xidata)
{
if (xidata.size() < 2) {
throw std::runtime_error("There must be at least 2 data points");
}
return Evaluate(xidata);
}
DoubleArray UnivariateCubicSmoothingSpline::operator()(const Size pcount, DoubleArray &xidata)
{
if (pcount < 2) {
throw std::runtime_error("There must be at least 2 data points");
}
xidata.resize(pcount);
xidata << DoubleArray::LinSpaced(pcount, m_xdata(0), m_xdata(m_xdata.size()-1));
return Evaluate(xidata);
}
void UnivariateCubicSmoothingSpline::MakeSpline()
{
const Size pcount = m_xdata.size();
const Size pcount_m1 = pcount - 1;
const Size pcount_m2 = pcount - 2;
DoubleArray dx = Diff(m_xdata);
DoubleArray dy = Diff(m_ydata);
DoubleArray divdydx = dy / dx;
double p = m_smooth;
if (pcount > 2) {
// Create diagonal sparse matrices
const Size n = dx.size() - 1;
DoubleArray2D diags(3, n);
DoubleArray head_r = dx.head(n);
DoubleArray tail_r = dx.tail(n);
diags.row(0) = tail_r;
diags.row(1) = 2 * (tail_r + head_r);
diags.row(2) = head_r;
IndexArray offsets(3);
offsets << -1, 0, 1;
DoubleSparseMatrix r = MakeSparseDiagMatrix(diags, offsets, pcount_m2, pcount_m2);
DoubleArray odx = 1. / dx;
DoubleArray head_qt = odx.head(n);
DoubleArray tail_qt = odx.tail(n);
diags.row(0) = head_qt;
diags.row(1) = -(tail_qt + head_qt);
diags.row(2) = tail_qt;
offsets << 0, 1, 2;
DoubleSparseMatrix qt = MakeSparseDiagMatrix(diags, offsets, pcount_m2, pcount);
DoubleArray ow = 1. / m_weights;
DoubleArray osqw = 1. / m_weights.sqrt();
offsets.resize(1);
offsets << 0;
DoubleSparseMatrix w = MakeSparseDiagMatrix(ow.transpose(), offsets, pcount, pcount);
DoubleSparseMatrix qw = MakeSparseDiagMatrix(osqw.transpose(), offsets, pcount, pcount);
DoubleSparseMatrix qtw = qt * qw;
DoubleSparseMatrix qtwq = qtw * qtw.transpose();
auto Trace = [](const DoubleSparseMatrix &m)
{
return m.diagonal().sum();
};
double p = m_smooth;
if (p < 0) {
p = 1. / (1. + Trace(r) / (6. * Trace(qtwq)));
}
DoubleSparseMatrix A = ((6. * (1. - p)) * qtwq) + (p * r);
A.makeCompressed();
DoubleArray b = Diff(divdydx);
// Solve linear system Ab = u
DoubleArray u = SolveLinearSystem(A, b);
DoubleArray d1 = DoubleArray::Zero(u.size() + 2);
d1.segment(1, u.size()) = u; d1 = Diff(d1) / dx;
DoubleArray d2 = DoubleArray::Zero(d1.size() + 2);
d2.segment(1, d1.size()) = d1; d2 = Diff(d2);
DoubleArray yi = m_ydata - ((6. * (1. - p)) * w * d2.matrix()).array();
DoubleArray c3 = DoubleArray::Zero(u.size() + 2);
c3.segment(1, u.size()) = p * u;
DoubleArray c2 = Diff(yi) / dx - dx * (2. * c3.head(pcount_m1) + c3.tail(pcount_m1));
m_coeffs.resize(pcount_m1, 4);
m_coeffs.col(0) = Diff(c3) / dx;
m_coeffs.col(1) = 3. * c3.head(pcount_m1);
m_coeffs.col(2) = c2;
m_coeffs.col(3) = yi.head(pcount_m1);
}
else {
p = 1.0;
m_coeffs.resize(1, 2);
m_coeffs(0, 0) = divdydx(0);
m_coeffs(0, 1) = m_ydata(0);
}
m_smooth = p;
}
DoubleArray UnivariateCubicSmoothingSpline::Evaluate(const DoubleArray & xidata)
{
const Size x_size = m_xdata.size();
DoubleArray mesh = m_xdata.segment(1, x_size - 2);
DoubleArray edges(x_size);
edges(0) = -DoubleLimits::infinity();
edges.segment(1, x_size - 2) = mesh;
edges(x_size - 1) = DoubleLimits::infinity();
IndexArray indexes = Digitize(xidata, edges);
// Use 0 as the start index
indexes -= 1;
const Size xi_size = xidata.size();
DoubleArray xidata_loc(xi_size);
DoubleArray yidata(xi_size);
for (Index i = 0; i < xi_size; ++i) {
Index index = indexes(i);
// Go to local coordinates
xidata_loc(i) = xidata(i) - m_xdata(index);
// Initial values
yidata(i) = m_coeffs(index, 0);
}
DoubleArray coeffs(xi_size);
for (Index i = 1; i < m_coeffs.cols(); ++i) {
for (Index k = 0; k < xi_size; ++k) {
coeffs(k) = m_coeffs(indexes(k), i);
}
yidata = xidata_loc * yidata + coeffs;
}
return yidata;
}
} // namespace csaps