reshape.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "main.h"
  11. template<typename T1,typename T2>
  12. typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
  13. is_same_eq(const T1& a, const T2& b)
  14. {
  15. return (a.array() == b.array()).all();
  16. }
  17. template <int Order,typename MatType>
  18. void check_auto_reshape4x4(MatType m)
  19. {
  20. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 1> v1( 1);
  21. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 2> v2( 2);
  22. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 4> v4( 4);
  23. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 8> v8( 8);
  24. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1:16> v16(16);
  25. VERIFY(is_same_eq(m.template reshaped<Order>( 1, AutoSize), m.template reshaped<Order>( 1, 16)));
  26. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 16 ), m.template reshaped<Order>( 1, 16)));
  27. VERIFY(is_same_eq(m.template reshaped<Order>( 2, AutoSize), m.template reshaped<Order>( 2, 8)));
  28. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 8 ), m.template reshaped<Order>( 2, 8)));
  29. VERIFY(is_same_eq(m.template reshaped<Order>( 4, AutoSize), m.template reshaped<Order>( 4, 4)));
  30. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 4 ), m.template reshaped<Order>( 4, 4)));
  31. VERIFY(is_same_eq(m.template reshaped<Order>( 8, AutoSize), m.template reshaped<Order>( 8, 2)));
  32. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 2 ), m.template reshaped<Order>( 8, 2)));
  33. VERIFY(is_same_eq(m.template reshaped<Order>(16, AutoSize), m.template reshaped<Order>(16, 1)));
  34. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, 1 ), m.template reshaped<Order>(16, 1)));
  35. VERIFY(is_same_eq(m.template reshaped<Order>(fix< 1>, AutoSize), m.template reshaped<Order>(fix< 1>, v16 )));
  36. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix<16> ), m.template reshaped<Order>( v1, fix<16>)));
  37. VERIFY(is_same_eq(m.template reshaped<Order>(fix< 2>, AutoSize), m.template reshaped<Order>(fix< 2>, v8 )));
  38. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix< 8> ), m.template reshaped<Order>( v2, fix< 8>)));
  39. VERIFY(is_same_eq(m.template reshaped<Order>(fix< 4>, AutoSize), m.template reshaped<Order>(fix< 4>, v4 )));
  40. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix< 4> ), m.template reshaped<Order>( v4, fix< 4>)));
  41. VERIFY(is_same_eq(m.template reshaped<Order>(fix< 8>, AutoSize), m.template reshaped<Order>(fix< 8>, v2 )));
  42. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix< 2> ), m.template reshaped<Order>( v8, fix< 2>)));
  43. VERIFY(is_same_eq(m.template reshaped<Order>(fix<16>, AutoSize), m.template reshaped<Order>(fix<16>, v1 )));
  44. VERIFY(is_same_eq(m.template reshaped<Order>(AutoSize, fix< 1> ), m.template reshaped<Order>(v16, fix< 1>)));
  45. }
  46. template <typename MatType>
  47. void check_direct_access_reshape4x4(MatType , internal::FixedInt<RowMajorBit>) {}
  48. template <typename MatType>
  49. void check_direct_access_reshape4x4(MatType m, internal::FixedInt<0>) {
  50. VERIFY_IS_EQUAL(m.reshaped( 1, 16).data(), m.data());
  51. VERIFY_IS_EQUAL(m.reshaped( 1, 16).innerStride(), 1);
  52. VERIFY_IS_EQUAL(m.reshaped( 2, 8).data(), m.data());
  53. VERIFY_IS_EQUAL(m.reshaped( 2, 8).innerStride(), 1);
  54. VERIFY_IS_EQUAL(m.reshaped( 2, 8).outerStride(), 2);
  55. }
  56. // just test a 4x4 matrix, enumerate all combination manually
  57. template <typename MatType>
  58. void reshape4x4(MatType m)
  59. {
  60. typedef typename MatType::Scalar Scalar;
  61. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 1> v1( 1);
  62. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 2> v2( 2);
  63. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 4> v4( 4);
  64. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1: 8> v8( 8);
  65. internal::VariableAndFixedInt<MatType::SizeAtCompileTime==Dynamic?-1:16> v16(16);
  66. if((MatType::Flags&RowMajorBit)==0)
  67. {
  68. typedef Map<MatrixXi> MapMat;
  69. // dynamic
  70. VERIFY_IS_EQUAL((m.reshaped( 1, 16)), MapMat(m.data(), 1, 16));
  71. VERIFY_IS_EQUAL((m.reshaped( 2, 8)), MapMat(m.data(), 2, 8));
  72. VERIFY_IS_EQUAL((m.reshaped( 4, 4)), MapMat(m.data(), 4, 4));
  73. VERIFY_IS_EQUAL((m.reshaped( 8, 2)), MapMat(m.data(), 8, 2));
  74. VERIFY_IS_EQUAL((m.reshaped(16, 1)), MapMat(m.data(), 16, 1));
  75. // static
  76. VERIFY_IS_EQUAL(m.reshaped(fix< 1>, fix<16>), MapMat(m.data(), 1, 16));
  77. VERIFY_IS_EQUAL(m.reshaped(fix< 2>, fix< 8>), MapMat(m.data(), 2, 8));
  78. VERIFY_IS_EQUAL(m.reshaped(fix< 4>, fix< 4>), MapMat(m.data(), 4, 4));
  79. VERIFY_IS_EQUAL(m.reshaped(fix< 8>, fix< 2>), MapMat(m.data(), 8, 2));
  80. VERIFY_IS_EQUAL(m.reshaped(fix<16>, fix< 1>), MapMat(m.data(), 16, 1));
  81. // reshape chain
  82. VERIFY_IS_EQUAL(
  83. (m
  84. .reshaped( 1, 16)
  85. .reshaped(fix< 2>,fix< 8>)
  86. .reshaped(16, 1)
  87. .reshaped(fix< 8>,fix< 2>)
  88. .reshaped( 2, 8)
  89. .reshaped(fix< 1>,fix<16>)
  90. .reshaped( 4, 4)
  91. .reshaped(fix<16>,fix< 1>)
  92. .reshaped( 8, 2)
  93. .reshaped(fix< 4>,fix< 4>)
  94. ),
  95. MapMat(m.data(), 4, 4)
  96. );
  97. }
  98. VERIFY(is_same_eq(m.reshaped( 1, AutoSize), m.reshaped( 1, 16)));
  99. VERIFY(is_same_eq(m.reshaped(AutoSize, 16), m.reshaped( 1, 16)));
  100. VERIFY(is_same_eq(m.reshaped( 2, AutoSize), m.reshaped( 2, 8)));
  101. VERIFY(is_same_eq(m.reshaped(AutoSize, 8), m.reshaped( 2, 8)));
  102. VERIFY(is_same_eq(m.reshaped( 4, AutoSize), m.reshaped( 4, 4)));
  103. VERIFY(is_same_eq(m.reshaped(AutoSize, 4), m.reshaped( 4, 4)));
  104. VERIFY(is_same_eq(m.reshaped( 8, AutoSize), m.reshaped( 8, 2)));
  105. VERIFY(is_same_eq(m.reshaped(AutoSize, 2), m.reshaped( 8, 2)));
  106. VERIFY(is_same_eq(m.reshaped(16, AutoSize), m.reshaped(16, 1)));
  107. VERIFY(is_same_eq(m.reshaped(AutoSize, 1), m.reshaped(16, 1)));
  108. VERIFY(is_same_eq(m.reshaped(fix< 1>, AutoSize), m.reshaped(fix< 1>, v16)));
  109. VERIFY(is_same_eq(m.reshaped(AutoSize, fix<16>), m.reshaped( v1, fix<16>)));
  110. VERIFY(is_same_eq(m.reshaped(fix< 2>, AutoSize), m.reshaped(fix< 2>, v8)));
  111. VERIFY(is_same_eq(m.reshaped(AutoSize, fix< 8>), m.reshaped( v2, fix< 8>)));
  112. VERIFY(is_same_eq(m.reshaped(fix< 4>, AutoSize), m.reshaped(fix< 4>, v4)));
  113. VERIFY(is_same_eq(m.reshaped(AutoSize, fix< 4>), m.reshaped( v4, fix< 4>)));
  114. VERIFY(is_same_eq(m.reshaped(fix< 8>, AutoSize), m.reshaped(fix< 8>, v2)));
  115. VERIFY(is_same_eq(m.reshaped(AutoSize, fix< 2>), m.reshaped( v8, fix< 2>)));
  116. VERIFY(is_same_eq(m.reshaped(fix<16>, AutoSize), m.reshaped(fix<16>, v1)));
  117. VERIFY(is_same_eq(m.reshaped(AutoSize, fix< 1>), m.reshaped(v16, fix< 1>)));
  118. check_auto_reshape4x4<ColMajor> (m);
  119. check_auto_reshape4x4<RowMajor> (m);
  120. check_auto_reshape4x4<AutoOrder>(m);
  121. check_auto_reshape4x4<ColMajor> (m.transpose());
  122. check_auto_reshape4x4<ColMajor> (m.transpose());
  123. check_auto_reshape4x4<AutoOrder>(m.transpose());
  124. check_direct_access_reshape4x4(m,fix<MatType::Flags&RowMajorBit>);
  125. if((MatType::Flags&RowMajorBit)==0)
  126. {
  127. VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2,8),m.reshaped(2,8));
  128. VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2,8),m.template reshaped<AutoOrder>(2,8));
  129. VERIFY_IS_EQUAL(m.transpose().template reshaped<RowMajor>(2,8),m.transpose().template reshaped<AutoOrder>(2,8));
  130. }
  131. else
  132. {
  133. VERIFY_IS_EQUAL(m.template reshaped<ColMajor>(2,8),m.reshaped(2,8));
  134. VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(2,8),m.template reshaped<AutoOrder>(2,8));
  135. VERIFY_IS_EQUAL(m.transpose().template reshaped<ColMajor>(2,8),m.transpose().template reshaped<AutoOrder>(2,8));
  136. VERIFY_IS_EQUAL(m.transpose().reshaped(2,8),m.transpose().template reshaped<AutoOrder>(2,8));
  137. }
  138. MatrixXi m28r1 = m.template reshaped<RowMajor>(2,8);
  139. MatrixXi m28r2 = m.transpose().template reshaped<ColMajor>(8,2).transpose();
  140. VERIFY_IS_EQUAL( m28r1, m28r2);
  141. VERIFY(is_same_eq(m.reshaped(v16,fix<1>), m.reshaped()));
  142. VERIFY_IS_EQUAL(m.reshaped(16,1).eval(), m.reshaped().eval());
  143. VERIFY_IS_EQUAL(m.reshaped(1,16).eval(), m.reshaped().transpose().eval());
  144. VERIFY_IS_EQUAL(m.reshaped().reshaped(2,8), m.reshaped(2,8));
  145. VERIFY_IS_EQUAL(m.reshaped().reshaped(4,4), m.reshaped(4,4));
  146. VERIFY_IS_EQUAL(m.reshaped().reshaped(8,2), m.reshaped(8,2));
  147. VERIFY_IS_EQUAL(m.reshaped(), m.template reshaped<ColMajor>());
  148. VERIFY_IS_EQUAL(m.transpose().reshaped(), m.template reshaped<RowMajor>());
  149. VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(AutoSize,fix<1>), m.template reshaped<RowMajor>());
  150. VERIFY_IS_EQUAL(m.template reshaped<AutoOrder>(AutoSize,fix<1>), m.template reshaped<AutoOrder>());
  151. VERIFY(is_same_eq(m.reshaped(AutoSize,fix<1>), m.reshaped()));
  152. VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(fix<1>,AutoSize), m.transpose().reshaped().transpose());
  153. // check assignment
  154. {
  155. Matrix<Scalar,Dynamic,1> m1x(m.size()); m1x.setRandom();
  156. VERIFY_IS_APPROX(m.reshaped() = m1x, m1x);
  157. VERIFY_IS_APPROX(m, m1x.reshaped(4,4));
  158. Matrix<Scalar,Dynamic,Dynamic> m28(2,8); m28.setRandom();
  159. VERIFY_IS_APPROX(m.reshaped(2,8) = m28, m28);
  160. VERIFY_IS_APPROX(m, m28.reshaped(4,4));
  161. VERIFY_IS_APPROX(m.template reshaped<RowMajor>(2,8) = m28, m28);
  162. Matrix<Scalar,Dynamic,Dynamic> m24(2,4); m24.setRandom();
  163. VERIFY_IS_APPROX(m(seq(0,last,2),all).reshaped(2,4) = m24, m24);
  164. // check constness:
  165. m.reshaped(2,8).nestedExpression() = m;
  166. }
  167. }
  168. EIGEN_DECLARE_TEST(reshape)
  169. {
  170. typedef Matrix<int,Dynamic,Dynamic,RowMajor> RowMatrixXi;
  171. typedef Matrix<int,4,4,RowMajor> RowMatrix4i;
  172. MatrixXi mx = MatrixXi::Random(4, 4);
  173. Matrix4i m4 = Matrix4i::Random(4, 4);
  174. RowMatrixXi rmx = RowMatrixXi::Random(4, 4);
  175. RowMatrix4i rm4 = RowMatrix4i::Random(4, 4);
  176. // test dynamic-size matrix
  177. CALL_SUBTEST(reshape4x4(mx));
  178. // test static-size matrix
  179. CALL_SUBTEST(reshape4x4(m4));
  180. // test dynamic-size const matrix
  181. CALL_SUBTEST(reshape4x4(static_cast<const MatrixXi>(mx)));
  182. // test static-size const matrix
  183. CALL_SUBTEST(reshape4x4(static_cast<const Matrix4i>(m4)));
  184. CALL_SUBTEST(reshape4x4(rmx));
  185. CALL_SUBTEST(reshape4x4(rm4));
  186. }