fn_inplace_strans.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup fn_inplace_strans
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. inplace_strans
  21. (
  22. Mat<eT>& X,
  23. const char* method = "std"
  24. )
  25. {
  26. arma_extra_debug_sigprint();
  27. const char sig = (method != NULL) ? method[0] : char(0);
  28. arma_debug_check( ((sig != 's') && (sig != 'l')), "inplace_strans(): unknown method specified" );
  29. const bool low_memory = (sig == 'l');
  30. if( (low_memory == false) || (X.n_rows == X.n_cols) )
  31. {
  32. op_strans::apply_mat_inplace(X);
  33. }
  34. else
  35. {
  36. // in-place algorithm inspired by:
  37. // Fred G. Gustavson, Tadeusz Swirszcz.
  38. // In-Place Transposition of Rectangular Matrices.
  39. // Applied Parallel Computing. State of the Art in Scientific Computing.
  40. // Lecture Notes in Computer Science. Volume 4699, pp. 560-569, 2007.
  41. // X.set_size() will check whether we can change the dimensions of X;
  42. // X.set_size() will also reuse existing memory, as the number of elements hasn't changed
  43. X.set_size(X.n_cols, X.n_rows);
  44. const uword m = X.n_cols;
  45. const uword n = X.n_rows;
  46. std::vector<bool> visited(X.n_elem); // TODO: replace std::vector<bool> with a better implementation
  47. for(uword col = 0; col < m; ++col)
  48. for(uword row = 0; row < n; ++row)
  49. {
  50. const uword pos = col*n + row;
  51. if(visited[pos] == false)
  52. {
  53. uword curr_pos = pos;
  54. eT val = X.at(row, col);
  55. while(visited[curr_pos] == false)
  56. {
  57. visited[curr_pos] = true;
  58. const uword j = curr_pos / m;
  59. const uword i = curr_pos - m * j;
  60. const eT tmp = X.at(j, i);
  61. X.at(j, i) = val;
  62. val = tmp;
  63. curr_pos = i*n + j;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. //! @}