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.

259 lines
10 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, typename Index, typename Scalar>
  12. typename StormEigen::internal::enable_if<!NumTraits<typename MatrixType::Scalar>::IsComplex,typename MatrixType::Scalar>::type
  13. block_real_only(const MatrixType &m1, Index r1, Index r2, Index c1, Index c2, const Scalar& s1) {
  14. // check cwise-Functions:
  15. VERIFY_IS_APPROX(m1.row(r1).cwiseMax(s1), m1.cwiseMax(s1).row(r1));
  16. VERIFY_IS_APPROX(m1.col(c1).cwiseMin(s1), m1.cwiseMin(s1).col(c1));
  17. VERIFY_IS_APPROX(m1.block(r1,c1,r2-r1+1,c2-c1+1).cwiseMin(s1), m1.cwiseMin(s1).block(r1,c1,r2-r1+1,c2-c1+1));
  18. VERIFY_IS_APPROX(m1.block(r1,c1,r2-r1+1,c2-c1+1).cwiseMax(s1), m1.cwiseMax(s1).block(r1,c1,r2-r1+1,c2-c1+1));
  19. return Scalar(0);
  20. }
  21. template<typename MatrixType, typename Index, typename Scalar>
  22. typename StormEigen::internal::enable_if<NumTraits<typename MatrixType::Scalar>::IsComplex,typename MatrixType::Scalar>::type
  23. block_real_only(const MatrixType &, Index, Index, Index, Index, const Scalar&) {
  24. return Scalar(0);
  25. }
  26. template<typename MatrixType> void block(const MatrixType& m)
  27. {
  28. typedef typename MatrixType::Index Index;
  29. typedef typename MatrixType::Scalar Scalar;
  30. typedef typename MatrixType::RealScalar RealScalar;
  31. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  32. typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
  33. typedef Matrix<Scalar, Dynamic, Dynamic> DynamicMatrixType;
  34. typedef Matrix<Scalar, Dynamic, 1> DynamicVectorType;
  35. Index rows = m.rows();
  36. Index cols = m.cols();
  37. MatrixType m1 = MatrixType::Random(rows, cols),
  38. m1_copy = m1,
  39. m2 = MatrixType::Random(rows, cols),
  40. m3(rows, cols),
  41. ones = MatrixType::Ones(rows, cols);
  42. VectorType v1 = VectorType::Random(rows);
  43. Scalar s1 = internal::random<Scalar>();
  44. Index r1 = internal::random<Index>(0,rows-1);
  45. Index r2 = internal::random<Index>(r1,rows-1);
  46. Index c1 = internal::random<Index>(0,cols-1);
  47. Index c2 = internal::random<Index>(c1,cols-1);
  48. block_real_only(m1, r1, r2, c1, c1, s1);
  49. //check row() and col()
  50. VERIFY_IS_EQUAL(m1.col(c1).transpose(), m1.transpose().row(c1));
  51. //check operator(), both constant and non-constant, on row() and col()
  52. m1 = m1_copy;
  53. m1.row(r1) += s1 * m1_copy.row(r2);
  54. VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + s1 * m1_copy.row(r2));
  55. // check nested block xpr on lhs
  56. m1.row(r1).row(0) += s1 * m1_copy.row(r2);
  57. VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + Scalar(2) * s1 * m1_copy.row(r2));
  58. m1 = m1_copy;
  59. m1.col(c1) += s1 * m1_copy.col(c2);
  60. VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + s1 * m1_copy.col(c2));
  61. m1.col(c1).col(0) += s1 * m1_copy.col(c2);
  62. VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + Scalar(2) * s1 * m1_copy.col(c2));
  63. //check block()
  64. Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
  65. RowVectorType br1(m1.block(r1,0,1,cols));
  66. VectorType bc1(m1.block(0,c1,rows,1));
  67. VERIFY_IS_EQUAL(b1, m1.block(r1,c1,1,1));
  68. VERIFY_IS_EQUAL(m1.row(r1), br1);
  69. VERIFY_IS_EQUAL(m1.col(c1), bc1);
  70. //check operator(), both constant and non-constant, on block()
  71. m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
  72. 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);
  73. enum {
  74. BlockRows = 2,
  75. BlockCols = 5
  76. };
  77. if (rows>=5 && cols>=8)
  78. {
  79. // test fixed block() as lvalue
  80. m1.template block<BlockRows,BlockCols>(1,1) *= s1;
  81. // test operator() on fixed block() both as constant and non-constant
  82. m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
  83. // check that fixed block() and block() agree
  84. Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
  85. VERIFY_IS_EQUAL(b, m1.block(3,3,BlockRows,BlockCols));
  86. // same tests with mixed fixed/dynamic size
  87. m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols) *= s1;
  88. m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols)(0,3) = m1.template block<2,5>(1,1)(1,2);
  89. Matrix<Scalar,Dynamic,Dynamic> b2 = m1.template block<Dynamic,BlockCols>(3,3,2,5);
  90. VERIFY_IS_EQUAL(b2, m1.block(3,3,BlockRows,BlockCols));
  91. }
  92. if (rows>2)
  93. {
  94. // test sub vectors
  95. VERIFY_IS_EQUAL(v1.template head<2>(), v1.block(0,0,2,1));
  96. VERIFY_IS_EQUAL(v1.template head<2>(), v1.head(2));
  97. VERIFY_IS_EQUAL(v1.template head<2>(), v1.segment(0,2));
  98. VERIFY_IS_EQUAL(v1.template head<2>(), v1.template segment<2>(0));
  99. Index i = rows-2;
  100. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.block(i,0,2,1));
  101. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.tail(2));
  102. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.segment(i,2));
  103. VERIFY_IS_EQUAL(v1.template tail<2>(), v1.template segment<2>(i));
  104. i = internal::random<Index>(0,rows-2);
  105. VERIFY_IS_EQUAL(v1.segment(i,2), v1.template segment<2>(i));
  106. }
  107. // stress some basic stuffs with block matrices
  108. VERIFY(numext::real(ones.col(c1).sum()) == RealScalar(rows));
  109. VERIFY(numext::real(ones.row(r1).sum()) == RealScalar(cols));
  110. VERIFY(numext::real(ones.col(c1).dot(ones.col(c2))) == RealScalar(rows));
  111. VERIFY(numext::real(ones.row(r1).dot(ones.row(r2))) == RealScalar(cols));
  112. // chekc that linear acccessors works on blocks
  113. m1 = m1_copy;
  114. if((MatrixType::Flags&RowMajorBit)==0)
  115. VERIFY_IS_EQUAL(m1.leftCols(c1).coeff(r1+c1*rows), m1(r1,c1));
  116. else
  117. VERIFY_IS_EQUAL(m1.topRows(r1).coeff(c1+r1*cols), m1(r1,c1));
  118. // now test some block-inside-of-block.
  119. // expressions with direct access
  120. 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)) );
  121. VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , (m1.row(r1).segment(c1,c2-c1+1)) );
  122. VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , (m1.col(c1).segment(r1,r2-r1+1)) );
  123. 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() );
  124. 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() );
  125. // expressions without direct access
  126. VERIFY_IS_APPROX( ((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)) );
  127. VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)) );
  128. VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , ((m1+m2).col(c1).segment(r1,r2-r1+1)) );
  129. VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)).transpose() );
  130. VERIFY_IS_APPROX( ((m1+m2).transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)).transpose() );
  131. // evaluation into plain matrices from expressions with direct access (stress MapBase)
  132. DynamicMatrixType dm;
  133. DynamicVectorType dv;
  134. dm.setZero();
  135. dm = m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2);
  136. VERIFY_IS_EQUAL(dm, (m1.block(r2,c2,rows-r2,cols-c2)));
  137. dm.setZero();
  138. dv.setZero();
  139. dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0).transpose();
  140. dv = m1.row(r1).segment(c1,c2-c1+1);
  141. VERIFY_IS_EQUAL(dv, dm);
  142. dm.setZero();
  143. dv.setZero();
  144. dm = m1.col(c1).segment(r1,r2-r1+1);
  145. dv = m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0);
  146. VERIFY_IS_EQUAL(dv, dm);
  147. dm.setZero();
  148. dv.setZero();
  149. dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0);
  150. dv = m1.row(r1).segment(c1,c2-c1+1);
  151. VERIFY_IS_EQUAL(dv, dm);
  152. dm.setZero();
  153. dv.setZero();
  154. dm = m1.row(r1).segment(c1,c2-c1+1).transpose();
  155. dv = m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0);
  156. VERIFY_IS_EQUAL(dv, dm);
  157. }
  158. template<typename MatrixType>
  159. void compare_using_data_and_stride(const MatrixType& m)
  160. {
  161. typedef typename MatrixType::Index Index;
  162. Index rows = m.rows();
  163. Index cols = m.cols();
  164. Index size = m.size();
  165. Index innerStride = m.innerStride();
  166. Index outerStride = m.outerStride();
  167. Index rowStride = m.rowStride();
  168. Index colStride = m.colStride();
  169. const typename MatrixType::Scalar* data = m.data();
  170. for(int j=0;j<cols;++j)
  171. for(int i=0;i<rows;++i)
  172. VERIFY(m.coeff(i,j) == data[i*rowStride + j*colStride]);
  173. if(!MatrixType::IsVectorAtCompileTime)
  174. {
  175. for(int j=0;j<cols;++j)
  176. for(int i=0;i<rows;++i)
  177. VERIFY(m.coeff(i,j) == data[(MatrixType::Flags&RowMajorBit)
  178. ? i*outerStride + j*innerStride
  179. : j*outerStride + i*innerStride]);
  180. }
  181. if(MatrixType::IsVectorAtCompileTime)
  182. {
  183. VERIFY(innerStride == int((&m.coeff(1))-(&m.coeff(0))));
  184. for (int i=0;i<size;++i)
  185. VERIFY(m.coeff(i) == data[i*innerStride]);
  186. }
  187. }
  188. template<typename MatrixType>
  189. void data_and_stride(const MatrixType& m)
  190. {
  191. typedef typename MatrixType::Index Index;
  192. Index rows = m.rows();
  193. Index cols = m.cols();
  194. Index r1 = internal::random<Index>(0,rows-1);
  195. Index r2 = internal::random<Index>(r1,rows-1);
  196. Index c1 = internal::random<Index>(0,cols-1);
  197. Index c2 = internal::random<Index>(c1,cols-1);
  198. MatrixType m1 = MatrixType::Random(rows, cols);
  199. compare_using_data_and_stride(m1.block(r1, c1, r2-r1+1, c2-c1+1));
  200. compare_using_data_and_stride(m1.transpose().block(c1, r1, c2-c1+1, r2-r1+1));
  201. compare_using_data_and_stride(m1.row(r1));
  202. compare_using_data_and_stride(m1.col(c1));
  203. compare_using_data_and_stride(m1.row(r1).transpose());
  204. compare_using_data_and_stride(m1.col(c1).transpose());
  205. }
  206. void test_block()
  207. {
  208. for(int i = 0; i < g_repeat; i++) {
  209. CALL_SUBTEST_1( block(Matrix<float, 1, 1>()) );
  210. CALL_SUBTEST_2( block(Matrix4d()) );
  211. CALL_SUBTEST_3( block(MatrixXcf(3, 3)) );
  212. CALL_SUBTEST_4( block(MatrixXi(8, 12)) );
  213. CALL_SUBTEST_5( block(MatrixXcd(20, 20)) );
  214. CALL_SUBTEST_6( block(MatrixXf(20, 20)) );
  215. CALL_SUBTEST_8( block(Matrix<float,Dynamic,4>(3, 4)) );
  216. #ifndef EIGEN_DEFAULT_TO_ROW_MAJOR
  217. CALL_SUBTEST_6( data_and_stride(MatrixXf(internal::random(5,50), internal::random(5,50))) );
  218. CALL_SUBTEST_7( data_and_stride(Matrix<int,Dynamic,Dynamic,RowMajor>(internal::random(5,50), internal::random(5,50))) );
  219. #endif
  220. }
  221. }