explode-collapse.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/examples - Demo collapse and explode operations.
  3. // (c) Daniel Llorens - 2016
  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 "ra/ra.hh"
  9. #include <iostream>
  10. using std::cout, std::endl, std::flush;
  11. template <class T, int rank> using Array = ra::Big<T, rank>;
  12. template <class T, int ... sizes> using Small = ra::Small<T, sizes ...>;
  13. int main()
  14. {
  15. // These operations let you view an array of rank n as an array of rank (n-k) of
  16. // subarrays of rank k (explode) or the opposite (collapse). However, there is a
  17. // caveat: the subarrays must be compact (stored contiguously) and the sizes of
  18. // the subarrays must be known statically. In effect, the subarray must be of
  19. // type ra::Small. For example:
  20. cout << "\nexplode\n" << endl;
  21. {
  22. Array<int, 2> A({2, 3}, ra::_0 - ra::_1);
  23. auto B = ra::explode<Small<int, 3>>(A);
  24. cout << "B(0): " << B(0) << endl; // [0 -1 -2], note the static size
  25. cout << "B(1): " << B(1) << endl; // [1, 0, -1], note the static size
  26. B(1) = 9;
  27. cout << "A after: " << A << endl;
  28. }
  29. // The collapsed/exploded arrays are views into the source. This is similar to a
  30. // Blitz++ feature called 'multicomponents'. For instance, suppose we have an
  31. // array of (static-sized) [x, y, z] vectors and wish to initialize the x, y, z
  32. // components separately:
  33. cout << "\ncollapse\n" << endl;
  34. {
  35. Array<Small<int, 3>, 1> A = {{0, -1, -2}, {1, 0, -1}, {2, 1, -1}};
  36. auto B = ra::collapse<int>(A);
  37. cout << "B before: " << B << endl;
  38. B(ra::all, 0) = 9; // initialize all x components
  39. B(ra::all, 1) = 7; // all y
  40. B(ra::all, 2) = 3; // all z
  41. cout << "B after: " << B << endl;
  42. cout << "A after: " << A << endl;
  43. }
  44. // collapse/explode can also be used to make views into the real and imag parts
  45. // of a complex array. To use explode in this way, the last axis must be
  46. // contiguous and of size 2, just as when using Small<>.
  47. cout << "\nreal/imag views\n" << endl;
  48. {
  49. using complex = std::complex<double>;
  50. Array<complex, 2> A({2, 2}, 0.);
  51. // construct array views into real and imag parts of A
  52. auto B = ra::collapse<double>(A);
  53. auto Bre = B(ra::all, ra::all, 0);
  54. auto Bim = B(ra::all, ra::all, 1);
  55. // set real and imag parts of A
  56. Bre = ra::_0-ra::_1;
  57. Bim = -3;
  58. cout << "Aa: " << A << endl;
  59. // of course, you could also do
  60. imag_part(A) = 7.;
  61. cout << "Ab: " << A << endl;
  62. // which doesn't construct an array view as collapse() does, but relies on the
  63. // expression template mechanism instead.
  64. // Constructing an array view is useful for example when you need to pass a
  65. // pointer to an external library. But be aware of the steps!
  66. double * re_ptr = Bre.data();
  67. double * im_ptr = Bim.data();
  68. re_ptr[0] = 77;
  69. im_ptr[0] = 99;
  70. cout << "Ac: " << A << endl;
  71. }
  72. return 0;
  73. }