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