reshape.cpp File Reference
#include "main.h"

Functions

template<typename T1 , typename T2 >
std::enable_if_t< internal::is_same< T1, T2 >::value, boolis_same_eq (const T1 &a, const T2 &b)
 
template<int Order, typename MatType >
void check_auto_reshape4x4 (const MatType &m)
 
template<typename MatType >
void check_direct_access_reshape4x4 (const MatType &, internal::FixedInt< RowMajorBit >)
 
template<typename MatType >
void check_direct_access_reshape4x4 (const MatType &m, internal::FixedInt< 0 >)
 
template<typename MatType >
void reshape4x4 (const MatType &m0)
 
template<typename BlockType >
void reshape_block (const BlockType &M)
 
 EIGEN_DECLARE_TEST (reshape)
 

Function Documentation

◆ check_auto_reshape4x4()

template<int Order, typename MatType >
void check_auto_reshape4x4 ( const MatType &  m)
22  {
23  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 1> v1(1);
24  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 2> v2(2);
25  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 4> v4(4);
26  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 8> v8(8);
27  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 16> v16(16);
28 
29  VERIFY(is_same_eq(m.template reshaped<Order>(1, AutoSize), m.template reshaped<Order>(1, 16)));
30  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 16), m.template reshaped<Order>(1, 16)));
31  VERIFY(is_same_eq(m.template reshaped<Order>(2, AutoSize), m.template reshaped<Order>(2, 8)));
32  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 8), m.template reshaped<Order>(2, 8)));
33  VERIFY(is_same_eq(m.template reshaped<Order>(4, AutoSize), m.template reshaped<Order>(4, 4)));
34  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 4), m.template reshaped<Order>(4, 4)));
35  VERIFY(is_same_eq(m.template reshaped<Order>(8, AutoSize), m.template reshaped<Order>(8, 2)));
36  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 2), m.template reshaped<Order>(8, 2)));
37  VERIFY(is_same_eq(m.template reshaped<Order>(16, AutoSize), m.template reshaped<Order>(16, 1)));
38  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 1), m.template reshaped<Order>(16, 1)));
39 
40  VERIFY(is_same_eq(m.template reshaped<Order>(fix<1>, AutoSize), m.template reshaped<Order>(fix<1>, v16)));
41  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix<16>), m.template reshaped<Order>(v1, fix<16>)));
42  VERIFY(is_same_eq(m.template reshaped<Order>(fix<2>, AutoSize), m.template reshaped<Order>(fix<2>, v8)));
43  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix<8>), m.template reshaped<Order>(v2, fix<8>)));
44  VERIFY(is_same_eq(m.template reshaped<Order>(fix<4>, AutoSize), m.template reshaped<Order>(fix<4>, v4)));
45  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix<4>), m.template reshaped<Order>(v4, fix<4>)));
46  VERIFY(is_same_eq(m.template reshaped<Order>(fix<8>, AutoSize), m.template reshaped<Order>(fix<8>, v2)));
47  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix<2>), m.template reshaped<Order>(v8, fix<2>)));
48  VERIFY(is_same_eq(m.template reshaped<Order>(fix<16>, AutoSize), m.template reshaped<Order>(fix<16>, v1)));
49  VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix<1>), m.template reshaped<Order>(v16, fix<1>)));
50 }
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY(a)
Definition: main.h:362
@ AutoSize
Definition: ReshapedHelper.h:18
const int Dynamic
Definition: Constants.h:25
std::enable_if_t< internal::is_same< T1, T2 >::value, bool > is_same_eq(const T1 &a, const T2 &b)
Definition: reshape.cpp:17

References Eigen::AutoSize, Eigen::Dynamic, is_same_eq(), m, v1(), v2(), and VERIFY.

◆ check_direct_access_reshape4x4() [1/2]

template<typename MatType >
void check_direct_access_reshape4x4 ( const MatType &  ,
internal::FixedInt< RowMajorBit >   
)
53 {}

Referenced by reshape4x4().

◆ check_direct_access_reshape4x4() [2/2]

