norms_test.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. * (C) Copyright Nick Thompson 2018.
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <cmath>
  8. #include <vector>
  9. #include <array>
  10. #include <forward_list>
  11. #include <algorithm>
  12. #include <random>
  13. #include <limits>
  14. #include <boost/core/lightweight_test.hpp>
  15. #include <boost/numeric/ublas/vector.hpp>
  16. #include <boost/math/constants/constants.hpp>
  17. #include <boost/math/tools/norms.hpp>
  18. #include <boost/multiprecision/cpp_bin_float.hpp>
  19. #include <boost/multiprecision/cpp_complex.hpp>
  20. using std::abs;
  21. using std::pow;
  22. using std::sqrt;
  23. using boost::multiprecision::cpp_bin_float_50;
  24. using boost::multiprecision::cpp_complex_50;
  25. using boost::math::tools::lp_norm;
  26. using boost::math::tools::l1_norm;
  27. using boost::math::tools::l2_norm;
  28. using boost::math::tools::sup_norm;
  29. using boost::math::tools::lp_distance;
  30. using boost::math::tools::l1_distance;
  31. using boost::math::tools::l2_distance;
  32. using boost::math::tools::sup_distance;
  33. using boost::math::tools::total_variation;
  34. /*
  35. * Test checklist:
  36. * 1) Does it work with multiprecision?
  37. * 2) Does it work with .cbegin()/.cend() if the data is not altered?
  38. * 3) Does it work with ublas and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.)
  39. * 4) Does it work with std::forward_list if a forward iterator is all that is required?
  40. * 5) Does it work with complex data if complex data is sensible?
  41. */
  42. // To stress test, set global_seed = 0, global_size = huge.
  43. static const constexpr size_t global_seed = 834;
  44. static const constexpr size_t global_size = 64;
  45. template<class T>
  46. std::vector<T> generate_random_vector(size_t size, size_t seed)
  47. {
  48. if (seed == 0)
  49. {
  50. std::random_device rd;
  51. seed = rd();
  52. }
  53. std::vector<T> v(size);
  54. std::mt19937 gen(seed);
  55. if constexpr (std::is_floating_point<T>::value)
  56. {
  57. std::normal_distribution<T> dis(0, 1);
  58. for (size_t i = 0; i < v.size(); ++i)
  59. {
  60. v[i] = dis(gen);
  61. }
  62. return v;
  63. }
  64. else if constexpr (std::is_integral<T>::value)
  65. {
  66. // Rescaling by larger than 2 is UB!
  67. std::uniform_int_distribution<T> dis(std::numeric_limits<T>::lowest()/2, (std::numeric_limits<T>::max)()/2);
  68. for (size_t i = 0; i < v.size(); ++i)
  69. {
  70. v[i] = dis(gen);
  71. }
  72. return v;
  73. }
  74. else if constexpr (boost::is_complex<T>::value)
  75. {
  76. std::normal_distribution<typename T::value_type> dis(0, 1);
  77. for (size_t i = 0; i < v.size(); ++i)
  78. {
  79. v[i] = {dis(gen), dis(gen)};
  80. }
  81. return v;
  82. }
  83. else if constexpr (boost::multiprecision::number_category<T>::value == boost::multiprecision::number_kind_complex)
  84. {
  85. std::normal_distribution<long double> dis(0, 1);
  86. for (size_t i = 0; i < v.size(); ++i)
  87. {
  88. v[i] = {dis(gen), dis(gen)};
  89. }
  90. return v;
  91. }
  92. else if constexpr (boost::multiprecision::number_category<T>::value == boost::multiprecision::number_kind_floating_point)
  93. {
  94. std::normal_distribution<long double> dis(0, 1);
  95. for (size_t i = 0; i < v.size(); ++i)
  96. {
  97. v[i] = dis(gen);
  98. }
  99. return v;
  100. }
  101. else
  102. {
  103. BOOST_ASSERT_MSG(false, "Could not identify type for random vector generation.");
  104. return v;
  105. }
  106. }
  107. template<class Real>
  108. void test_lp()
  109. {
  110. Real tol = 50*std::numeric_limits<Real>::epsilon();
  111. std::array<Real, 3> u{1,0,0};
  112. Real l3 = lp_norm(u.begin(), u.end(), 3);
  113. BOOST_TEST(abs(l3 - 1) < tol);
  114. u[0] = -8;
  115. l3 = lp_norm(u.cbegin(), u.cend(), 3);
  116. BOOST_TEST(abs(l3 - 8) < tol);
  117. std::vector<Real> v(500);
  118. for (size_t i = 0; i < v.size(); ++i) {
  119. v[i] = 7;
  120. }
  121. Real l8 = lp_norm(v, 8);
  122. Real expected = 7*pow(v.size(), static_cast<Real>(1)/static_cast<Real>(8));
  123. BOOST_TEST(abs(l8 - expected) < tol*abs(expected));
  124. // Does it work with ublas vectors?
  125. // Does it handle the overflow of intermediates?
  126. boost::numeric::ublas::vector<Real> w(4);
  127. Real bignum = sqrt((std::numeric_limits<Real>::max)())/256;
  128. for (size_t i = 0; i < w.size(); ++i)
  129. {
  130. w[i] = bignum;
  131. }
  132. Real l20 = lp_norm(w.cbegin(), w.cend(), 4);
  133. expected = bignum*pow(w.size(), static_cast<Real>(1)/static_cast<Real>(4));
  134. BOOST_TEST(abs(l20 - expected) < tol*expected);
  135. v = generate_random_vector<Real>(global_size, global_seed);
  136. Real scale = 8;
  137. Real l7 = scale*lp_norm(v, 7);
  138. for (auto & x : v)
  139. {
  140. x *= -scale;
  141. }
  142. Real l7_ = lp_norm(v, 7);
  143. BOOST_TEST(abs(l7_ - l7) < tol*l7);
  144. }
  145. template<class Complex>
  146. void test_complex_lp()
  147. {
  148. typedef typename Complex::value_type Real;
  149. Real tol = 50*std::numeric_limits<Real>::epsilon();
  150. std::vector<Complex> v{{1,0}, {0,0}, {0,0}};
  151. Real l3 = lp_norm(v.cbegin(), v.cend(), 3);
  152. BOOST_TEST(abs(l3 - 1) < tol);
  153. l3 = lp_norm(v, 3);
  154. BOOST_TEST(abs(l3 - 1) < tol);
  155. v = generate_random_vector<Complex>(global_size, global_seed);
  156. Real scale = 8;
  157. Real l7 = scale*lp_norm(v, 7);
  158. for (auto & x : v)
  159. {
  160. x *= -scale;
  161. }
  162. Real l7_ = lp_norm(v, 7);
  163. BOOST_TEST(abs(l7_ - l7) < tol*l7);
  164. }
  165. template<class Z>
  166. void test_integer_lp()
  167. {
  168. double tol = 100*std::numeric_limits<double>::epsilon();
  169. std::array<Z, 3> u{1,0,0};
  170. double l3 = lp_norm(u.begin(), u.end(), 3);
  171. BOOST_TEST(abs(l3 - 1) < tol);
  172. auto v = generate_random_vector<Z>(global_size, global_seed);
  173. Z scale = 2;
  174. double l7 = scale*lp_norm(v, 7);
  175. for (auto & x : v)
  176. {
  177. x *= scale;
  178. }
  179. double l7_ = lp_norm(v, 7);
  180. BOOST_TEST(abs(l7_ - l7) < tol*l7);
  181. }
  182. template<class Real>
  183. void test_lp_distance()
  184. {
  185. Real tol = 100*std::numeric_limits<Real>::epsilon();
  186. std::vector<Real> u{1,0,0};
  187. std::vector<Real> v{0,0,0};
  188. Real dist = lp_distance(u,u, 3);
  189. BOOST_TEST(abs(dist) < tol);
  190. dist = lp_distance(u,v, 3);
  191. BOOST_TEST(abs(dist - 1) < tol);
  192. v = generate_random_vector<Real>(global_size, global_seed);
  193. u = generate_random_vector<Real>(global_size, global_seed+1);
  194. Real dist1 = lp_distance(u, v, 7);
  195. Real dist2 = lp_distance(v, u, 7);
  196. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  197. }
  198. template<class Complex>
  199. void test_complex_lp_distance()
  200. {
  201. using Real = typename Complex::value_type;
  202. Real tol = 100*std::numeric_limits<Real>::epsilon();
  203. std::vector<Complex> u{{1,0},{0,0},{0,0}};
  204. std::vector<Complex> v{{0,0},{0,0},{0,0}};
  205. Real dist = boost::math::tools::lp_distance(u,u, 3);
  206. BOOST_TEST(abs(dist) < tol);
  207. dist = boost::math::tools::lp_distance(u,v, 3);
  208. BOOST_TEST(abs(dist - 1) < tol);
  209. v = generate_random_vector<Complex>(global_size, global_seed);
  210. u = generate_random_vector<Complex>(global_size, global_seed + 1);
  211. Real dist1 = lp_distance(u, v, 7);
  212. Real dist2 = lp_distance(v, u, 7);
  213. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  214. }
  215. template<class Z>
  216. void test_integer_lp_distance()
  217. {
  218. double tol = 100*std::numeric_limits<double>::epsilon();
  219. std::array<Z, 3> u{1,0,0};
  220. std::array<Z, 3> w{0,0,0};
  221. double l3 = lp_distance(u, w, 3);
  222. BOOST_TEST(abs(l3 - 1) < tol);
  223. auto v = generate_random_vector<Z>(global_size, global_seed);
  224. Z scale = 2;
  225. for (auto & x : v)
  226. {
  227. x *= scale;
  228. }
  229. auto s = generate_random_vector<Z>(global_size, global_seed + 1);
  230. double dist1 = lp_distance(v, s, 7);
  231. double dist2 = lp_distance(s, v, 7);
  232. BOOST_TEST(abs(dist1 - dist2) < tol*dist2);
  233. }
  234. template<class Z>
  235. void test_integer_total_variation()
  236. {
  237. double eps = std::numeric_limits<double>::epsilon();
  238. std::vector<Z> v{1,1};
  239. double tv = boost::math::tools::total_variation(v);
  240. BOOST_TEST_EQ(tv, 0);
  241. v[1] = 2;
  242. tv = boost::math::tools::total_variation(v.begin(), v.end());
  243. BOOST_TEST_EQ(tv, 1);
  244. v.resize(16);
  245. for (size_t i = 0; i < v.size(); ++i) {
  246. v[i] = i;
  247. }
  248. tv = boost::math::tools::total_variation(v);
  249. BOOST_TEST_EQ(tv, v.size() -1);
  250. for (size_t i = 0; i < v.size(); ++i)
  251. {
  252. v[i] = i*i;
  253. }
  254. tv = boost::math::tools::total_variation(v);
  255. BOOST_TEST_EQ(tv, (v.size() - 1)*(v.size() - 1));
  256. // Work with std::array?
  257. std::array<Z, 2> w{1,1};
  258. tv = boost::math::tools::total_variation(w);
  259. BOOST_TEST_EQ(tv,0);
  260. std::array<Z, 4> u{1, 2, 1, 2};
  261. tv = boost::math::tools::total_variation(u);
  262. BOOST_TEST_EQ(tv, 3);
  263. v = generate_random_vector<Z>(global_size, global_seed);
  264. double tv1 = 2*total_variation(v);
  265. Z scale = 2;
  266. for (auto & x : v)
  267. {
  268. x *= scale;
  269. }
  270. double tv2 = total_variation(v);
  271. BOOST_TEST(abs(tv1 - tv2) < tv1*eps);
  272. }
  273. template<class Real>
  274. void test_total_variation()
  275. {
  276. Real tol = std::numeric_limits<Real>::epsilon();
  277. std::vector<Real> v{1,1};
  278. Real tv = total_variation(v.begin(), v.end());
  279. BOOST_TEST(tv >= 0 && abs(tv) < tol);
  280. tv = total_variation(v);
  281. BOOST_TEST(tv >= 0 && abs(tv) < tol);
  282. v[1] = 2;
  283. tv = total_variation(v.begin(), v.end());
  284. BOOST_TEST(abs(tv - 1) < tol);
  285. v.resize(50);
  286. for (size_t i = 0; i < v.size(); ++i) {
  287. v[i] = i;
  288. }
  289. tv = total_variation(v.begin(), v.end());
  290. BOOST_TEST(abs(tv - (v.size() -1)) < tol);
  291. for (size_t i = 0; i < v.size(); ++i) {
  292. v[i] = i*i;
  293. }
  294. tv = total_variation(v.begin(), v.end());
  295. BOOST_TEST(abs(tv - (v.size() - 1)*(v.size() - 1)) < tol);
  296. v = generate_random_vector<Real>(global_size, global_seed);
  297. Real scale = 8;
  298. Real tv1 = scale*total_variation(v);
  299. for (auto & x : v)
  300. {
  301. x *= -scale;
  302. }
  303. Real tv2 = total_variation(v);
  304. BOOST_TEST(abs(tv1 - tv2) < tol*tv1);
  305. }
  306. template<class Real>
  307. void test_sup_norm()
  308. {
  309. Real tol = std::numeric_limits<Real>::epsilon();
  310. std::vector<Real> v{-2,1,0};
  311. Real s = boost::math::tools::sup_norm(v.begin(), v.end());
  312. BOOST_TEST(abs(s - 2) < tol);
  313. s = boost::math::tools::sup_norm(v);
  314. BOOST_TEST(abs(s - 2) < tol);
  315. // Work with std::array?
  316. std::array<Real, 3> w{-2,1,0};
  317. s = boost::math::tools::sup_norm(w);
  318. BOOST_TEST(abs(s - 2) < tol);
  319. v = generate_random_vector<Real>(global_size, global_seed);
  320. Real scale = 8;
  321. Real sup1 = scale*sup_norm(v);
  322. for (auto & x : v)
  323. {
  324. x *= -scale;
  325. }
  326. Real sup2 = sup_norm(v);
  327. BOOST_TEST(abs(sup1 - sup2) < tol*sup1);
  328. }
  329. template<class Z>
  330. void test_integer_sup_norm()
  331. {
  332. double eps = std::numeric_limits<double>::epsilon();
  333. std::vector<Z> v{2,1,0};
  334. Z s = sup_norm(v.begin(), v.end());
  335. BOOST_TEST_EQ(s, 2);
  336. s = sup_norm(v);
  337. BOOST_TEST_EQ(s,2);
  338. v = generate_random_vector<Z>(global_size, global_seed);
  339. double sup1 = 2*sup_norm(v);
  340. Z scale = 2;
  341. for (auto & x : v)
  342. {
  343. x *= scale;
  344. }
  345. double sup2 = sup_norm(v);
  346. BOOST_TEST(abs(sup1 - sup2) < sup1*eps);
  347. }
  348. template<class Complex>
  349. void test_complex_sup_norm()
  350. {
  351. typedef typename Complex::value_type Real;
  352. Real tol = std::numeric_limits<Real>::epsilon();
  353. std::vector<Complex> w{{0,-8}, {1,1}, {3,2}};
  354. Real s = sup_norm(w.cbegin(), w.cend());
  355. BOOST_TEST(abs(s-8) < tol);
  356. s = sup_norm(w);
  357. BOOST_TEST(abs(s-8) < tol);
  358. auto v = generate_random_vector<Complex>(global_size, global_seed);
  359. Real scale = 8;
  360. Real sup1 = scale*sup_norm(v);
  361. for (auto & x : v)
  362. {
  363. x *= -scale;
  364. }
  365. Real sup2 = sup_norm(v);
  366. BOOST_TEST(abs(sup1 - sup2) < tol*sup1);
  367. }
  368. template<class Real>
  369. void test_l0_pseudo_norm()
  370. {
  371. std::vector<Real> v{0,0,1};
  372. size_t count = boost::math::tools::l0_pseudo_norm(v.begin(), v.end());
  373. BOOST_TEST_EQ(count, 1);
  374. // Compiles with cbegin()/cend()?
  375. count = boost::math::tools::l0_pseudo_norm(v.cbegin(), v.cend());
  376. BOOST_TEST_EQ(count, 1);
  377. count = boost::math::tools::l0_pseudo_norm(v);
  378. BOOST_TEST_EQ(count, 1);
  379. std::array<Real, 3> w{0,0,1};
  380. count = boost::math::tools::l0_pseudo_norm(w);
  381. BOOST_TEST_EQ(count, 1);
  382. }
  383. template<class Complex>
  384. void test_complex_l0_pseudo_norm()
  385. {
  386. std::vector<Complex> v{{0,0}, {0,0}, {1,0}};
  387. size_t count = boost::math::tools::l0_pseudo_norm(v.begin(), v.end());
  388. BOOST_TEST_EQ(count, 1);
  389. count = boost::math::tools::l0_pseudo_norm(v);
  390. BOOST_TEST_EQ(count, 1);
  391. }
  392. template<class Z>
  393. void test_hamming_distance()
  394. {
  395. std::vector<Z> v{1,2,3};
  396. std::vector<Z> w{1,2,4};
  397. size_t count = boost::math::tools::hamming_distance(v, w);
  398. BOOST_TEST_EQ(count, 1);
  399. count = boost::math::tools::hamming_distance(v, v);
  400. BOOST_TEST_EQ(count, 0);
  401. }
  402. template<class Real>
  403. void test_l1_norm()
  404. {
  405. Real tol = std::numeric_limits<Real>::epsilon();
  406. std::vector<Real> v{1,1,1};
  407. Real l1 = l1_norm(v.begin(), v.end());
  408. BOOST_TEST(abs(l1 - 3) < tol);
  409. l1 = l1_norm(v);
  410. BOOST_TEST(abs(l1 - 3) < tol);
  411. std::array<Real, 3> w{1,1,1};
  412. l1 = l1_norm(w);
  413. BOOST_TEST(abs(l1 - 3) < tol);
  414. v = generate_random_vector<Real>(global_size, global_seed);
  415. Real scale = 8;
  416. Real l1_1 = scale*l1_norm(v);
  417. for (auto & x : v)
  418. {
  419. x *= -scale;
  420. }
  421. Real l1_2 = l1_norm(v);
  422. BOOST_TEST(abs(l1_1 - l1_2) < tol*l1_1);
  423. }
  424. template<class Z>
  425. void test_integer_l1_norm()
  426. {
  427. double eps = std::numeric_limits<double>::epsilon();
  428. std::vector<Z> v{1,1,1};
  429. Z l1 = boost::math::tools::l1_norm(v.begin(), v.end());
  430. BOOST_TEST_EQ(l1, 3);
  431. v = generate_random_vector<Z>(global_size, global_seed);
  432. double l1_1 = 2*l1_norm(v);
  433. Z scale = 2;
  434. for (auto & x : v)
  435. {
  436. x *= scale;
  437. }
  438. double l1_2 = l1_norm(v);
  439. BOOST_TEST(l1_1 > 0);
  440. BOOST_TEST(l1_2 > 0);
  441. if (abs(l1_1 - l1_2) > 2*l1_1*eps)
  442. {
  443. std::cout << std::setprecision(std::numeric_limits<double>::digits10);
  444. std::cout << "L1_1 = " << l1_1 << "\n";
  445. std::cout << "L1_2 = " << l1_2 << "\n";
  446. BOOST_TEST(abs(l1_1 - l1_2) < 2*l1_1*eps);
  447. }
  448. }
  449. template<class Complex>
  450. void test_complex_l1_norm()
  451. {
  452. typedef typename Complex::value_type Real;
  453. Real tol = std::numeric_limits<Real>::epsilon();
  454. std::vector<Complex> v{{1,0}, {0,1},{0,-1}};
  455. Real l1 = l1_norm(v.begin(), v.end());
  456. BOOST_TEST(abs(l1 - 3) < tol);
  457. l1 = l1_norm(v);
  458. BOOST_TEST(abs(l1 - 3) < tol);
  459. v = generate_random_vector<Complex>(global_size, global_seed);
  460. Real scale = 8;
  461. Real l1_1 = scale*l1_norm(v);
  462. for (auto & x : v)
  463. {
  464. x *= -scale;
  465. }
  466. Real l1_2 = l1_norm(v);
  467. BOOST_TEST(abs(l1_1 - l1_2) < tol*l1_1);
  468. }
  469. template<class Real>
  470. void test_l1_distance()
  471. {
  472. Real tol = std::numeric_limits<Real>::epsilon();
  473. std::vector<Real> v{1,2,3};
  474. std::vector<Real> w{1,1,1};
  475. Real l1 = boost::math::tools::l1_distance(v, v);
  476. BOOST_TEST(abs(l1) < tol);
  477. l1 = boost::math::tools::l1_distance(w, v);
  478. BOOST_TEST(abs(l1 - 3) < tol);
  479. l1 = boost::math::tools::l1_distance(v, w);
  480. BOOST_TEST(abs(l1 - 3) < tol);
  481. v = generate_random_vector<Real>(global_size, global_seed);
  482. w = generate_random_vector<Real>(global_size, global_seed+1);
  483. Real dist1 = l1_distance(v, w);
  484. Real dist2 = l1_distance(w, v);
  485. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  486. }
  487. template<class Z>
  488. void test_integer_l1_distance()
  489. {
  490. double tol = std::numeric_limits<double>::epsilon();
  491. std::vector<Z> v{1,2,3};
  492. std::vector<Z> w{1,1,1};
  493. double l1 = boost::math::tools::l1_distance(v, v);
  494. BOOST_TEST(abs(l1) < tol);
  495. l1 = boost::math::tools::l1_distance(w, v);
  496. BOOST_TEST(abs(l1 - 3) < tol);
  497. l1 = boost::math::tools::l1_distance(v, w);
  498. BOOST_TEST(abs(l1 - 3) < tol);
  499. v = generate_random_vector<Z>(global_size, global_seed);
  500. w = generate_random_vector<Z>(global_size, global_seed + 1);
  501. double dist1 = l1_distance(v, w);
  502. double dist2 = l1_distance(w, v);
  503. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  504. }
  505. template<class Complex>
  506. void test_complex_l1_distance()
  507. {
  508. typedef typename Complex::value_type Real;
  509. Real tol = std::numeric_limits<Real>::epsilon();
  510. std::vector<Complex> v{{1,0}, {0,1},{0,-1}};
  511. Real l1 = boost::math::tools::l1_distance(v, v);
  512. BOOST_TEST(abs(l1) < tol);
  513. std::vector<Complex> w{{2,0}, {0,1},{0,-1}};
  514. l1 = boost::math::tools::l1_distance(v.cbegin(), v.cend(), w.cbegin());
  515. BOOST_TEST(abs(l1 - 1) < tol);
  516. v = generate_random_vector<Complex>(global_size, global_seed);
  517. w = generate_random_vector<Complex>(global_size, global_seed + 1);
  518. Real dist1 = l1_distance(v, w);
  519. Real dist2 = l1_distance(w, v);
  520. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  521. }
  522. template<class Real>
  523. void test_l2_norm()
  524. {
  525. using std::sqrt;
  526. Real tol = std::numeric_limits<Real>::epsilon();
  527. std::vector<Real> v{1,1,1,1};
  528. Real l2 = boost::math::tools::l2_norm(v.begin(), v.end());
  529. BOOST_TEST(abs(l2 - 2) < tol);
  530. l2 = boost::math::tools::l2_norm(v);
  531. BOOST_TEST(abs(l2 - 2) < tol);
  532. std::array<Real, 4> w{1,1,1,1};
  533. l2 = boost::math::tools::l2_norm(w);
  534. BOOST_TEST(abs(l2 - 2) < tol);
  535. Real bignum = 4*sqrt((std::numeric_limits<Real>::max)());
  536. v[0] = bignum;
  537. v[1] = 0;
  538. v[2] = 0;
  539. v[3] = 0;
  540. l2 = boost::math::tools::l2_norm(v.begin(), v.end());
  541. BOOST_TEST(abs(l2 - bignum) < tol*l2);
  542. v = generate_random_vector<Real>(global_size, global_seed);
  543. Real scale = 8;
  544. Real l2_1 = scale*l2_norm(v);
  545. for (auto & x : v)
  546. {
  547. x *= -scale;
  548. }
  549. Real l2_2 = l2_norm(v);
  550. BOOST_TEST(l2_1 > 0);
  551. BOOST_TEST(l2_2 > 0);
  552. BOOST_TEST(abs(l2_1 - l2_2) < tol*l2_1);
  553. }
  554. template<class Z>
  555. void test_integer_l2_norm()
  556. {
  557. double tol = 100*std::numeric_limits<double>::epsilon();
  558. std::vector<Z> v{1,1,1,1};
  559. double l2 = boost::math::tools::l2_norm(v.begin(), v.end());
  560. BOOST_TEST(abs(l2 - 2) < tol);
  561. v = generate_random_vector<Z>(global_size, global_seed);
  562. Z scale = 2;
  563. double l2_1 = scale*l2_norm(v);
  564. for (auto & x : v)
  565. {
  566. x *= scale;
  567. }
  568. double l2_2 = l2_norm(v);
  569. BOOST_TEST(l2_1 > 0);
  570. BOOST_TEST(l2_2 > 0);
  571. BOOST_TEST(abs(l2_1 - l2_2) < tol*l2_1);
  572. }
  573. template<class Complex>
  574. void test_complex_l2_norm()
  575. {
  576. typedef typename Complex::value_type Real;
  577. Real tol = 100*std::numeric_limits<Real>::epsilon();
  578. std::vector<Complex> v{{1,0}, {0,1},{0,-1}, {1,0}};
  579. Real l2 = boost::math::tools::l2_norm(v.begin(), v.end());
  580. BOOST_TEST(abs(l2 - 2) < tol);
  581. l2 = boost::math::tools::l2_norm(v);
  582. BOOST_TEST(abs(l2 - 2) < tol);
  583. v = generate_random_vector<Complex>(global_size, global_seed);
  584. Real scale = 8;
  585. Real l2_1 = scale*l2_norm(v);
  586. for (auto & x : v)
  587. {
  588. x *= -scale;
  589. }
  590. Real l2_2 = l2_norm(v);
  591. BOOST_TEST(abs(l2_1 - l2_2) < tol*l2_1);
  592. }
  593. template<class Real>
  594. void test_l2_distance()
  595. {
  596. Real tol = std::numeric_limits<Real>::epsilon();
  597. std::vector<Real> v{1,1,1,1};
  598. Real l2 = boost::math::tools::l2_distance(v, v);
  599. BOOST_TEST(abs(l2) < tol);
  600. v = generate_random_vector<Real>(global_size, global_seed);
  601. auto w = generate_random_vector<Real>(global_size, global_seed + 1);
  602. Real dist1 = l2_distance(v, w);
  603. Real dist2 = l2_distance(w, v);
  604. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  605. }
  606. template<class Z>
  607. void test_integer_l2_distance()
  608. {
  609. double tol = std::numeric_limits<double>::epsilon();
  610. std::vector<Z> v{1,1,1,1};
  611. double l2 = boost::math::tools::l2_distance(v, v);
  612. BOOST_TEST(abs(l2) < tol);
  613. v = generate_random_vector<Z>(global_size, global_seed);
  614. auto w = generate_random_vector<Z>(global_size, global_seed + 1);
  615. double dist1 = l2_distance(v, w);
  616. double dist2 = l2_distance(w, v);
  617. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  618. }
  619. template<class Complex>
  620. void test_complex_l2_distance()
  621. {
  622. typedef typename Complex::value_type Real;
  623. Real tol = 100*std::numeric_limits<Real>::epsilon();
  624. std::vector<Complex> v{{1,0}, {0,1},{0,-1}, {1,0}};
  625. Real l2 = boost::math::tools::l2_distance(v, v);
  626. BOOST_TEST(abs(l2) < tol);
  627. v = generate_random_vector<Complex>(global_size, global_seed);
  628. auto w = generate_random_vector<Complex>(global_size, global_seed + 1);
  629. Real dist1 = l2_distance(v, w);
  630. Real dist2 = l2_distance(w, v);
  631. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  632. }
  633. template<class Real>
  634. void test_sup_distance()
  635. {
  636. Real tol = std::numeric_limits<Real>::epsilon();
  637. std::vector<Real> v{1,1,1,1};
  638. std::vector<Real> w{0,0,0,0};
  639. Real sup = boost::math::tools::sup_distance(v, v);
  640. BOOST_TEST(abs(sup) < tol);
  641. sup = boost::math::tools::sup_distance(v, w);
  642. BOOST_TEST(abs(sup -1) < tol);
  643. v = generate_random_vector<Real>(global_size, global_seed);
  644. w = generate_random_vector<Real>(global_size, global_seed + 1);
  645. Real dist1 = sup_distance(v, w);
  646. Real dist2 = sup_distance(w, v);
  647. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  648. }
  649. template<class Z>
  650. void test_integer_sup_distance()
  651. {
  652. double tol = std::numeric_limits<double>::epsilon();
  653. std::vector<Z> v{1,1,1,1};
  654. std::vector<Z> w{0,0,0,0};
  655. double sup = boost::math::tools::sup_distance(v, v);
  656. BOOST_TEST(abs(sup) < tol);
  657. sup = boost::math::tools::sup_distance(v, w);
  658. BOOST_TEST(abs(sup -1) < tol);
  659. v = generate_random_vector<Z>(global_size, global_seed);
  660. w = generate_random_vector<Z>(global_size, global_seed + 1);
  661. double dist1 = sup_distance(v, w);
  662. double dist2 = sup_distance(w, v);
  663. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  664. }
  665. template<class Complex>
  666. void test_complex_sup_distance()
  667. {
  668. typedef typename Complex::value_type Real;
  669. Real tol = 100*std::numeric_limits<Real>::epsilon();
  670. std::vector<Complex> v{{1,0}, {0,1},{0,-1}, {1,0}};
  671. Real sup = boost::math::tools::sup_distance(v, v);
  672. BOOST_TEST(abs(sup) < tol);
  673. v = generate_random_vector<Complex>(global_size, global_seed);
  674. auto w = generate_random_vector<Complex>(global_size, global_seed + 1);
  675. Real dist1 = sup_distance(v, w);
  676. Real dist2 = sup_distance(w, v);
  677. BOOST_TEST(abs(dist1 - dist2) < tol*dist1);
  678. }
  679. int main()
  680. {
  681. test_l0_pseudo_norm<unsigned>();
  682. test_l0_pseudo_norm<int>();
  683. test_l0_pseudo_norm<float>();
  684. test_l0_pseudo_norm<double>();
  685. test_l0_pseudo_norm<long double>();
  686. test_l0_pseudo_norm<cpp_bin_float_50>();
  687. test_complex_l0_pseudo_norm<std::complex<float>>();
  688. test_complex_l0_pseudo_norm<std::complex<double>>();
  689. test_complex_l0_pseudo_norm<std::complex<long double>>();
  690. test_complex_l0_pseudo_norm<cpp_complex_50>();
  691. test_hamming_distance<int>();
  692. test_hamming_distance<unsigned>();
  693. test_l1_norm<float>();
  694. test_l1_norm<double>();
  695. test_l1_norm<long double>();
  696. test_l1_norm<cpp_bin_float_50>();
  697. test_integer_l1_norm<int>();
  698. test_integer_l1_norm<unsigned>();
  699. test_complex_l1_norm<std::complex<float>>();
  700. test_complex_l1_norm<std::complex<double>>();
  701. test_complex_l1_norm<std::complex<long double>>();
  702. test_complex_l1_norm<cpp_complex_50>();
  703. test_l1_distance<float>();
  704. test_l1_distance<cpp_bin_float_50>();
  705. test_integer_l1_distance<int>();
  706. test_integer_l1_distance<unsigned>();
  707. test_complex_l1_distance<std::complex<float>>();
  708. test_complex_l1_distance<cpp_complex_50>();
  709. test_complex_l2_norm<std::complex<float>>();
  710. test_complex_l2_norm<std::complex<double>>();
  711. test_complex_l2_norm<std::complex<long double>>();
  712. test_complex_l2_norm<cpp_complex_50>();
  713. test_l2_norm<float>();
  714. test_l2_norm<double>();
  715. test_l2_norm<long double>();
  716. test_l2_norm<cpp_bin_float_50>();
  717. test_integer_l2_norm<int>();
  718. test_integer_l2_norm<unsigned>();
  719. test_l2_distance<double>();
  720. test_l2_distance<cpp_bin_float_50>();
  721. test_integer_l2_distance<int>();
  722. test_integer_l2_distance<unsigned>();
  723. test_complex_l2_distance<std::complex<double>>();
  724. test_complex_l2_distance<cpp_complex_50>();
  725. test_lp<float>();
  726. test_lp<double>();
  727. test_lp<long double>();
  728. test_lp<cpp_bin_float_50>();
  729. test_complex_lp<std::complex<float>>();
  730. test_complex_lp<std::complex<double>>();
  731. test_complex_lp<std::complex<long double>>();
  732. test_complex_lp<cpp_complex_50>();
  733. test_integer_lp<int>();
  734. test_integer_lp<unsigned>();
  735. test_lp_distance<double>();
  736. test_lp_distance<cpp_bin_float_50>();
  737. test_complex_lp_distance<std::complex<double>>();
  738. test_complex_lp_distance<cpp_complex_50>();
  739. test_integer_lp_distance<int>();
  740. test_integer_lp_distance<unsigned>();
  741. test_sup_norm<float>();
  742. test_sup_norm<double>();
  743. test_sup_norm<long double>();
  744. test_sup_norm<cpp_bin_float_50>();
  745. test_integer_sup_norm<int>();
  746. test_integer_sup_norm<unsigned>();
  747. test_complex_sup_norm<std::complex<float>>();
  748. test_complex_sup_norm<std::complex<double>>();
  749. test_complex_sup_norm<std::complex<long double>>();
  750. test_complex_sup_norm<cpp_complex_50>();
  751. test_sup_distance<double>();
  752. test_sup_distance<cpp_bin_float_50>();
  753. test_integer_sup_distance<int>();
  754. test_integer_sup_distance<unsigned>();
  755. test_complex_sup_distance<std::complex<double>>();
  756. test_complex_sup_distance<cpp_complex_50>();
  757. test_total_variation<float>();
  758. test_total_variation<double>();
  759. test_total_variation<long double>();
  760. test_total_variation<cpp_bin_float_50>();
  761. test_integer_total_variation<uint32_t>();
  762. test_integer_total_variation<int>();
  763. return boost::report_errors();
  764. }