test_barycentric_rational.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright Nick Thompson, 2017
  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 barycentric_rational
  7. #include <cmath>
  8. #include <random>
  9. #include <boost/random/uniform_real_distribution.hpp>
  10. #include <boost/type_index.hpp>
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <boost/test/tools/floating_point_comparison.hpp>
  13. #include <boost/math/interpolators/barycentric_rational.hpp>
  14. #include <boost/multiprecision/cpp_bin_float.hpp>
  15. #ifdef BOOST_HAS_FLOAT128
  16. #include <boost/multiprecision/float128.hpp>
  17. #endif
  18. using std::sqrt;
  19. using std::abs;
  20. using std::numeric_limits;
  21. using boost::multiprecision::cpp_bin_float_50;
  22. template<class Real>
  23. void test_interpolation_condition()
  24. {
  25. std::cout << "Testing interpolation condition for barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  26. std::mt19937 gen(4);
  27. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  28. std::vector<Real> x(100);
  29. std::vector<Real> y(100);
  30. x[0] = dis(gen);
  31. y[0] = dis(gen);
  32. for (size_t i = 1; i < x.size(); ++i)
  33. {
  34. x[i] = x[i-1] + dis(gen);
  35. y[i] = dis(gen);
  36. }
  37. boost::math::barycentric_rational<Real> interpolator(x.data(), y.data(), y.size());
  38. for (size_t i = 0; i < x.size(); ++i)
  39. {
  40. Real z = interpolator(x[i]);
  41. BOOST_CHECK_CLOSE(z, y[i], 100*numeric_limits<Real>::epsilon());
  42. }
  43. // Make sure that the move constructor does the same thing:
  44. std::vector<Real> x_copy = x;
  45. std::vector<Real> y_copy = y;
  46. boost::math::barycentric_rational<Real> move_interpolator(std::move(x), std::move(y));
  47. for (size_t i = 0; i < x_copy.size(); ++i)
  48. {
  49. Real z = move_interpolator(x_copy[i]);
  50. BOOST_CHECK_CLOSE(z, y_copy[i], 100*numeric_limits<Real>::epsilon());
  51. }
  52. }
  53. template<class Real>
  54. void test_interpolation_condition_high_order()
  55. {
  56. std::cout << "Testing interpolation condition in high order for barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  57. std::mt19937 gen(5);
  58. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  59. std::vector<Real> x(100);
  60. std::vector<Real> y(100);
  61. x[0] = dis(gen);
  62. y[0] = dis(gen);
  63. for (size_t i = 1; i < x.size(); ++i)
  64. {
  65. x[i] = x[i-1] + dis(gen);
  66. y[i] = dis(gen);
  67. }
  68. // Order 5 approximation:
  69. boost::math::barycentric_rational<Real> interpolator(x.data(), y.data(), y.size(), 5);
  70. for (size_t i = 0; i < x.size(); ++i)
  71. {
  72. Real z = interpolator(x[i]);
  73. BOOST_CHECK_CLOSE(z, y[i], 100*numeric_limits<Real>::epsilon());
  74. }
  75. }
  76. template<class Real>
  77. void test_constant()
  78. {
  79. std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  80. std::mt19937 gen(6);
  81. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  82. std::vector<Real> x(100);
  83. std::vector<Real> y(100);
  84. Real constant = -8;
  85. x[0] = dis(gen);
  86. y[0] = constant;
  87. for (size_t i = 1; i < x.size(); ++i)
  88. {
  89. x[i] = x[i-1] + dis(gen);
  90. y[i] = y[0];
  91. }
  92. boost::math::barycentric_rational<Real> interpolator(x.data(), y.data(), y.size());
  93. for (size_t i = 0; i < x.size(); ++i)
  94. {
  95. // Don't evaluate the constant at x[i]; that's already tested in the interpolation condition test.
  96. Real t = x[i] + dis(gen);
  97. Real z = interpolator(t);
  98. BOOST_CHECK_CLOSE(z, constant, 100*sqrt(numeric_limits<Real>::epsilon()));
  99. BOOST_CHECK_SMALL(interpolator.prime(t), sqrt(numeric_limits<Real>::epsilon()));
  100. }
  101. }
  102. template<class Real>
  103. void test_constant_high_order()
  104. {
  105. std::cout << "Testing that constants are interpolated correctly in high order using barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  106. std::mt19937 gen(7);
  107. boost::random::uniform_real_distribution<Real> dis(0.1f, 1);
  108. std::vector<Real> x(100);
  109. std::vector<Real> y(100);
  110. Real constant = 5;
  111. x[0] = dis(gen);
  112. y[0] = constant;
  113. for (size_t i = 1; i < x.size(); ++i)
  114. {
  115. x[i] = x[i-1] + dis(gen);
  116. y[i] = y[0];
  117. }
  118. // Set interpolation order to 7:
  119. boost::math::barycentric_rational<Real> interpolator(x.data(), y.data(), y.size(), 7);
  120. for (size_t i = 0; i < x.size(); ++i)
  121. {
  122. Real t = x[i] + dis(gen);
  123. Real z = interpolator(t);
  124. BOOST_CHECK_CLOSE(z, constant, 1000*sqrt(numeric_limits<Real>::epsilon()));
  125. BOOST_CHECK_SMALL(interpolator.prime(t), 100*sqrt(numeric_limits<Real>::epsilon()));
  126. }
  127. }
  128. template<class Real>
  129. void test_runge()
  130. {
  131. std::cout << "Testing interpolation of Runge's 1/(1+25x^2) function using barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  132. std::mt19937 gen(8);
  133. boost::random::uniform_real_distribution<Real> dis(0.005f, 0.01f);
  134. std::vector<Real> x(100);
  135. std::vector<Real> y(100);
  136. x[0] = -2;
  137. y[0] = 1/(1+25*x[0]*x[0]);
  138. for (size_t i = 1; i < x.size(); ++i)
  139. {
  140. x[i] = x[i-1] + dis(gen);
  141. y[i] = 1/(1+25*x[i]*x[i]);
  142. }
  143. boost::math::barycentric_rational<Real> interpolator(x.data(), y.data(), y.size(), 5);
  144. for (size_t i = 0; i < x.size(); ++i)
  145. {
  146. Real t = x[i];
  147. Real z = interpolator(t);
  148. BOOST_CHECK_CLOSE(z, y[i], 0.03);
  149. Real z_prime = interpolator.prime(t);
  150. Real num = -50*t;
  151. Real denom = (1+25*t*t)*(1+25*t*t);
  152. if (abs(num/denom) > 0.00001)
  153. {
  154. BOOST_CHECK_CLOSE_FRACTION(z_prime, num/denom, 0.03);
  155. }
  156. }
  157. Real tol = 0.0001;
  158. for (size_t i = 0; i < x.size(); ++i)
  159. {
  160. Real t = x[i] + dis(gen);
  161. Real z = interpolator(t);
  162. BOOST_CHECK_CLOSE(z, 1/(1+25*t*t), tol);
  163. Real z_prime = interpolator.prime(t);
  164. Real num = -50*t;
  165. Real denom = (1+25*t*t)*(1+25*t*t);
  166. Real runge_prime = num/denom;
  167. if (abs(runge_prime) > 0 && abs(z_prime - runge_prime)/abs(runge_prime) > tol)
  168. {
  169. std::cout << "Error too high for t = " << t << " which is a distance " << t - x[i] << " from node " << i << "/" << x.size() << " associated with data (" << x[i] << ", " << y[i] << ")\n";
  170. BOOST_CHECK_CLOSE_FRACTION(z_prime, runge_prime, tol);
  171. }
  172. }
  173. }
  174. template<class Real>
  175. void test_weights()
  176. {
  177. std::cout << "Testing weights are calculated correctly using barycentric interpolation on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  178. std::mt19937 gen(9);
  179. boost::random::uniform_real_distribution<Real> dis(0.005, 0.01);
  180. std::vector<Real> x(100);
  181. std::vector<Real> y(100);
  182. x[0] = -2;
  183. y[0] = 1/(1+25*x[0]*x[0]);
  184. for (size_t i = 1; i < x.size(); ++i)
  185. {
  186. x[i] = x[i-1] + dis(gen);
  187. y[i] = 1/(1+25*x[i]*x[i]);
  188. }
  189. boost::math::detail::barycentric_rational_imp<Real> interpolator(x.data(), x.data() + x.size(), y.data(), 0);
  190. for (size_t i = 0; i < x.size(); ++i)
  191. {
  192. Real w = interpolator.weight(i);
  193. if (i % 2 == 0)
  194. {
  195. BOOST_CHECK_CLOSE(w, 1, 0.00001);
  196. }
  197. else
  198. {
  199. BOOST_CHECK_CLOSE(w, -1, 0.00001);
  200. }
  201. }
  202. // d = 1:
  203. interpolator = boost::math::detail::barycentric_rational_imp<Real>(x.data(), x.data() + x.size(), y.data(), 1);
  204. for (size_t i = 1; i < x.size() -1; ++i)
  205. {
  206. Real w = interpolator.weight(i);
  207. Real w_expect = 1/(x[i] - x[i - 1]) + 1/(x[i+1] - x[i]);
  208. if (i % 2 == 0)
  209. {
  210. BOOST_CHECK_CLOSE(w, -w_expect, 0.00001);
  211. }
  212. else
  213. {
  214. BOOST_CHECK_CLOSE(w, w_expect, 0.00001);
  215. }
  216. }
  217. }
  218. BOOST_AUTO_TEST_CASE(barycentric_rational)
  219. {
  220. // The tests took too long at the higher precisions.
  221. // They still pass, but the CI system is starting to time out,
  222. // so I figured it'd be polite to comment out the most expensive tests.
  223. test_weights<double>();
  224. test_constant<float>();
  225. //test_constant<double>();
  226. test_constant<long double>();
  227. //test_constant<cpp_bin_float_50>();
  228. //test_constant_high_order<float>();
  229. test_constant_high_order<double>();
  230. //test_constant_high_order<long double>();
  231. //test_constant_high_order<cpp_bin_float_50>();
  232. test_interpolation_condition<float>();
  233. test_interpolation_condition<double>();
  234. //test_interpolation_condition<long double>();
  235. //test_interpolation_condition<cpp_bin_float_50>();
  236. //test_interpolation_condition_high_order<float>();
  237. test_interpolation_condition_high_order<double>();
  238. //test_interpolation_condition_high_order<long double>();
  239. //test_interpolation_condition_high_order<cpp_bin_float_50>();
  240. test_runge<double>();
  241. //test_runge<long double>();
  242. //test_runge<cpp_bin_float_50>();
  243. #ifdef BOOST_HAS_FLOAT128
  244. //test_interpolation_condition<boost::multiprecision::float128>();
  245. //test_constant<boost::multiprecision::float128>();
  246. //test_constant_high_order<boost::multiprecision::float128>();
  247. //test_interpolation_condition_high_order<boost::multiprecision::float128>();
  248. //test_runge<boost::multiprecision::float128>();
  249. #endif
  250. }