template<typename MatType >
void check_direct_access_reshape4x4 ( const MatType &  m,
internal::FixedInt< 0 >   
)
56  {
57  VERIFY_IS_EQUAL(m.reshaped(1, 16).data(), m.data());
58  VERIFY_IS_EQUAL(m.reshaped(1, 16).innerStride(), 1);
59 
60  VERIFY_IS_EQUAL(m.reshaped(2, 8).data(), m.data());
61  VERIFY_IS_EQUAL(m.reshaped(2, 8).innerStride(), 1);
62  VERIFY_IS_EQUAL(m.reshaped(2, 8).outerStride(), 2);
63 }
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367

References m, and VERIFY_IS_EQUAL.

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( reshape  )
206  {
207  typedef Matrix<int, Dynamic, Dynamic, RowMajor> RowMatrixXi;
208  typedef Matrix<int, 4, 4, RowMajor> RowMatrix4i;
209  MatrixXi mx = MatrixXi::Random(4, 4);
210  Matrix4i m4 = Matrix4i::Random(4, 4);
211  RowMatrixXi rmx = RowMatrixXi::Random(4, 4);
212  RowMatrix4i rm4 = RowMatrix4i::Random(4, 4);
213 
214  // test dynamic-size matrix
216  // test static-size matrix
218  // test dynamic-size const matrix
219  CALL_SUBTEST(reshape4x4(static_cast<const MatrixXi>(mx)));
220  // test static-size const matrix
221  CALL_SUBTEST(reshape4x4(static_cast<const Matrix4i>(m4)));
222 
223  CALL_SUBTEST(reshape4x4(rmx));
224  CALL_SUBTEST(reshape4x4(rm4));
225  CALL_SUBTEST(reshape_block(rm4.col(1)));
226 }
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
void reshape4x4(const MatType &m0)
Definition: reshape.cpp:67
void reshape_block(const BlockType &M)
Definition: reshape.cpp:191

References CALL_SUBTEST, reshape4x4(), and reshape_block().

◆ is_same_eq()

template<typename T1 , typename T2 >
std::enable_if_t<internal::is_same<T1, T2>::value, bool> is_same_eq ( const T1 &  a,
const T2 &  b 
)
17  {
18  return (a.array() == b.array()).all();
19 }
Scalar * b
Definition: benchVecAdd.cpp:17
const Scalar * a
Definition: level2_cplx_impl.h:32

References a, and b.

Referenced by check_auto_reshape4x4(), and reshape4x4().

◆ reshape4x4()

