diff options
author | Benoit Jacob <jacob.benoit.1@gmail.com> | 2009-09-28 09:40:18 -0400 |
---|---|---|
committer | Benoit Jacob <jacob.benoit.1@gmail.com> | 2009-09-28 09:40:18 -0400 |
commit | 67bf7c90c5c41d8b62411c298d657908537118ea (patch) | |
tree | 5c1606a4ab8174f93d7b561b543644be83fcbbe9 /test/qr_colpivoting.cpp | |
parent | de942a44c2459a19a12b0b6309cbc226867ea691 (diff) |
* update test to expose bug #57
* update createRandomMatrixOfRank to support fixed size
Diffstat (limited to 'test/qr_colpivoting.cpp')
-rw-r--r-- | test/qr_colpivoting.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/test/qr_colpivoting.cpp b/test/qr_colpivoting.cpp index 588a41e56..e0edad842 100644 --- a/test/qr_colpivoting.cpp +++ b/test/qr_colpivoting.cpp @@ -63,6 +63,40 @@ template<typename MatrixType> void qr() VERIFY(!qr.solve(m3, &m2)); } +template<typename MatrixType, int Cols2> void qr_fixedsize() +{ + enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime }; + typedef typename MatrixType::Scalar Scalar; + int rank = ei_random<int>(1, std::min(int(Rows), int(Cols))-1); + Matrix<Scalar,Rows,Cols> m1; + createRandomMatrixOfRank(rank,Rows,Cols,m1); + ColPivotingHouseholderQR<Matrix<Scalar,Rows,Cols> > qr(m1); + VERIFY_IS_APPROX(rank, qr.rank()); + VERIFY(Cols - qr.rank() == qr.dimensionOfKernel()); + VERIFY(!qr.isInjective()); + VERIFY(!qr.isInvertible()); + VERIFY(!qr.isSurjective()); + + Matrix<Scalar,Rows,Cols> r = qr.matrixQR(); + // FIXME need better way to construct trapezoid + for(int i = 0; i < Rows; i++) for(int j = 0; j < Cols; j++) if(i>j) r(i,j) = Scalar(0); + + Matrix<Scalar,Rows,Cols> b = qr.matrixQ() * r; + + Matrix<Scalar,Rows,Cols> c = MatrixType::Zero(Rows,Cols); + + for(int i = 0; i < Cols; ++i) c.col(qr.colsPermutation().coeff(i)) = b.col(i); + VERIFY_IS_APPROX(m1, c); + + Matrix<Scalar,Cols,Cols2> m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2); + Matrix<Scalar,Rows,Cols2> m3 = m1*m2; + m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2); + VERIFY(qr.solve(m3, &m2)); + VERIFY_IS_APPROX(m3, m1*m2); + m3 = Matrix<Scalar,Rows,Cols2>::Random(Rows,Cols2); + VERIFY(!qr.solve(m3, &m2)); +} + template<typename MatrixType> void qr_invertible() { typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar; @@ -121,6 +155,9 @@ void test_qr_colpivoting() CALL_SUBTEST( qr<MatrixXd>() ); CALL_SUBTEST( qr<MatrixXcd>() ); } + + CALL_SUBTEST(( qr_fixedsize<Matrix<float,3,5>, 4 >() )); + CALL_SUBTEST(( qr_fixedsize<Matrix<double,6,2>, 3 >() )); for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST( qr_invertible<MatrixXf>() ); |