1 // Copyright Nick Thompson, 2019
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #define BOOST_TEST_MODULE vector_barycentric_rational
12 #include <Eigen/Dense>
13 #include <boost/numeric/ublas/vector.hpp>
14 #include <boost/random/uniform_real_distribution.hpp>
15 #include <boost/type_index.hpp>
16 #include <boost/test/included/unit_test.hpp>
17 #include <boost/test/tools/floating_point_comparison.hpp>
18 #include <boost/math/interpolators/barycentric_rational.hpp>
19 #include <boost/math/interpolators/vector_barycentric_rational.hpp>
23 using std::numeric_limits
;
26 void test_agreement_with_1d()
28 std::cout
<< "Testing with 1D interpolation on type "
29 << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
30 std::mt19937
gen(4723);
31 boost::random::uniform_real_distribution
<Real
> dis(0.1f
, 1);
32 std::vector
<Real
> t(100);
33 std::vector
<Eigen::Vector2d
> y(100);
37 for (size_t i
= 1; i
< t
.size(); ++i
)
39 t
[i
] = t
[i
-1] + dis(gen
);
44 std::vector
<Eigen::Vector2d
> y_copy
= y
;
45 std::vector
<Real
> t_copy
= t
;
46 std::vector
<Real
> t_copy0
= t
;
47 std::vector
<Real
> t_copy1
= t
;
49 std::vector
<Real
> y_copy0(y
.size());
50 std::vector
<Real
> y_copy1(y
.size());
51 for (size_t i
= 0; i
< y
.size(); ++i
) {
56 boost::random::uniform_real_distribution
<Real
> dis2(t
[0], t
[t
.size()-1]);
57 boost::math::vector_barycentric_rational
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
));
58 boost::math::barycentric_rational
<Real
> scalar_interpolator0(std::move(t_copy0
), std::move(y_copy0
));
59 boost::math::barycentric_rational
<Real
> scalar_interpolator1(std::move(t_copy1
), std::move(y_copy1
));
65 while (samples
++ < 1000)
69 BOOST_CHECK_CLOSE(z
[0], scalar_interpolator0(t
), 10000*numeric_limits
<Real
>::epsilon());
70 BOOST_CHECK_CLOSE(z
[1], scalar_interpolator1(t
), 10000*numeric_limits
<Real
>::epsilon());
76 void test_interpolation_condition_eigen()
78 std::cout
<< "Testing interpolation condition for barycentric interpolation on Eigen vectors of type "
79 << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
80 std::mt19937
gen(4723);
81 boost::random::uniform_real_distribution
<Real
> dis(0.1f
, 1);
82 std::vector
<Real
> t(100);
83 std::vector
<Eigen::Vector2d
> y(100);
87 for (size_t i
= 1; i
< t
.size(); ++i
)
89 t
[i
] = t
[i
-1] + dis(gen
);
94 std::vector
<Eigen::Vector2d
> y_copy
= y
;
95 std::vector
<Real
> t_copy
= t
;
96 boost::math::vector_barycentric_rational
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
));
99 for (size_t i
= 0; i
< t_copy
.size(); ++i
)
101 interpolator(z
, t_copy
[i
]);
102 BOOST_CHECK_CLOSE(z
[0], y_copy
[i
][0], 100*numeric_limits
<Real
>::epsilon());
103 BOOST_CHECK_CLOSE(z
[1], y_copy
[i
][1], 100*numeric_limits
<Real
>::epsilon());
108 void test_interpolation_condition_std_array()
110 std::cout
<< "Testing interpolation condition for barycentric interpolation on std::array vectors of type "
111 << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
112 std::mt19937
gen(4723);
113 boost::random::uniform_real_distribution
<Real
> dis(0.1f
, 1);
114 std::vector
<Real
> t(100);
115 std::vector
<std::array
<Real
, 2>> y(100);
119 for (size_t i
= 1; i
< t
.size(); ++i
)
121 t
[i
] = t
[i
-1] + dis(gen
);
126 std::vector
<std::array
<Real
, 2>> y_copy
= y
;
127 std::vector
<Real
> t_copy
= t
;
128 boost::math::vector_barycentric_rational
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
));
130 std::array
<Real
, 2> z
;
131 for (size_t i
= 0; i
< t_copy
.size(); ++i
)
133 interpolator(z
, t_copy
[i
]);
134 BOOST_CHECK_CLOSE(z
[0], y_copy
[i
][0], 100*numeric_limits
<Real
>::epsilon());
135 BOOST_CHECK_CLOSE(z
[1], y_copy
[i
][1], 100*numeric_limits
<Real
>::epsilon());
141 void test_interpolation_condition_ublas()
143 std::cout
<< "Testing interpolation condition for barycentric interpolation ublas vectors of type "
144 << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
145 std::mt19937
gen(4723);
146 boost::random::uniform_real_distribution
<Real
> dis(0.1f
, 1);
147 std::vector
<Real
> t(100);
148 std::vector
<boost::numeric::ublas::vector
<Real
>> y(100);
153 for (size_t i
= 1; i
< t
.size(); ++i
)
155 t
[i
] = t
[i
-1] + dis(gen
);
161 std::vector
<Real
> t_copy
= t
;
162 std::vector
<boost::numeric::ublas::vector
<Real
>> y_copy
= y
;
164 boost::math::vector_barycentric_rational
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
));
166 boost::numeric::ublas::vector
<Real
> z(2);
167 for (size_t i
= 0; i
< t_copy
.size(); ++i
)
169 interpolator(z
, t_copy
[i
]);
170 BOOST_CHECK_CLOSE(z
[0], y_copy
[i
][0], 100*numeric_limits
<Real
>::epsilon());
171 BOOST_CHECK_CLOSE(z
[1], y_copy
[i
][1], 100*numeric_limits
<Real
>::epsilon());
176 void test_interpolation_condition_high_order()
178 std::cout
<< "Testing interpolation condition in high order for barycentric interpolation on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
180 boost::random::uniform_real_distribution
<Real
> dis(0.1f
, 1);
181 std::vector
<Real
> t(100);
182 std::vector
<Eigen::Vector2d
> y(100);
186 for (size_t i
= 1; i
< t
.size(); ++i
)
188 t
[i
] = t
[i
-1] + dis(gen
);
193 std::vector
<Eigen::Vector2d
> y_copy
= y
;
194 std::vector
<Real
> t_copy
= t
;
195 boost::math::vector_barycentric_rational
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
), 5);
198 for (size_t i
= 0; i
< t_copy
.size(); ++i
)
200 interpolator(z
, t_copy
[i
]);
201 BOOST_CHECK_CLOSE(z
[0], y_copy
[i
][0], 100*numeric_limits
<Real
>::epsilon());
202 BOOST_CHECK_CLOSE(z
[1], y_copy
[i
][1], 100*numeric_limits
<Real
>::epsilon());
208 void test_constant_eigen()
210 std::cout
<< "Testing that constants are interpolated correctly using barycentric interpolation on Eigen vectors of type "
211 << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
214 boost::random::uniform_real_distribution
<Real
> dis(0.1f
, 1);
215 std::vector
<Real
> t(100);
216 std::vector
<Eigen::Vector2d
> y(100);
218 Real constant0
= dis(gen
);
219 Real constant1
= dis(gen
);
222 for (size_t i
= 1; i
< t
.size(); ++i
)
224 t
[i
] = t
[i
-1] + dis(gen
);
229 std::vector
<Eigen::Vector2d
> y_copy
= y
;
230 std::vector
<Real
> t_copy
= t
;
231 boost::math::vector_barycentric_rational
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
));
234 for (size_t i
= 0; i
< t_copy
.size(); ++i
)
236 // Don't evaluate the constant at x[i]; that's already tested in the interpolation condition test.
237 Real t
= t_copy
[i
] + dis(gen
);
239 BOOST_CHECK_CLOSE(z
[0], constant0
, 100*sqrt(numeric_limits
<Real
>::epsilon()));
240 BOOST_CHECK_CLOSE(z
[1], constant1
, 100*sqrt(numeric_limits
<Real
>::epsilon()));
241 Eigen::Vector2d zprime
= interpolator
.prime(t
);
242 Real zero_0
= zprime
[0];
243 Real zero_1
= zprime
[1];
244 BOOST_CHECK_SMALL(zero_0
, sqrt(numeric_limits
<Real
>::epsilon()));
245 BOOST_CHECK_SMALL(zero_1
, sqrt(numeric_limits
<Real
>::epsilon()));
251 void test_constant_std_array()
253 std::cout
<< "Testing that constants are interpolated correctly using barycentric interpolation on std::array vectors of type "
254 << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
257 boost::random::uniform_real_distribution
<Real
> dis(0.1f
, 1);
258 std::vector
<Real
> t(100);
259 std::vector
<std::array
<Real
, 2>> y(100);
261 Real constant0
= dis(gen
);
262 Real constant1
= dis(gen
);
265 for (size_t i
= 1; i
< t
.size(); ++i
)
267 t
[i
] = t
[i
-1] + dis(gen
);
272 std::vector
<std::array
<Real
,2>> y_copy
= y
;
273 std::vector
<Real
> t_copy
= t
;
274 boost::math::vector_barycentric_rational
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
));
276 std::array
<Real
, 2> z
;
277 for (size_t i
= 0; i
< t_copy
.size(); ++i
)
279 // Don't evaluate the constant at x[i]; that's already tested in the interpolation condition test.
280 Real t
= t_copy
[i
] + dis(gen
);
282 BOOST_CHECK_CLOSE(z
[0], constant0
, 100*sqrt(numeric_limits
<Real
>::epsilon()));
283 BOOST_CHECK_CLOSE(z
[1], constant1
, 100*sqrt(numeric_limits
<Real
>::epsilon()));
284 std::array
<Real
, 2> zprime
= interpolator
.prime(t
);
285 Real zero_0
= zprime
[0];
286 Real zero_1
= zprime
[1];
287 BOOST_CHECK_SMALL(zero_0
, sqrt(numeric_limits
<Real
>::epsilon()));
288 BOOST_CHECK_SMALL(zero_1
, sqrt(numeric_limits
<Real
>::epsilon()));
294 void test_constant_high_order()
296 std::cout
<< "Testing that constants are interpolated correctly using barycentric interpolation on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
299 boost::random::uniform_real_distribution
<Real
> dis(0.1f
, 1);
300 std::vector
<Real
> t(100);
301 std::vector
<Eigen::Vector2d
> y(100);
303 Real constant0
= dis(gen
);
304 Real constant1
= dis(gen
);
307 for (size_t i
= 1; i
< t
.size(); ++i
)
309 t
[i
] = t
[i
-1] + dis(gen
);
314 std::vector
<Eigen::Vector2d
> y_copy
= y
;
315 std::vector
<Real
> t_copy
= t
;
316 boost::math::vector_barycentric_rational
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
), 5);
319 for (size_t i
= 0; i
< t_copy
.size(); ++i
)
321 // Don't evaluate the constant at x[i]; that's already tested in the interpolation condition test.
322 Real t
= t_copy
[i
] + dis(gen
);
324 BOOST_CHECK_CLOSE(z
[0], constant0
, 100*sqrt(numeric_limits
<Real
>::epsilon()));
325 BOOST_CHECK_CLOSE(z
[1], constant1
, 100*sqrt(numeric_limits
<Real
>::epsilon()));
326 Eigen::Vector2d zprime
= interpolator
.prime(t
);
327 Real zero_0
= zprime
[0];
328 Real zero_1
= zprime
[1];
329 BOOST_CHECK_SMALL(zero_0
, sqrt(numeric_limits
<Real
>::epsilon()));
330 BOOST_CHECK_SMALL(zero_1
, sqrt(numeric_limits
<Real
>::epsilon()));
338 std::cout
<< "Testing weights are calculated correctly using barycentric interpolation on type " << boost::typeindex::type_id
<Real
>().pretty_name() << "\n";
341 boost::random::uniform_real_distribution
<Real
> dis(0.005, 0.01);
342 std::vector
<Real
> t(100);
343 std::vector
<Eigen::Vector2d
> y(100);
347 for (size_t i
= 1; i
< t
.size(); ++i
)
349 t
[i
] = t
[i
-1] + dis(gen
);
354 std::vector
<Eigen::Vector2d
> y_copy
= y
;
355 std::vector
<Real
> t_copy
= t
;
356 boost::math::detail::vector_barycentric_rational_imp
<decltype(t
), decltype(y
)> interpolator(std::move(t
), std::move(y
), 1);
358 for (size_t i
= 1; i
< t_copy
.size() - 1; ++i
)
360 Real w
= interpolator
.weight(i
);
361 Real w_expect
= 1/(t_copy
[i
] - t_copy
[i
- 1]) + 1/(t_copy
[i
+1] - t_copy
[i
]);
364 BOOST_CHECK_CLOSE(w
, -w_expect
, 0.00001);
368 BOOST_CHECK_CLOSE(w
, w_expect
, 0.00001);
374 BOOST_AUTO_TEST_CASE(vector_barycentric_rational
)
376 test_weights
<double>();
377 test_constant_eigen
<double>();
378 test_constant_std_array
<double>();
379 test_constant_high_order
<double>();
380 test_interpolation_condition_eigen
<double>();
381 test_interpolation_condition_ublas
<double>();
382 test_interpolation_condition_std_array
<double>();
383 test_interpolation_condition_high_order
<double>();
384 test_agreement_with_1d
<double>();