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.

222 lines
8.7 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. }
  71. if (rows>2)
  72. {
  73. // test sub vectors
  74. VERIFY_IS_EQUAL(v1.template head<2>(), v1.block(0,0,2,1));
  75. VERIFY_IS_EQUAL(v1.template head<2>(), v1.head(2));
  76. VERIFY_IS_EQUAL(v1.template head<2>(), v1.segment(0,2));
  77. VERIFY_IS_EQUAL(v1.template head<2>(), v1.template segment<2>(0));
  78. Index i = rows-2;
  79. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.block(i,0,2,1));
  80. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.tail(2));
  81. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.segment(i,2));
  82. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.template segment<2>(i));
  83. i = internal::random<Index>(0,rows-2);
  84. VERIFY_IS_EQUAL(v1.segment(i,2), v1.template segment<2>(i));
  85. }
  86. // stress some basic stuffs with block matrices
  87. VERIFY(internal::real(ones.col(c1).sum()) == RealScalar(rows));
  88. VERIFY(internal::real(ones.row(r1).sum()) == RealScalar(cols));
  89. VERIFY(internal::real(ones.col(c1).dot(ones.col(c2))) == RealScalar(rows));
  90. VERIFY(internal::real(ones.row(r1).dot(ones.row(r2))) == RealScalar(cols));
  91. // now test some block-inside-of-block.
  92. // expressions with direct access
  93. 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)) );
  94. VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , (m1.row(r1).segment(c1,c2-c1+1)) );
  95. VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , (m1.col(c1).segment(r1,r2-r1+1)) );
  96. 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() );
  97. 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() );
  98. // expressions without direct access
  99. 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)) );
  100. 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)) );
  101. 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)) );
  102. 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() );
  103. 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() );
  104. // evaluation into plain matrices from expressions with direct access (stress MapBase)
  105. DynamicMatrixType dm;
  106. DynamicVectorType dv;
  107. dm.setZero();
  108. dm = m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2);
  109. VERIFY_IS_EQUAL(dm, (m1.block(r2,c2,rows-r2,cols-c2)));
  110. dm.setZero();
  111. dv.setZero();
  112. dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0).transpose();
  113. dv = m1.row(r1).segment(c1,c2-c1+1);
  114. VERIFY_IS_EQUAL(dv, dm);
  115. dm.setZero();
  116. dv.setZero();
  117. dm = m1.col(c1).segment(r1,r2-r1+1);
  118. dv = m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0);
  119. VERIFY_IS_EQUAL(dv, dm);
  120. dm.setZero();
  121. dv.setZero();
  122. dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0);
  123. dv = m1.row(r1).segment(c1,c2-c1+1);
  124. VERIFY_IS_EQUAL(dv, dm);
  125. dm.setZero();
  126. dv.setZero();
  127. dm = m1.row(r1).segment(c1,c2-c1+1).transpose();
  128. dv = m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0);
  129. VERIFY_IS_EQUAL(dv, dm);
  130. }
  131. template<typename MatrixType>
  132. void compare_using_data_and_stride(const MatrixType& m)
  133. {
  134. typedef typename MatrixType::Index Index;
  135. Index rows = m.rows();
  136. Index cols = m.cols();
  137. Index size = m.size();
  138. Index innerStride = m.innerStride();
  139. Index outerStride = m.outerStride();
  140. Index rowStride = m.rowStride();
  141. Index colStride = m.colStride();
  142. const typename MatrixType::Scalar* data = m.data();
  143. for(int j=0;j<cols;++j)
  144. for(int i=0;i<rows;++i)
  145. VERIFY(m.coeff(i,j) == data[i*rowStride + j*colStride]);
  146. if(!MatrixType::IsVectorAtCompileTime)
  147. {
  148. for(int j=0;j<cols;++j)
  149. for(int i=0;i<rows;++i)
  150. VERIFY(m.coeff(i,j) == data[(MatrixType::Flags&RowMajorBit)
  151. ? i*outerStride + j*innerStride
  152. : j*outerStride + i*innerStride]);
  153. }
  154. if(MatrixType::IsVectorAtCompileTime)
  155. {
  156. VERIFY(innerStride == int((&m.coeff(1))-(&m.coeff(0))));
  157. for (int i=0;i<size;++i)
  158. VERIFY(m.coeff(i) == data[i*innerStride]);
  159. }
  160. }
  161. template<typename MatrixType>
  162. void data_and_stride(const MatrixType& m)
  163. {
  164. typedef typename MatrixType::Index Index;
  165. Index rows = m.rows();
  166. Index cols = m.cols();
  167. Index r1 = internal::random<Index>(0,rows-1);
  168. Index r2 = internal::random<Index>(r1,rows-1);
  169. Index c1 = internal::random<Index>(0,cols-1);
  170. Index c2 = internal::random<Index>(c1,cols-1);
  171. MatrixType m1 = MatrixType::Random(rows, cols);
  172. compare_using_data_and_stride(m1.block(r1, c1, r2-r1+1, c2-c1+1));
  173. compare_using_data_and_stride(m1.transpose().block(c1, r1, c2-c1+1, r2-r1+1));
  174. compare_using_data_and_stride(m1.row(r1));
  175. compare_using_data_and_stride(m1.col(c1));
  176. compare_using_data_and_stride(m1.row(r1).transpose());
  177. compare_using_data_and_stride(m1.col(c1).transpose());
  178. }
  179. void test_block()
  180. {
  181. for(int i = 0; i < g_repeat; i++) {
  182. CALL_SUBTEST_1( block(Matrix<float, 1, 1>()) );
  183. CALL_SUBTEST_2( block(Matrix4d()) );
  184. CALL_SUBTEST_3( block(MatrixXcf(3, 3)) );
  185. CALL_SUBTEST_4( block(MatrixXi(8, 12)) );
  186. CALL_SUBTEST_5( block(MatrixXcd(20, 20)) );
  187. CALL_SUBTEST_6( block(MatrixXf(20, 20)) );
  188. CALL_SUBTEST_8( block(Matrix<float,Dynamic,4>(3, 4)) );
  189. #ifndef EIGEN_DEFAULT_TO_ROW_MAJOR
  190. CALL_SUBTEST_6( data_and_stride(MatrixXf(internal::random(5,50), internal::random(5,50))) );
  191. CALL_SUBTEST_7( data_and_stride(Matrix<int,Dynamic,Dynamic,RowMajor>(internal::random(5,50), internal::random(5,50))) );
  192. #endif
  193. }
  194. }