You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
9.1 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #define EIGEN_NO_STATIC_ASSERT // otherwise we fail at compile time on unused paths
  10. #include "main.h"
  11. template<typename MatrixType> void block(const MatrixType& m)
  12. {
  13. typedef typename MatrixType::Index Index;
  14. typedef typename MatrixType::Scalar Scalar;
  15. typedef typename MatrixType::RealScalar RealScalar;
  16. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  17. typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
  18. typedef Matrix<Scalar, Dynamic, Dynamic> DynamicMatrixType;
  19. typedef Matrix<Scalar, Dynamic, 1> DynamicVectorType;
  20. Index rows = m.rows();
  21. Index cols = m.cols();
  22. MatrixType m1 = MatrixType::Random(rows, cols),
  23. m1_copy = m1,
  24. m2 = MatrixType::Random(rows, cols),
  25. m3(rows, cols),
  26. ones = MatrixType::Ones(rows, cols);
  27. VectorType v1 = VectorType::Random(rows);
  28. Scalar s1 = internal::random<Scalar>();
  29. Index r1 = internal::random<Index>(0,rows-1);
  30. Index r2 = internal::random<Index>(r1,rows-1);
  31. Index c1 = internal::random<Index>(0,cols-1);
  32. Index c2 = internal::random<Index>(c1,cols-1);
  33. //check row() and col()
  34. VERIFY_IS_EQUAL(m1.col(c1).transpose(), m1.transpose().row(c1));
  35. //check operator(), both constant and non-constant, on row() and col()
  36. m1 = m1_copy;
  37. m1.row(r1) += s1 * m1_copy.row(r2);
  38. VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + s1 * m1_copy.row(r2));
  39. // check nested block xpr on lhs
  40. m1.row(r1).row(0) += s1 * m1_copy.row(r2);
  41. VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + Scalar(2) * s1 * m1_copy.row(r2));
  42. m1 = m1_copy;
  43. m1.col(c1) += s1 * m1_copy.col(c2);
  44. VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + s1 * m1_copy.col(c2));
  45. m1.col(c1).col(0) += s1 * m1_copy.col(c2);
  46. VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + Scalar(2) * s1 * m1_copy.col(c2));
  47. //check block()
  48. Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
  49. RowVectorType br1(m1.block(r1,0,1,cols));
  50. VectorType bc1(m1.block(0,c1,rows,1));
  51. VERIFY_IS_EQUAL(b1, m1.block(r1,c1,1,1));
  52. VERIFY_IS_EQUAL(m1.row(r1), br1);
  53. VERIFY_IS_EQUAL(m1.col(c1), bc1);
  54. //check operator(), both constant and non-constant, on block()
  55. m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
  56. m1.block(r1,c1,r2-r1+1,c2-c1+1)(r2-r1,c2-c1) = m2.block(0, 0, r2-r1+1,c2-c1+1)(0,0);
  57. enum {
  58. BlockRows = 2,
  59. BlockCols = 5
  60. };
  61. if (rows>=5 && cols>=8)
  62. {
  63. // test fixed block() as lvalue
  64. m1.template block<BlockRows,BlockCols>(1,1) *= s1;
  65. // test operator() on fixed block() both as constant and non-constant
  66. m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
  67. // check that fixed block() and block() agree
  68. Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
  69. VERIFY_IS_EQUAL(b, m1.block(3,3,BlockRows,BlockCols));
  70. // same tests with mixed fixed/dynamic size
  71. m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols) *= s1;
  72. m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols)(0,3) = m1.template block<2,5>(1,1)(1,2);
  73. Matrix<Scalar,Dynamic,Dynamic> b2 = m1.template block<Dynamic,BlockCols>(3,3,2,5);
  74. VERIFY_IS_EQUAL(b2, m1.block(3,3,BlockRows,BlockCols));
  75. }
  76. if (rows>2)
  77. {
  78. // test sub vectors
  79. VERIFY_IS_EQUAL(v1.template head<2>(), v1.block(0,0,2,1));
  80. VERIFY_IS_EQUAL(v1.template head<2>(), v1.head(2));
  81. VERIFY_IS_EQUAL(v1.template head<2>(), v1.segment(0,2));
  82. VERIFY_IS_EQUAL(v1.template head<2>(), v1.template segment<2>(0));
  83. Index i = rows-2;
  84. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.block(i,0,2,1));
  85. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.tail(2));
  86. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.segment(i,2));
  87. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.template segment<2>(i));
  88. i = internal::random<Index>(0,rows-2);
  89. VERIFY_IS_EQUAL(v1.segment(i,2), v1.template segment<2>(i));
  90. }
  91. // stress some basic stuffs with block matrices
  92. VERIFY(numext::real(ones.col(c1).sum()) == RealScalar(rows));
  93. VERIFY(numext::real(ones.row(r1).sum()) == RealScalar(cols));
  94. VERIFY(numext::real(ones.col(c1).dot(ones.col(c2))) == RealScalar(rows));
  95. VERIFY(numext::real(ones.row(r1).dot(ones.row(r2))) == RealScalar(cols));
  96. // now test some block-inside-of-block.
  97. // expressions with direct access
  98. VERIFY_IS_EQUAL( (m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2)) , (m1.block(r2,c2,rows-r2,cols-c2)) );
  99. VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , (m1.row(r1).segment(c1,c2-c1+1)) );
  100. VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , (m1.col(c1).segment(r1,r2-r1+1)) );
  101. VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , (m1.row(r1).segment(c1,c2-c1+1)).transpose() );
  102. VERIFY_IS_EQUAL( (m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , (m1.row(r1).segment(c1,c2-c1+1)).transpose() );
  103. // expressions without direct access
  104. VERIFY_IS_EQUAL( ((m1+m2).block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2)) , ((m1+m2).block(r2,c2,rows-r2,cols-c2)) );
  105. VERIFY_IS_EQUAL( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)) );
  106. VERIFY_IS_EQUAL( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , ((m1+m2).col(c1).segment(r1,r2-r1+1)) );
  107. VERIFY_IS_EQUAL( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)).transpose() );
  108. VERIFY_IS_EQUAL( ((m1+m2).transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)).transpose() );
  109. // evaluation into plain matrices from expressions with direct access (stress MapBase)
  110. DynamicMatrixType dm;
  111. DynamicVectorType dv;
  112. dm.setZero();
  113. dm = m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2);
  114. VERIFY_IS_EQUAL(dm, (m1.block(r2,c2,rows-r2,cols-c2)));
  115. dm.setZero();
  116. dv.setZero();
  117. dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0).transpose();
  118. dv = m1.row(r1).segment(c1,c2-c1+1);
  119. VERIFY_IS_EQUAL(dv, dm);
  120. dm.setZero();
  121. dv.setZero();
  122. dm = m1.col(c1).segment(r1,r2-r1+1);
  123. dv = m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0);
  124. VERIFY_IS_EQUAL(dv, dm);
  125. dm.setZero();
  126. dv.setZero();
  127. dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0);
  128. dv = m1.row(r1).segment(c1,c2-c1+1);
  129. VERIFY_IS_EQUAL(dv, dm);
  130. dm.setZero();
  131. dv.setZero();
  132. dm = m1.row(r1).segment(c1,c2-c1+1).transpose();
  133. dv = m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0);
  134. VERIFY_IS_EQUAL(dv, dm);
  135. }
  136. template<typename MatrixType>
  137. void compare_using_data_and_stride(const MatrixType& m)
  138. {
  139. typedef typename MatrixType::Index Index;
  140. Index rows = m.rows();
  141. Index cols = m.cols();
  142. Index size = m.size();
  143. Index innerStride = m.innerStride();
  144. Index outerStride = m.outerStride();
  145. Index rowStride = m.rowStride();
  146. Index colStride = m.colStride();
  147. const typename MatrixType::Scalar* data = m.data();
  148. for(int j=0;j<cols;++j)
  149. for(int i=0;i<rows;++i)
  150. VERIFY(m.coeff(i,j) == data[i*rowStride + j*colStride]);
  151. if(!MatrixType::IsVectorAtCompileTime)
  152. {
  153. for(int j=0;j<cols;++j)
  154. for(int i=0;i<rows;++i)
  155. VERIFY(m.coeff(i,j) == data[(MatrixType::Flags&RowMajorBit)
  156. ? i*outerStride + j*innerStride
  157. : j*outerStride + i*innerStride]);
  158. }
  159. if(MatrixType::IsVectorAtCompileTime)
  160. {
  161. VERIFY(innerStride == int((&m.coeff(1))-(&m.coeff(0))));
  162. for (int i=0;i<size;++i)
  163. VERIFY(m.coeff(i) == data[i*innerStride]);
  164. }
  165. }
  166. template<typename MatrixType>
  167. void data_and_stride(const MatrixType& m)
  168. {
  169. typedef typename MatrixType::Index Index;
  170. Index rows = m.rows();
  171. Index cols = m.cols();
  172. Index r1 = internal::random<Index>(0,rows-1);
  173. Index r2 = internal::random<Index>(r1,rows-1);
  174. Index c1 = internal::random<Index>(0,cols-1);
  175. Index c2 = internal::random<Index>(c1,cols-1);
  176. MatrixType m1 = MatrixType::Random(rows, cols);
  177. compare_using_data_and_stride(m1.block(r1, c1, r2-r1+1, c2-c1+1));
  178. compare_using_data_and_stride(m1.transpose().block(c1, r1, c2-c1+1, r2-r1+1));
  179. compare_using_data_and_stride(m1.row(r1));
  180. compare_using_data_and_stride(m1.col(c1));
  181. compare_using_data_and_stride(m1.row(r1).transpose());
  182. compare_using_data_and_stride(m1.col(c1).transpose());
  183. }
  184. void test_block()
  185. {
  186. for(int i = 0; i < g_repeat; i++) {
  187. CALL_SUBTEST_1( block(Matrix<float, 1, 1>()) );
  188. CALL_SUBTEST_2( block(Matrix4d()) );
  189. CALL_SUBTEST_3( block(MatrixXcf(3, 3)) );
  190. CALL_SUBTEST_4( block(MatrixXi(8, 12)) );
  191. CALL_SUBTEST_5( block(MatrixXcd(20, 20)) );
  192. CALL_SUBTEST_6( block(MatrixXf(20, 20)) );
  193. CALL_SUBTEST_8( block(Matrix<float,Dynamic,4>(3, 4)) );
  194. #ifndef EIGEN_DEFAULT_TO_ROW_MAJOR
  195. CALL_SUBTEST_6( data_and_stride(MatrixXf(internal::random(5,50), internal::random(5,50))) );
  196. CALL_SUBTEST_7( data_and_stride(Matrix<int,Dynamic,Dynamic,RowMajor>(internal::random(5,50), internal::random(5,50))) );
  197. #endif
  198. }
  199. }