explode-0.C 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file explode-0.C
  3. /// @brief Tests for explode() and collapse().
  4. // (c) Daniel Llorens - 2013-2016
  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/view-ops.H"
  15. #include "ra/operators.H"
  16. #include "ra/io.H"
  17. #include "ra/mpdebug.H"
  18. using std::cout, std::endl, std::flush;
  19. using real = double;
  20. using complex = std::complex<double>;
  21. int main()
  22. {
  23. TestRecorder tr(std::cout);
  24. tr.section("explode");
  25. {
  26. ra::Big<int, 2> A({2, 3}, ra::_0 - ra::_1);
  27. auto B = ra::explode<ra::Small<int, 3>>(A);
  28. tr.test_eq(3, size_s(B(0)));
  29. tr.test_eq(ra::Small<int, 3> {0, -1, -2}, B(0));
  30. tr.test_eq(ra::Small<int, 3> {1, 0, -1}, B(1));
  31. B(1) = 9;
  32. tr.test_eq(ra::Small<int, 3> {0, -1, -2}, B(0));
  33. tr.test_eq(ra::Small<int, 3> {9, 9, 9}, B(1));
  34. }
  35. // note that dynamic-rank operator() returns a rank 0 array (since the rank
  36. // cannot be known at compile time). So we have to peel that back.
  37. {
  38. ra::Big<int> A({2, 3}, ra::_0 - ra::_1);
  39. auto B = ra::explode<ra::Small<int, 3>>(A);
  40. tr.test_eq(3, size_s(*(B(0).data())));
  41. tr.test_eq(ra::scalar(ra::Small<int, 3> {0, -1, -2}), B(0));
  42. tr.test_eq(ra::scalar(ra::Small<int, 3> {1, 0, -1}), B(1));
  43. B(1) = 9;
  44. tr.test_eq(ra::scalar(ra::Small<int, 3> {0, -1, -2}), B(0));
  45. tr.test_eq(ra::scalar(ra::Small<int, 3> {9, 9, 9}), B(1));
  46. }
  47. tr.section("explode<complex>");
  48. {
  49. ra::Big<real, 3> A({2, 3, 2}, ra::_0 - ra::_1 + ra::_2);
  50. auto B = ra::explode<complex>(A);
  51. tr.test_eq(2, B.rank());
  52. tr.test_eq(ra::Small<real, 2, 3> {0, -1, -2, 1, 0, -1}, real_part(B));
  53. tr.test_eq(ra::Small<real, 2, 3> {1, 0, -1, 2, 1, 0}, imag_part(B));
  54. imag_part(B(1)) = 9;
  55. tr.test_eq(ra::Small<real, 2, 3> {0, -1, -2, 1, 0, -1}, A(ra::all, ra::all, 0));
  56. tr.test_eq(ra::Small<real, 2, 3> {1, 0, -1, 9, 9, 9}, A(ra::all, ra::all, 1));
  57. }
  58. {
  59. ra::Big<real> A({2, 3, 2}, ra::_0 - ra::_1 + ra::_2);
  60. auto B = ra::explode<complex>(A);
  61. tr.test_eq(2, B.rank());
  62. tr.test_eq(ra::Small<real, 2, 3> {0, -1, -2, 1, 0, -1}, real_part(B));
  63. tr.test_eq(ra::Small<real, 2, 3> {1, 0, -1, 2, 1, 0}, imag_part(B));
  64. imag_part(B(1)) = 9;
  65. tr.test_eq(ra::Small<real, 2, 3> {0, -1, -2, 1, 0, -1}, A(ra::all, ra::all, 0));
  66. tr.test_eq(ra::Small<real, 2, 3> {1, 0, -1, 9, 9, 9}, A(ra::all, ra::all, 1));
  67. }
  68. tr.section("collapse");
  69. {
  70. tr.section("sub is real to super complex");
  71. {
  72. auto test_sub_real = [&tr](auto && A)
  73. {
  74. A = ra::cast<double>(ra::_0)*complex(4, 1) + ra::cast<double>(ra::_1)*complex(1, 4);
  75. auto B = ra::collapse<double>(A);
  76. tr.test_eq(real_part(A), B(ra::all, ra::all, 0));
  77. tr.test_eq(imag_part(A), B(ra::all, ra::all, 1));
  78. };
  79. test_sub_real(ra::Unique<complex, 2>({4, 4}, ra::none));
  80. test_sub_real(ra::Unique<complex>({4, 4}, ra::none));
  81. }
  82. tr.section("sub is int to super Small of rank 1");
  83. {
  84. using r2 = ra::Small<int, 2>;
  85. auto test_sub_small2 = [&tr](auto && A)
  86. {
  87. A = map([](int i, int j) { return r2 {i+j, i-j}; }, ra::_0, ra::_1);
  88. auto B = ra::collapse<int>(A);
  89. tr.test_eq(B(ra::all, ra::all, 0), map([](auto && a) { return a(0); }, A));
  90. tr.test_eq(B(ra::all, ra::all, 1), map([](auto && a) { return a(1); }, A));
  91. };
  92. test_sub_small2(ra::Unique<r2, 2>({4, 4}, ra::none));
  93. test_sub_small2(ra::Unique<r2>({4, 4}, ra::none));
  94. }
  95. tr.section("sub is int to super Small of rank 2");
  96. {
  97. using super = ra::Small<int, 2, 3>;
  98. auto test_sub_small23 = [&tr](auto && A)
  99. {
  100. A = map([](int i, int j) { return super(i-j+ra::_0-ra::_1); }, ra::_0, ra::_1);
  101. auto B = ra::collapse<int>(A);
  102. for (int i=0; i<super::size(0); ++i) {
  103. for (int j=0; j<super::size(1); ++j) {
  104. tr.test_eq(B(ra::all, ra::all, i, j), map([i, j](auto && a) { return a(i, j); }, A));
  105. }
  106. }
  107. };
  108. test_sub_small23(ra::Unique<super, 2>({2, 2}, ra::none));
  109. test_sub_small23(ra::Unique<super>({2, 2}, ra::none));
  110. }
  111. tr.section("sub is Small of rank 1 to super Small of rank 2");
  112. {
  113. using super = ra::Small<int, 2, 3>;
  114. auto test_sub_small23 = [&tr](auto && A)
  115. {
  116. A = map([](int i, int j) { return super(i-j+ra::_0-ra::_1); }, ra::_0, ra::_1);
  117. using sub = ra::Small<int, 3>;
  118. auto B = ra::collapse<sub>(A);
  119. // TODO sub() is used to cover a problem with where() and SmallView/SmallArray, since they convert to each other
  120. tr.test_eq(B(ra::all, ra::all, 0), map([](auto && a) { return sub(a(0)); }, A));
  121. tr.test_eq(B(ra::all, ra::all, 1), map([](auto && a) { return sub(a(1)); }, A));
  122. };
  123. test_sub_small23(ra::Unique<super, 2>({2, 2}, ra::none));
  124. test_sub_small23(ra::Unique<super>({2, 2}, ra::none));
  125. }
  126. tr.section("sub is real to super complex Small of rank 2");
  127. {
  128. using super = ra::Small<complex, 2, 2>;
  129. auto test_sub_real = [&tr](auto && A)
  130. {
  131. A = map([](complex a) { return super { a, conj(a), -conj(a), -a }; },
  132. ra::cast<double>(ra::_0)*complex(4, 1) + ra::cast<double>(ra::_1)*complex(1, 4));
  133. auto B = ra::collapse<double>(A);
  134. for (int i=0; i<super::size(0); ++i) {
  135. for (int j=0; j<super::size(1); ++j) {
  136. tr.test_eq(B(ra::all, ra::all, i, j, 0), map([i, j](auto && a) { return real_part(a(i, j)); }, A));
  137. tr.test_eq(B(ra::all, ra::all, i, j, 1), map([i, j](auto && a) { return imag_part(a(i, j)); }, A));
  138. }
  139. }
  140. };
  141. test_sub_real(ra::Unique<super, 2>({4, 4}, ra::none));
  142. test_sub_real(ra::Unique<super>({4, 4}, ra::none));
  143. }
  144. }
  145. tr.section("old tests");
  146. {
  147. tr.section("super rank 1");
  148. {
  149. auto test = [&tr](auto && A)
  150. {
  151. auto B = ra::explode<ra::Small<double, 2>>(A);
  152. for (int i=0; i<3; ++i) {
  153. tr.test_eq(i*2, B[i](0));
  154. tr.test_eq(i*2+1, B[i](1));
  155. }
  156. };
  157. test(ra::Unique<double, 2>({4, 2}, ra::_0*2 + ra::_1));
  158. test(ra::Unique<double>({4, 2}, ra::_0*2 + ra::_1));
  159. }
  160. tr.section("super rank 0");
  161. {
  162. #define TEST(CHECK_RANK_S) \
  163. [&tr](auto && A) \
  164. { \
  165. auto B = ra::explode_<complex, 1>(A); \
  166. static_assert(ra::ra_traits<decltype(B)>::rank_s()==CHECK_RANK_S, "bad static rank"); \
  167. cout << B << endl; \
  168. /* TODO B(0) etc. doesn't get converted to r2x2 & for RANK_ANY, and it should. */ \
  169. for (int i=0; i<3; ++i) { \
  170. tr.test_eq(i*2, B[i].real()); \
  171. tr.test_eq(i*2+1, B[i].imag()); \
  172. } \
  173. }
  174. TEST(ra::RANK_ANY)(ra::Unique<double>({4, 2}, ra::_0*2 + ra::_1));
  175. TEST(1)(ra::Unique<double, 2>({4, 2}, ra::_0*2 + ra::_1));
  176. }
  177. tr.section("super rank 2");
  178. {
  179. using r2x2 = ra::Small<double, 2, 2>;
  180. auto test = [&tr](auto && A)
  181. {
  182. auto B = ra::explode<r2x2>(A);
  183. tr.test_eq(1, B.rank());
  184. // TODO B(0) etc. doesn't get converted to r2x2 & for RANK_ANY, and it should.
  185. tr.test_eq(r2x2 { 0, 1, 2, 3 }, B[0]);
  186. tr.test_eq(r2x2 { 4, 5, 6, 7 }, B[1]);
  187. tr.test_eq(r2x2 { 8, 9, 10, 11 }, B[2]);
  188. tr.test_eq(r2x2 { 12, 13, 14, 15}, B[3]);
  189. };
  190. test(ra::Unique<double, 3>({4, 2, 2}, ra::_0*4 + ra::_1*2 + ra::_2));
  191. test(ra::Unique<double>({4, 2, 2}, ra::_0*4 + ra::_1*2 + ra::_2));
  192. }
  193. }
  194. tr.section("explode for Small");
  195. {
  196. ra::Small<double, 2, 3> a(ra::_0 + 10*ra::_1);
  197. auto c = ra::explode<ra::Small<double, 3>>(a);
  198. using sizes = std::decay_t<decltype(c)>::sizes;
  199. using strides = std::decay_t<decltype(c)>::strides;
  200. tr.info(mp::print_int_list<sizes> {}).test(std::is_same_v<mp::int_list<2>, sizes>);
  201. tr.info(mp::print_int_list<strides> {}).test(std::is_same_v<mp::int_list<1>, strides>);
  202. tr.test_eq(ra::scalar(a[0].data()), ra::scalar(c[0].data()));
  203. tr.test_eq(ra::scalar(a[1].data()), ra::scalar(c[1].data()));
  204. c[1] = { 3, 2, 1 };
  205. tr.test_eq(ra::Small<double, 3> { 0, 10, 20 }, c[0]);
  206. tr.test_eq(ra::Small<double, 3> { 0, 10, 20 }, a[0]);
  207. tr.test_eq(ra::Small<double, 3> { 3, 2, 1 }, a[1]);
  208. }
  209. return tr.summary();
  210. }