slicing.C 1.4 KB

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