view.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file view.cc
  3. /// @brief Checks for view operations
  4. // (c) Daniel Llorens - 2013-2015, 2019
  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/test.hh"
  13. #include "ra/ra.hh"
  14. #include "ra/mpdebug.hh"
  15. using std::cout, std::endl, std::flush, ra::TestRecorder;
  16. template <int i> using TI = ra::TensorIndex<i, int>;
  17. template <class A>
  18. void CheckReverse(TestRecorder & tr, A && a)
  19. {
  20. std::iota(a.begin(), a.end(), 1);
  21. cout << "a: " << a << endl;
  22. auto b0 = reverse(a, 0);
  23. cout << "b: " << b0 << endl;
  24. double check0[24] = { 17, 18, 19, 20, 21, 22, 23, 24,
  25. 9, 10, 11, 12, 13, 14, 15, 16,
  26. 1, 2, 3, 4, 5, 6, 7, 8 };
  27. tr.test(std::equal(check0, check0+24, b0.begin()));
  28. auto b1 = reverse(a, 1);
  29. cout << "b: " << b1 << endl;
  30. double check1[24] = { 5, 6, 7, 8, 1, 2, 3, 4,
  31. 13, 14, 15, 16, 9, 10, 11, 12,
  32. 21, 22, 23, 24, 17, 18, 19, 20 };
  33. tr.test(std::equal(check1, check1+24, b1.begin()));
  34. auto b2 = reverse(a, 2);
  35. cout << "b: " << b2 << endl;
  36. double check2[24] = { 4, 3, 2, 1, 8, 7, 6, 5,
  37. 12, 11, 10, 9, 16, 15, 14, 13,
  38. 20, 19, 18, 17, 24, 23, 22, 21 };
  39. tr.test(std::equal(check2, check2+24, b2.begin()));
  40. }
  41. template <class A>
  42. void CheckTranspose1(TestRecorder & tr, A && a)
  43. {
  44. {
  45. std::iota(a.begin(), a.end(), 1);
  46. cout << "a: " << a << endl;
  47. auto b = transpose(ra::Small<int, 2>{1, 0}, a);
  48. cout << "b: " << b << endl;
  49. double check[6] = {1, 3, 5, 2, 4, 6};
  50. tr.test(std::equal(b.begin(), b.end(), check));
  51. }
  52. {
  53. std::iota(a.begin(), a.end(), 1);
  54. cout << "a: " << a << endl;
  55. auto b = transpose<1, 0>(a);
  56. cout << "b: " << b << endl;
  57. double check[6] = {1, 3, 5, 2, 4, 6};
  58. tr.test(std::equal(b.begin(), b.end(), check));
  59. }
  60. }
  61. int main()
  62. {
  63. TestRecorder tr(std::cout);
  64. tr.section("reverse array types");
  65. {
  66. CheckReverse(tr, ra::Unique<double>({ 3, 2, 4 }, ra::none));
  67. CheckReverse(tr, ra::Unique<double, 3>({ 3, 2, 4 }, ra::none));
  68. }
  69. tr.section("transpose of 0 rank");
  70. {
  71. {
  72. ra::Unique<double, 0> a({}, 99);
  73. auto b = transpose(ra::Small<int, 0> {}, a);
  74. tr.test_eq(0, b.rank());
  75. tr.test_eq(99, b());
  76. }
  77. {
  78. ra::Unique<double, 0> a({}, 99);
  79. auto b = transpose(a);
  80. tr.test_eq(0, b.rank());
  81. tr.test_eq(99, b());
  82. }
  83. // FIXME this doesn't work because init_list {} competes with mp::int_list<>.
  84. // {
  85. // ra::Unique<double, 0> a({}, 99);
  86. // auto b = transpose({}, a);
  87. // tr.test_eq(0, b.rank());
  88. // tr.test_eq(99, b());
  89. // }
  90. {
  91. ra::Unique<double, 0> a({}, 99);
  92. auto b = transpose<>(a);
  93. tr.test_eq(0, b.rank());
  94. tr.test_eq(99, b());
  95. }
  96. }
  97. tr.section("transpose A");
  98. {
  99. CheckTranspose1(tr, ra::Unique<double>({ 3, 2 }, ra::none));
  100. CheckTranspose1(tr, ra::Unique<double, 2>({ 3, 2 }, ra::none));
  101. }
  102. tr.section("transpose B");
  103. {
  104. auto transpose_test = [&tr](auto && b)
  105. {
  106. tr.test_eq(1, b.rank());
  107. tr.test_eq(2, b.size());
  108. tr.test_eq(1, b[0]);
  109. tr.test_eq(4, b[1]);
  110. };
  111. ra::Unique<double> a({3, 2}, ra::_0*2 + ra::_1 + 1);
  112. cout << "A: " << a << endl;
  113. transpose_test(transpose(ra::Small<int, 2> { 0, 0 }, a)); // dyn rank to dyn rank
  114. transpose_test(transpose<0, 0>(a)); // dyn rank to static rank
  115. ra::Unique<double, 2> b({3, 2}, ra::_0*2 + ra::_1*1 + 1);
  116. transpose_test(transpose(ra::Small<int, 2> { 0, 0 }, b)); // static rank to dyn rank
  117. transpose_test(transpose<0, 0>(b)); // static rank to static rank
  118. }
  119. tr.section("transpose C");
  120. {
  121. auto transpose_test = [&tr](auto && b)
  122. {
  123. tr.test_eq(1, b.rank());
  124. tr.test_eq(2, b.size());
  125. tr.test_eq(1, b[0]);
  126. tr.test_eq(5, b[1]);
  127. };
  128. ra::Unique<double> a({2, 3}, ra::_0*3 + ra::_1 + 1);
  129. transpose_test(transpose(ra::Small<int, 2> { 0, 0 }, a)); // dyn rank to dyn rank
  130. transpose_test(transpose<0, 0>(a)); // dyn rank to static rank
  131. ra::Unique<double, 2> b({2, 3}, ra::_0*3 + ra::_1 + 1);
  132. transpose_test(transpose(ra::Small<int, 2> { 0, 0 }, b)); // static rank to dyn rank
  133. transpose_test(transpose<0, 0>(b)); // static rank to static rank
  134. }
  135. tr.section("transpose D");
  136. {
  137. auto transpose_test = [&tr](auto && b)
  138. {
  139. tr.test_eq(1, b.rank());
  140. tr.test_eq(2, b.size());
  141. tr.test_eq(1, b[0]);
  142. tr.test_eq(5, b[1]);
  143. };
  144. ra::Unique<double> a({2, 3}, ra::_0*3 + ra::_1 + 1);
  145. transpose_test(transpose({ 0, 0 }, a)); // dyn rank to dyn rank
  146. }
  147. tr.section("transpose E");
  148. {
  149. ra::Unique<double> a({3}, {1, 2, 3});
  150. tr.test_eq(a-a(ra::insert<1>), a-transpose({1}, a));
  151. }
  152. tr.section("transpose F");
  153. {
  154. ra::Unique<double> a({3}, {1, 2, 3});
  155. tr.test_eq(a-a(ra::insert<1>), a-transpose<1>(a));
  156. }
  157. tr.section("transpose G");
  158. {
  159. ra::Unique<double, 1> a({3}, {1, 2, 3});
  160. tr.test_eq(a-a(ra::insert<1>), a-transpose<1>(a));
  161. }
  162. // FIXME Small cannot have DIM_BAD sizes yet.
  163. // tr.section("transpose K");
  164. // {
  165. // ra::Small<double, 3> a = {1, 2, 3};
  166. // cout << transpose<1>(a) << endl;
  167. // }
  168. tr.section("transpose Q [ma117]");
  169. {
  170. ra::Small<int, 2> axes = {0, 1};
  171. ra::Unique<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
  172. cout << "A: " << transpose(axes, a) << endl;
  173. axes = {1, 0};
  174. cout << "B: " << transpose(axes, a) << endl;
  175. }
  176. tr.section("is_ravel_free");
  177. {
  178. ra::Unique<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
  179. tr.test(is_ravel_free(a));
  180. auto b = transpose<1, 0>(a);
  181. tr.test(!is_ravel_free(b));
  182. }
  183. // trailing singleton dimensions
  184. {
  185. ra::Unique<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
  186. ra::View<double *, 2> c{{{2, 1}, {1, 3}}, a.data()}; // FIXME [ra28]
  187. tr.test(is_ravel_free(c));
  188. tr.test_eq(ra::Small<int, 2>{1, 2}, ravel_free(c));
  189. }
  190. // singleton dimensions elsewhere
  191. {
  192. ra::Unique<double, 1> a = ra::iota(30);
  193. ra::View<double *, 3> c{{{2, 10}, {1, 8}, {5, 2}}, a.data()}; // FIXME [ra28]
  194. tr.test(is_ravel_free(c));
  195. tr.test_eq(ra::iota(10, 0, 2), ravel_free(c));
  196. }
  197. return tr.summary();
  198. }