qr.cpp (4673B)
1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> 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 #include <Eigen/QR> 12 #include "solverbase.h" 13 14 template<typename MatrixType> void qr(const MatrixType& m) 15 { 16 Index rows = m.rows(); 17 Index cols = m.cols(); 18 19 typedef typename MatrixType::Scalar Scalar; 20 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> MatrixQType; 21 22 MatrixType a = MatrixType::Random(rows,cols); 23 HouseholderQR<MatrixType> qrOfA(a); 24 25 MatrixQType q = qrOfA.householderQ(); 26 VERIFY_IS_UNITARY(q); 27 28 MatrixType r = qrOfA.matrixQR().template triangularView<Upper>(); 29 VERIFY_IS_APPROX(a, qrOfA.householderQ() * r); 30 } 31 32 template<typename MatrixType, int Cols2> void qr_fixedsize() 33 { 34 enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime }; 35 typedef typename MatrixType::Scalar Scalar; 36 Matrix<Scalar,Rows,Cols> m1 = Matrix<Scalar,Rows,Cols>::Random(); 37 HouseholderQR<Matrix<Scalar,Rows,Cols> > qr(m1); 38 39 Matrix<Scalar,Rows,Cols> r = qr.matrixQR(); 40 // FIXME need better way to construct trapezoid 41 for(int i = 0; i < Rows; i++) for(int j = 0; j < Cols; j++) if(i>j) r(i,j) = Scalar(0); 42 43 VERIFY_IS_APPROX(m1, qr.householderQ() * r); 44 45 check_solverbase<Matrix<Scalar,Cols,Cols2>, Matrix<Scalar,Rows,Cols2> >(m1, qr, Rows, Cols, Cols2); 46 } 47 48 template<typename MatrixType> void qr_invertible() 49 { 50 using std::log; 51 using std::abs; 52 using std::pow; 53 using std::max; 54 typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar; 55 typedef typename MatrixType::Scalar Scalar; 56 57 STATIC_CHECK(( internal::is_same<typename HouseholderQR<MatrixType>::StorageIndex,int>::value )); 58 59 int size = internal::random<int>(10,50); 60 61 MatrixType m1(size, size), m2(size, size), m3(size, size); 62 m1 = MatrixType::Random(size,size); 63 64 if (internal::is_same<RealScalar,float>::value) 65 { 66 // let's build a matrix more stable to inverse 67 MatrixType a = MatrixType::Random(size,size*4); 68 m1 += a * a.adjoint(); 69 } 70 71 HouseholderQR<MatrixType> qr(m1); 72 73 check_solverbase<MatrixType, MatrixType>(m1, qr, size, size, size); 74 75 // now construct a matrix with prescribed determinant 76 m1.setZero(); 77 for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>(); 78 RealScalar absdet = abs(m1.diagonal().prod()); 79 m3 = qr.householderQ(); // get a unitary 80 m1 = m3 * m1 * m3; 81 qr.compute(m1); 82 VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant()); 83 // This test is tricky if the determinant becomes too small. 84 // Since we generate random numbers with magnitude range [0,1], the average determinant is 0.5^size 85 VERIFY_IS_MUCH_SMALLER_THAN( abs(absdet-qr.absDeterminant()), numext::maxi(RealScalar(pow(0.5,size)),numext::maxi<RealScalar>(abs(absdet),abs(qr.absDeterminant()))) ); 86 87 } 88 89 template<typename MatrixType> void qr_verify_assert() 90 { 91 MatrixType tmp; 92 93 HouseholderQR<MatrixType> qr; 94 VERIFY_RAISES_ASSERT(qr.matrixQR()) 95 VERIFY_RAISES_ASSERT(qr.solve(tmp)) 96 VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp)) 97 VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp)) 98 VERIFY_RAISES_ASSERT(qr.householderQ()) 99 VERIFY_RAISES_ASSERT(qr.absDeterminant()) 100 VERIFY_RAISES_ASSERT(qr.logAbsDeterminant()) 101 } 102 103 EIGEN_DECLARE_TEST(qr) 104 { 105 for(int i = 0; i < g_repeat; i++) { 106 CALL_SUBTEST_1( qr(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 107 CALL_SUBTEST_2( qr(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2),internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) ); 108 CALL_SUBTEST_3(( qr_fixedsize<Matrix<float,3,4>, 2 >() )); 109 CALL_SUBTEST_4(( qr_fixedsize<Matrix<double,6,2>, 4 >() )); 110 CALL_SUBTEST_5(( qr_fixedsize<Matrix<double,2,5>, 7 >() )); 111 CALL_SUBTEST_11( qr(Matrix<float,1,1>()) ); 112 } 113 114 for(int i = 0; i < g_repeat; i++) { 115 CALL_SUBTEST_1( qr_invertible<MatrixXf>() ); 116 CALL_SUBTEST_6( qr_invertible<MatrixXd>() ); 117 CALL_SUBTEST_7( qr_invertible<MatrixXcf>() ); 118 CALL_SUBTEST_8( qr_invertible<MatrixXcd>() ); 119 } 120 121 CALL_SUBTEST_9(qr_verify_assert<Matrix3f>()); 122 CALL_SUBTEST_10(qr_verify_assert<Matrix3d>()); 123 CALL_SUBTEST_1(qr_verify_assert<MatrixXf>()); 124 CALL_SUBTEST_6(qr_verify_assert<MatrixXd>()); 125 CALL_SUBTEST_7(qr_verify_assert<MatrixXcf>()); 126 CALL_SUBTEST_8(qr_verify_assert<MatrixXcd>()); 127 128 // Test problem size constructors 129 CALL_SUBTEST_12(HouseholderQR<MatrixXf>(10, 20)); 130 }