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