ra-1.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file ra-1.C
  3. /// @brief Fundamental tests for ra::.
  4. // (c) Daniel Llorens - 2013-2015
  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 <numeric>
  12. #include "ra/complex.H"
  13. #include "ra/test.H"
  14. #include "ra/ra.H"
  15. #include "test/old.H"
  16. using std::cout, std::endl, std::flush;
  17. using A2 = ra::Unique<int, 2>;
  18. using A1 = ra::Unique<int, 1>;
  19. using int3 = ra::Small<int, 3>;
  20. using int2 = ra::Small<int, 2>;
  21. using std_int3 = std::array<int, 3>;
  22. using std_int2 = std::array<int, 2>;
  23. template <int i> using TI = ra::TensorIndex<i, int>;
  24. template <class AA>
  25. void CheckPlyReverse1(TestRecorder & tr, AA && a)
  26. {
  27. std::iota(a.begin(), a.end(), 1);
  28. auto invert = [](int & a) { a = -a; return a; };
  29. ply_ravel(ra::expr(invert, a.iter()));
  30. for (int i=0; i<6; ++i) {
  31. tr.test_eq(-(i+1), a(i));
  32. }
  33. auto b = reverse(a, 0);
  34. ply_ravel(ra::expr(invert, b.iter()));
  35. for (int i=0; i<6; ++i) {
  36. tr.test_eq(6-i, b(i));
  37. tr.test_eq(i+1, a(i));
  38. }
  39. }
  40. template <class CC, class AA, class BB>
  41. void CheckPly(TestRecorder & tr, char const * tag, AA && A, BB && B)
  42. {
  43. // need to slice because B may be Unique (!) and I have left own-type constructors as default on purpose. Here, I need C's contents to be a fresh copy of B's.
  44. CC C(B());
  45. auto sub = [](int & b, int const a) -> int { return b -= a; };
  46. ra::ply_ravel(ra::expr(sub, B.iter(), A.iter()));
  47. for (int i=0; i!=A.size(0); ++i) {
  48. for (int j=0; j!=A.size(1); ++j) {
  49. tr.info(tag, " ravel").test_eq(C(i, j)-A(i, j), B(i, j));
  50. }
  51. }
  52. auto add = [](int & b, int const a) -> int { return b += a; };
  53. ra::ply_ravel(ra::expr(add, B.iter(), A.iter()));
  54. ra::ply_index(ra::expr(sub, B.iter(), A.iter()));
  55. for (int i=0; i!=A.size(0); ++i) {
  56. for (int j=0; j!=A.size(1); ++j) {
  57. tr.info(tag, " index").test_eq(C(i, j)-A(i, j), B(i, j));
  58. }
  59. }
  60. }
  61. using complex = std::complex<double>;
  62. int main()
  63. {
  64. TestRecorder tr(std::cout);
  65. tr.section("nested, with references, ply_index or ply_ravel");
  66. {
  67. int check[3] = {0, 2, 4};
  68. ra::Small<int, 3> A {1, 0, -1};
  69. ra::Small<int, 3> B {1, 2, 3};
  70. #define TEST(plier) \
  71. [&tr, &A, &B, check](auto && C) \
  72. { \
  73. std::fill(C.begin(), C.end(), -99); \
  74. plier(ra::expr([](int & k, int const i) { k = -i; }, \
  75. C.iter(), \
  76. ra::expr([](int const i, int const j) { return i-j; }, \
  77. A.iter(), B.iter()))); \
  78. tr.test(std::equal(check, check+3, C.begin())); \
  79. }
  80. #define TEST2(plier) \
  81. TEST(plier)(ra::Small<int, 3> {}); \
  82. TEST(plier)(ra::Unique<int, 1>({3}, ra::none));
  83. TEST2(ply_ravel)
  84. TEST2(ply_index)
  85. TEST2(plyf)
  86. TEST2(plyf_index)
  87. #undef TEST2
  88. #undef TEST
  89. }
  90. tr.section("with ref terms only");
  91. {
  92. #define TEST(plier, Biter, Citer) \
  93. [&tr](auto && B, auto && C) \
  94. { \
  95. plier(ra::expr([](int & k, int const i, int const j) { k = i+j; return k; }, \
  96. Citer, Biter, Biter)); \
  97. tr.test_eq(2, C[0]); \
  98. tr.test_eq(4, C[1]); \
  99. tr.test_eq(6, C[2]); \
  100. }
  101. #define TEST2(plier) \
  102. TEST(plier, B.iter(), C.iter())(int3 { 1, 2, 3 }, int3 { 77, 88, 99 }); \
  103. TEST(plier, ra::vector(B), ra::vector(C))(std_int3 {{ 1, 2, 3 }}, std_int3 {{ 77, 88, 99 }});
  104. TEST2(ply_ravel)
  105. TEST2(ply_index)
  106. TEST2(plyf)
  107. TEST2(plyf_index)
  108. #undef TEST2
  109. #undef TEST
  110. }
  111. tr.section("with ref & value terms");
  112. {
  113. #define TEST(plier, Biter, Citer, Btemp) \
  114. [&tr](auto && B, auto && C) \
  115. { \
  116. plier(ra::expr([](int & k, int const i, int const j) { k = i*j; return k; }, \
  117. Citer, Btemp, Biter)); \
  118. tr.test_eq(1, C[0]); \
  119. tr.test_eq(4, C[1]); \
  120. tr.test_eq(9, C[2]); \
  121. }
  122. TEST(ply_ravel, B.iter(), C.iter(), (int3 {1, 2, 3}.iter()))(int3 { 1, 2, 3 }, int3 { 77, 88, 99 });
  123. TEST(ply_index, B.iter(), C.iter(), (int3 {1, 2, 3}.iter()))(int3 { 1, 2, 3 }, int3 { 77, 88, 99 });
  124. TEST(ply_ravel, ra::vector(B), ra::vector(C), ra::vector(std_int3 {{1, 2, 3}}))
  125. (std_int3 {{ 1, 2, 3 }}, std_int3 {{ 77, 88, 99 }});
  126. TEST(ply_index, ra::vector(B), ra::vector(C), ra::vector(std_int3 {{1, 2, 3}}))
  127. (std_int3 {{ 1, 2, 3 }}, std_int3 {{ 77, 88, 99 }});
  128. TEST(ply_index, ra::vector(B), ra::vector(C), ra::start({1, 2, 3}))(int3 { 1, 2, 3 }, int3 { 77, 88, 99 });
  129. #undef TEST
  130. }
  131. tr.section("complex or nested types");
  132. {
  133. int i;
  134. using A2of2 = ra::Unique<int2, 2>;
  135. auto sum2 = [](int2 const i, int2 const j, int2 & x) { x = { i[0]+j[0], i[1]+j[1] }; };
  136. A2of2 A({2, 3}, { {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6} });
  137. ply_index(ra::expr([](int2 & a, int i, int j) { int k = i*3+j; a = {k, k}; },
  138. A.iter(), TI<0>(), TI<1>()));
  139. A2of2 B({2, 3}, ra::scalar(int2 {0, 0}));
  140. cout << "A: " << A << endl;
  141. cout << "B: " << B << endl;
  142. cout << "\ntraverse_index..." << endl;
  143. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  144. ply_index(ra::expr(sum2, A.iter(), ra::scalar(int2{2, 2}), B.iter()));
  145. cout << B << endl;
  146. i=2; for (int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  147. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  148. ply_index(ra::expr(sum2, ra::scalar(int2{3, 3}), A.iter(), B.iter()));
  149. cout << B << endl;
  150. i=3; for (int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  151. cout << "\ntraverse..." << endl;
  152. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  153. ply_ravel(ra::expr(sum2, A.iter(), ra::scalar(int2{4, 5}), B.iter()));
  154. cout << B << endl;
  155. i=4; for (int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i+1, b[1]); ++i; }
  156. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  157. ply_ravel(ra::expr(sum2, ra::scalar(int2{5, 5}), A.iter(), B.iter()));
  158. cout << B << endl;
  159. i=5; for (int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  160. }
  161. tr.section("reversed arrays, ply_index");
  162. {
  163. ra::Unique<int, 1> A({ 6 }, ra::none);
  164. std::iota(A.begin(), A.end(), 1);
  165. ra::Unique<int, 1> B { {6}, ra::scalar(99) };
  166. auto copy = [](int & b, int const a) { b = a; return b; };
  167. ply_index(ra::expr(copy, B.iter(), A.iter()));
  168. for (int i=0; i<6; ++i) {
  169. tr.test_eq(i+1, B(i));
  170. }
  171. ply_index(ra::expr(copy, B.iter(), reverse(A, 0).iter()));
  172. for (int i=0; i<6; ++i) {
  173. tr.test_eq(6-i, B(i));
  174. }
  175. }
  176. tr.section("reversed arrays, traverse, only one");
  177. {
  178. CheckPlyReverse1(tr, ra::Unique<int, 1>({ 6 }, ra::none));
  179. CheckPlyReverse1(tr, ra::Unique<int>({ 6 }, ra::none));
  180. }
  181. tr.section("mismatched strides");
  182. {
  183. auto sum2 = [](int a, int b, int & c) { return c = a-b; };
  184. A2 a = A2({2, 3}, ra::none); std::iota(a.begin(), a.end(), 1);
  185. A2 b = A2({3, 2}, ra::none); std::iota(b.begin(), b.end(), 1);
  186. A2 c = A2({2, 3}, ra::none);
  187. int check[6] = {0, -1, -2, 2, 1, 0};
  188. #define TEST(plier) \
  189. { \
  190. std::fill(c.begin(), c.end(), 0); \
  191. plier(ra::expr(sum2, a.iter(), transpose<1, 0>(b).iter(), c.iter())); \
  192. tr.info(STRINGIZE(plier)).test(std::equal(check, check+6, c.begin())); \
  193. } \
  194. { \
  195. std::fill(c.begin(), c.end(), 0); \
  196. plier(ra::expr(sum2, transpose<1, 0>(a).iter(), b.iter(), transpose<1, 0>(c).iter())); \
  197. tr.info(STRINGIZE(plier)).test(std::equal(check, check+6, c.begin())); \
  198. }
  199. TEST(ply_ravel);
  200. TEST(ply_index);
  201. TEST(plyf);
  202. TEST(plyf_index);
  203. #undef TEST
  204. }
  205. tr.section("reverse 1/1 axis, traverse");
  206. #define TEST(plier) \
  207. { \
  208. A1 a({ 6 }, ra::none); \
  209. std::iota(a.begin(), a.end(), 1); \
  210. A1 b { {6}, ra::scalar(99) }; \
  211. auto copy = [](int & b, int const a) { b = a; }; \
  212. plier(ra::expr(copy, b.iter(), a.iter())); \
  213. cout << flush; \
  214. for (int i=0; i<6; ++i) { \
  215. tr.test_eq(i+1, b[i]); \
  216. } \
  217. plier(ra::expr(copy, b.iter(), reverse(a, 0).iter())); \
  218. for (int i=0; i<6; ++i) { \
  219. tr.test_eq(6-i, b(i)); \
  220. } \
  221. }
  222. TEST(ply_index)
  223. TEST(ply_ravel)
  224. TEST(plyf)
  225. TEST(plyf_index)
  226. #undef TEST
  227. tr.section("reverse (ref & non ref), traverse");
  228. {
  229. A2 A({2, 3}, { 1, 2, 3, 4, 5, 6 });
  230. A2 B({2, 3}, { 1, 2, 3, 4, 5, 6 });
  231. CheckPly<A2>(tr, "(a)", A, B);
  232. CheckPly<A2>(tr, "(b)", reverse(A, 0), B);
  233. CheckPly<A2>(tr, "(c)", A, reverse(B, 0));
  234. CheckPly<A2>(tr, "(d)", reverse(A, 0), reverse(B, 0));
  235. CheckPly<A2>(tr, "(e)", reverse(A, 1), B);
  236. CheckPly<A2>(tr, "(f)", A, reverse(B, 1));
  237. CheckPly<A2>(tr, "(g)", reverse(A, 1), reverse(B, 1));
  238. // When BOTH strides are negative, B is still compact and this can be reduced to a single loop.
  239. // TODO Enforce that the loop is linearized over both dimensions.
  240. CheckPly<A2>(tr, "(h)", A, reverse(reverse(B, 0), 1));
  241. CheckPly<A2>(tr, "(i)", reverse(reverse(A, 0), 1), B);
  242. CheckPly<A2>(tr, "(j)", reverse(reverse(A, 0), 1), reverse(reverse(B, 0), 1));
  243. }
  244. tr.section("reverse & transpose (ref & non ref), traverse");
  245. {
  246. using A2 = ra::Unique<int, 2>;
  247. A2 A({2, 2}, { 1, 2, 3, 4 });
  248. A2 B({2, 2}, { 1, 2, 3, 4 });
  249. CheckPly<A2>(tr, "(a)", transpose({1, 0}, A), B);
  250. CheckPly<A2>(tr, "(b)", A, transpose({1, 0}, B));
  251. CheckPly<A2>(tr, "(c)", reverse(reverse(transpose({1, 0}, A), 1), 0), B);
  252. CheckPly<A2>(tr, "(d)", A, reverse(reverse(transpose({1, 0}, B), 1), 0));
  253. CheckPly<A2>(tr, "(e)", transpose<1, 0>(A), B);
  254. CheckPly<A2>(tr, "(f)", A, transpose<1, 0>(B));
  255. CheckPly<A2>(tr, "(g)", reverse(reverse(transpose<1, 0>(A), 1), 0), B);
  256. CheckPly<A2>(tr, "(h)", A, reverse(reverse(transpose<1, 0>(B), 1), 0));
  257. CheckPly<A2>(tr, "(i)", transpose(mp::int_list<1, 0>(), A), B);
  258. CheckPly<A2>(tr, "(j)", A, transpose(mp::int_list<1, 0>(), B));
  259. CheckPly<A2>(tr, "(k)", reverse(reverse(transpose(mp::int_list<1, 0>(), A), 1), 0), B);
  260. CheckPly<A2>(tr, "(l)", A, reverse(reverse(transpose(mp::int_list<1, 0>(), B), 1), 0));
  261. }
  262. return tr.summary();
  263. }