test-explode-collapse.C 9.1 KB

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