| 31 | |
| 32 | |
| 33 | template<typename MatrixType> void block(const MatrixType& m) |
| 34 | { |
| 35 | typedef typename MatrixType::Index Index; |
| 36 | typedef typename MatrixType::Scalar Scalar; |
| 37 | typedef typename MatrixType::RealScalar RealScalar; |
| 38 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 39 | typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType; |
| 40 | typedef Matrix<Scalar, Dynamic, Dynamic> DynamicMatrixType; |
| 41 | typedef Matrix<Scalar, Dynamic, 1> DynamicVectorType; |
| 42 | |
| 43 | Index rows = m.rows(); |
| 44 | Index cols = m.cols(); |
| 45 | |
| 46 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 47 | m1_copy = m1, |
| 48 | m2 = MatrixType::Random(rows, cols), |
| 49 | m3(rows, cols), |
| 50 | ones = MatrixType::Ones(rows, cols); |
| 51 | VectorType v1 = VectorType::Random(rows); |
| 52 | |
| 53 | Scalar s1 = internal::random<Scalar>(); |
| 54 | |
| 55 | Index r1 = internal::random<Index>(0,rows-1); |
| 56 | Index r2 = internal::random<Index>(r1,rows-1); |
| 57 | Index c1 = internal::random<Index>(0,cols-1); |
| 58 | Index c2 = internal::random<Index>(c1,cols-1); |
| 59 | |
| 60 | block_real_only(m1, r1, r2, c1, c1, s1); |
| 61 | |
| 62 | //check row() and col() |
| 63 | VERIFY_IS_EQUAL(m1.col(c1).transpose(), m1.transpose().row(c1)); |
| 64 | //check operator(), both constant and non-constant, on row() and col() |
| 65 | m1 = m1_copy; |
| 66 | m1.row(r1) += s1 * m1_copy.row(r2); |
| 67 | VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + s1 * m1_copy.row(r2)); |
| 68 | // check nested block xpr on lhs |
| 69 | m1.row(r1).row(0) += s1 * m1_copy.row(r2); |
| 70 | VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + Scalar(2) * s1 * m1_copy.row(r2)); |
| 71 | m1 = m1_copy; |
| 72 | m1.col(c1) += s1 * m1_copy.col(c2); |
| 73 | VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + s1 * m1_copy.col(c2)); |
| 74 | m1.col(c1).col(0) += s1 * m1_copy.col(c2); |
| 75 | VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + Scalar(2) * s1 * m1_copy.col(c2)); |
| 76 | |
| 77 | |
| 78 | //check block() |
| 79 | Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1); |
| 80 | |
| 81 | RowVectorType br1(m1.block(r1,0,1,cols)); |
| 82 | VectorType bc1(m1.block(0,c1,rows,1)); |
| 83 | VERIFY_IS_EQUAL(b1, m1.block(r1,c1,1,1)); |
| 84 | VERIFY_IS_EQUAL(m1.row(r1), br1); |
| 85 | VERIFY_IS_EQUAL(m1.col(c1), bc1); |
| 86 | //check operator(), both constant and non-constant, on block() |
| 87 | m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1); |
| 88 | 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); |
| 89 | |
| 90 | enum { |