nested-0.cc 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file nested-0.cc
  3. /// @brief Using nested arrays as if they were arrays if higher rank.
  4. // (c) Daniel Llorens - 2014
  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. // TODO Make more things work and work efficiently.
  10. #include <sstream>
  11. #include <iostream>
  12. #include <iterator>
  13. #include "ra/test.hh"
  14. #include "ra/complex.hh"
  15. using std::cout, std::endl, std::flush, std::cerr, ra::TestRecorder;
  16. template <class T, int N> using Array = ra::Big<T, N>;
  17. template <class T> using Vec = Array<T, 1>;
  18. int main()
  19. {
  20. TestRecorder tr;
  21. tr.section("operators on nested arrays");
  22. {
  23. // default init is required to make vector-of-vector, but I still want Vec {} to mean 'an empty vector' and not a default-init vector.
  24. {
  25. auto c = Vec<int> {};
  26. tr.test_eq(0, c.len(0));
  27. tr.test_eq(0, c.size());
  28. }
  29. {
  30. auto c = Vec<Vec<int>> { {} , {1}, {1, 2} };
  31. std::ostringstream os;
  32. os << c;
  33. std::istringstream is(os.str());
  34. Vec<Vec<int>> d;
  35. is >> d;
  36. tr.test_eq(3, d.size());
  37. tr.test_eq(d[0], Vec<int>{});
  38. tr.test_eq(d[1], Vec<int>{1});
  39. tr.test_eq(d[2], Vec<int>{1, 2});
  40. // TODO Actually nested 'as if higher rank' should allow just (every(c==d)). This is explicit nesting.
  41. tr.test(every(ra::expr([](auto & c, auto & d) { return every(c==d); }, c.iter(), d.iter())));
  42. }
  43. }
  44. tr.section("selector experiments");
  45. {
  46. // These is an investigation of how to make a(ra::all, i) or a(i, ra::all) work.
  47. // The problem with a(ra::all, i) here is that we probably want to leave the iteration on ra::all for last. Otherwise the indexing is redone for each rank-1 cell.
  48. Vec<int> i = {0, 3, 1, 2};
  49. Array<double, 2> a({4, 4}, ra::_0-ra::_1);
  50. Array<double, 2> b = from([](auto && a, auto && i) -> decltype(auto) { return a(i); }, a.iter<1>(), start(i));
  51. tr.test_eq(a(0, i), b(0));
  52. tr.test_eq(a(1, i), b(1));
  53. tr.test_eq(a(2, i), b(2));
  54. tr.test_eq(a(3, i), b(3));
  55. // The problem with a(i) = a(i, ra::all) is that a(i) returns a nested expression, so it isn't equivalent to a(i, [0 1 ...]), and if we want to write it as a rank 2 expression, we can't use from() as above because the iterator we want is a(i).iter(), it depends on i.
  56. // So ...
  57. }
  58. tr.section("copying btw arrays nested in the same way");
  59. {
  60. Vec<ra::Small<int, 2>> a {{1, 2}, {3, 4}, {5, 6}};
  61. ra::Small<ra::Small<int, 2>, 3> b = a;
  62. tr.test_eq(ra::Small<int, 2> {1, 2}, b(0));
  63. tr.test_eq(ra::Small<int, 2> {3, 4}, b(1));
  64. tr.test_eq(ra::Small<int, 2> {5, 6}, b(2));
  65. b(0) = ra::Small<int, 2> {7, 9};
  66. b(1) = ra::Small<int, 2> {3, 4};
  67. b(2) = ra::Small<int, 2> {1, 6};
  68. a = b;
  69. tr.test_eq(ra::Small<int, 2> {7, 9}, a(0));
  70. tr.test_eq(ra::Small<int, 2> {3, 4}, a(1));
  71. tr.test_eq(ra::Small<int, 2> {1, 6}, a(2));
  72. }
  73. tr.section("TODO copying btw arrays nested in different ways");
  74. {
  75. Vec<ra::Small<int, 2>> a {{1, 2}, {3, 4}, {5, 6}};
  76. Array<int, 2> b({3, 2}, {1, 2, 3, 4, 5, 6});
  77. // there's one level of matching so a(0) matches to b(0, 0) and b(0, 1), a(1) to b(1, 0) and b(1, 1), etc. So this results in a(0) = 1 overwritten with a(0) = 2, etc. finally a = [[2, 2], [4, 4], [6, 6]]. Probably not what we want.
  78. a = b;
  79. // b = a; // dnc bc [x, y] ← z is ok but z ← [x, y] is not.
  80. cout << a << endl;
  81. }
  82. return tr.summary();
  83. }