test-from.C 16 KB

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