template<typename MatType >
void reshape4x4 ( const MatType &  m0)
67  {
68  typedef typename MatType::Scalar Scalar;
69  MatType m = m0;
70 
71  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 1> v1(1);
72  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 2> v2(2);
73  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 4> v4(4);
74  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 8> v8(8);
75  internal::VariableAndFixedInt<MatType::SizeAtCompileTime == Dynamic ? -1 : 16> v16(16);
76 
77  if ((MatType::Flags & RowMajorBit) == 0) {
78  typedef Map<MatrixXi> MapMat;
79  // dynamic
80  VERIFY_IS_EQUAL((m.reshaped(1, 16)), MapMat(m.data(), 1, 16));
81  VERIFY_IS_EQUAL((m.reshaped(2, 8)), MapMat(m.data(), 2, 8));
82  VERIFY_IS_EQUAL((m.reshaped(4, 4)), MapMat(m.data(), 4, 4));
83  VERIFY_IS_EQUAL((m.reshaped(8, 2)), MapMat(m.data(), 8, 2));
84  VERIFY_IS_EQUAL((m.reshaped(16, 1)), MapMat(m.data(), 16, 1));
85 
86  // static
87  VERIFY_IS_EQUAL(m.reshaped(fix<1>, fix<16>), MapMat(m.data(), 1, 16));
88  VERIFY_IS_EQUAL(m.reshaped(fix<2>, fix<8>), MapMat(m.data(), 2, 8));
89  VERIFY_IS_EQUAL(m.reshaped(fix<4>, fix<4>), MapMat(m.data(), 4, 4));
90  VERIFY_IS_EQUAL(m.reshaped(fix<8>, fix<2>), MapMat(m.data(), 8, 2));
91  VERIFY_IS_EQUAL(m.reshaped(fix<16>, fix<1>), MapMat(m.data(), 16, 1));
92 
93  // reshape chain
94  VERIFY_IS_EQUAL((m.reshaped(1, 16)
95  .reshaped(fix<2>, fix<8>)
96  .reshaped(16, 1)
97  .reshaped(fix<8>, fix<2>)
98  .reshaped(2, 8)
99  .reshaped(fix<1>, fix<16>)
100  .reshaped(4, 4)
101  .reshaped(fix<16>, fix<1>)
102  .reshaped(8, 2)
103  .reshaped(fix<4>, fix<4>)),
104  MapMat(m.data(), 4, 4));
105  }
106 
107  VERIFY(is_same_eq(m.reshaped(1, AutoSize), m.reshaped(1, 16)));
108  VERIFY(is_same_eq(m.reshaped(AutoSize, 16), m.reshaped(1, 16)));
109  VERIFY(is_same_eq(m.reshaped(2, AutoSize), m.reshaped(2, 8)));
110  VERIFY(is_same_eq(m.reshaped(AutoSize, 8), m.reshaped(2, 8)));
111  VERIFY(is_same_eq(m.reshaped(4, AutoSize), m.reshaped(4, 4)));
112  VERIFY(is_same_eq(m.reshaped(AutoSize, 4), m.reshaped(4, 4)));
113  VERIFY(is_same_eq(m.reshaped(8, AutoSize), m.reshaped(8, 2)));
114  VERIFY(is_same_eq(m.reshaped(AutoSize, 2), m.reshaped(8, 2)));
115  VERIFY(is_same_eq(m.reshaped(16, AutoSize), m.reshaped(16, 1)));
116  VERIFY(is_same_eq(m.reshaped(AutoSize, 1), m.reshaped(16, 1)));
117 
118  VERIFY(is_same_eq(m.reshaped(fix<1>, AutoSize), m.reshaped(fix<1>, v16)));
119  VERIFY(is_same_eq(m.reshaped(AutoSize, fix<16>), m.reshaped(v1, fix<16>)));
120  VERIFY(is_same_eq(m.reshaped(fix<2>, AutoSize), m.reshaped(fix<2>, v8)));
121  VERIFY(is_same_eq(m.reshaped(AutoSize, fix<8>), m.reshaped(v2, fix<8>)));
122  VERIFY(is_same_eq(m.reshaped(fix<4>, AutoSize), m.reshaped(fix<4>, v4)));
123  VERIFY(is_same_eq(m.reshaped(AutoSize, fix<4>), m.reshaped(v4, fix<4>)));
124  VERIFY(is_same_eq(m.reshaped(fix<8>, AutoSize), m.reshaped(fix<8>, v2)));
125  VERIFY(is_same_eq(m.reshaped(AutoSize, fix<2>), m.reshaped(v8, fix<2>)));
126  VERIFY(is_same_eq(m.reshaped(fix<16>, AutoSize), m.reshaped(fix<16>, v1)));
127  VERIFY(is_same_eq(m.reshaped(AutoSize, fix<1>), m.reshaped(v16, fix<1>)));
128 
129  check_auto_reshape4x4<ColMajor>(m);
130  check_auto_reshape4x4<RowMajor>(m);
131  check_auto_reshape4x4<AutoOrder>(m);
132  check_auto_reshape4x4<ColMajor>(m.transpose());
133  check_auto_reshape4x4<ColMajor>(m.transpose());
134  check_auto_reshape4x4<AutoOrder>(m.transpose());
135 
136  check_direct_access_reshape4x4(m, fix<MatType::Flags & RowMajorBit>);
137 
138  if ((MatType::Flags & RowMajorBit) == 0) {
139  VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2, 8), m.reshaped(2, 8));
140  VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2, 8), m.template reshaped<AutoOrder>(2, 8));
141  VERIFY_IS_EQUAL(m.transpose().template reshaped<RowMajor>(2, 8), m.transpose().template reshaped<AutoOrder>(2, 8));
142  } else {
143  VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2, 8), m.reshaped(2, 8));
144  VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(2, 8), m.template reshaped<AutoOrder>(2, 8));
145  VERIFY_IS_EQUAL(m.transpose().template reshaped<ColMajor>(2, 8), m.transpose().template reshaped<AutoOrder>(2, 8));
146  VERIFY_IS_EQUAL(m.transpose().reshaped(2, 8), m.transpose().template reshaped<AutoOrder>(2, 8));
147  }
148 
149  MatrixXi m28r1 = m.template reshaped<RowMajor>(2, 8);
150  MatrixXi m28r2 = m.transpose().template reshaped<ColMajor>(8, 2).transpose();
151  VERIFY_IS_EQUAL(m28r1, m28r2);
152 
153  VERIFY(is_same_eq(m.reshaped(v16, fix<1>), m.reshaped()));
154  VERIFY_IS_EQUAL(m.reshaped(16, 1).eval(), m.reshaped().eval());
155  VERIFY_IS_EQUAL(m.reshaped(1, 16).eval(), m.reshaped().transpose().eval());
156  VERIFY_IS_EQUAL(m.reshaped().reshaped(2, 8), m.reshaped(2, 8));
157  VERIFY_IS_EQUAL(m.reshaped().reshaped(4, 4), m.reshaped(4, 4));
158  VERIFY_IS_EQUAL(m.reshaped().reshaped(8, 2), m.reshaped(8, 2));
159 
160  VERIFY_IS_EQUAL(m.reshaped(), m.template reshaped<ColMajor>());
161  VERIFY_IS_EQUAL(m.transpose().reshaped(), m.template reshaped<RowMajor>());
162  VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(AutoSize, fix<1>), m.template reshaped<RowMajor>());
163  VERIFY_IS_EQUAL(m.template reshaped<AutoOrder>(AutoSize, fix<1>), m.template reshaped<AutoOrder>());
164 
165  VERIFY(is_same_eq(m.reshaped(AutoSize, fix<1>), m.reshaped()));
166  VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(fix<1>, AutoSize), m.transpose().reshaped().transpose());
167 
168  // check assignment
169  {
170  Matrix<Scalar, Dynamic, 1> m1x(m.size());
171  m1x.setRandom();
172  VERIFY_IS_APPROX(m.reshaped() = m1x, m1x);
173  VERIFY_IS_APPROX(m, m1x.reshaped(4, 4));
174 
176  m28.setRandom();
177  VERIFY_IS_APPROX(m.reshaped(2, 8) = m28, m28);
178  VERIFY_IS_APPROX(m, m28.reshaped(4, 4));
179  VERIFY_IS_APPROX(m.template reshaped<RowMajor>(2, 8) = m28, m28);
180 
182  m24.setRandom();
183  VERIFY_IS_APPROX(m(seq(0, last, 2), all).reshaped(2, 4) = m24, m24);
184 
185  // check constness:
186  m.reshaped(2, 8).nestedExpression() = m;
187  }
188 }
A reshaped()
SCALAR Scalar
Definition: bench_gemm.cpp:45
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
static constexpr Eigen::internal::all_t all
Definition: IndexedViewHelper.h:86
static constexpr const last_t last
Definition: IndexedViewHelper.h:48
const unsigned int RowMajorBit
Definition: Constants.h:70
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
auto seq(FirstType f, LastType l) -> decltype(seqN(typename internal::cleanup_index_type< FirstType >::type(f),(typename internal::cleanup_index_type< LastType >::type(l) - typename internal::cleanup_index_type< FirstType >::type(f)+fix< 1 >())))
Definition: ArithmeticSequence.h:152
void check_direct_access_reshape4x4(const MatType &, internal::FixedInt< RowMajorBit >)
Definition: reshape.cpp:53

References Eigen::placeholders::all, Eigen::AutoSize, check_direct_access_reshape4x4(), Eigen::Dynamic, is_same_eq(), Eigen::placeholders::last, m, reshaped(), Eigen::RowMajorBit, Eigen::seq(), Eigen::PlainObjectBase< Derived >::setRandom(), v1(), v2(), VERIFY, VERIFY_IS_APPROX, and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ reshape_block()

template<typename BlockType >
void reshape_block ( const BlockType &  M)
191  {
192  auto dense = M.eval();
193  Index rows = M.size() / 2;
194  Index cols = M.size() / rows;
195  VERIFY_IS_EQUAL(dense.reshaped(rows, cols), M.reshaped(rows, cols));
196 
197  for (Index i = 0; i < rows; ++i) {
198  VERIFY_IS_EQUAL(dense.reshaped(rows, cols).row(i), M.reshaped(rows, cols).row(i));
199  }
200 
201  for (Index j = 0; j < cols; ++j) {
202  VERIFY_IS_EQUAL(dense.reshaped(rows, cols).col(j), M.reshaped(rows, cols).col(j));
203  }
204 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References cols, i, j, rows, and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().