explode-collapse.cc 3.1 KB

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