comparison armadillo-2.4.4/include/armadillo_bits/op_reshape_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14
15 //! \addtogroup op_reshape
16 //! @{
17
18
19
20 template<typename T1>
21 inline
22 void
23 op_reshape::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_reshape>& in)
24 {
25 arma_extra_debug_sigprint();
26
27 typedef typename T1::elem_type eT;
28
29 const unwrap<T1> tmp(in.m);
30 const Mat<eT>& A = tmp.M;
31
32 const uword in_n_rows = in.aux_uword_a;
33 const uword in_n_cols = in.aux_uword_b;
34 const uword in_dim = in.aux_uword_c;
35
36 const uword in_n_elem = in_n_rows * in_n_cols;
37
38 if(A.n_elem == in_n_elem)
39 {
40 if(in_dim == 0)
41 {
42 if(&out != &A)
43 {
44 out.set_size(in_n_rows, in_n_cols);
45 arrayops::copy( out.memptr(), A.memptr(), out.n_elem );
46 }
47 else // &out == &A, i.e. inplace resize
48 {
49 const bool same_size = ( (out.n_rows == in_n_rows) && (out.n_cols == in_n_cols) );
50
51 if(same_size == false)
52 {
53 arma_debug_check
54 (
55 (out.mem_state == 3),
56 "reshape(): size can't be changed as template based size specification is in use"
57 );
58
59 access::rw(out.n_rows) = in_n_rows;
60 access::rw(out.n_cols) = in_n_cols;
61 }
62 }
63 }
64 else
65 {
66 unwrap_check< Mat<eT> > tmp(A, out);
67 const Mat<eT>& B = tmp.M;
68
69 out.set_size(in_n_rows, in_n_cols);
70
71 eT* out_mem = out.memptr();
72 uword i = 0;
73
74 const uword B_n_rows = B.n_rows;
75 const uword B_n_cols = B.n_cols;
76
77 for(uword row=0; row<B_n_rows; ++row)
78 {
79 for(uword col=0; col<B_n_cols; ++col)
80 {
81 out_mem[i] = B.at(row,col);
82 ++i;
83 }
84 }
85
86 }
87 }
88 else
89 {
90 const unwrap_check< Mat<eT> > tmp(A, out);
91 const Mat<eT>& B = tmp.M;
92
93 const uword n_elem_to_copy = (std::min)(B.n_elem, in_n_elem);
94
95 out.set_size(in_n_rows, in_n_cols);
96
97 eT* out_mem = out.memptr();
98
99 if(in_dim == 0)
100 {
101 arrayops::copy( out_mem, B.memptr(), n_elem_to_copy );
102 }
103 else
104 {
105 uword row = 0;
106 uword col = 0;
107
108 const uword B_n_cols = B.n_cols;
109
110 for(uword i=0; i<n_elem_to_copy; ++i)
111 {
112 out_mem[i] = B.at(row,col);
113
114 ++col;
115
116 if(col >= B_n_cols)
117 {
118 col = 0;
119 ++row;
120 }
121 }
122 }
123
124 for(uword i=n_elem_to_copy; i<in_n_elem; ++i)
125 {
126 out_mem[i] = eT(0);
127 }
128
129 }
130 }
131
132
133
134 template<typename T1>
135 inline
136 void
137 op_reshape::apply(Cube<typename T1::elem_type>& out, const OpCube<T1,op_reshape>& in)
138 {
139 arma_extra_debug_sigprint();
140
141 typedef typename T1::elem_type eT;
142
143 const unwrap_cube<T1> tmp(in.m);
144 const Cube<eT>& A = tmp.M;
145
146 const uword in_n_rows = in.aux_uword_a;
147 const uword in_n_cols = in.aux_uword_b;
148 const uword in_n_slices = in.aux_uword_c;
149 const uword in_dim = in.aux_uword_d;
150
151 const uword in_n_elem = in_n_rows * in_n_cols * in_n_slices;
152
153 if(A.n_elem == in_n_elem)
154 {
155 if(in_dim == 0)
156 {
157 if(&out != &A)
158 {
159 out.set_size(in_n_rows, in_n_cols, in_n_slices);
160 arrayops::copy( out.memptr(), A.memptr(), out.n_elem );
161 }
162 else // &out == &A, i.e. inplace resize
163 {
164 const bool same_size = ( (out.n_rows == in_n_rows) && (out.n_cols == in_n_cols) && (out.n_slices == in_n_slices) );
165
166 if(same_size == false)
167 {
168 arma_debug_check
169 (
170 (out.mem_state == 3),
171 "reshape(): size can't be changed as template based size specification is in use"
172 );
173
174 out.delete_mat();
175
176 access::rw(out.n_rows) = in_n_rows;
177 access::rw(out.n_cols) = in_n_cols;
178 access::rw(out.n_elem_slice) = in_n_rows * in_n_cols;
179 access::rw(out.n_slices) = in_n_slices;
180
181 out.create_mat();
182 }
183 }
184 }
185 else
186 {
187 unwrap_cube_check< Cube<eT> > tmp(A, out);
188 const Cube<eT>& B = tmp.M;
189
190 out.set_size(in_n_rows, in_n_cols, in_n_slices);
191
192 eT* out_mem = out.memptr();
193 uword i = 0;
194
195 const uword B_n_rows = B.n_rows;
196 const uword B_n_cols = B.n_cols;
197 const uword B_n_slices = B.n_slices;
198
199 for(uword slice=0; slice<B_n_slices; ++slice)
200 {
201 for(uword row=0; row<B_n_rows; ++row)
202 {
203 for(uword col=0; col<B_n_cols; ++col)
204 {
205 out_mem[i] = B.at(row,col,slice);
206 ++i;
207 }
208 }
209 }
210
211 }
212 }
213 else
214 {
215 const unwrap_cube_check< Cube<eT> > tmp(A, out);
216 const Cube<eT>& B = tmp.M;
217
218 const uword n_elem_to_copy = (std::min)(B.n_elem, in_n_elem);
219
220 out.set_size(in_n_rows, in_n_cols, in_n_slices);
221
222 eT* out_mem = out.memptr();
223
224 if(in_dim == 0)
225 {
226 arrayops::copy( out_mem, B.memptr(), n_elem_to_copy );
227 }
228 else
229 {
230 uword row = 0;
231 uword col = 0;
232 uword slice = 0;
233
234 const uword B_n_rows = B.n_rows;
235 const uword B_n_cols = B.n_cols;
236
237 for(uword i=0; i<n_elem_to_copy; ++i)
238 {
239 out_mem[i] = B.at(row,col,slice);
240
241 ++col;
242
243 if(col >= B_n_cols)
244 {
245 col = 0;
246 ++row;
247
248 if(row >= B_n_rows)
249 {
250 row = 0;
251 ++slice;
252 }
253 }
254 }
255 }
256
257 for(uword i=n_elem_to_copy; i<in_n_elem; ++i)
258 {
259 out_mem[i] = eT(0);
260 }
261
262 }
263 }
264
265
266
267 //! @}