reduction.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // (c) Daniel Llorens - 2014
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file reduction.C
  7. /// @brief Test array reductions.
  8. #include <iostream>
  9. #include <iterator>
  10. #include "ra/mpdebug.H"
  11. #include "ra/complex.H"
  12. #include "ra/format.H"
  13. #include "ra/test.H"
  14. #include "ra/big.H"
  15. #include "ra/operators.H"
  16. #include "ra/io.H"
  17. using std::cout, std::endl, std::flush, std::tuple, ra::QNAN;
  18. using real = double;
  19. using complex = std::complex<double>;
  20. int main()
  21. {
  22. TestRecorder tr(std::cout);
  23. tr.section("amax with different expr types");
  24. {
  25. auto test_amax_expr = [&tr](auto && a, auto && b)
  26. {
  27. a = ra::Small<real, 2, 2> {1, 2, 9, -10};
  28. tr.test_eq(amax(a), 9);
  29. b = ra::Small<real, 2, 2> {1, 1, 1, 1};
  30. tr.test_eq(amax(a+b), 10);
  31. };
  32. test_amax_expr(ra::Unique<real, 2>({2, 2}, 0.), ra::Unique<real, 2>({2, 2}, 0.));
  33. test_amax_expr(ra::Small<real, 2, 2>(), ra::Small<real, 2, 2>());
  34. // failed in gcc 5.1 when amax() took its args by plain auto (now auto &&).
  35. test_amax_expr(ra::Unique<real, 2>({2, 2}, 0.), ra::Small<real, 2, 2>());
  36. }
  37. tr.section("every / any");
  38. {
  39. tr.test(every(ra::Unique<real, 2>({4, 4}, 10+ra::_0-ra::_1)));
  40. tr.test(any(ra::Unique<real, 2>({4, 4}, ra::_0-ra::_1)));
  41. tr.test(every(true));
  42. tr.test(!every(false));
  43. tr.test(any(true));
  44. tr.test(!any(false));
  45. tr.test(every(ra::Unique<int, 1> {5, 5}==5));
  46. tr.test(!every(ra::Unique<int, 1> {2, 5}==5));
  47. tr.test(!every(ra::Unique<int, 1> {5, 2}==5));
  48. tr.test(!every(ra::Unique<int, 1> {2, 3}==5));
  49. tr.test(any(ra::Unique<int, 1> {5, 5}==5));
  50. tr.test(any(ra::Unique<int, 1> {2, 5}==5));
  51. tr.test(any(ra::Unique<int, 1> {5, 2}==5));
  52. tr.test(!any(ra::Unique<int, 1> {2, 3}==5));
  53. }
  54. tr.section("norm2");
  55. {
  56. ra::Small<real, 2> a {1, 2};
  57. tr.test_abs_error(std::sqrt(5.), norm2(a), 1e-15);
  58. ra::Small<float, 2> b {1, 2};
  59. tr.test_abs_error(std::sqrt(5.f), norm2(b), 4e-8);
  60. tr.info("type of norm2(floats)").test(std::is_same_v<float, decltype(norm2(b))>);
  61. tr.info("type of reduce_sqrm(floats)").test(std::is_same_v<float, decltype(reduce_sqrm(b))>);
  62. tr.info("type of sqrm(floats)").test(std::is_same_v<float, decltype(sqrm(b[0]))>);
  63. }
  64. tr.section("normv");
  65. {
  66. ra::Small<real, 2> a {1, 2};
  67. ra::Small<real, 2> b;
  68. b = normv(a);
  69. cout << "normv of lvalue: " << b << endl;
  70. tr.test_eq(b[0], 1./sqrt(5));
  71. tr.test_eq(b[1], 2./sqrt(5));
  72. b = normv(ra::Small<real, 2> {2, 1});
  73. cout << "normv of rvalue: "<< b << endl;
  74. tr.test_eq(b[0], 2./sqrt(5));
  75. tr.test_eq(b[1], 1./sqrt(5));
  76. }
  77. tr.section("reductions");
  78. {
  79. auto test_dot = [](auto && test) // TODO Use this for other real reductions.
  80. {
  81. test(ra::Small<complex, 2>{1, 2}, ra::Small<real, 2>{3, 4});
  82. test(ra::Small<real, 2>{1, 2}, ra::Small<complex, 2>{3, 4});
  83. test(ra::Small<real, 2>{1, 2}, ra::Small<real, 2>{3, 4});
  84. test(ra::Small<complex, 2>{1, 2}, ra::Small<complex, 2>{3, 4});
  85. test(ra::Big<complex, 1>{1, 2}, ra::Big<real, 1>{3, 4});
  86. test(ra::Big<real, 1>{1, 2}, ra::Big<complex, 1>{3, 4});
  87. test(ra::Big<real, 1>{1, 2}, ra::Big<real, 1>{3, 4});
  88. test(ra::Big<complex, 1>{1, 2}, ra::Big<complex, 1>{3, 4});
  89. test(ra::Small<complex, 2>{1, 2}, ra::Big<real, 1>{3, 4});
  90. test(ra::Small<real, 2>{1, 2}, ra::Big<complex, 1>{3, 4});
  91. test(ra::Small<real, 2>{1, 2}, ra::Big<real, 1>{3, 4});
  92. test(ra::Small<complex, 2>{1, 2}, ra::Big<complex, 1>{3, 4});
  93. test(ra::Big<complex, 1>{1, 2}, ra::Small<real, 2>{3, 4});
  94. test(ra::Big<real, 1>{1, 2}, ra::Small<complex, 2>{3, 4});
  95. test(ra::Big<real, 1>{1, 2}, ra::Small<real, 2>{3, 4});
  96. test(ra::Big<complex, 1>{1, 2}, ra::Small<complex, 2>{3, 4});
  97. };
  98. test_dot([&tr](auto && a, auto && b) { tr.test_eq(11., dot(a, b)); });
  99. test_dot([&tr](auto && a, auto && b) { tr.test_eq(11., cdot(a, b)); });
  100. test_dot([&tr](auto && a, auto && b) { tr.test_eq(sqrt(8.), norm2(a-b)); });
  101. test_dot([&tr](auto && a, auto && b) { tr.test_eq(8., reduce_sqrm(a-b)); });
  102. auto test_cdot = [](auto && test)
  103. {
  104. test(ra::Small<complex, 2>{1, complex(2, 3)}, ra::Small<complex, 2>{complex(4, 5), 6});
  105. test(ra::Big<complex, 1>{1, complex(2, 3)}, ra::Small<complex, 2>{complex(4, 5), 6});
  106. test(ra::Small<complex, 2>{1, complex(2, 3)}, ra::Big<complex, 1>{complex(4, 5), 6});
  107. test(ra::Big<complex, 1>{1, complex(2, 3)}, ra::Big<complex, 1>{complex(4, 5), 6});
  108. };
  109. complex value = conj(1.)*complex(4., 5.) + conj(complex(2., 3.))*6.;
  110. tr.test_eq(value, complex(16, -13));
  111. test_cdot([&tr](auto && a, auto && b) { tr.test_eq(complex(16., -13.), cdot(a, b)); });
  112. test_cdot([&tr](auto && a, auto && b) { tr.test_eq(sqrt(59.), norm2(a-b)); });
  113. test_cdot([&tr](auto && a, auto && b) { tr.test_eq(59., reduce_sqrm(a-b)); });
  114. auto test_sum = [](auto && test)
  115. {
  116. test(ra::Small<complex, 2>{complex(4, 5), 6});
  117. test(ra::Big<complex, 1>{complex(4, 5), 6});
  118. };
  119. test_sum([&tr](auto && a) { tr.test_eq(complex(10, 5), sum(a)); });
  120. test_sum([&tr](auto && a) { tr.test_eq(complex(24, 30), prod(a)); });
  121. test_sum([&tr](auto && a) { tr.test_eq(sqrt(41.), amax(abs(a))); });
  122. test_sum([&tr](auto && a) { tr.test_eq(6., amin(abs(a))); });
  123. }
  124. tr.section("amax/amin ignore NaN");
  125. {
  126. tr.test_eq(std::numeric_limits<real>::lowest(), std::max(std::numeric_limits<real>::lowest(), QNAN));
  127. tr.test_eq(-std::numeric_limits<real>::infinity(), amax(ra::Small<real, 3>(QNAN)));
  128. tr.test_eq(std::numeric_limits<real>::infinity(), amin(ra::Small<real, 3>(QNAN)));
  129. }
  130. // TODO these reductions require a destination argument; there are no exprs really.
  131. tr.section("to sum columns in crude ways");
  132. {
  133. ra::Unique<real, 2> A({100, 111}, ra::_0 - ra::_1);
  134. ra::Unique<real, 1> B({100}, 0.);
  135. for (int i=0, iend=A.size(0); i<iend; ++i) {
  136. B(i) = sum(A(i));
  137. }
  138. {
  139. ra::Unique<real, 1> C({100}, 0.);
  140. for_each([](auto & c, auto a) { c += a; }, C, A);
  141. tr.test_eq(B, C);
  142. }
  143. // This depends on matching frames for += just as for any other op, which is at odds with e.g. amend.
  144. {
  145. ra::Unique<real, 1> C({100}, 0.);
  146. C += A;
  147. tr.test_eq(B, C);
  148. }
  149. // Same as above.
  150. {
  151. ra::Unique<real, 1> C({100}, 0.);
  152. C = C + A;
  153. tr.test_eq(B, C);
  154. }
  155. // It cannot work with a lhs scalar value since += must be a class member, but it will work with a rank 0 array or with ra::Scalar.
  156. {
  157. ra::Unique<real, 0> C({}, 0.);
  158. C += A(0);
  159. tr.test_eq(B(0), C);
  160. real c(0.);
  161. ra::scalar(c) += A(0);
  162. tr.test_eq(B(0), c);
  163. }
  164. // This will fail because the assumed driver (RANK_ANY) has lower actual rank than the other argument. TODO check that it fails.
  165. // {
  166. // ra::Unique<real, 2> A({2, 3}, {1, 2, 3, 4 ,5, 6});
  167. // ra::Unique<real> C({}, 0.);
  168. // C += A(0);
  169. // }
  170. }
  171. tr.section("to sum rows in crude ways");
  172. {
  173. ra::Unique<real, 2> A({100, 111}, ra::_0 - ra::_1);
  174. ra::Unique<real, 1> B({111}, 0.);
  175. for (int j=0, jend=A.size(1); j<jend; ++j) {
  176. B(j) = sum(A(ra::all, j));
  177. }
  178. {
  179. ra::Unique<real, 1> C({111}, 0.);
  180. for_each([&C](auto && a) { C += a; }, A.iter<1>());
  181. tr.info("rhs iterator of rank > 0").test_eq(B, C);
  182. }
  183. {
  184. ra::Unique<real, 1> C({111}, 0.);
  185. for_each(ra::wrank<1, 1>([](auto & c, auto && a) { c += a; }), C, A);
  186. tr.info("rank conjuction").test_eq(B, C);
  187. }
  188. {
  189. ra::Unique<real, 1> C({111}, 0.);
  190. for_each(ra::wrank<1, 1>(ra::wrank<0, 0>([](auto & c, auto a) { c += a; })), C, A);
  191. tr.info("double rank conjunction").test_eq(B, C);
  192. }
  193. {
  194. ra::Unique<real, 1> C({111}, 0.);
  195. ra::scalar(C) += A.iter<1>();
  196. tr.info("scalar() and iterators of rank > 0").test_eq(B, C);
  197. }
  198. {
  199. ra::Unique<real, 1> C({111}, 0.);
  200. C.iter<1>() += A.iter<1>();
  201. tr.info("assign to iterators of rank > 0").test_eq(B, C);
  202. }
  203. }
  204. tr.section("reductions with amax");
  205. {
  206. ra::Big<int, 2> c({2, 3}, {1, 3, 2, 7, 1, 3});
  207. tr.info("max of rows").test_eq(ra::Big<int, 1> {3, 7}, map([](auto && a) { return amax(a); }, iter<1>(c)));
  208. ra::Big<int, 1> m({3}, 0);
  209. scalar(m) = max(scalar(m), iter<1>(c));
  210. tr.info("max of columns I").test_eq(ra::Big<int, 1> {7, 3, 3}, m);
  211. m = 0;
  212. iter<1>(m) = max(iter<1>(m), iter<1>(c));
  213. tr.info("max of columns III [ma113]").test_eq(ra::Big<int, 1> {7, 3, 3}, m);
  214. m = 0;
  215. for_each([&m](auto && a) { m = max(m, a); }, iter<1>(c));
  216. tr.info("max of columns II").test_eq(ra::Big<int, 1> {7, 3, 3}, m);
  217. ra::Big<double, 1> q({0}, {});
  218. tr.info("amax default").test_eq(std::numeric_limits<double>::infinity(), amin(q));
  219. tr.info("amin default").test_eq(-std::numeric_limits<double>::infinity(), amax(q));
  220. }
  221. tr.section("vector-matrix reductions");
  222. {
  223. auto test = [&tr](auto t, auto s, auto r)
  224. {
  225. using T = decltype(t);
  226. using S = decltype(s);
  227. using R = decltype(r);
  228. S x[4] = {1, 2, 3, 4};
  229. ra::Small<T, 3, 4> a = ra::_0 - ra::_1;
  230. R y[3] = {99, 99, 99};
  231. ra::start(y) = ra::gemv(a, x);
  232. auto z = ra::gemv(a, x);
  233. tr.test_eq(ra::Small<R, 3> {-20, -10, 0}, y);
  234. tr.test_eq(ra::Small<R, 3> {-20, -10, 0}, z);
  235. };
  236. test(double(0), double(0), double(0));
  237. test(std::complex<double>(0), std::complex<double>(0), std::complex<double>(0));
  238. test(int(0), int(0), int(0));
  239. test(int(0), double(0), double(0));
  240. test(double(0), int(0), double(0));
  241. }
  242. {
  243. auto test = [&tr](auto t, auto s, auto r)
  244. {
  245. using T = decltype(t);
  246. using S = decltype(s);
  247. using R = decltype(r);
  248. S x[4] = {1, 2, 3, 4};
  249. ra::Small<T, 4, 3> a = ra::_1 - ra::_0;
  250. R y[3] = {99, 99, 99};
  251. ra::start(y) = ra::gevm(x, a);
  252. auto z = ra::gevm(x, a);
  253. tr.test_eq(ra::Small<R, 3> {-20, -10, 0}, y);
  254. tr.test_eq(ra::Small<R, 3> {-20, -10, 0}, z);
  255. };
  256. test(double(0), double(0), double(0));
  257. test(std::complex<double>(0), std::complex<double>(0), std::complex<double>(0));
  258. test(int(0), int(0), int(0));
  259. test(int(0), double(0), double(0));
  260. test(double(0), int(0), double(0));
  261. }
  262. tr.section("matrix-matrix reductions");
  263. {
  264. ra::Big<double, 2> A({0, 0}, 0.);
  265. ra::Big<double, 2> B({0, 0}, 0.);
  266. auto C = gemm(A, B);
  267. tr.test_eq(0, C.size(0));
  268. tr.test_eq(0, C.size(1));
  269. }
  270. tr.section("reference reductions");
  271. {
  272. ra::Big<double, 2> A({2, 3}, ra::_1 - ra::_0);
  273. double & mn = refmin(A);
  274. tr.test_eq(-1, mn);
  275. mn = -99;
  276. ra::Big<double, 2> B({2, 3}, ra::_1 - ra::_0);
  277. B(1, 0) = -99;
  278. tr.test_eq(B, A);
  279. double & mx = refmin(A, std::greater<double>());
  280. tr.test_eq(2, mx);
  281. mx = 0;
  282. B(0, 2) = 0;
  283. tr.test_eq(B, A);
  284. double & my = refmax(A);
  285. tr.test_eq(1, my);
  286. my = 77;
  287. B(0, 1) = 77;
  288. tr.test_eq(B, A);
  289. // cout << refmin(A+B) << endl; // compile error
  290. }
  291. return tr.summary();
  292. }