-
Notifications
You must be signed in to change notification settings - Fork 7
/
numeric_utils_tests.cc
313 lines (252 loc) · 11.5 KB
/
numeric_utils_tests.cc
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
#include <cmath>
#include <iostream>
#include <vector>
#include <catch2/catch.hpp>
#include <Eigen/Dense>
#include "numeric_utils.h"
TEST_CASE("Test correlation to covariance functionality", "[Helpers]") {
SECTION("Correlation is diagonal matrix with values of 1.0 along diagonal") {
Eigen::MatrixXd test_corr = Eigen::MatrixXd::Zero(3, 3);
test_corr(0, 0) = 1.0;
test_corr(1, 1) = 1.0;
test_corr(2, 2) = 1.0;
Eigen::MatrixXd std_dev = Eigen::VectorXd::Ones(3);
std_dev << 2.0, 3.0, 4.0;
auto cov = numeric_utils::corr_to_cov(test_corr, std_dev);
Eigen::MatrixXd expected_matrix = Eigen::MatrixXd::Zero(3, 3);
expected_matrix(0, 0) = 4.0;
expected_matrix(1, 1) = 9.0;
expected_matrix(2, 2) = 16.0;
REQUIRE(cov(0, 0) == expected_matrix(0, 0));
REQUIRE(cov(1, 1) == expected_matrix(1, 1));
REQUIRE(cov(2, 2) == expected_matrix(2, 2));
REQUIRE(expected_matrix.lpNorm<2>() == Approx(cov.lpNorm<2>()).epsilon(0.01));
}
}
TEST_CASE("Test one dimensional convolution", "[Helpers][Convolution]") {
SECTION("One dimensional convolution of vectors with length 1") {
std::vector<double> input_x{1.0};
std::vector<double> input_y{2.0};
std::vector<double> response;
bool status;
try {
status = numeric_utils::convolve_1d(input_x, input_y, response);
} catch (std::exception &exception) {
std::cout << "Convolution error: " << exception.what() << std::endl;
FAIL("One dimensional convolution function throws exception, check where "
"exception is being generated interally");
}
REQUIRE(status);
REQUIRE(response.size() == 1);
REQUIRE(response[0] == Approx(2.0).epsilon(0.01));
}
SECTION("One dimensional convolution of vectors with length 2 and 3") {
std::vector<double> input_x{3.0, 4.0, 5.0};
std::vector<double> input_y{2.0, 1.0};
std::vector<double> response(1);
bool status;
try {
status = numeric_utils::convolve_1d(input_x, input_y, response);
} catch (std::exception &exception) {
std::cout << "Convolution error: " << exception.what() << std::endl;
FAIL("One dimensional convolution function throws exception, check where "
"exception is being generated interally");
}
REQUIRE(status);
REQUIRE(response.size() == input_x.size() + input_y.size() - 1);
REQUIRE(response[0] == Approx(6.0).epsilon(0.01));
REQUIRE(response[1] == Approx(11.0).epsilon(0.01));
REQUIRE(response[2] == Approx(14.0).epsilon(0.01));
REQUIRE(response[3] == Approx(5.0).epsilon(0.01));
}
}
TEST_CASE("Test trapazoid rule", "[Helpers][Trapazoid]") {
SECTION("STL vector with unit spacing") {
std::vector<double> input_vector{1, 4, 9, 16, 25};
auto integral = numeric_utils::trapazoid_rule(input_vector, 1.0);
REQUIRE(integral == 42);
}
SECTION("STL vector with non-unit spacing") {
std::vector<double> input_vector(101, 0.0);
double accumulator = 0.0;
for (unsigned int i = 1; i < input_vector.size(); ++i) {
accumulator += M_PI / 100.0;
input_vector[i] = std::sin(accumulator);
}
auto integral = numeric_utils::trapazoid_rule(input_vector, M_PI / 100.0);
REQUIRE(integral == Approx(1.9998).epsilon(0.01));
}
SECTION("Eigen vector with unit spacing") {
Eigen::VectorXd input_vector(5);
input_vector << 1, 4, 9, 16, 25;
auto integral = numeric_utils::trapazoid_rule(input_vector, 1.0);
REQUIRE(integral == 42);
}
SECTION("Eigen vector with non-unit spacing") {
Eigen::VectorXd input_vector = Eigen::VectorXd::Zero(101);
double accumulator = 0.0;
for (unsigned int i = 1; i < input_vector.size(); ++i) {
accumulator += M_PI / 100.0;
input_vector[i] = std::sin(accumulator);
}
auto integral = numeric_utils::trapazoid_rule(input_vector, M_PI / 100.0);
REQUIRE(integral == Approx(1.9998).epsilon(0.01));
}
}
TEST_CASE("Test 1-D inverse Fast Fourier Transform", "[Helpers][FFT]") {
SECTION("Calculate real portion of one-dimesional inverse FFT") {
std::vector<std::complex<double>> input_vector = {
{15.0, 0.0},
{-2.5, 3.440954801177933},
{-2.5, 0.812299240582266},
{-2.5, -0.812299240582266},
{-2.5, -3.440954801177933}};
std::vector<double> output_vector(4);
auto status = numeric_utils::inverse_fft(input_vector, output_vector);
REQUIRE(status);
REQUIRE(output_vector[0] == Approx(1.0).epsilon(0.01));
REQUIRE(output_vector[1] == Approx(2.0).epsilon(0.01));
REQUIRE(output_vector[2] == Approx(3.0).epsilon(0.01));
REQUIRE(output_vector[3] == Approx(4.0).epsilon(0.01));
REQUIRE(output_vector[4] == Approx(5.0).epsilon(0.01));
}
SECTION("Calculate real portion of one-dimesional inverse FFT") {
Eigen::VectorXcd input_vector(5);
input_vector << std::complex<double>(15.0, 0.0),
std::complex<double>(-2.5, 3.440954801177933),
std::complex<double>(-2.5, 0.812299240582266),
std::complex<double>(-2.5, -0.812299240582266),
std::complex<double>(-2.5, -3.440954801177933);
Eigen::VectorXd output_vector;
auto status = numeric_utils::inverse_fft(input_vector, output_vector);
REQUIRE(status);
REQUIRE(output_vector[0] == Approx(1.0).epsilon(0.01));
REQUIRE(output_vector[1] == Approx(2.0).epsilon(0.01));
REQUIRE(output_vector[2] == Approx(3.0).epsilon(0.01));
REQUIRE(output_vector[3] == Approx(4.0).epsilon(0.01));
REQUIRE(output_vector[4] == Approx(5.0).epsilon(0.01));
}
}
TEST_CASE("Test 1-D Fast Fourier Transform", "[Helpers][FFT]") {
SECTION("Calculate one-dimesional FFT") {
std::vector<double> input_vector = {3.0, 1.0, 0.0, 0.0};
std::vector<std::complex<double>> output_vector(4);
auto status = numeric_utils::fft(input_vector, output_vector);
REQUIRE(status);
REQUIRE(real(output_vector[0]) == Approx(4.0).epsilon(0.01));
REQUIRE(imag(output_vector[0]) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(real(output_vector[1]) == Approx(3.0).epsilon(0.01));
REQUIRE(imag(output_vector[1]) == Approx(-1.0).epsilon(0.01));
REQUIRE(real(output_vector[2]) == Approx(2.0).epsilon(0.01));
REQUIRE(imag(output_vector[2]) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(real(output_vector[3]) == Approx(3.0).epsilon(0.01));
REQUIRE(imag(output_vector[3]) == Approx(1.0).epsilon(0.01));
}
SECTION("Calculate one-dimesional FFT") {
Eigen::VectorXd input_vector(4);
input_vector << 3.0, 1.0, 0.0, 0.0;
std::vector<std::complex<double>> output_vector(4);
auto status = numeric_utils::fft(input_vector, output_vector);
REQUIRE(status);
REQUIRE(real(output_vector[0]) == Approx(4.0).epsilon(0.01));
REQUIRE(imag(output_vector[0]) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(real(output_vector[1]) == Approx(3.0).epsilon(0.01));
REQUIRE(imag(output_vector[1]) == Approx(-1.0).epsilon(0.01));
REQUIRE(real(output_vector[2]) == Approx(2.0).epsilon(0.01));
REQUIRE(imag(output_vector[2]) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(real(output_vector[3]) == Approx(3.0).epsilon(0.01));
REQUIRE(imag(output_vector[3]) == Approx(1.0).epsilon(0.01));
}
SECTION("Calculate one-dimesional FFT") {
Eigen::VectorXd input_vector(4);
input_vector << 3.0, 1.0, 0.0, 0.0;
Eigen::VectorXcd output_vector(4);
auto status = numeric_utils::fft(input_vector, output_vector);
REQUIRE(status);
REQUIRE(real(output_vector(0)) == Approx(4.0).epsilon(0.01));
REQUIRE(imag(output_vector(0)) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(real(output_vector(1)) == Approx(3.0).epsilon(0.01));
REQUIRE(imag(output_vector(1)) == Approx(-1.0).epsilon(0.01));
REQUIRE(real(output_vector(2)) == Approx(2.0).epsilon(0.01));
REQUIRE(imag(output_vector(2)) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(real(output_vector(3)) == Approx(3.0).epsilon(0.01));
REQUIRE(imag(output_vector(3)) == Approx(1.0).epsilon(0.01));
}
}
TEST_CASE("Test polynomial curve fitting, derivatives, and evaluation",
"[Helpers][Polynomial]") {
SECTION("Fit polynomial with non-zero intercept--should be degree 0") {
Eigen::VectorXd points(4);
points << 1.0, 2.0, 3.0, 4.0;
Eigen::VectorXd data(4);
data << 4.0, 4.0, 4.0, 4.0;
auto poly_coeffs = numeric_utils::polyfit_intercept(points, data, 4.0, 3);
REQUIRE(poly_coeffs(0) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(poly_coeffs(1) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(poly_coeffs(2) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(poly_coeffs(3) == Approx(4.0).epsilon(0.01));
}
SECTION("Fit polynomial with non-zero intercept--should be degree 0") {
Eigen::VectorXd points(4);
points << 1.0, 2.0, 3.0, 4.0;
Eigen::VectorXd data(4);
data << 4.0, 4.0, 4.0, 4.0;
auto poly_coeffs = numeric_utils::polyfit_intercept(points, data, 4.0, 2);
REQUIRE(poly_coeffs(0) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(poly_coeffs(1) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(poly_coeffs(2) == Approx(4.0).epsilon(0.01));
}
SECTION("Fit polynomial with zero intercept--should be degree 3") {
Eigen::VectorXd points(4);
points << 1.0, 2.0, 3.0, 4.0;
Eigen::VectorXd data(4);
data << 1.0, 8.0, 27.0, 64.0;
auto poly_coeffs = numeric_utils::polyfit_intercept(points, data, 0.0, 3);
REQUIRE(poly_coeffs(0) == Approx(1.0).epsilon(0.01));
REQUIRE(poly_coeffs(1) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(poly_coeffs(2) + 1.0 == Approx(1.0).epsilon(0.01));
REQUIRE(poly_coeffs(3) + 1.0 == Approx(1.0).epsilon(0.01));
}
SECTION("Take derivative of polynomial") {
Eigen::VectorXd coefficients(4);
coefficients << 2.0, 2.0, 2.0, 2.0;
auto derivs = numeric_utils::polynomial_derivative(coefficients);
REQUIRE(coefficients.size() - 1 == derivs.size());
REQUIRE(derivs(0) == Approx(6.0).epsilon(0.01));
REQUIRE(derivs(1) == Approx(4.0).epsilon(0.01));
REQUIRE(derivs(2) == Approx(2.0).epsilon(0.01));
}
SECTION("Evaluate polynomial") {
Eigen::VectorXd coefficients(4);
coefficients << 2.0, 2.0, 2.0, 2.0;
Eigen::VectorXd points(4);
points << 1.0, 2.0, 3.0, 4.0;
auto evaluations = numeric_utils::evaluate_polynomial(coefficients, points);
REQUIRE(points.size() == evaluations.size());
REQUIRE(evaluations(0) == Approx(8.0).epsilon(0.01));
REQUIRE(evaluations(1) == Approx(30.0).epsilon(0.01));
REQUIRE(evaluations(2) == Approx(80.0).epsilon(0.01));
REQUIRE(evaluations(3) == Approx(170.0).epsilon(0.01));
}
SECTION("Evaluate polynomial") {
Eigen::VectorXd coefficients(4);
coefficients << 2.0, 2.0, 2.0, 2.0;
std::vector<double> points = {1.0, 2.0, 3.0, 4.0};
auto evaluations = numeric_utils::evaluate_polynomial(coefficients, points);
REQUIRE(points.size() == evaluations.size());
REQUIRE(evaluations(0) == Approx(8.0).epsilon(0.01));
REQUIRE(evaluations(1) == Approx(30.0).epsilon(0.01));
REQUIRE(evaluations(2) == Approx(80.0).epsilon(0.01));
REQUIRE(evaluations(3) == Approx(170.0).epsilon(0.01));
}
SECTION("Evaluate polynomial") {
std::vector<double> coefficients = {2.0, 2.0, 2.0, 2.0};
std::vector<double> points = {1.0, 2.0, 3.0, 4.0};
auto evaluations = numeric_utils::evaluate_polynomial(coefficients, points);
REQUIRE(points.size() == evaluations.size());
REQUIRE(evaluations[0] == Approx(8.0).epsilon(0.01));
REQUIRE(evaluations[1] == Approx(30.0).epsilon(0.01));
REQUIRE(evaluations[2] == Approx(80.0).epsilon(0.01));
REQUIRE(evaluations[3] == Approx(170.0).epsilon(0.01));
}
}