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;
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();
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);
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;
114  VERIFY_IS_EQUAL(rank, qr.rank());
115  VERIFY_IS_EQUAL(cols - qr.rank(), qr.dimensionOfKernel());
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();
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);
172  VERIFY_IS_EQUAL(rank, qr.rank());
173  VERIFY_IS_EQUAL(Cols - qr.rank(), qr.dimensionOfKernel());
174  VERIFY_IS_EQUAL(qr.isInjective(), (rank == Rows));
175  VERIFY_IS_EQUAL(qr.isSurjective(), (rank == Cols));
176  VERIFY_IS_EQUAL(qr.isInvertible(), (qr.isInjective() && qr.isSurjective()));
177 
178  Matrix<Scalar,Rows,Cols> r = qr.matrixQR().template triangularView<Upper>();
179  Matrix<Scalar,Rows,Cols> c = qr.householderQ() * r * qr.colsPermutation().inverse();
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);
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 
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 
289  VERIFY_RAISES_ASSERT(qr.matrixQR())
290  VERIFY_RAISES_ASSERT(qr.solve(tmp))
291  VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp))
292  VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp))
293  VERIFY_RAISES_ASSERT(qr.householderQ())
294  VERIFY_RAISES_ASSERT(qr.dimensionOfKernel())
295  VERIFY_RAISES_ASSERT(qr.isInjective())
296  VERIFY_RAISES_ASSERT(qr.isSurjective())
297  VERIFY_RAISES_ASSERT(qr.isInvertible())
298  VERIFY_RAISES_ASSERT(qr.inverse())
299  VERIFY_RAISES_ASSERT(qr.absDeterminant())
300  VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
301 }
302 
303 template<typename MatrixType> void cod_verify_assert()
304 {
305  MatrixType tmp;
306 
308  VERIFY_RAISES_ASSERT(cod.matrixQTZ())
309  VERIFY_RAISES_ASSERT(cod.solve(tmp))
310  VERIFY_RAISES_ASSERT(cod.transpose().solve(tmp))
311  VERIFY_RAISES_ASSERT(cod.adjoint().solve(tmp))
312  VERIFY_RAISES_ASSERT(cod.householderQ())
313  VERIFY_RAISES_ASSERT(cod.dimensionOfKernel())
314  VERIFY_RAISES_ASSERT(cod.isInjective())
315  VERIFY_RAISES_ASSERT(cod.isSurjective())
316  VERIFY_RAISES_ASSERT(cod.isInvertible())
317  VERIFY_RAISES_ASSERT(cod.pseudoInverse())
318  VERIFY_RAISES_ASSERT(cod.absDeterminant())
319  VERIFY_RAISES_ASSERT(cod.logAbsDeterminant())
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 }
Eigen::Inverse
Expression of the inverse of another expression.
Definition: Inverse.h:43
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::PlainObjectBase::setRandom
Derived & setRandom(Index size)
Definition: Random.h:151
qr_invertible
void qr_invertible()
Definition: qr_colpivoting.cpp:250
Eigen::ComputeFullV
@ ComputeFullV
Definition: Constants.h:397
s
RealScalar s
Definition: level1_cplx_impl.h:126
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
qr_kahan_matrix
void qr_kahan_matrix()
Definition: qr_colpivoting.cpp:210
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(qr_colpivoting)
Definition: qr_colpivoting.cpp:322
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
x
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
Definition: gnuplot_common_settings.hh:12
qr
void qr()
Definition: qr_colpivoting.cpp:99
m1
Matrix3d m1
Definition: IOFormat.cpp:2
Eigen::ComputeFullU
@ ComputeFullU
Definition: Constants.h:393
log
const EIGEN_DEVICE_FUNC LogReturnType log() const
Definition: ArrayCwiseUnaryOps.h:128
CALL_SUBTEST_9
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
svd
cout<< "Here is the matrix m:"<< endl<< m<< endl;JacobiSVD< MatrixXf > svd(m, ComputeThinU|ComputeThinV)
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
cod
void cod()
Definition: qr_colpivoting.cpp:17
cod_verify_assert
void cod_verify_assert()
Definition: qr_colpivoting.cpp:303
boost::multiprecision::test_isApproxOrLessThan
bool test_isApproxOrLessThan(const Real &a, const Real &b)
Definition: boostmultiprec.cpp:131
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
epsilon
static double epsilon
Definition: testRot3.cpp:37
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
Eigen::numext::q
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:1984
Eigen::createRandomPIMatrixOfRank
void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType &m)
Definition: main.h:653
Eigen::ComputeThinU
@ ComputeThinU
Definition: Constants.h:395
qr_verify_assert
void qr_verify_assert()
Definition: qr_colpivoting.cpp:284
qr_fixedsize
void qr_fixedsize()
Definition: qr_colpivoting.cpp:161
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::CompleteOrthogonalDecomposition
Complete orthogonal decomposition (COD) of a matrix.
Definition: ForwardDeclarations.h:276
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
cod_fixedsize
void cod_fixedsize()
Definition: qr_colpivoting.cpp:67
ceres::pow
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
Eigen::ColPivHouseholderQR
Householder rank-revealing QR decomposition of a matrix with column-pivoting.
Definition: ForwardDeclarations.h:274
CALL_SUBTEST_6
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
VERIFY_IS_UNITARY
#define VERIFY_IS_UNITARY(a)
Definition: main.h:395
y
Scalar * y
Definition: level1_cplx_impl.h:124
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
Eigen::JacobiSVD
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: ForwardDeclarations.h:278
m3
static const DiscreteKey m3(M(3), 2)
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
solverbase.h
Eigen::ComputeThinV
@ ComputeThinV
Definition: Constants.h:399
main.h
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
min
#define min(a, b)
Definition: datatypes.h:19
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
abs
#define abs(x)
Definition: datatypes.h:17
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
align_3::t
Point2 t(10, 10)
CALL_SUBTEST_8
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
test_callbacks.value
value
Definition: test_callbacks.py:160
eval
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:38
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
VERIFY_IS_APPROX_OR_LESS_THAN
#define VERIFY_IS_APPROX_OR_LESS_THAN(a, b)
Definition: main.h:392
STATIC_CHECK
#define STATIC_CHECK(COND)
Definition: main.h:397
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Cols
static const int Cols
Definition: testCallRecord.cpp:32


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:54