reshape.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Tests for reshape().
  3. // (c) Daniel Llorens - 2017
  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 <memory>
  9. #include "ra/test.hh"
  10. #include "mpdebug.hh"
  11. using std::cout, std::endl, ra::TestRecorder;
  12. int main()
  13. {
  14. TestRecorder tr(std::cout);
  15. tr.section("reshape");
  16. {
  17. ra::Big<int, 3> aa({2, 3, 3}, ra::_0*3+ra::_1);
  18. auto a = aa(ra::all, ra::all, 0);
  19. tr.info("ravel_free").test_eq(ra::iota(6), ravel_free(a));
  20. tr.test_eq(ra::scalar(a.data()), ra::scalar(ravel_free(a).data()));
  21. // select.
  22. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, reshape(a, ra::Small<int, 1> {3}));
  23. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 1> {3}).data()));
  24. // tile.
  25. auto tilea = reshape(a, ra::Small<int, 3> {2, 2, 3});
  26. tr.info("reshape select").test_eq(ra::Big<int, 3>({2, 2, 3}, {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5}), tilea);
  27. tr.info("some tile-reshapes are free (I)").test_eq(0, tilea.step(0));
  28. tr.info("some tile-reshapes are free (II)").test_eq(ra::scalar(a.data()), ra::scalar(tilea.data()));
  29. // reshape with free ravel
  30. tr.info("reshape w/free ravel I").test_eq(ra::Big<int, 2>({3, 2}, {0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 2> {3, 2}));
  31. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 2> {3, 2}).data()));
  32. tr.info("reshape w/free ravel II").test_eq(ra::Big<int, 3>({2, 1, 2}, {0, 1, 2, 3}), reshape(a, ra::Small<int, 3> {2, 1, 2}));
  33. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 3> {2, 1, 2}).data()));
  34. tr.info("reshape w/free ravel III").test_eq(ra::Big<int, 2>({3, 2}, {0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 2> {-1, 2}));
  35. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 2> {-1, 2}).data()));
  36. tr.info("reshape w/free ravel IV").test_eq(ra::Big<int, 2>({2, 3}, {0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 2> {2, -1}));
  37. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 2> {2, -1}).data()));
  38. tr.info("reshape w/free ravel V").test_eq(ra::Big<int, 3>({2, 1, 3}, {0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 3> {2, -1, 3}));
  39. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 3> {2, -1, 3}).data()));
  40. }
  41. tr.section("reshape from var rank to fixed rank");
  42. {
  43. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  44. auto b = reshape(a, ra::Small<int, 1> {3});
  45. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  46. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data()));
  47. tr.info("reshape can fix rank").test_eq(1, rank_s(b));
  48. }
  49. tr.section("reshape from var rank to fixed rank using the initializer_list shim");
  50. {
  51. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  52. auto b = reshape(a, {3, 2});
  53. tr.info("reshape").test_eq(ra::Big<int, 2> {{0, 1}, {2, 3}, {4, 5}}, b);
  54. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data()));
  55. tr.info("reshape can return fixed rank (2)").test_eq(2, rank_s(b));
  56. auto c = reshape(a, {3l, 2l}); // check deduction works regardless
  57. tr.info("reshape").test_eq(ra::Big<int, 2> {{0, 1}, {2, 3}, {4, 5}}, c);
  58. tr.test_eq(ra::scalar(a.data()), ra::scalar(c.data()));
  59. tr.info("reshape can return fixed rank (3)").test_eq(2, rank_s(c));
  60. }
  61. tr.section("reshape from var rank to var rank");
  62. {
  63. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  64. auto b = reshape(a, ra::Big<int, 1> {3});
  65. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  66. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data()));
  67. tr.info("reshape can return var rank (1)").test_eq(ra::ANY, rank_s(b));
  68. }
  69. tr.section("reshape to fixed rank to var rank");
  70. {
  71. ra::Big<int, 2> a({2, 3}, ra::_0*3+ra::_1);
  72. auto b = reshape(a, ra::Big<int, 1> {3});
  73. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  74. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data()));
  75. tr.info("reshape can return var rank").test_eq(ra::ANY, rank_s(b));
  76. }
  77. tr.section("conversion from var rank to fixed rank");
  78. {
  79. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  80. ra::ViewBig<int, 2> b = a;
  81. tr.info("fixing rank").test_eq(ra::_0*3+ra::_1, b);
  82. tr.info("fixing rank is view").test(a.data()==b.data());
  83. }
  84. return tr.summary();
  85. }