slicing.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // Adapted from blitz++/examples/slicing.cpp
  3. // Daniel Llorens - 2015
  4. #include "ra/ra.hh"
  5. #include <iostream>
  6. using std::cout, std::endl, std::flush;
  7. int main()
  8. {
  9. ra::Big<int, 2> A({6, 6}, 99);
  10. // Set the upper left quadrant of A to 5
  11. A(ra::iota(3), ra::iota(3)) = 5;
  12. // Set the upper right quadrant of A to an identity matrix
  13. A(ra::iota(3), ra::iota(3, 3)) = { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
  14. // Set the fourth row to 1 (any of these ---trailing ra::all can be omitted).
  15. A(3, ra::all) = 1;
  16. A(ra::iota(1, 3), ra::all) = 1;
  17. A(3) = 1;
  18. // Set the last two rows to 0 (any of these)
  19. // TODO we don't have toEnd yet (would be ra::iota(2, toEnd-2))
  20. A(ra::iota(2, 4), ra::all) = 0;
  21. A(ra::iota(2, 4)) = 0;
  22. // Set the bottom right element to 8
  23. A(5, 5) = 8;
  24. cout << "\nA = " << A << endl;
  25. // Demonstrating multi-axis selectors. Use these to skip over any number of
  26. // axes, just as ra::all skips over one axis.
  27. ra::Big<int, 3> B({3, 5, 5}, 99);
  28. // These are equivalent.
  29. B(ra::all, ra::all, 2) = 3.;
  30. B(ra::dots<2>, 2) = 3.;
  31. // These are all equivalent.
  32. B(1, ra::all, ra::all) = 7.;
  33. B(1) = 7.;
  34. B(1, ra::dots<2>) = 7.;
  35. // These are equivalent.
  36. B(2, ra::all, 1) = 5.;
  37. B(2, ra::dots<1>, 1) = 5.;
  38. // These are equivalent.
  39. B(0, 2, 3) = 1.;
  40. B(ra::dots<0>, 0, ra::dots<0>, 2, ra::dots<0>, 3, ra::dots<0>) = 1.;
  41. cout << "\nB = " << B << endl;
  42. return 0;
  43. }