xref: /aosp_15_r20/external/eigen/test/reshape.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2017 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2014 yoco <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li 
11*bf2c3715SXin Li #include "main.h"
12*bf2c3715SXin Li 
13*bf2c3715SXin Li template<typename T1,typename T2>
14*bf2c3715SXin Li typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
is_same_eq(const T1 & a,const T2 & b)15*bf2c3715SXin Li is_same_eq(const T1& a, const T2& b)
16*bf2c3715SXin Li {
17*bf2c3715SXin Li   return (a.array() == b.array()).all();
18*bf2c3715SXin Li }
19*bf2c3715SXin Li 
20*bf2c3715SXin Li template <int Order,typename MatType>
check_auto_reshape4x4(MatType m)21*bf2c3715SXin Li void check_auto_reshape4x4(MatType m)
22*bf2c3715SXin Li {
23*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 1>  v1( 1);
24*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 2>  v2( 2);
25*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 4>  v4( 4);
26*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 8>  v8( 8);
27*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1:16> v16(16);
28*bf2c3715SXin Li 
29*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>( 1,       AutoSize), m.template reshaped<Order>( 1, 16)));
30*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 16      ), m.template reshaped<Order>( 1, 16)));
31*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>( 2,       AutoSize), m.template reshaped<Order>( 2,  8)));
32*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 8       ), m.template reshaped<Order>( 2,  8)));
33*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>( 4,       AutoSize), m.template reshaped<Order>( 4,  4)));
34*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 4       ), m.template reshaped<Order>( 4,  4)));
35*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>( 8,       AutoSize), m.template reshaped<Order>( 8,  2)));
36*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 2       ), m.template reshaped<Order>( 8,  2)));
37*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(16,       AutoSize), m.template reshaped<Order>(16,  1)));
38*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 1       ), m.template reshaped<Order>(16,  1)));
39*bf2c3715SXin Li 
40*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(fix< 1>,   AutoSize),  m.template reshaped<Order>(fix< 1>, v16    )));
41*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize,  fix<16> ),  m.template reshaped<Order>( v1,     fix<16>)));
42*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(fix< 2>,   AutoSize),  m.template reshaped<Order>(fix< 2>, v8     )));
43*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize,  fix< 8> ),  m.template reshaped<Order>( v2,     fix< 8>)));
44*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(fix< 4>,   AutoSize),  m.template reshaped<Order>(fix< 4>, v4     )));
45*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize,  fix< 4> ),  m.template reshaped<Order>( v4,     fix< 4>)));
46*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(fix< 8>,   AutoSize),  m.template reshaped<Order>(fix< 8>, v2     )));
47*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize,  fix< 2> ),  m.template reshaped<Order>( v8,     fix< 2>)));
48*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(fix<16>,   AutoSize),  m.template reshaped<Order>(fix<16>, v1     )));
49*bf2c3715SXin Li   VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize,  fix< 1> ),  m.template reshaped<Order>(v16,     fix< 1>)));
50*bf2c3715SXin Li }
51*bf2c3715SXin Li 
52*bf2c3715SXin Li template <typename MatType>
check_direct_access_reshape4x4(MatType,internal::FixedInt<RowMajorBit>)53*bf2c3715SXin Li void check_direct_access_reshape4x4(MatType , internal::FixedInt<RowMajorBit>) {}
54*bf2c3715SXin Li 
55*bf2c3715SXin Li template <typename MatType>
check_direct_access_reshape4x4(MatType m,internal::FixedInt<0>)56*bf2c3715SXin Li void check_direct_access_reshape4x4(MatType m, internal::FixedInt<0>) {
57*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped( 1, 16).data(), m.data());
58*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped( 1, 16).innerStride(), 1);
59*bf2c3715SXin Li 
60*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped( 2, 8).data(), m.data());
61*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped( 2, 8).innerStride(), 1);
62*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped( 2, 8).outerStride(), 2);
63*bf2c3715SXin Li }
64*bf2c3715SXin Li 
65*bf2c3715SXin Li // just test a 4x4 matrix, enumerate all combination manually
66*bf2c3715SXin Li template <typename MatType>
reshape4x4(MatType m)67*bf2c3715SXin Li void reshape4x4(MatType m)
68*bf2c3715SXin Li {
69*bf2c3715SXin Li   typedef typename MatType::Scalar Scalar;
70*bf2c3715SXin Li 
71*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 1>  v1( 1);
72*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 2>  v2( 2);
73*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 4>  v4( 4);
74*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 8>  v8( 8);
75*bf2c3715SXin Li   internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1:16> v16(16);
76*bf2c3715SXin Li 
77*bf2c3715SXin Li   if((MatType::Flags&RowMajorBit)==0)
78*bf2c3715SXin Li   {
79*bf2c3715SXin Li     typedef Map<MatrixXi> MapMat;
80*bf2c3715SXin Li     // dynamic
81*bf2c3715SXin Li     VERIFY_IS_EQUAL((m.reshaped( 1, 16)), MapMat(m.data(),  1, 16));
82*bf2c3715SXin Li     VERIFY_IS_EQUAL((m.reshaped( 2,  8)), MapMat(m.data(),  2,  8));
83*bf2c3715SXin Li     VERIFY_IS_EQUAL((m.reshaped( 4,  4)), MapMat(m.data(),  4,  4));
84*bf2c3715SXin Li     VERIFY_IS_EQUAL((m.reshaped( 8,  2)), MapMat(m.data(),  8,  2));
85*bf2c3715SXin Li     VERIFY_IS_EQUAL((m.reshaped(16,  1)), MapMat(m.data(), 16,  1));
86*bf2c3715SXin Li 
87*bf2c3715SXin Li     // static
88*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.reshaped(fix< 1>, fix<16>), MapMat(m.data(),  1, 16));
89*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.reshaped(fix< 2>, fix< 8>), MapMat(m.data(),  2,  8));
90*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.reshaped(fix< 4>, fix< 4>), MapMat(m.data(),  4,  4));
91*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.reshaped(fix< 8>, fix< 2>), MapMat(m.data(),  8,  2));
92*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.reshaped(fix<16>, fix< 1>), MapMat(m.data(), 16,  1));
93*bf2c3715SXin Li 
94*bf2c3715SXin Li 
95*bf2c3715SXin Li     // reshape chain
96*bf2c3715SXin Li     VERIFY_IS_EQUAL(
97*bf2c3715SXin Li       (m
98*bf2c3715SXin Li       .reshaped( 1, 16)
99*bf2c3715SXin Li       .reshaped(fix< 2>,fix< 8>)
100*bf2c3715SXin Li       .reshaped(16,  1)
101*bf2c3715SXin Li       .reshaped(fix< 8>,fix< 2>)
102*bf2c3715SXin Li       .reshaped( 2,  8)
103*bf2c3715SXin Li       .reshaped(fix< 1>,fix<16>)
104*bf2c3715SXin Li       .reshaped( 4,  4)
105*bf2c3715SXin Li       .reshaped(fix<16>,fix< 1>)
106*bf2c3715SXin Li       .reshaped( 8,  2)
107*bf2c3715SXin Li       .reshaped(fix< 4>,fix< 4>)
108*bf2c3715SXin Li       ),
109*bf2c3715SXin Li       MapMat(m.data(), 4,  4)
110*bf2c3715SXin Li     );
111*bf2c3715SXin Li   }
112*bf2c3715SXin Li 
113*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped( 1,       AutoSize), m.reshaped( 1, 16)));
114*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize, 16),       m.reshaped( 1, 16)));
115*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped( 2,       AutoSize), m.reshaped( 2,  8)));
116*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize, 8),        m.reshaped( 2,  8)));
117*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped( 4,       AutoSize), m.reshaped( 4,  4)));
118*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize, 4),        m.reshaped( 4,  4)));
119*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped( 8,       AutoSize), m.reshaped( 8,  2)));
120*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize, 2),        m.reshaped( 8,  2)));
121*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(16,       AutoSize), m.reshaped(16,  1)));
122*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize,  1),       m.reshaped(16,  1)));
123*bf2c3715SXin Li 
124*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(fix< 1>,   AutoSize),  m.reshaped(fix< 1>, v16)));
125*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize,  fix<16>),   m.reshaped( v1,     fix<16>)));
126*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(fix< 2>,   AutoSize),  m.reshaped(fix< 2>, v8)));
127*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize,  fix< 8>),   m.reshaped( v2,     fix< 8>)));
128*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(fix< 4>,   AutoSize),  m.reshaped(fix< 4>, v4)));
129*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize,  fix< 4>),   m.reshaped( v4,     fix< 4>)));
130*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(fix< 8>,   AutoSize),  m.reshaped(fix< 8>, v2)));
131*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize,  fix< 2>),   m.reshaped( v8,     fix< 2>)));
132*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(fix<16>,   AutoSize),  m.reshaped(fix<16>, v1)));
133*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize,  fix< 1>),   m.reshaped(v16,     fix< 1>)));
134*bf2c3715SXin Li 
135*bf2c3715SXin Li   check_auto_reshape4x4<ColMajor> (m);
136*bf2c3715SXin Li   check_auto_reshape4x4<RowMajor> (m);
137*bf2c3715SXin Li   check_auto_reshape4x4<AutoOrder>(m);
138*bf2c3715SXin Li   check_auto_reshape4x4<ColMajor> (m.transpose());
139*bf2c3715SXin Li   check_auto_reshape4x4<ColMajor> (m.transpose());
140*bf2c3715SXin Li   check_auto_reshape4x4<AutoOrder>(m.transpose());
141*bf2c3715SXin Li 
142*bf2c3715SXin Li   check_direct_access_reshape4x4(m,fix<MatType::Flags&RowMajorBit>);
143*bf2c3715SXin Li 
144*bf2c3715SXin Li   if((MatType::Flags&RowMajorBit)==0)
145*bf2c3715SXin Li   {
146*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2,8),m.reshaped(2,8));
147*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2,8),m.template reshaped<AutoOrder>(2,8));
148*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.transpose().template reshaped<RowMajor>(2,8),m.transpose().template reshaped<AutoOrder>(2,8));
149*bf2c3715SXin Li   }
150*bf2c3715SXin Li   else
151*bf2c3715SXin Li   {
152*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2,8),m.reshaped(2,8));
153*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(2,8),m.template reshaped<AutoOrder>(2,8));
154*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.transpose().template reshaped<ColMajor>(2,8),m.transpose().template reshaped<AutoOrder>(2,8));
155*bf2c3715SXin Li     VERIFY_IS_EQUAL(m.transpose().reshaped(2,8),m.transpose().template reshaped<AutoOrder>(2,8));
156*bf2c3715SXin Li   }
157*bf2c3715SXin Li 
158*bf2c3715SXin Li   MatrixXi m28r1 = m.template reshaped<RowMajor>(2,8);
159*bf2c3715SXin Li   MatrixXi m28r2 = m.transpose().template reshaped<ColMajor>(8,2).transpose();
160*bf2c3715SXin Li   VERIFY_IS_EQUAL( m28r1, m28r2);
161*bf2c3715SXin Li 
162*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(v16,fix<1>), m.reshaped()));
163*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped(16,1).eval(), m.reshaped().eval());
164*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped(1,16).eval(), m.reshaped().transpose().eval());
165*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped().reshaped(2,8), m.reshaped(2,8));
166*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped().reshaped(4,4), m.reshaped(4,4));
167*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped().reshaped(8,2), m.reshaped(8,2));
168*bf2c3715SXin Li 
169*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.reshaped(), m.template reshaped<ColMajor>());
170*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.transpose().reshaped(), m.template reshaped<RowMajor>());
171*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(AutoSize,fix<1>), m.template reshaped<RowMajor>());
172*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.template reshaped<AutoOrder>(AutoSize,fix<1>), m.template reshaped<AutoOrder>());
173*bf2c3715SXin Li 
174*bf2c3715SXin Li   VERIFY(is_same_eq(m.reshaped(AutoSize,fix<1>), m.reshaped()));
175*bf2c3715SXin Li   VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(fix<1>,AutoSize), m.transpose().reshaped().transpose());
176*bf2c3715SXin Li 
177*bf2c3715SXin Li   // check assignment
178*bf2c3715SXin Li   {
179*bf2c3715SXin Li     Matrix<Scalar,Dynamic,1> m1x(m.size()); m1x.setRandom();
180*bf2c3715SXin Li     VERIFY_IS_APPROX(m.reshaped() = m1x, m1x);
181*bf2c3715SXin Li     VERIFY_IS_APPROX(m, m1x.reshaped(4,4));
182*bf2c3715SXin Li 
183*bf2c3715SXin Li     Matrix<Scalar,Dynamic,Dynamic> m28(2,8); m28.setRandom();
184*bf2c3715SXin Li     VERIFY_IS_APPROX(m.reshaped(2,8) = m28, m28);
185*bf2c3715SXin Li     VERIFY_IS_APPROX(m, m28.reshaped(4,4));
186*bf2c3715SXin Li     VERIFY_IS_APPROX(m.template reshaped<RowMajor>(2,8) = m28, m28);
187*bf2c3715SXin Li 
188*bf2c3715SXin Li     Matrix<Scalar,Dynamic,Dynamic> m24(2,4); m24.setRandom();
189*bf2c3715SXin Li     VERIFY_IS_APPROX(m(seq(0,last,2),all).reshaped(2,4) = m24, m24);
190*bf2c3715SXin Li 
191*bf2c3715SXin Li     // check constness:
192*bf2c3715SXin Li     m.reshaped(2,8).nestedExpression() = m;
193*bf2c3715SXin Li   }
194*bf2c3715SXin Li }
195*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(reshape)196*bf2c3715SXin Li EIGEN_DECLARE_TEST(reshape)
197*bf2c3715SXin Li {
198*bf2c3715SXin Li   typedef Matrix<int,Dynamic,Dynamic,RowMajor> RowMatrixXi;
199*bf2c3715SXin Li   typedef Matrix<int,4,4,RowMajor> RowMatrix4i;
200*bf2c3715SXin Li   MatrixXi mx = MatrixXi::Random(4, 4);
201*bf2c3715SXin Li   Matrix4i m4 = Matrix4i::Random(4, 4);
202*bf2c3715SXin Li   RowMatrixXi rmx = RowMatrixXi::Random(4, 4);
203*bf2c3715SXin Li   RowMatrix4i rm4 = RowMatrix4i::Random(4, 4);
204*bf2c3715SXin Li 
205*bf2c3715SXin Li   // test dynamic-size matrix
206*bf2c3715SXin Li   CALL_SUBTEST(reshape4x4(mx));
207*bf2c3715SXin Li   // test static-size matrix
208*bf2c3715SXin Li   CALL_SUBTEST(reshape4x4(m4));
209*bf2c3715SXin Li   // test dynamic-size const matrix
210*bf2c3715SXin Li   CALL_SUBTEST(reshape4x4(static_cast<const MatrixXi>(mx)));
211*bf2c3715SXin Li   // test static-size const matrix
212*bf2c3715SXin Li   CALL_SUBTEST(reshape4x4(static_cast<const Matrix4i>(m4)));
213*bf2c3715SXin Li 
214*bf2c3715SXin Li   CALL_SUBTEST(reshape4x4(rmx));
215*bf2c3715SXin Li   CALL_SUBTEST(reshape4x4(rm4));
216*bf2c3715SXin Li }
217