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 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11 #include "main.h"
12 #include <Eigen/QR>
13
qr()14 template<typename MatrixType> void qr()
15 {
16 typedef typename MatrixType::Index Index;
17
18 Index rows = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols2 = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE);
19 Index rank = internal::random<Index>(1, (std::min)(rows, cols)-1);
20
21 typedef typename MatrixType::Scalar Scalar;
22 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> MatrixQType;
23 MatrixType m1;
24 createRandomPIMatrixOfRank(rank,rows,cols,m1);
25 ColPivHouseholderQR<MatrixType> qr(m1);
26 VERIFY(rank == qr.rank());
27 VERIFY(cols - qr.rank() == qr.dimensionOfKernel());
28 VERIFY(!qr.isInjective());
29 VERIFY(!qr.isInvertible());
30 VERIFY(!qr.isSurjective());
31
32 MatrixQType q = qr.householderQ();
33 VERIFY_IS_UNITARY(q);
34
35 MatrixType r = qr.matrixQR().template triangularView<Upper>();
36 MatrixType c = q * r * qr.colsPermutation().inverse();
37 VERIFY_IS_APPROX(m1, c);
38
39 MatrixType m2 = MatrixType::Random(cols,cols2);
40 MatrixType m3 = m1*m2;
41 m2 = MatrixType::Random(cols,cols2);
42 m2 = qr.solve(m3);
43 VERIFY_IS_APPROX(m3, m1*m2);
44 }
45
qr_fixedsize()46 template<typename MatrixType, int Cols2> void qr_fixedsize()
47 {
48 enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
49 typedef typename MatrixType::Scalar Scalar;
50 int rank = internal::random<int>(1, (std::min)(int(Rows), int(Cols))-1);
51 Matrix<Scalar,Rows,Cols> m1;
52 createRandomPIMatrixOfRank(rank,Rows,Cols,m1);
53 ColPivHouseholderQR<Matrix<Scalar,Rows,Cols> > qr(m1);
54 VERIFY(rank == qr.rank());
55 VERIFY(Cols - qr.rank() == qr.dimensionOfKernel());
56 VERIFY(qr.isInjective() == (rank == Rows));
57 VERIFY(qr.isSurjective() == (rank == Cols));
58 VERIFY(qr.isInvertible() == (qr.isInjective() && qr.isSurjective()));
59
60 Matrix<Scalar,Rows,Cols> r = qr.matrixQR().template triangularView<Upper>();
61 Matrix<Scalar,Rows,Cols> c = qr.householderQ() * r * qr.colsPermutation().inverse();
62 VERIFY_IS_APPROX(m1, c);
63
64 Matrix<Scalar,Cols,Cols2> m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2);
65 Matrix<Scalar,Rows,Cols2> m3 = m1*m2;
66 m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2);
67 m2 = qr.solve(m3);
68 VERIFY_IS_APPROX(m3, m1*m2);
69 }
70
qr_invertible()71 template<typename MatrixType> void qr_invertible()
72 {
73 using std::log;
74 using std::abs;
75 typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
76 typedef typename MatrixType::Scalar Scalar;
77
78 int size = internal::random<int>(10,50);
79
80 MatrixType m1(size, size), m2(size, size), m3(size, size);
81 m1 = MatrixType::Random(size,size);
82
83 if (internal::is_same<RealScalar,float>::value)
84 {
85 // let's build a matrix more stable to inverse
86 MatrixType a = MatrixType::Random(size,size*2);
87 m1 += a * a.adjoint();
88 }
89
90 ColPivHouseholderQR<MatrixType> qr(m1);
91 m3 = MatrixType::Random(size,size);
92 m2 = qr.solve(m3);
93 //VERIFY_IS_APPROX(m3, m1*m2);
94
95 // now construct a matrix with prescribed determinant
96 m1.setZero();
97 for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
98 RealScalar absdet = abs(m1.diagonal().prod());
99 m3 = qr.householderQ(); // get a unitary
100 m1 = m3 * m1 * m3;
101 qr.compute(m1);
102 VERIFY_IS_APPROX(absdet, qr.absDeterminant());
103 VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
104 }
105
qr_verify_assert()106 template<typename MatrixType> void qr_verify_assert()
107 {
108 MatrixType tmp;
109
110 ColPivHouseholderQR<MatrixType> qr;
111 VERIFY_RAISES_ASSERT(qr.matrixQR())
112 VERIFY_RAISES_ASSERT(qr.solve(tmp))
113 VERIFY_RAISES_ASSERT(qr.householderQ())
114 VERIFY_RAISES_ASSERT(qr.dimensionOfKernel())
115 VERIFY_RAISES_ASSERT(qr.isInjective())
116 VERIFY_RAISES_ASSERT(qr.isSurjective())
117 VERIFY_RAISES_ASSERT(qr.isInvertible())
118 VERIFY_RAISES_ASSERT(qr.inverse())
119 VERIFY_RAISES_ASSERT(qr.absDeterminant())
120 VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
121 }
122
test_qr_colpivoting()123 void test_qr_colpivoting()
124 {
125 for(int i = 0; i < g_repeat; i++) {
126 CALL_SUBTEST_1( qr<MatrixXf>() );
127 CALL_SUBTEST_2( qr<MatrixXd>() );
128 CALL_SUBTEST_3( qr<MatrixXcd>() );
129 CALL_SUBTEST_4(( qr_fixedsize<Matrix<float,3,5>, 4 >() ));
130 CALL_SUBTEST_5(( qr_fixedsize<Matrix<double,6,2>, 3 >() ));
131 CALL_SUBTEST_5(( qr_fixedsize<Matrix<double,1,1>, 1 >() ));
132 }
133
134 for(int i = 0; i < g_repeat; i++) {
135 CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
136 CALL_SUBTEST_2( qr_invertible<MatrixXd>() );
137 CALL_SUBTEST_6( qr_invertible<MatrixXcf>() );
138 CALL_SUBTEST_3( qr_invertible<MatrixXcd>() );
139 }
140
141 CALL_SUBTEST_7(qr_verify_assert<Matrix3f>());
142 CALL_SUBTEST_8(qr_verify_assert<Matrix3d>());
143 CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
144 CALL_SUBTEST_2(qr_verify_assert<MatrixXd>());
145 CALL_SUBTEST_6(qr_verify_assert<MatrixXcf>());
146 CALL_SUBTEST_3(qr_verify_assert<MatrixXcd>());
147
148 // Test problem size constructors
149 CALL_SUBTEST_9(ColPivHouseholderQR<MatrixXf>(10, 20));
150 }
151