op_reshape_meat.hpp
Go to the documentation of this file.
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 
17 
18 
19 
20 template<typename T1>
21 inline
22 void
24  {
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  {
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
138  {
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  {
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 
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the matrix
Definition: Mat_meat.hpp:4024
arma_aligned uword aux_uword_d
storage of auxiliary data, uword format
void set_size(const uword in_elem)
change the matrix to have user specified dimensions (data is not preserved)
Definition: Mat_meat.hpp:4211
const uhword mem_state
Definition: Mat_bones.hpp:33
arma_hot static arma_inline void copy(eT *dest, const eT *src, const uword n_elem)
void delete_mat()
Definition: Cube_meat.hpp:364
const uword mem_state
Definition: Cube_bones.hpp:42
arma_aligned uword aux_uword_b
storage of auxiliary data, uword format
arma_aligned uword aux_uword_c
storage of auxiliary data, uword format
Definition: Op_bones.hpp:49
arma_aligned uword aux_uword_c
storage of auxiliary data, uword format
void set_size(const uword in_rows, const uword in_cols, const uword in_slices)
change the cube to have user specified dimensions (data is not preserved)
Definition: Cube_meat.hpp:2414
const uword n_cols
number of columns in the matrix (read-only)
Definition: Mat_bones.hpp:30
arma_aligned uword aux_uword_b
storage of auxiliary data, uword format
Definition: Op_bones.hpp:48
const uword n_elem
number of elements in the matrix (read-only)
Definition: Mat_bones.hpp:31
const uword n_rows
number of rows in the matrix (read-only)
Definition: Mat_bones.hpp:29
arma_aligned const T1 & m
storage of reference to the operand (eg. a matrix)
Definition: Op_bones.hpp:45
arma_inline const Op< T1, op_min > min(const Base< typename T1::elem_type, T1 > &X, const uword dim=0)
Delayed &#39;minimum values&#39; operation. The dimension, along which the minima are found, is set via &#39;dim&#39;. For dim = 0, the minimum value of each column is found (i.e. searches by traversing across rows). For dim = 1, the minimum value of each row is found (i.e. searches by traversing across columns). The default is dim = 0.
Definition: fn_min.hpp:27
u32 uword
Definition: typedef.hpp:85
arma_inline arma_warn_unused eT * memptr()
returns a pointer to array of eTs used by the cube
Definition: Cube_meat.hpp:2260
const uword n_cols
number of columns in each slice (read-only)
Definition: Cube_bones.hpp:38
static arma_inline T1 & rw(const T1 &x)
internal function to allow modification of data declared as read-only
Definition: access.hpp:23
#define arma_debug_check
Definition: debug.hpp:1084
const Mat< eT > M
Definition: unwrap.hpp:32
arma_inline arma_warn_unused eT & at(const uword i)
linear element accessor (treats the matrix as a vector); no bounds check.
Definition: Mat_meat.hpp:3692
Dense cube class.
Definition: Cube_bones.hpp:30
static void apply(Mat< typename T1::elem_type > &out, const Op< T1, op_reshape > &in)
const Cube< eT > M
Definition: unwrap_cube.hpp:75
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
Analog of the Op class, intended for cubes.
arma_aligned uword aux_uword_a
storage of auxiliary data, uword format
arma_aligned const T1 & m
storage of reference to the operand (e.g. a cube)
const uword n_elem
number of elements in the cube (read-only)
Definition: Cube_bones.hpp:41
Dense matrix class.
const Cube< eT > M
Definition: unwrap_cube.hpp:33
void create_mat()
Definition: Cube_meat.hpp:387
const Mat< eT > M
Definition: unwrap.hpp:142
arma_aligned uword aux_uword_a
storage of auxiliary data, uword format
Definition: Op_bones.hpp:47
const uword n_slices
number of slices in the cube (read-only)
Definition: Cube_bones.hpp:40
const uword n_elem_slice
DEPRECATED: do not use this member variable – it will be removed in version 3.0. ...
Definition: Cube_bones.hpp:39
const uword n_rows
number of rows in each slice (read-only)
Definition: Cube_bones.hpp:37
arma_inline arma_warn_unused eT & at(const uword i)
linear element accessor (treats the cube as a vector); no bounds check.
Definition: Cube_meat.hpp:2024


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