test_vector_barycentric_rational.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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)
  6. #define BOOST_TEST_MODULE vector_barycentric_rational
  7. #include <cmath>
  8. #include <random>
  9. #include <array>
  10. #include <Eigen/Dense>
  11. #include <boost/numeric/ublas/vector.hpp>
  12. #include <boost/random/uniform_real_distribution.hpp>
  13. #include <boost/type_index.hpp>
  14. #include <boost/test/included/unit_test.hpp>
  15. #include <boost/test/tools/floating_point_comparison.hpp>
  16. #include <boost/math/interpolators/barycentric_rational.hpp>
  17. #include <boost/math/interpolators/vector_barycentric_rational.hpp>
  18. using std::sqrt;
  19. using std::abs;
  20. using std::numeric_limits;
  21. template<class Real>
  22. void test_agreement_with_1d()
  23. {
  24. std::cout << "Testing with 1D interpolation on type "
  25. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  26. std::mt19937 gen(4723);
  27. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  28. std::vector<Real> t(100);
  29. std::vector<Eigen::Vector2d> y(100);
  30. t[0] = dis(gen);
  31. y[0][0] = dis(gen);
  32. y[0][1] = dis(gen);
  33. for (size_t i = 1; i < t.size(); ++i)
  34. {
  35. t[i] = t[i-1] + dis(gen);
  36. y[i][0] = dis(gen);
  37. y[i][1] = dis(gen);
  38. }
  39. std::vector<Eigen::Vector2d> y_copy = y;
  40. std::vector<Real> t_copy = t;
  41. std::vector<Real> t_copy0 = t;
  42. std::vector<Real> t_copy1 = t;
  43. std::vector<Real> y_copy0(y.size());
  44. std::vector<Real> y_copy1(y.size());
  45. for (size_t i = 0; i < y.size(); ++i) {
  46. y_copy0[i] = y[i][0];
  47. y_copy1[i] = y[i][1];
  48. }
  49. boost::random::uniform_real_distribution<Real> dis2(t[0], t[t.size()-1]);
  50. boost::math::vector_barycentric_rational<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y));
  51. boost::math::barycentric_rational<Real> scalar_interpolator0(std::move(t_copy0), std::move(y_copy0));
  52. boost::math::barycentric_rational<Real> scalar_interpolator1(std::move(t_copy1), std::move(y_copy1));
  53. Eigen::Vector2d z;
  54. size_t samples = 0;
  55. while (samples++ < 1000)
  56. {
  57. Real t = dis2(gen);
  58. interpolator(z, t);
  59. BOOST_CHECK_CLOSE(z[0], scalar_interpolator0(t), 10000*numeric_limits<Real>::epsilon());
  60. BOOST_CHECK_CLOSE(z[1], scalar_interpolator1(t), 10000*numeric_limits<Real>::epsilon());
  61. }
  62. }
  63. template<class Real>
  64. void test_interpolation_condition_eigen()
  65. {
  66. std::cout << "Testing interpolation condition for barycentric interpolation on Eigen vectors of type "
  67. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  68. std::mt19937 gen(4723);
  69. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  70. std::vector<Real> t(100);
  71. std::vector<Eigen::Vector2d> y(100);
  72. t[0] = dis(gen);
  73. y[0][0] = dis(gen);
  74. y[0][1] = dis(gen);
  75. for (size_t i = 1; i < t.size(); ++i)
  76. {
  77. t[i] = t[i-1] + dis(gen);
  78. y[i][0] = dis(gen);
  79. y[i][1] = dis(gen);
  80. }
  81. std::vector<Eigen::Vector2d> y_copy = y;
  82. std::vector<Real> t_copy = t;
  83. boost::math::vector_barycentric_rational<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y));
  84. Eigen::Vector2d z;
  85. for (size_t i = 0; i < t_copy.size(); ++i)
  86. {
  87. interpolator(z, t_copy[i]);
  88. BOOST_CHECK_CLOSE(z[0], y_copy[i][0], 100*numeric_limits<Real>::epsilon());
  89. BOOST_CHECK_CLOSE(z[1], y_copy[i][1], 100*numeric_limits<Real>::epsilon());
  90. }
  91. }
  92. template<class Real>
  93. void test_interpolation_condition_std_array()
  94. {
  95. std::cout << "Testing interpolation condition for barycentric interpolation on std::array vectors of type "
  96. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  97. std::mt19937 gen(4723);
  98. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  99. std::vector<Real> t(100);
  100. std::vector<std::array<Real, 2>> y(100);
  101. t[0] = dis(gen);
  102. y[0][0] = dis(gen);
  103. y[0][1] = dis(gen);
  104. for (size_t i = 1; i < t.size(); ++i)
  105. {
  106. t[i] = t[i-1] + dis(gen);
  107. y[i][0] = dis(gen);
  108. y[i][1] = dis(gen);
  109. }
  110. std::vector<std::array<Real, 2>> y_copy = y;
  111. std::vector<Real> t_copy = t;
  112. boost::math::vector_barycentric_rational<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y));
  113. std::array<Real, 2> z;
  114. for (size_t i = 0; i < t_copy.size(); ++i)
  115. {
  116. interpolator(z, t_copy[i]);
  117. BOOST_CHECK_CLOSE(z[0], y_copy[i][0], 100*numeric_limits<Real>::epsilon());
  118. BOOST_CHECK_CLOSE(z[1], y_copy[i][1], 100*numeric_limits<Real>::epsilon());
  119. }
  120. }
  121. template<class Real>
  122. void test_interpolation_condition_ublas()
  123. {
  124. std::cout << "Testing interpolation condition for barycentric interpolation ublas vectors of type "
  125. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  126. std::mt19937 gen(4723);
  127. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  128. std::vector<Real> t(100);
  129. std::vector<boost::numeric::ublas::vector<Real>> y(100);
  130. t[0] = dis(gen);
  131. y[0].resize(2);
  132. y[0][0] = dis(gen);
  133. y[0][1] = dis(gen);
  134. for (size_t i = 1; i < t.size(); ++i)
  135. {
  136. t[i] = t[i-1] + dis(gen);
  137. y[i].resize(2);
  138. y[i][0] = dis(gen);
  139. y[i][1] = dis(gen);
  140. }
  141. std::vector<Real> t_copy = t;
  142. std::vector<boost::numeric::ublas::vector<Real>> y_copy = y;
  143. boost::math::vector_barycentric_rational<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y));
  144. boost::numeric::ublas::vector<Real> z(2);
  145. for (size_t i = 0; i < t_copy.size(); ++i)
  146. {
  147. interpolator(z, t_copy[i]);
  148. BOOST_CHECK_CLOSE(z[0], y_copy[i][0], 100*numeric_limits<Real>::epsilon());
  149. BOOST_CHECK_CLOSE(z[1], y_copy[i][1], 100*numeric_limits<Real>::epsilon());
  150. }
  151. }
  152. template<class Real>
  153. void test_interpolation_condition_high_order()
  154. {
  155. std::cout << "Testing interpolation condition in high order for barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  156. std::mt19937 gen(5);
  157. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  158. std::vector<Real> t(100);
  159. std::vector<Eigen::Vector2d> y(100);
  160. t[0] = dis(gen);
  161. y[0][0] = dis(gen);
  162. y[0][1] = dis(gen);
  163. for (size_t i = 1; i < t.size(); ++i)
  164. {
  165. t[i] = t[i-1] + dis(gen);
  166. y[i][0] = dis(gen);
  167. y[i][1] = dis(gen);
  168. }
  169. std::vector<Eigen::Vector2d> y_copy = y;
  170. std::vector<Real> t_copy = t;
  171. boost::math::vector_barycentric_rational<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y), 5);
  172. Eigen::Vector2d z;
  173. for (size_t i = 0; i < t_copy.size(); ++i)
  174. {
  175. interpolator(z, t_copy[i]);
  176. BOOST_CHECK_CLOSE(z[0], y_copy[i][0], 100*numeric_limits<Real>::epsilon());
  177. BOOST_CHECK_CLOSE(z[1], y_copy[i][1], 100*numeric_limits<Real>::epsilon());
  178. }
  179. }
  180. template<class Real>
  181. void test_constant_eigen()
  182. {
  183. std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on Eigen vectors of type "
  184. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  185. std::mt19937 gen(6);
  186. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  187. std::vector<Real> t(100);
  188. std::vector<Eigen::Vector2d> y(100);
  189. t[0] = dis(gen);
  190. Real constant0 = dis(gen);
  191. Real constant1 = dis(gen);
  192. y[0][0] = constant0;
  193. y[0][1] = constant1;
  194. for (size_t i = 1; i < t.size(); ++i)
  195. {
  196. t[i] = t[i-1] + dis(gen);
  197. y[i][0] = constant0;
  198. y[i][1] = constant1;
  199. }
  200. std::vector<Eigen::Vector2d> y_copy = y;
  201. std::vector<Real> t_copy = t;
  202. boost::math::vector_barycentric_rational<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y));
  203. Eigen::Vector2d z;
  204. for (size_t i = 0; i < t_copy.size(); ++i)
  205. {
  206. // Don't evaluate the constant at x[i]; that's already tested in the interpolation condition test.
  207. Real t = t_copy[i] + dis(gen);
  208. z = interpolator(t);
  209. BOOST_CHECK_CLOSE(z[0], constant0, 100*sqrt(numeric_limits<Real>::epsilon()));
  210. BOOST_CHECK_CLOSE(z[1], constant1, 100*sqrt(numeric_limits<Real>::epsilon()));
  211. Eigen::Vector2d zprime = interpolator.prime(t);
  212. Real zero_0 = zprime[0];
  213. Real zero_1 = zprime[1];
  214. BOOST_CHECK_SMALL(zero_0, sqrt(numeric_limits<Real>::epsilon()));
  215. BOOST_CHECK_SMALL(zero_1, sqrt(numeric_limits<Real>::epsilon()));
  216. }
  217. }
  218. template<class Real>
  219. void test_constant_std_array()
  220. {
  221. std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on std::array vectors of type "
  222. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  223. std::mt19937 gen(6);
  224. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  225. std::vector<Real> t(100);
  226. std::vector<std::array<Real, 2>> y(100);
  227. t[0] = dis(gen);
  228. Real constant0 = dis(gen);
  229. Real constant1 = dis(gen);
  230. y[0][0] = constant0;
  231. y[0][1] = constant1;
  232. for (size_t i = 1; i < t.size(); ++i)
  233. {
  234. t[i] = t[i-1] + dis(gen);
  235. y[i][0] = constant0;
  236. y[i][1] = constant1;
  237. }
  238. std::vector<std::array<Real,2>> y_copy = y;
  239. std::vector<Real> t_copy = t;
  240. boost::math::vector_barycentric_rational<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y));
  241. std::array<Real, 2> z;
  242. for (size_t i = 0; i < t_copy.size(); ++i)
  243. {
  244. // Don't evaluate the constant at x[i]; that's already tested in the interpolation condition test.
  245. Real t = t_copy[i] + dis(gen);
  246. z = interpolator(t);
  247. BOOST_CHECK_CLOSE(z[0], constant0, 100*sqrt(numeric_limits<Real>::epsilon()));
  248. BOOST_CHECK_CLOSE(z[1], constant1, 100*sqrt(numeric_limits<Real>::epsilon()));
  249. std::array<Real, 2> zprime = interpolator.prime(t);
  250. Real zero_0 = zprime[0];
  251. Real zero_1 = zprime[1];
  252. BOOST_CHECK_SMALL(zero_0, sqrt(numeric_limits<Real>::epsilon()));
  253. BOOST_CHECK_SMALL(zero_1, sqrt(numeric_limits<Real>::epsilon()));
  254. }
  255. }
  256. template<class Real>
  257. void test_constant_high_order()
  258. {
  259. std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  260. std::mt19937 gen(6);
  261. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  262. std::vector<Real> t(100);
  263. std::vector<Eigen::Vector2d> y(100);
  264. t[0] = dis(gen);
  265. Real constant0 = dis(gen);
  266. Real constant1 = dis(gen);
  267. y[0][0] = constant0;
  268. y[0][1] = constant1;
  269. for (size_t i = 1; i < t.size(); ++i)
  270. {
  271. t[i] = t[i-1] + dis(gen);
  272. y[i][0] = constant0;
  273. y[i][1] = constant1;
  274. }
  275. std::vector<Eigen::Vector2d> y_copy = y;
  276. std::vector<Real> t_copy = t;
  277. boost::math::vector_barycentric_rational<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y), 5);
  278. Eigen::Vector2d z;
  279. for (size_t i = 0; i < t_copy.size(); ++i)
  280. {
  281. // Don't evaluate the constant at x[i]; that's already tested in the interpolation condition test.
  282. Real t = t_copy[i] + dis(gen);
  283. z = interpolator(t);
  284. BOOST_CHECK_CLOSE(z[0], constant0, 100*sqrt(numeric_limits<Real>::epsilon()));
  285. BOOST_CHECK_CLOSE(z[1], constant1, 100*sqrt(numeric_limits<Real>::epsilon()));
  286. Eigen::Vector2d zprime = interpolator.prime(t);
  287. Real zero_0 = zprime[0];
  288. Real zero_1 = zprime[1];
  289. BOOST_CHECK_SMALL(zero_0, sqrt(numeric_limits<Real>::epsilon()));
  290. BOOST_CHECK_SMALL(zero_1, sqrt(numeric_limits<Real>::epsilon()));
  291. }
  292. }
  293. template<class Real>
  294. void test_weights()
  295. {
  296. std::cout << "Testing weights are calculated correctly using barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  297. std::mt19937 gen(9);
  298. boost::random::uniform_real_distribution<Real> dis(0.005, 0.01);
  299. std::vector<Real> t(100);
  300. std::vector<Eigen::Vector2d> y(100);
  301. t[0] = dis(gen);
  302. y[0][0] = dis(gen);
  303. y[0][1] = dis(gen);
  304. for (size_t i = 1; i < t.size(); ++i)
  305. {
  306. t[i] = t[i-1] + dis(gen);
  307. y[i][0] = dis(gen);
  308. y[i][1] = dis(gen);
  309. }
  310. std::vector<Eigen::Vector2d> y_copy = y;
  311. std::vector<Real> t_copy = t;
  312. boost::math::detail::vector_barycentric_rational_imp<decltype(t), decltype(y)> interpolator(std::move(t), std::move(y), 1);
  313. for (size_t i = 1; i < t_copy.size() - 1; ++i)
  314. {
  315. Real w = interpolator.weight(i);
  316. Real w_expect = 1/(t_copy[i] - t_copy[i - 1]) + 1/(t_copy[i+1] - t_copy[i]);
  317. if (i % 2 == 0)
  318. {
  319. BOOST_CHECK_CLOSE(w, -w_expect, 0.00001);
  320. }
  321. else
  322. {
  323. BOOST_CHECK_CLOSE(w, w_expect, 0.00001);
  324. }
  325. }
  326. }
  327. BOOST_AUTO_TEST_CASE(vector_barycentric_rational)
  328. {
  329. test_weights<double>();
  330. test_constant_eigen<double>();
  331. test_constant_std_array<double>();
  332. test_constant_high_order<double>();
  333. test_interpolation_condition_eigen<double>();
  334. test_interpolation_condition_ublas<double>();
  335. test_interpolation_condition_std_array<double>();
  336. test_interpolation_condition_high_order<double>();
  337. test_agreement_with_1d<double>();
  338. }