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
|
/*
* Copyright Nick Thompson, 2020
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "math_unit_test.hpp"
#include <numeric>
#include <utility>
#include <random>
#include <boost/math/interpolators/makima.hpp>
#include <boost/circular_buffer.hpp>
#ifdef BOOST_HAS_FLOAT128
#include <boost/multiprecision/float128.hpp>
using boost::multiprecision::float128;
#endif
using boost::math::interpolators::makima;
template<typename Real>
void test_constant()
{
std::vector<Real> x{0,1,2,3, 9, 22, 81};
std::vector<Real> y(x.size());
for (auto & t : y) {
t = 7;
}
auto x_copy = x;
auto y_copy = y;
auto akima = makima(std::move(x_copy), std::move(y_copy));
for (Real t = x[0]; t <= x.back(); t += 0.25) {
CHECK_ULP_CLOSE(Real(7), akima(t), 2);
CHECK_ULP_CLOSE(Real(0), akima.prime(t), 2);
}
boost::circular_buffer<Real> x_buf(x.size());
for (auto & t : x) {
x_buf.push_back(t);
}
boost::circular_buffer<Real> y_buf(x.size());
for (auto & t : y) {
y_buf.push_back(t);
}
auto circular_akima = makima(std::move(x_buf), std::move(y_buf));
for (Real t = x[0]; t <= x.back(); t += 0.25) {
CHECK_ULP_CLOSE(Real(7), circular_akima(t), 2);
CHECK_ULP_CLOSE(Real(0), akima.prime(t), 2);
}
circular_akima.push_back(x.back() + 1, 7);
CHECK_ULP_CLOSE(Real(0), circular_akima.prime(x.back()+1), 2);
}
template<typename Real>
void test_linear()
{
std::vector<Real> x{0,1,2,3};
std::vector<Real> y{0,1,2,3};
auto x_copy = x;
auto y_copy = y;
auto akima = makima(std::move(x_copy), std::move(y_copy));
CHECK_ULP_CLOSE(y[0], akima(x[0]), 0);
CHECK_ULP_CLOSE(Real(1)/Real(2), akima(Real(1)/Real(2)), 10);
CHECK_ULP_CLOSE(y[1], akima(x[1]), 0);
CHECK_ULP_CLOSE(Real(3)/Real(2), akima(Real(3)/Real(2)), 10);
CHECK_ULP_CLOSE(y[2], akima(x[2]), 0);
CHECK_ULP_CLOSE(Real(5)/Real(2), akima(Real(5)/Real(2)), 10);
CHECK_ULP_CLOSE(y[3], akima(x[3]), 0);
x.resize(45);
y.resize(45);
for (size_t i = 0; i < x.size(); ++i) {
x[i] = i;
y[i] = i;
}
x_copy = x;
y_copy = y;
akima = makima(std::move(x_copy), std::move(y_copy));
for (Real t = 0; t < x.back(); t += 0.5) {
CHECK_ULP_CLOSE(t, akima(t), 0);
CHECK_ULP_CLOSE(Real(1), akima.prime(t), 0);
}
x_copy = x;
y_copy = y;
// Test endpoint derivatives:
akima = makima(std::move(x_copy), std::move(y_copy), Real(1), Real(1));
for (Real t = 0; t < x.back(); t += 0.5) {
CHECK_ULP_CLOSE(t, akima(t), 0);
CHECK_ULP_CLOSE(Real(1), akima.prime(t), 0);
}
boost::circular_buffer<Real> x_buf(x.size());
for (auto & t : x) {
x_buf.push_back(t);
}
boost::circular_buffer<Real> y_buf(x.size());
for (auto & t : y) {
y_buf.push_back(t);
}
auto circular_akima = makima(std::move(x_buf), std::move(y_buf));
for (Real t = x[0]; t <= x.back(); t += 0.25) {
CHECK_ULP_CLOSE(t, circular_akima(t), 2);
CHECK_ULP_CLOSE(Real(1), circular_akima.prime(t), 2);
}
circular_akima.push_back(x.back() + 1, y.back()+1);
CHECK_ULP_CLOSE(Real(y.back() + 1), circular_akima(Real(x.back()+1)), 2);
CHECK_ULP_CLOSE(Real(1), circular_akima.prime(Real(x.back()+1)), 2);
}
template<typename Real>
void test_interpolation_condition()
{
for (size_t n = 4; n < 50; ++n) {
std::vector<Real> x(n);
std::vector<Real> y(n);
std::default_random_engine rd;
std::uniform_real_distribution<Real> dis(0,1);
Real x0 = dis(rd);
x[0] = x0;
y[0] = dis(rd);
for (size_t i = 1; i < n; ++i) {
x[i] = x[i-1] + dis(rd);
y[i] = dis(rd);
}
auto x_copy = x;
auto y_copy = y;
auto s = makima(std::move(x_copy), std::move(y_copy));
//std::cout << "s = " << s << "\n";
for (size_t i = 0; i < x.size(); ++i) {
CHECK_ULP_CLOSE(y[i], s(x[i]), 2);
}
x_copy = x;
y_copy = y;
// The interpolation condition is not affected by the endpoint derivatives, even though these derivatives might be super weird:
s = makima(std::move(x_copy), std::move(y_copy), Real(0), Real(0));
//std::cout << "s = " << s << "\n";
for (size_t i = 0; i < x.size(); ++i) {
CHECK_ULP_CLOSE(y[i], s(x[i]), 2);
}
}
}
int main()
{
test_constant<float>();
test_linear<float>();
test_interpolation_condition<float>();
test_constant<double>();
test_linear<double>();
test_interpolation_condition<double>();
test_constant<long double>();
test_linear<long double>();
test_interpolation_condition<long double>();
#ifdef BOOST_HAS_FLOAT128
test_constant<float128>();
test_linear<float128>();
#endif
return boost::math::test::report_errors();
}
|