from.cc 16 KB

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