slicing.cc 1.5 KB

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