explode-0.cc 9.9 KB

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