from.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Checks for index selectors, both immediate and delayed.
  3. // (c) Daniel Llorens - 2014
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #include <iostream>
  9. #include <iterator>
  10. #include "ra/test.hh"
  11. #include "ra/mpdebug.hh"
  12. #include "ra/complex.hh"
  13. using std::cout, std::endl, std::flush, std::tuple, ra::TestRecorder;
  14. using real = double;
  15. template <int rank=ra::RANK_ANY> using Ureal = ra::Unique<real, rank>;
  16. using Vint = ra::Unique<int, 1>;
  17. int main()
  18. {
  19. TestRecorder tr(std::cout);
  20. tr.section("shortcuts");
  21. {
  22. auto check_selection_shortcuts = [&tr](auto && a)
  23. {
  24. tr.info("a()").test_eq(Ureal<2>({4, 4}, ra::_0-ra::_1), a());
  25. tr.info("a(2, :)").test_eq(Ureal<1>({4}, 2-ra::_0), a(2, ra::all));
  26. tr.info("a(2)").test_eq(Ureal<1>({4}, 2-ra::_0), a(2));
  27. tr.info("a(:, 3)").test_eq(Ureal<1>({4}, ra::_0-3), a(ra::all, 3));
  28. tr.info("a(:, :)").test_eq(Ureal<2>({4, 4}, ra::_0-ra::_1), a(ra::all, ra::all));
  29. tr.info("a(:)").test_eq(Ureal<2>({4, 4}, ra::_0-ra::_1), a(ra::all));
  30. tr.info("a(1)").test_eq(Ureal<1>({4}, 1-ra::_0), a(1));
  31. tr.info("a(2, 2)").test_eq(0, a(2, 2));
  32. tr.info("a(0:2:, 0:2:)").test_eq(Ureal<2>({2, 2}, 2*(ra::_0-ra::_1)),
  33. a(ra::iota(2, 0, 2), ra::iota(2, 0, 2)));
  34. tr.info("a(1:2:, 0:2:)").test_eq(Ureal<2>({2, 2}, 2*ra::_0+1-2*ra::_1),
  35. a(ra::iota(2, 1, 2), ra::iota(2, 0, 2)));
  36. tr.info("a(0:2:, :)").test_eq(Ureal<2>({2, 4}, 2*ra::_0-ra::_1),
  37. a(ra::iota(2, 0, 2), ra::all));
  38. tr.info("a(0:2:)").test_eq(a(ra::iota(2, 0, 2), ra::all), a(ra::iota(2, 0, 2)));
  39. };
  40. check_selection_shortcuts(Ureal<2>({4, 4}, ra::_0-ra::_1));
  41. check_selection_shortcuts(Ureal<>({4, 4}, ra::_0-ra::_1));
  42. }
  43. tr.section("Iota<int> or Iota<ra::dim_t> are both beatable");
  44. {
  45. Ureal<2> a({4, 4}, 0.);
  46. {
  47. auto i = ra::iota<int>(2, 1);
  48. auto b = a(i);
  49. tr.test_eq(2, b.dimv[0].len);
  50. tr.test_eq(4, b.dimv[1].len);
  51. tr.test_eq(4, b.dimv[0].step);
  52. tr.test_eq(1, b.dimv[1].step);
  53. }
  54. {
  55. auto i = ra::iota<ra::dim_t>(2, 1);
  56. auto b = a(i);
  57. tr.test_eq(2, b.dimv[0].len);
  58. tr.test_eq(4, b.dimv[1].len);
  59. tr.test_eq(4, b.dimv[0].step);
  60. tr.test_eq(1, b.dimv[1].step);
  61. }
  62. }
  63. tr.section("trivial case");
  64. {
  65. ra::Big<int, 3> a({2, 3, 4}, ra::_0*100 + ra::_1*10 + ra::_2);
  66. tr.test_eq(ra::_0*100 + ra::_1*10 + ra::_2, from(a));
  67. }
  68. tr.section("beatable multi-axis selectors, var size");
  69. {
  70. static_assert(ra::is_beatable<ra::dots_t<0>>::value, "dots_t<0> is beatable");
  71. ra::Big<int, 3> a({2, 3, 4}, ra::_0*100 + ra::_1*10 + ra::_2);
  72. tr.info("a(ra::dots<0> ...)").test_eq(a(0), a(ra::dots<0>, 0));
  73. tr.info("a(ra::dots<0> ...)").test_eq(a(1), a(ra::dots<0>, 1));
  74. tr.info("a(ra::dots<1> ...)").test_eq(a(ra::all, 0), a(ra::dots<1>, 0));
  75. tr.info("a(ra::dots<1> ...)").test_eq(a(ra::all, 1), a(ra::dots<1>, 1));
  76. tr.info("a(ra::dots<2> ...)").test_eq(a(ra::all, ra::all, 0), a(ra::dots<2>, 0));
  77. tr.info("a(ra::dots<2> ...)").test_eq(a(ra::all, ra::all, 1), a(ra::dots<2>, 1));
  78. tr.info("a(0)").test_eq(a(0, ra::all, ra::all), a(0));
  79. tr.info("a(1)").test_eq(a(1, ra::all, ra::all), a(1));
  80. tr.info("a(0, ra::dots<2>)").test_eq(a(0, ra::all, ra::all), a(0, ra::dots<2>));
  81. tr.info("a(1, ra::dots<2>)").test_eq(a(1, ra::all, ra::all), a(1, ra::dots<2>));
  82. }
  83. tr.section("beatable multi-axis selectors, fixed size");
  84. {
  85. static_assert(ra::is_beatable<ra::dots_t<0>>::value, "dots_t<0> is beatable");
  86. ra::Small<int, 2, 3, 4> a = ra::_0*100 + ra::_1*10 + ra::_2;
  87. tr.info("a(ra::dots<0> ...)").test_eq(a(0), a(ra::dots<0>, 0));
  88. tr.info("a(ra::dots<0> ...)").test_eq(a(1), a(ra::dots<0>, 1));
  89. tr.info("a(ra::dots<1> ...)").test_eq(a(ra::all, 0), a(ra::dots<1>, 0));
  90. tr.info("a(ra::dots<1> ...)").test_eq(a(ra::all, 1), a(ra::dots<1>, 1));
  91. tr.info("a(ra::dots<2> ...)").test_eq(a(ra::all, ra::all, 0), a(ra::dots<2>, 0));
  92. tr.info("a(ra::dots<2> ...)").test_eq(a(ra::all, ra::all, 1), a(ra::dots<2>, 1));
  93. tr.info("a(0)").test_eq(a(0, ra::all, ra::all), a(0));
  94. tr.info("a(1)").test_eq(a(1, ra::all, ra::all), a(1));
  95. tr.info("a(0, ra::dots<2>)").test_eq(a(0, ra::all, ra::all), a(0, ra::dots<2>));
  96. tr.info("a(1, ra::dots<2>)").test_eq(a(1, ra::all, ra::all), a(1, ra::dots<2>));
  97. }
  98. tr.section("insert, var size");
  99. {
  100. static_assert(ra::is_beatable<ra::insert_t<1>>::value, "insert_t<1> is beatable");
  101. ra::Big<int, 3> a({2, 3, 4}, ra::_0*100 + ra::_1*10 + ra::_2);
  102. tr.info("a(ra::insert<0> ...)").test_eq(a(0), a(ra::insert<0>, 0));
  103. ra::Big<int, 4> a1({1, 2, 3, 4}, ra::_1*100 + ra::_2*10 + ra::_3);
  104. tr.info("a(ra::insert<1> ...)").test_eq(a1, a(ra::insert<1>));
  105. ra::Big<int, 4> a2({2, 1, 3, 4}, ra::_0*100 + ra::_2*10 + ra::_3);
  106. tr.info("a(ra::all, ra::insert<1>, ...)").test_eq(a2, a(ra::all, ra::insert<1>));
  107. ra::Big<int, 5> a3({2, 1, 1, 3, 4}, ra::_0*100 + ra::_3*10 + ra::_4);
  108. tr.info("a(ra::all, ra::insert<2>, ...)").test_eq(a3, a(ra::all, ra::insert<2>));
  109. tr.info("a(0, ra::insert<1>, ...)").test_eq(a1(ra::all, 0), a(0, ra::insert<1>));
  110. tr.info("a(ra::insert<1>, 0, ...)").test_eq(a1(ra::all, 0), a(ra::insert<1>, 0));
  111. ra::Big<int, 4> aa1({2, 2, 3, 4}, a(ra::insert<1>));
  112. tr.info("insert with undefined len 0").test_eq(a, aa1(0));
  113. tr.info("insert with undefined len 1").test_eq(a, aa1(1));
  114. }
  115. tr.section("insert, var rank");
  116. {
  117. static_assert(ra::is_beatable<ra::insert_t<1>>::value, "insert_t<1> is beatable");
  118. ra::Big<int> a({2, 3, 4}, ra::_0*100 + ra::_1*10 + ra::_2);
  119. tr.info("a(ra::insert<0> ...)").test_eq(a(0), a(ra::insert<0>, 0));
  120. ra::Big<int> a1({1, 2, 3, 4}, ra::_1*100 + ra::_2*10 + ra::_3);
  121. tr.info("a(ra::insert<1> ...)").test_eq(a1, a(ra::insert<1>));
  122. ra::Big<int> a2({2, 1, 3, 4}, ra::_0*100 + ra::_2*10 + ra::_3);
  123. tr.info("a(ra::all, ra::insert<1>, ...)").test_eq(a2, a(ra::all, ra::insert<1>));
  124. ra::Big<int> a3({2, 1, 1, 3, 4}, ra::_0*100 + ra::_3*10 + ra::_4);
  125. tr.info("a(ra::all, ra::insert<2>, ...)").test_eq(a3, a(ra::all, ra::insert<2>));
  126. tr.info("a(0, ra::insert<1>, ...)").test_eq(a1(ra::all, 0), a(0, ra::insert<1>));
  127. tr.info("a(ra::insert<1>, 0, ...)").test_eq(a1(ra::all, 0), a(ra::insert<1>, 0));
  128. }
  129. tr.section("unbeatable, 1D");
  130. {
  131. auto check_selection_unbeatable_1 = [&tr](auto && a)
  132. {
  133. using CT = ra::Small<real, 4>;
  134. tr.info("a(i ...)").test_eq(CT {a[3], a[2], a[0], a[1]}, a(Vint {3, 2, 0, 1}));
  135. tr.info("a(i ...)").test_eq(CT {a[3], a[2], a[0], a[1]}, from(a, Vint {3, 2, 0, 1}));
  136. a = 0.;
  137. a(Vint {3, 2, 0, 1}) = CT {9, 7, 1, 4};
  138. tr.info("a(i ...) as lvalue").test_eq(CT {1, 4, 7, 9}, a);
  139. a = 0.;
  140. from(a, Vint {3, 2, 0, 1}) = CT {9, 7, 1, 4};
  141. tr.info("from(a i ...) as lvalue").test_eq(CT {1, 4, 7, 9}, a);
  142. a = 0.;
  143. from(a, Vint {3, 2, 0, 1}) = 77.;
  144. tr.info("from(a i ...) as lvalue, rank extend of right hand").test_eq(a, 77.);
  145. ra::Small<real, 2, 2> c = from(a, ra::Small<int, 2, 2> {3, 2, 0, 1});
  146. tr.info("a([x y; z w])").test_eq(ra::Small<real, 2, 2> {a[3], a[2], a[0], a[1]}, c);
  147. };
  148. check_selection_unbeatable_1(Ureal<1> {7, 9, 3, 4});
  149. check_selection_unbeatable_1(ra::Small<real, 4> {7, 9, 3, 4});
  150. check_selection_unbeatable_1(Ureal<>({4}, {7, 9, 3, 4}));
  151. }
  152. tr.section("unbeatable, 2D");
  153. {
  154. auto check_selection_unbeatable_2 = [&tr](auto && a)
  155. {
  156. using CT22 = ra::Small<real, 2, 2>;
  157. using CT2 = ra::Small<real, 2>;
  158. tr.info("a([0 1], [0 1])").test_eq(CT22 {a(0, 0), a(0, 1), a(1, 0), a(1, 1)},
  159. from(a, Vint {0, 1}, Vint {0, 1}));
  160. tr.info("a([0 1], [1 0])").test_eq(CT22 {a(0, 1), a(0, 0), a(1, 1), a(1, 0)},
  161. from(a, Vint {0, 1}, Vint {1, 0}));
  162. tr.info("a([1 0], [0 1])").test_eq(CT22 {a(1, 0), a(1, 1), a(0, 0), a(0, 1)},
  163. from(a, Vint {1, 0}, Vint {0, 1}));
  164. tr.info("a([1 0], [1 0])").test_eq(CT22 {a(1, 1), a(1, 0), a(0, 1), a(0, 0)},
  165. from(a, Vint {1, 0}, Vint {1, 0}));
  166. // TODO This is a nested array, which is a problem, we would use it just as from(a, [0 1], [0 1]).
  167. std::cout << "TODO [" << from(a, Vint {0, 1}) << "]" << std::endl;
  168. a = 0.;
  169. from(a, Vint {1, 0}, Vint {1, 0}) = CT22 {9, 7, 1, 4};
  170. tr.info("a([1 0], [1 0]) as lvalue").test_eq(CT22 {4, 1, 7, 9}, a);
  171. from(a, Vint {1, 0}, Vint {1, 0}) *= CT22 {9, 7, 1, 4};
  172. tr.info("a([1 0], [1 0]) as lvalue, *=").test_eq(CT22 {16, 1, 49, 81}, a);
  173. // Note the difference with J amend, which requires x in (x m} y) ~ (y[m] = x) to be a suffix of y[m]; but we apply the general mechanism which is prefix matching.
  174. from(a, Vint {1, 0}, Vint {1, 0}) = CT2 {9, 7};
  175. tr.info("a([1 0], [1 0]) as lvalue, rank extend of right hand").test_eq(CT22 {7, 7, 9, 9}, a);
  176. // TODO Test cases with rank!=1, starting with this couple which should work the same.
  177. std::cout << "-> " << from(a, Vint{1, 0}, 0) << std::endl;
  178. a = CT22 {4, 1, 7, 9};
  179. tr.info("a(rank1, rank0)").test_eq(ra::Small<real, 2>{9, 1}, from(a, Vint{1, 0}, ra::Small<int>(1).iter()));
  180. tr.info("a(rank0, rank1)").test_eq(ra::Small<real, 2>{9, 7}, from(a, ra::Small<int>(1).iter(), Vint{1, 0}));
  181. };
  182. check_selection_unbeatable_2(Ureal<2>({2, 2}, {1, 2, 3, 4}));
  183. check_selection_unbeatable_2(ra::Small<real, 2, 2>({1, 2, 3, 4}));
  184. check_selection_unbeatable_2(Ureal<>({2, 2}, {1, 2, 3, 4}));
  185. }
  186. tr.section("mixed scalar/unbeatable, 2D -> 1D");
  187. {
  188. auto check_selection_unbeatable_mixed = [&tr](auto && a)
  189. {
  190. using CT2 = ra::Small<real, 2>;
  191. tr.info("from(a [0 1], 1)").test_eq(CT2 {a(0, 1), a(1, 1)}, from(a, Vint {0, 1}, 1));
  192. tr.info("from(a [1 0], 1)").test_eq(CT2 {a(1, 1), a(0, 1)}, from(a, Vint {1, 0}, 1));
  193. tr.info("from(a 1, [0 1])").test_eq(CT2 {a(1, 0), a(1, 1)}, from(a, 1, Vint {0, 1}));
  194. tr.info("from(a 1, [1 0])").test_eq(CT2 {a(1, 1), a(1, 0)}, from(a, 1, Vint {1, 0}));
  195. tr.info("a([0 1], 1)").test_eq(CT2 {a(0, 1), a(1, 1)}, a(Vint {0, 1}, 1));
  196. tr.info("a([1 0], 1)").test_eq(CT2 {a(1, 1), a(0, 1)}, a(Vint {1, 0}, 1));
  197. tr.info("a(1, [0 1])").test_eq(CT2 {a(1, 0), a(1, 1)}, a(1, Vint {0, 1}));
  198. tr.info("a(1, [1 0])").test_eq(CT2 {a(1, 1), a(1, 0)}, a(1, Vint {1, 0}));
  199. };
  200. check_selection_unbeatable_mixed(Ureal<2>({2, 2}, {1, 2, 3, 4}));
  201. check_selection_unbeatable_mixed(ra::Small<real, 2, 2>({1, 2, 3, 4}));
  202. }
  203. tr.section("mixed unbeatable/dots, 2D -> 2D (TODO)");
  204. {
  205. // auto check_selection_unbeatable_dots = [&tr](auto && a)
  206. // {
  207. // using CT2 = ra::Small<real, 2>;
  208. // tr.info("a({0, 0}, ra::all)").test_eq(a(CT2 {0, 0}, ra::all), a(CT2 {0, 0}, CT2 {0, 1}));
  209. // tr.info("a({0, 1}, ra::all)").test_eq(a(CT2 {0, 1}, ra::all), a(CT2 {0, 1}, CT2 {0, 1}));
  210. // tr.info("a({1, 0}, ra::all)").test_eq(a(CT2 {1, 0}, ra::all), a(CT2 {1, 0}, CT2 {0, 1}));
  211. // tr.info("a({1, 1}, ra::all)").test_eq(a(CT2 {1, 1}, ra::all), a(CT2 {1, 1}, CT2 {0, 1}));
  212. // };
  213. // TODO doesn't work because dots_t<> can only be beaten on, not iterated on, and the beating cases are missing.
  214. // check_selection_unbeatable_dots(Ureal<2>({2, 2}, {1, 2, 3, 4}));
  215. // check_selection_unbeatable_dots(ra::Small<real, 2, 2>({1, 2, 3, 4}));
  216. }
  217. tr.section("unbeatable, 3D & higher");
  218. {
  219. // see src/test/bench-from.cc for examples of higher-D.
  220. }
  221. tr.section("TensorIndex / where TODO elsewhere");
  222. {
  223. Ureal<2> a({4, 4}, 1.);
  224. a(3, 3) = 7.;
  225. tr.test(every(ra::map([](auto a, int i, int j)
  226. {
  227. return a==(i==3 && j==3 ? 7. : 1.);
  228. },
  229. a, ra::_0, ra::_1)));
  230. tr.test_eq(where(ra::_0==3 && ra::_1==3, 7., 1.), a);
  231. }
  232. // The implementation of from() uses FrameMatch / Reframe and can't handle this yet.
  233. tr.section("TensorIndex<i> as subscript, using ra::Expr directly.");
  234. {
  235. auto i = ra::_0;
  236. auto j = ra::_1;
  237. Ureal<2> a({4, 3}, i-j);
  238. Ureal<2> b({3, 4}, 0.);
  239. b = map([&a](int i, int j) { return a(i, j); }, j, i);
  240. tr.test_eq(i-j, a);
  241. tr.test_eq(j-i, b);
  242. }
  243. tr.section("TensorIndex<i> as subscripts, 1 subscript TODO elsewhere");
  244. {
  245. Ureal<1> a {1, 4, 2, 3};
  246. Ureal<1> b({4}, 0.);
  247. // these work bc there's another term to drive the expr.
  248. b = a(3-ra::_0);
  249. tr.test_eq(Ureal<1> {3, 2, 4, 1}, b);
  250. b(3-ra::_0) = a;
  251. tr.test_eq(Ureal<1> {3, 2, 4, 1}, b);
  252. }
  253. tr.section("TODO TensorIndex<i> as subscripts, 2 subscript (case I)");
  254. {
  255. Ureal<2> a({4, 4}, ra::_0-ra::_1);
  256. Ureal<2> b({4, 4}, -99.);
  257. cout << a << endl;
  258. cout << b << endl;
  259. // b = a(ra::_0, ra::_0);
  260. }
  261. tr.section("TODO TensorIndex<i> as subscripts, 2 subscript (case II)");
  262. {
  263. Ureal<2> a({4, 4}, ra::_0-ra::_1);
  264. Ureal<2> b({4, 4}, 0.);
  265. cout << a << endl;
  266. cout << b << endl;
  267. // TODO these instantiate flat() when they should not (FIXME was for old OldTensorIndex; recheck)
  268. // tr.info("by_index I").test(ra::by_index<decltype(a(ra::_1, ra::_0))>);
  269. // cout << ra::mp::ref<decltype(a(ra::_1, ra::_0))>::rank_s() << endl;
  270. // these don't work because a(j, i) has rank 3 = [(w=1)+1 + (w=0)+1] and so it drives, but tensorindex exprs shouldn't ever drive.
  271. // tr.info("by_index II").test(ra::by_index<decltype(b+a(ra::_1, ra::_0))>);
  272. // cout << ra::mp::ref<decltype(b+a(ra::_1, ra::_0))::T, 0>::rank_s() << endl;
  273. // cout << ra::mp::ref<decltype(b+a(ra::_1, ra::_0))::T, 1>::rank_s() << endl;
  274. cout << ra::mp::ref<decltype(ra::_1)>::rank_s() << endl;
  275. // b = a(ra::_1, ra::_0);
  276. }
  277. // Small(Iota) isn't beaten because the the output type cannot depend on argument values. So we treat it as a common expr.
  278. tr.section("ra::Small(Iota)");
  279. {
  280. ra::Small<real, 4> a = ra::_0;
  281. tr.test_eq(a(ra::iota(2, 1)), Ureal<1> { 1, 2 });
  282. }
  283. // Indirection operator using list of coordinates.
  284. tr.section("at() indirection");
  285. {
  286. ra::Big<int, 2> A({4, 4}, 0), B({4, 4}, 10*ra::_0 + ra::_1);
  287. using coord = ra::Small<int, 2>;
  288. ra::Big<coord, 1> I = { {1, 1}, {2, 2} };
  289. at(A, I) = at(B, I);
  290. tr.test_eq(ra::Big<int>({4, 4}, {0, 0, 0, 0, /**/ 0, 11, 0, 0, /**/ 0, 0, 22, 0, /**/ 0, 0, 0, 0}), A);
  291. // TODO this is why we need ops to have explicit rank.
  292. at(A, ra::scalar(coord{3, 2})) = 99.;
  293. tr.test_eq(ra::Big<int>({4, 4}, {0, 0, 0, 0, /**/ 0, 11, 0, 0, /**/ 0, 0, 22, 0, /**/ 0, 0, 99, 0}), A);
  294. }
  295. // From the manual [ra30]
  296. {
  297. ra::Big<int, 2> A = {{100, 101}, {110, 111}, {120, 121}};
  298. ra::Big<ra::Small<int, 2>, 2> i = {{{0, 1}, {2, 0}}, {{1, 0}, {2, 1}}};
  299. ra::Big<int, 2> B = at(A, i);
  300. tr.test_eq(ra::Big<int, 2> {{101, 120}, {110, 121}}, at(A, i));
  301. }
  302. return tr.summary();
  303. }