qr_colpivoting.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 #include <Eigen/SVD>
14 #include "solverbase.h"
15 
16 template <typename MatrixType>
17 void cod() {
19 
20  Index rows = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
21  Index cols = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
22  Index cols2 = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE);
23  Index rank = internal::random<Index>(1, (std::min)(rows, cols) - 1);
24 
25  typedef typename MatrixType::Scalar Scalar;
26  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime,
27  MatrixType::RowsAtCompileTime>
28  MatrixQType;
30  createRandomPIMatrixOfRank(rank, rows, cols, matrix);
32  VERIFY(rank == cod.rank());
33  VERIFY(cols - cod.rank() == cod.dimensionOfKernel());
34  VERIFY(!cod.isInjective());
35  VERIFY(!cod.isInvertible());
36  VERIFY(!cod.isSurjective());
37 
38  MatrixQType q = cod.householderQ();
40 
41  MatrixType z = cod.matrixZ();
43 
44  MatrixType t;
45  t.setZero(rows, cols);
46  t.topLeftCorner(rank, rank) =
47  cod.matrixT().topLeftCorner(rank, rank).template triangularView<Upper>();
48 
49  MatrixType c = q * t * z * cod.colsPermutation().inverse();
50  VERIFY_IS_APPROX(matrix, c);
51 
52  check_solverbase<MatrixType, MatrixType>(matrix, cod, rows, cols, cols2);
53 
54  // Verify that we get the same minimum-norm solution as the SVD.
55  MatrixType exact_solution = MatrixType::Random(cols, cols2);
56  MatrixType rhs = matrix * exact_solution;
57  MatrixType cod_solution = cod.solve(rhs);
59  MatrixType svd_solution = svd.solve(rhs);
60  VERIFY_IS_APPROX(cod_solution, svd_solution);
61 
62  MatrixType pinv = cod.pseudoInverse();
63  VERIFY_IS_APPROX(cod_solution, pinv * rhs);
64 }
65 
66 template <typename MatrixType, int Cols2>
67 void cod_fixedsize() {
68  enum {
69  Rows = MatrixType::RowsAtCompileTime,
70  Cols = MatrixType::ColsAtCompileTime
71  };
72  typedef typename MatrixType::Scalar Scalar;
74  int rank = internal::random<int>(1, (std::min)(int(Rows), int(Cols)) - 1);
76  createRandomPIMatrixOfRank(rank, Rows, Cols, matrix);
77  COD cod(matrix);
78  VERIFY(rank == cod.rank());
79  VERIFY(Cols - cod.rank() == cod.dimensionOfKernel());
80  VERIFY(cod.isInjective() == (rank == Rows));
81  VERIFY(cod.isSurjective() == (rank == Cols));
82  VERIFY(cod.isInvertible() == (cod.isInjective() && cod.isSurjective()));
83 
84  check_solverbase<Matrix<Scalar, Cols, Cols2>, Matrix<Scalar, Rows, Cols2> >(matrix, cod, Rows, Cols, Cols2);
85 
86  // Verify that we get the same minimum-norm solution as the SVD.
87  Matrix<Scalar, Cols, Cols2> exact_solution;
88  exact_solution.setRandom(Cols, Cols2);
89  Matrix<Scalar, Rows, Cols2> rhs = matrix * exact_solution;
90  Matrix<Scalar, Cols, Cols2> cod_solution = cod.solve(rhs);
92  Matrix<Scalar, Cols, Cols2> svd_solution = svd.solve(rhs);
93  VERIFY_IS_APPROX(cod_solution, svd_solution);
94 
95  typename Inverse<COD>::PlainObject pinv = cod.pseudoInverse();
96  VERIFY_IS_APPROX(cod_solution, pinv * rhs);
97 }
98 
99 template<typename MatrixType> void qr()
100 {
101  using std::sqrt;
102 
103  STATIC_CHECK(( internal::is_same<typename ColPivHouseholderQR<MatrixType>::StorageIndex,int>::value ));
104 
105  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);
106  Index rank = internal::random<Index>(1, (std::min)(rows, cols)-1);
107 
108  typedef typename MatrixType::Scalar Scalar;
109  typedef typename MatrixType::RealScalar RealScalar;
111  MatrixType m1;
112  createRandomPIMatrixOfRank(rank,rows,cols,m1);
114  VERIFY_IS_EQUAL(rank, qr.rank());
116  VERIFY(!qr.isInjective());
117  VERIFY(!qr.isInvertible());
118  VERIFY(!qr.isSurjective());
119 
120  MatrixQType q = qr.householderQ();
122 
123  MatrixType r = qr.matrixQR().template triangularView<Upper>();
124  MatrixType c = q * r * qr.colsPermutation().inverse();
125  VERIFY_IS_APPROX(m1, c);
126 
127  // Verify that the absolute value of the diagonal elements in R are
128  // non-increasing until they reach the singularity threshold.
129  RealScalar threshold =
131  for (Index i = 0; i < (std::min)(rows, cols) - 1; ++i) {
132  RealScalar x = numext::abs(r(i, i));
133  RealScalar y = numext::abs(r(i + 1, i + 1));
134  if (x < threshold && y < threshold) continue;
135  if (!test_isApproxOrLessThan(y, x)) {
136  for (Index j = 0; j < (std::min)(rows, cols); ++j) {
137  std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl;
138  }
139  std::cout << "Failure at i=" << i << ", rank=" << rank
140  << ", threshold=" << threshold << std::endl;
141  }
143  }
144 
145  check_solverbase<MatrixType, MatrixType>(m1, qr, rows, cols, cols2);
146 
147  {
148  MatrixType m2, m3;
149  Index size = rows;
150  do {
151  m1 = MatrixType::Random(size,size);
152  qr.compute(m1);
153  } while(!qr.isInvertible());
154  MatrixType m1_inv = qr.inverse();
155  m3 = m1 * MatrixType::Random(size,cols2);
156  m2 = qr.solve(m3);
157  VERIFY_IS_APPROX(m2, m1_inv*m3);
158  }
159 }
160 
161 template<typename MatrixType, int Cols2> void qr_fixedsize()
162 {
163  using std::sqrt;
164  using std::abs;
165  enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
166  typedef typename MatrixType::Scalar Scalar;
167  typedef typename MatrixType::RealScalar RealScalar;
168  int rank = internal::random<int>(1, (std::min)(int(Rows), int(Cols))-1);
170  createRandomPIMatrixOfRank(rank,Rows,Cols,m1);
172  VERIFY_IS_EQUAL(rank, qr.rank());
174  VERIFY_IS_EQUAL(qr.isInjective(), (rank == Rows));
175  VERIFY_IS_EQUAL(qr.isSurjective(), (rank == Cols));
177 
178  Matrix<Scalar,Rows,Cols> r = qr.matrixQR().template triangularView<Upper>();
180  VERIFY_IS_APPROX(m1, c);
181 
182  check_solverbase<Matrix<Scalar,Cols,Cols2>, Matrix<Scalar,Rows,Cols2> >(m1, qr, Rows, Cols, Cols2);
183 
184  // Verify that the absolute value of the diagonal elements in R are
185  // non-increasing until they reache the singularity threshold.
186  RealScalar threshold =
187  sqrt(RealScalar(Rows)) * (std::abs)(r(0, 0)) * NumTraits<Scalar>::epsilon();
188  for (Index i = 0; i < (std::min)(int(Rows), int(Cols)) - 1; ++i) {
189  RealScalar x = numext::abs(r(i, i));
190  RealScalar y = numext::abs(r(i + 1, i + 1));
191  if (x < threshold && y < threshold) continue;
192  if (!test_isApproxOrLessThan(y, x)) {
193  for (Index j = 0; j < (std::min)(int(Rows), int(Cols)); ++j) {
194  std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl;
195  }
196  std::cout << "Failure at i=" << i << ", rank=" << rank
197  << ", threshold=" << threshold << std::endl;
198  }
200  }
201 }
202 
203 // This test is meant to verify that pivots are chosen such that
204 // even for a graded matrix, the diagonal of R falls of roughly
205 // monotonically until it reaches the threshold for singularity.
206 // We use the so-called Kahan matrix, which is a famous counter-example
207 // for rank-revealing QR. See
208 // http://www.netlib.org/lapack/lawnspdf/lawn176.pdf
209 // page 3 for more detail.
210 template<typename MatrixType> void qr_kahan_matrix()
211 {
212  using std::sqrt;
213  using std::abs;
214  typedef typename MatrixType::Scalar Scalar;
215  typedef typename MatrixType::RealScalar RealScalar;
216 
217  Index rows = 300, cols = rows;
218 
219  MatrixType m1;
220  m1.setZero(rows,cols);
221  RealScalar s = std::pow(NumTraits<RealScalar>::epsilon(), 1.0 / rows);
222  RealScalar c = std::sqrt(1 - s*s);
223  RealScalar pow_s_i(1.0); // pow(s,i)
224  for (Index i = 0; i < rows; ++i) {
225  m1(i, i) = pow_s_i;
226  m1.row(i).tail(rows - i - 1) = -pow_s_i * c * MatrixType::Ones(1, rows - i - 1);
227  pow_s_i *= s;
228  }
229  m1 = (m1 + m1.transpose()).eval();
231  MatrixType r = qr.matrixQR().template triangularView<Upper>();
232 
233  RealScalar threshold =
235  for (Index i = 0; i < (std::min)(rows, cols) - 1; ++i) {
236  RealScalar x = numext::abs(r(i, i));
237  RealScalar y = numext::abs(r(i + 1, i + 1));
238  if (x < threshold && y < threshold) continue;
239  if (!test_isApproxOrLessThan(y, x)) {
240  for (Index j = 0; j < (std::min)(rows, cols); ++j) {
241  std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl;
242  }
243  std::cout << "Failure at i=" << i << ", rank=" << qr.rank()
244  << ", threshold=" << threshold << std::endl;
245  }
247  }
248 }
249 
250 template<typename MatrixType> void qr_invertible()
251 {
252  using std::log;
253  using std::abs;
255  typedef typename MatrixType::Scalar Scalar;
256 
257  int size = internal::random<int>(10,50);
258 
259  MatrixType m1(size, size), m2(size, size), m3(size, size);
260  m1 = MatrixType::Random(size,size);
261 
263  {
264  // let's build a matrix more stable to inverse
265  MatrixType a = MatrixType::Random(size,size*2);
266  m1 += a * a.adjoint();
267  }
268 
270 
271  check_solverbase<MatrixType, MatrixType>(m1, qr, size, size, size);
272 
273  // now construct a matrix with prescribed determinant
274  m1.setZero();
275  for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
276  RealScalar absdet = abs(m1.diagonal().prod());
277  m3 = qr.householderQ(); // get a unitary
278  m1 = m3 * m1 * m3;
279  qr.compute(m1);
280  VERIFY_IS_APPROX(absdet, qr.absDeterminant());
281  VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
282 }
283 
284 template<typename MatrixType> void qr_verify_assert()
285 {
286  MatrixType tmp;
287 
290  VERIFY_RAISES_ASSERT(qr.solve(tmp))
291  VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp))
292  VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp))
301 }
302 
303 template<typename MatrixType> void cod_verify_assert()
304 {
305  MatrixType tmp;
306 
309  VERIFY_RAISES_ASSERT(cod.solve(tmp))
310  VERIFY_RAISES_ASSERT(cod.transpose().solve(tmp))
311  VERIFY_RAISES_ASSERT(cod.adjoint().solve(tmp))
320 }
321 
322 EIGEN_DECLARE_TEST(qr_colpivoting)
323 {
324  for(int i = 0; i < g_repeat; i++) {
325  CALL_SUBTEST_1( qr<MatrixXf>() );
326  CALL_SUBTEST_2( qr<MatrixXd>() );
327  CALL_SUBTEST_3( qr<MatrixXcd>() );
331  }
332 
333  for(int i = 0; i < g_repeat; i++) {
334  CALL_SUBTEST_1( cod<MatrixXf>() );
335  CALL_SUBTEST_2( cod<MatrixXd>() );
336  CALL_SUBTEST_3( cod<MatrixXcd>() );
340  }
341 
342  for(int i = 0; i < g_repeat; i++) {
343  CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
344  CALL_SUBTEST_2( qr_invertible<MatrixXd>() );
345  CALL_SUBTEST_6( qr_invertible<MatrixXcf>() );
346  CALL_SUBTEST_3( qr_invertible<MatrixXcd>() );
347  }
348 
349  CALL_SUBTEST_7(qr_verify_assert<Matrix3f>());
350  CALL_SUBTEST_8(qr_verify_assert<Matrix3d>());
351  CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
352  CALL_SUBTEST_2(qr_verify_assert<MatrixXd>());
353  CALL_SUBTEST_6(qr_verify_assert<MatrixXcf>());
354  CALL_SUBTEST_3(qr_verify_assert<MatrixXcd>());
355 
356  CALL_SUBTEST_7(cod_verify_assert<Matrix3f>());
357  CALL_SUBTEST_8(cod_verify_assert<Matrix3d>());
358  CALL_SUBTEST_1(cod_verify_assert<MatrixXf>());
359  CALL_SUBTEST_2(cod_verify_assert<MatrixXd>());
360  CALL_SUBTEST_6(cod_verify_assert<MatrixXcf>());
361  CALL_SUBTEST_3(cod_verify_assert<MatrixXcd>());
362 
363  // Test problem size constructors
365 
366  CALL_SUBTEST_1( qr_kahan_matrix<MatrixXf>() );
367  CALL_SUBTEST_2( qr_kahan_matrix<MatrixXd>() );
368 }
const Solve< SVDBase< JacobiSVD< _MatrixType, QRPreconditioner > >, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SolverBase.h:106
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
cout<< "Here is the matrix m:"<< endl<< m<< endl;JacobiSVD< MatrixXf > svd(m, ComputeThinU|ComputeThinV)
#define CALL_SUBTEST_9(FUNC)
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
Scalar * y
InverseReturnType inverse() const
#define min(a, b)
Definition: datatypes.h:19
#define CALL_SUBTEST_3(FUNC)
MatrixType m2(n_dims)
#define CALL_SUBTEST_7(FUNC)
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
#define STATIC_CHECK(COND)
Definition: main.h:397
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
void cod_fixedsize()
Complete orthogonal decomposition (COD) of a matrix.
EIGEN_DEVICE_FUNC const LogReturnType log() const
void cod()
static double epsilon
Definition: testRot3.cpp:37
void qr_invertible()
bool test_isApproxOrLessThan(const Real &a, const Real &b)
Expression of the inverse of another expression.
Definition: Inverse.h:43
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
HouseholderSequenceType householderQ() const
#define VERIFY_IS_APPROX(a, b)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
#define CALL_SUBTEST_1(FUNC)
void qr()
Matrix3d m1
Definition: IOFormat.cpp:2
ConstTransposeReturnType transpose() const
Definition: SolverBase.h:121
Householder rank-revealing QR decomposition of a matrix with column-pivoting.
static int g_repeat
Definition: main.h:169
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
#define CALL_SUBTEST_8(FUNC)
void cod_verify_assert()
RealScalar s
EIGEN_DEVICE_FUNC const Scalar & q
HouseholderSequenceType householderQ(void) const
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
void qr_kahan_matrix()
static const int Cols
MatrixType::RealScalar absDeterminant() const
#define CALL_SUBTEST_5(FUNC)
#define VERIFY(a)
Definition: main.h:380
#define EIGEN_TEST_MAX_SIZE
MatrixType::RealScalar logAbsDeterminant() const
void qr_verify_assert()
#define VERIFY_IS_UNITARY(a)
Definition: main.h:395
Two-sided Jacobi SVD decomposition of a rectangular matrix.
void qr_fixedsize()
#define VERIFY_IS_APPROX_OR_LESS_THAN(a, b)
Definition: main.h:392
void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType &m)
Definition: main.h:653
#define CALL_SUBTEST_2(FUNC)
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
const Inverse< CompleteOrthogonalDecomposition > pseudoInverse() const
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
The matrix class, also used for vectors and row-vectors.
AdjointReturnType adjoint() const
Definition: SolverBase.h:140
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
const MatrixType & matrixQR() const
#define abs(x)
Definition: datatypes.h:17
const PermutationType & colsPermutation() const
Derived & setRandom(Index size)
Definition: Random.h:151
const Inverse< ColPivHouseholderQR > inverse() const
EIGEN_DECLARE_TEST(qr_colpivoting)
std::ptrdiff_t j
Point2 t(10, 10)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:27