op_shuffle_meat.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 op_shuffle
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. op_shuffle::apply_direct(Mat<eT>& out, const Mat<eT>& X, const uword dim)
  21. {
  22. arma_extra_debug_sigprint();
  23. if(X.is_empty()) { out.copy_size(X); return; }
  24. const uword N = (dim == 0) ? X.n_rows : X.n_cols;
  25. // see op_sort_index_bones.hpp for the definition of arma_sort_index_packet
  26. // and the associated comparison functor
  27. std::vector< arma_sort_index_packet<int> > packet_vec(N);
  28. for(uword i=0; i<N; ++i)
  29. {
  30. packet_vec[i].val = int(arma_rng::randi<int>());
  31. packet_vec[i].index = i;
  32. }
  33. arma_sort_index_helper_ascend<int> comparator;
  34. std::sort( packet_vec.begin(), packet_vec.end(), comparator );
  35. const bool is_alias = (&out == &X);
  36. if(X.is_vec() == false)
  37. {
  38. if(is_alias == false)
  39. {
  40. arma_extra_debug_print("op_shuffle::apply(): matrix");
  41. out.copy_size(X);
  42. if(dim == 0)
  43. {
  44. for(uword i=0; i<N; ++i) { out.row(i) = X.row(packet_vec[i].index); }
  45. }
  46. else
  47. {
  48. for(uword i=0; i<N; ++i) { out.col(i) = X.col(packet_vec[i].index); }
  49. }
  50. }
  51. else // in-place shuffle
  52. {
  53. arma_extra_debug_print("op_shuffle::apply(): in-place matrix");
  54. // reuse the val member variable of packet_vec
  55. // to indicate whether a particular row or column
  56. // has already been shuffled
  57. for(uword i=0; i<N; ++i)
  58. {
  59. packet_vec[i].val = 0;
  60. }
  61. if(dim == 0)
  62. {
  63. for(uword i=0; i<N; ++i)
  64. {
  65. if(packet_vec[i].val == 0)
  66. {
  67. const uword j = packet_vec[i].index;
  68. out.swap_rows(i, j);
  69. packet_vec[j].val = 1;
  70. }
  71. }
  72. }
  73. else
  74. {
  75. for(uword i=0; i<N; ++i)
  76. {
  77. if(packet_vec[i].val == 0)
  78. {
  79. const uword j = packet_vec[i].index;
  80. out.swap_cols(i, j);
  81. packet_vec[j].val = 1;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. else // we're dealing with a vector
  88. {
  89. if(is_alias == false)
  90. {
  91. arma_extra_debug_print("op_shuffle::apply(): vector");
  92. out.copy_size(X);
  93. if(dim == 0)
  94. {
  95. if(X.n_rows > 1) // i.e. column vector
  96. {
  97. for(uword i=0; i<N; ++i) { out[i] = X[ packet_vec[i].index ]; }
  98. }
  99. else
  100. {
  101. out = X;
  102. }
  103. }
  104. else
  105. {
  106. if(X.n_cols > 1) // i.e. row vector
  107. {
  108. for(uword i=0; i<N; ++i) { out[i] = X[ packet_vec[i].index ]; }
  109. }
  110. else
  111. {
  112. out = X;
  113. }
  114. }
  115. }
  116. else // in-place shuffle
  117. {
  118. arma_extra_debug_print("op_shuffle::apply(): in-place vector");
  119. // reuse the val member variable of packet_vec
  120. // to indicate whether a particular row or column
  121. // has already been shuffled
  122. for(uword i=0; i<N; ++i)
  123. {
  124. packet_vec[i].val = 0;
  125. }
  126. if(dim == 0)
  127. {
  128. if(X.n_rows > 1) // i.e. column vector
  129. {
  130. for(uword i=0; i<N; ++i)
  131. {
  132. if(packet_vec[i].val == 0)
  133. {
  134. const uword j = packet_vec[i].index;
  135. std::swap(out[i], out[j]);
  136. packet_vec[j].val = 1;
  137. }
  138. }
  139. }
  140. }
  141. else
  142. {
  143. if(X.n_cols > 1) // i.e. row vector
  144. {
  145. for(uword i=0; i<N; ++i)
  146. {
  147. if(packet_vec[i].val == 0)
  148. {
  149. const uword j = packet_vec[i].index;
  150. std::swap(out[i], out[j]);
  151. packet_vec[j].val = 1;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. template<typename T1>
  160. inline
  161. void
  162. op_shuffle::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_shuffle>& in)
  163. {
  164. arma_extra_debug_sigprint();
  165. const unwrap<T1> U(in.m);
  166. const uword dim = in.aux_uword_a;
  167. arma_debug_check( (dim > 1), "shuffle(): parameter 'dim' must be 0 or 1" );
  168. op_shuffle::apply_direct(out, U.M, dim);
  169. }
  170. template<typename T1>
  171. inline
  172. void
  173. op_shuffle_vec::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_shuffle_vec>& in)
  174. {
  175. arma_extra_debug_sigprint();
  176. const unwrap<T1> U(in.m);
  177. const uword dim = (T1::is_xvec) ? uword(U.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0);
  178. op_shuffle::apply_direct(out, U.M, dim);
  179. }
  180. //! @}