qr_fullpivoting.cpp
Go to the documentation of this file.
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 
14 template<typename MatrixType> void qr()
15 {
16  Index max_size = EIGEN_TEST_MAX_SIZE;
17  Index min_size = numext::maxi(1,EIGEN_TEST_MAX_SIZE/10);
18  Index rows = internal::random<Index>(min_size,max_size),
19  cols = internal::random<Index>(min_size,max_size),
20  cols2 = internal::random<Index>(min_size,max_size),
21  rank = internal::random<Index>(1, (std::min)(rows, cols)-1);
22 
23  typedef typename MatrixType::Scalar Scalar;
25  MatrixType m1;
26  createRandomPIMatrixOfRank(rank,rows,cols,m1);
28  VERIFY_IS_EQUAL(rank, qr.rank());
30  VERIFY(!qr.isInjective());
31  VERIFY(!qr.isInvertible());
32  VERIFY(!qr.isSurjective());
33 
34  MatrixType r = qr.matrixQR();
35 
36  MatrixQType q = qr.matrixQ();
38 
39  // FIXME need better way to construct trapezoid
40  for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) if(i>j) r(i,j) = Scalar(0);
41 
42  MatrixType c = qr.matrixQ() * r * qr.colsPermutation().inverse();
43 
44  VERIFY_IS_APPROX(m1, c);
45 
46  // stress the ReturnByValue mechanism
47  MatrixType tmp;
48  VERIFY_IS_APPROX(tmp.noalias() = qr.matrixQ() * r, (qr.matrixQ() * r).eval());
49 
50  MatrixType m2 = MatrixType::Random(cols,cols2);
51  MatrixType m3 = m1*m2;
52  m2 = MatrixType::Random(cols,cols2);
53  m2 = qr.solve(m3);
54  VERIFY_IS_APPROX(m3, m1*m2);
55 
56  {
57  Index size = rows;
58  do {
59  m1 = MatrixType::Random(size,size);
60  qr.compute(m1);
61  } while(!qr.isInvertible());
62  MatrixType m1_inv = qr.inverse();
63  m3 = m1 * MatrixType::Random(size,cols2);
64  m2 = qr.solve(m3);
65  VERIFY_IS_APPROX(m2, m1_inv*m3);
66  }
67 }
68 
69 template<typename MatrixType> void qr_invertible()
70 {
71  using std::log;
72  using std::abs;
74  typedef typename MatrixType::Scalar Scalar;
75 
76  Index max_size = numext::mini(50,EIGEN_TEST_MAX_SIZE);
77  Index min_size = numext::maxi(1,EIGEN_TEST_MAX_SIZE/10);
78  Index size = internal::random<Index>(min_size,max_size);
79 
80  MatrixType m1(size, size), m2(size, size), m3(size, size);
81  m1 = MatrixType::Random(size,size);
82 
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 
91  VERIFY(qr.isInjective());
92  VERIFY(qr.isInvertible());
93  VERIFY(qr.isSurjective());
94 
95  m3 = MatrixType::Random(size,size);
96  m2 = qr.solve(m3);
97  VERIFY_IS_APPROX(m3, m1*m2);
98 
99  // now construct a matrix with prescribed determinant
100  m1.setZero();
101  for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
102  RealScalar absdet = abs(m1.diagonal().prod());
103  m3 = qr.matrixQ(); // get a unitary
104  m1 = m3 * m1 * m3;
105  qr.compute(m1);
106  VERIFY_IS_APPROX(absdet, qr.absDeterminant());
107  VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
108 }
109 
110 template<typename MatrixType> void qr_verify_assert()
111 {
112  MatrixType tmp;
113 
116  VERIFY_RAISES_ASSERT(qr.solve(tmp))
125 }
126 
128 {
129  for(int i = 0; i < 1; i++) {
130  // FIXME : very weird bug here
131 // CALL_SUBTEST(qr(Matrix2f()) );
132  CALL_SUBTEST_1( qr<MatrixXf>() );
133  CALL_SUBTEST_2( qr<MatrixXd>() );
134  CALL_SUBTEST_3( qr<MatrixXcd>() );
135  }
136 
137  for(int i = 0; i < g_repeat; i++) {
138  CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
139  CALL_SUBTEST_2( qr_invertible<MatrixXd>() );
140  CALL_SUBTEST_4( qr_invertible<MatrixXcf>() );
141  CALL_SUBTEST_3( qr_invertible<MatrixXcd>() );
142  }
143 
144  CALL_SUBTEST_5(qr_verify_assert<Matrix3f>());
145  CALL_SUBTEST_6(qr_verify_assert<Matrix3d>());
146  CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
147  CALL_SUBTEST_2(qr_verify_assert<MatrixXd>());
148  CALL_SUBTEST_4(qr_verify_assert<MatrixXcf>());
149  CALL_SUBTEST_3(qr_verify_assert<MatrixXcd>());
150 
151  // Test problem size constructors
152  CALL_SUBTEST_7(FullPivHouseholderQR<MatrixXf>(10, 20));
153  CALL_SUBTEST_7((FullPivHouseholderQR<Matrix<float,10,20> >(10,20)));
155  CALL_SUBTEST_7((FullPivHouseholderQR<Matrix<float,20,10> >(20,10)));
157 }
SCALAR Scalar
Definition: bench_gemm.cpp:33
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:285
void test_qr_fullpivoting()
#define min(a, b)
Definition: datatypes.h:19
MatrixQReturnType matrixQ(void) const
MatrixType m2(n_dims)
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC const LogReturnType log() const
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
FullPivHouseholderQR & compute(const EigenBase< InputType > &matrix)
Array33i a
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
const Solve< FullPivHouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
void qr_verify_assert()
#define VERIFY_IS_APPROX(a, b)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:331
const PermutationType & colsPermutation() const
Matrix3d m1
Definition: IOFormat.cpp:2
void qr()
void qr_invertible()
static int g_repeat
Definition: main.h:144
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
EIGEN_DEVICE_FUNC const Scalar & q
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:34
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
InverseReturnType inverse() const
MatrixType::RealScalar logAbsDeterminant() const
#define VERIFY(a)
Definition: main.h:325
#define EIGEN_TEST_MAX_SIZE
MatrixType::RealScalar absDeterminant() const
const MatrixType & matrixQR() const
#define VERIFY_IS_UNITARY(a)
Definition: main.h:340
void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType &m)
Definition: main.h:603
internal::nested_eval< T, 1 >::type eval(const T &xpr)
The matrix class, also used for vectors and row-vectors.
#define abs(x)
Definition: datatypes.h:17
std::ptrdiff_t j
const Inverse< FullPivHouseholderQR > inverse() const


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:46