op_shuffle_meat.hpp
Go to the documentation of this file.
1 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2009-2011 Conrad Sanderson
3 // Copyright (C) 2009-2010 Dimitrios Bouzas
4 //
5 // This file is part of the Armadillo C++ library.
6 // It is provided without any warranty of fitness
7 // for any purpose. You can redistribute this file
8 // and/or modify it under the terms of the GNU
9 // Lesser General Public License (LGPL) as published
10 // by the Free Software Foundation, either version 3
11 // of the License or (at your option) any later version.
12 // (see http://www.opensource.org/licenses for more info)
13 
14 
15 
18 
19 
20 
21 template<typename T1>
22 inline
23 void
25  {
27 
28  typedef typename T1::elem_type eT;
29 
30  const unwrap<T1> tmp(in.m);
31  const Mat<eT>& X = tmp.M;
32 
33  if(X.is_empty())
34  {
35  out.copy_size(X);
36  return;
37  }
38 
39  const uword dim = in.aux_uword_a;
40  const uword N = (dim == 0) ? X.n_rows : X.n_cols;
41 
42  // see "fn_sort_index.hpp" for the definition of "arma_sort_index_packet_ascend"
43  // and the associated "operator<"
44  std::vector< arma_sort_index_packet_ascend<int,uword> > packet_vec(N);
45 
46  for(uword i=0; i<N; ++i)
47  {
48  packet_vec[i].val = std::rand();
49  packet_vec[i].index = i;
50  }
51 
52  std::sort( packet_vec.begin(), packet_vec.end() );
53 
54  if(X.is_vec() == false)
55  {
56  if(&out != &X)
57  {
58  arma_extra_debug_print("op_shuffle::apply(): matrix");
59 
60  out.copy_size(X);
61 
62  if(dim == 0)
63  {
64  for(uword i=0; i<N; ++i)
65  {
66  out.row(i) = X.row(packet_vec[i].index);
67  }
68  }
69  else
70  {
71  for(uword i=0; i<N; ++i)
72  {
73  out.col(i) = X.col(packet_vec[i].index);
74  }
75  }
76  }
77  else // in-place shuffle
78  {
79  arma_extra_debug_print("op_shuffle::apply(): in-place matrix");
80 
81  // reuse the val member variable of packet_vec
82  // to indicate whether a particular row or column
83  // has already been shuffled
84 
85  for(uword i=0; i<N; ++i)
86  {
87  packet_vec[i].val = 0;
88  }
89 
90  if(dim == 0)
91  {
92  for(uword i=0; i<N; ++i)
93  {
94  if(packet_vec[i].val == 0)
95  {
96  const uword j = packet_vec[i].index;
97 
98  out.swap_rows(i, j);
99 
100  packet_vec[j].val = 1;
101  }
102  }
103  }
104  else
105  {
106  for(uword i=0; i<N; ++i)
107  {
108  if(packet_vec[i].val == 0)
109  {
110  const uword j = packet_vec[i].index;
111 
112  out.swap_cols(i, j);
113 
114  packet_vec[j].val = 1;
115  }
116  }
117  }
118  }
119  }
120  else // we're dealing with a vector
121  {
122  if(&out != &X)
123  {
124  arma_extra_debug_print("op_shuffle::apply(): vector");
125 
126  out.copy_size(X);
127 
128  if(dim == 0)
129  {
130  if(X.n_rows > 1) // i.e. column vector
131  {
132  for(uword i=0; i<N; ++i)
133  {
134  out[i] = X[ packet_vec[i].index ];
135  }
136  }
137  else
138  {
139  out = X;
140  }
141  }
142  else
143  {
144  if(X.n_cols > 1) // i.e. row vector
145  {
146  for(uword i=0; i<N; ++i)
147  {
148  out[i] = X[ packet_vec[i].index ];
149  }
150  }
151  else
152  {
153  out = X;
154  }
155  }
156  }
157  else // in-place shuffle
158  {
159  arma_extra_debug_print("op_shuffle::apply(): in-place vector");
160 
161  // reuse the val member variable of packet_vec
162  // to indicate whether a particular row or column
163  // has already been shuffled
164 
165  for(uword i=0; i<N; ++i)
166  {
167  packet_vec[i].val = 0;
168  }
169 
170  if(dim == 0)
171  {
172  if(X.n_rows > 1) // i.e. column vector
173  {
174  for(uword i=0; i<N; ++i)
175  {
176  if(packet_vec[i].val == 0)
177  {
178  const uword j = packet_vec[i].index;
179 
180  std::swap(out[i], out[j]);
181 
182  packet_vec[j].val = 1;
183  }
184  }
185  }
186  }
187  else
188  {
189  if(X.n_cols > 1) // i.e. row vector
190  {
191  for(uword i=0; i<N; ++i)
192  {
193  if(packet_vec[i].val == 0)
194  {
195  const uword j = packet_vec[i].index;
196 
197  std::swap(out[i], out[j]);
198 
199  packet_vec[j].val = 1;
200  }
201  }
202  }
203  }
204  }
205  }
206 
207  }
208 
209 
arma_inline arma_warn_unused bool is_vec() const
returns true if the object can be interpreted as a column or row vector
Definition: Mat_meat.hpp:3824
const uword n_cols
number of columns in the matrix (read-only)
Definition: Mat_bones.hpp:30
arma_inline subview_col< eT > col(const uword col_num)
creation of subview (column vector)
Definition: Mat_meat.hpp:1880
arma_inline subview_row< eT > row(const uword row_num)
creation of subview (row vector)
Definition: Mat_meat.hpp:1792
const uword n_rows
number of rows in the matrix (read-only)
Definition: Mat_bones.hpp:29
#define arma_extra_debug_print
Definition: debug.hpp:1118
arma_aligned const T1 & m
storage of reference to the operand (eg. a matrix)
Definition: Op_bones.hpp:45
u32 uword
Definition: typedef.hpp:85
const Mat< eT > M
Definition: unwrap.hpp:32
void copy_size(const Mat< eT2 > &m)
change the matrix (without preserving data) to have the same dimensions as the given matrix ...
Definition: Mat_meat.hpp:4303
void swap_cols(const uword in_col1, const uword in_col2)
Definition: Mat_meat.hpp:2359
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
arma_inline const Op< T1, op_sort > sort(const Base< typename T1::elem_type, T1 > &X, const uword sort_type=0, const uword dim=0)
Definition: fn_sort.hpp:21
static void apply(Mat< typename T1::elem_type > &out, const Op< T1, op_shuffle > &in)
Dense matrix class.
arma_aligned uword aux_uword_a
storage of auxiliary data, uword format
Definition: Op_bones.hpp:47
void swap_rows(const uword in_row1, const uword in_row2)
Definition: Mat_meat.hpp:2331
arma_inline arma_warn_unused bool is_empty() const
returns true if the matrix has no elements
Definition: Mat_meat.hpp:3812


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:58