1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. Eigen itself is part of the KDE project. 3 // 4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) 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 10 #include "main.h" 11 12 // check minor separately in order to avoid the possible creation of a zero-sized 13 // array. Comes from a compilation error with gcc-3.4 or gcc-4 with -ansi -pedantic. 14 // Another solution would be to declare the array like this: T m_data[Size==0?1:Size]; in ei_matrix_storage 15 // but this is probably not bad to raise such an error at compile time... 16 template<typename Scalar, int _Rows, int _Cols> struct CheckMinor 17 { 18 typedef Matrix<Scalar, _Rows, _Cols> MatrixType; 19 CheckMinor(MatrixType& m1, int r1, int c1) 20 { 21 int rows = m1.rows(); 22 int cols = m1.cols(); 23 24 Matrix<Scalar, Dynamic, Dynamic> mi = m1.minor(0,0).eval(); 25 VERIFY_IS_APPROX(mi, m1.block(1,1,rows-1,cols-1)); 26 mi = m1.minor(r1,c1); 27 VERIFY_IS_APPROX(mi.transpose(), m1.transpose().minor(c1,r1)); 28 //check operator(), both constant and non-constant, on minor() 29 m1.minor(r1,c1)(0,0) = m1.minor(0,0)(0,0); 30 } 31 }; 32 33 template<typename Scalar> struct CheckMinor<Scalar,1,1> 34 { 35 typedef Matrix<Scalar, 1, 1> MatrixType; 36 CheckMinor(MatrixType&, int, int) {} 37 }; 38 39 template<typename MatrixType> void submatrices(const MatrixType& m) 40 { 41 /* this test covers the following files: 42 Row.h Column.h Block.h Minor.h DiagonalCoeffs.h 43 */ 44 typedef typename MatrixType::Scalar Scalar; 45 typedef typename MatrixType::RealScalar RealScalar; 46 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; 47 typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType; 48 int rows = m.rows(); 49 int cols = m.cols(); 50 51 MatrixType m1 = MatrixType::Random(rows, cols), 52 m2 = MatrixType::Random(rows, cols), 53 m3(rows, cols), 54 mzero = MatrixType::Zero(rows, cols), 55 ones = MatrixType::Ones(rows, cols), 56 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> 57 ::Identity(rows, rows), 58 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> 59 ::Random(rows, rows); 60 VectorType v1 = VectorType::Random(rows), 61 v2 = VectorType::Random(rows), 62 v3 = VectorType::Random(rows), 63 vzero = VectorType::Zero(rows); 64 65 Scalar s1 = ei_random<Scalar>(); 66 67 int r1 = ei_random<int>(0,rows-1); 68 int r2 = ei_random<int>(r1,rows-1); 69 int c1 = ei_random<int>(0,cols-1); 70 int c2 = ei_random<int>(c1,cols-1); 71 72 //check row() and col() 73 VERIFY_IS_APPROX(m1.col(c1).transpose(), m1.transpose().row(c1)); 74 VERIFY_IS_APPROX(square.row(r1).eigen2_dot(m1.col(c1)), (square.lazy() * m1.conjugate())(r1,c1)); 75 //check operator(), both constant and non-constant, on row() and col() 76 m1.row(r1) += s1 * m1.row(r2); 77 m1.col(c1) += s1 * m1.col(c2); 78 79 //check block() 80 Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1); 81 RowVectorType br1(m1.block(r1,0,1,cols)); 82 VectorType bc1(m1.block(0,c1,rows,1)); 83 VERIFY_IS_APPROX(b1, m1.block(r1,c1,1,1)); 84 VERIFY_IS_APPROX(m1.row(r1), br1); 85 VERIFY_IS_APPROX(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 //check minor() 91 CheckMinor<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> checkminor(m1,r1,c1); 92 93 //check diagonal() 94 VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal()); 95 m2.diagonal() = 2 * m1.diagonal(); 96 m2.diagonal()[0] *= 3; 97 VERIFY_IS_APPROX(m2.diagonal()[0], static_cast<Scalar>(6) * m1.diagonal()[0]); 98 99 enum { 100 BlockRows = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::RowsAtCompileTime,2), 101 BlockCols = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::ColsAtCompileTime,5) 102 }; 103 if (rows>=5 && cols>=8) 104 { 105 // test fixed block() as lvalue 106 m1.template block<BlockRows,BlockCols>(1,1) *= s1; 107 // test operator() on fixed block() both as constant and non-constant 108 m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2); 109 // check that fixed block() and block() agree 110 Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3); 111 VERIFY_IS_APPROX(b, m1.block(3,3,BlockRows,BlockCols)); 112 } 113 114 if (rows>2) 115 { 116 // test sub vectors 117 VERIFY_IS_APPROX(v1.template start<2>(), v1.block(0,0,2,1)); 118 VERIFY_IS_APPROX(v1.template start<2>(), v1.start(2)); 119 VERIFY_IS_APPROX(v1.template start<2>(), v1.segment(0,2)); 120 VERIFY_IS_APPROX(v1.template start<2>(), v1.template segment<2>(0)); 121 int i = rows-2; 122 VERIFY_IS_APPROX(v1.template end<2>(), v1.block(i,0,2,1)); 123 VERIFY_IS_APPROX(v1.template end<2>(), v1.end(2)); 124 VERIFY_IS_APPROX(v1.template end<2>(), v1.segment(i,2)); 125 VERIFY_IS_APPROX(v1.template end<2>(), v1.template segment<2>(i)); 126 i = ei_random(0,rows-2); 127 VERIFY_IS_APPROX(v1.segment(i,2), v1.template segment<2>(i)); 128 } 129 130 // stress some basic stuffs with block matrices 131 VERIFY(ei_real(ones.col(c1).sum()) == RealScalar(rows)); 132 VERIFY(ei_real(ones.row(r1).sum()) == RealScalar(cols)); 133 134 VERIFY(ei_real(ones.col(c1).eigen2_dot(ones.col(c2))) == RealScalar(rows)); 135 VERIFY(ei_real(ones.row(r1).eigen2_dot(ones.row(r2))) == RealScalar(cols)); 136 } 137 138 void test_eigen2_submatrices() 139 { 140 for(int i = 0; i < g_repeat; i++) { 141 CALL_SUBTEST_1( submatrices(Matrix<float, 1, 1>()) ); 142 CALL_SUBTEST_2( submatrices(Matrix4d()) ); 143 CALL_SUBTEST_3( submatrices(MatrixXcf(3, 3)) ); 144 CALL_SUBTEST_4( submatrices(MatrixXi(8, 12)) ); 145 CALL_SUBTEST_5( submatrices(MatrixXcd(20, 20)) ); 146 CALL_SUBTEST_6( submatrices(MatrixXf(20, 20)) ); 147 } 148 } 149