qr.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 //
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 
13 template<typename MatrixType> void qr(const MatrixType& m)
14 {
15  Index rows = m.rows();
16  Index cols = m.cols();
17 
18  typedef typename MatrixType::Scalar Scalar;
20 
21  MatrixType a = MatrixType::Random(rows,cols);
23 
24  MatrixQType q = qrOfA.householderQ();
26 
27  MatrixType r = qrOfA.matrixQR().template triangularView<Upper>();
28  VERIFY_IS_APPROX(a, qrOfA.householderQ() * r);
29 }
30 
31 template<typename MatrixType, int Cols2> void qr_fixedsize()
32 {
33  enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
34  typedef typename MatrixType::Scalar Scalar;
37 
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  VERIFY_IS_APPROX(m1, qr.householderQ() * r);
43 
47  m2 = qr.solve(m3);
48  VERIFY_IS_APPROX(m3, m1*m2);
49 }
50 
51 template<typename MatrixType> void qr_invertible()
52 {
53  using std::log;
54  using std::abs;
55  using std::pow;
56  using std::max;
58  typedef typename MatrixType::Scalar Scalar;
59 
60  int size = internal::random<int>(10,50);
61 
62  MatrixType m1(size, size), m2(size, size), m3(size, size);
63  m1 = MatrixType::Random(size,size);
64 
66  {
67  // let's build a matrix more stable to inverse
68  MatrixType a = MatrixType::Random(size,size*4);
69  m1 += a * a.adjoint();
70  }
71 
73  m3 = MatrixType::Random(size,size);
74  m2 = qr.solve(m3);
75  VERIFY_IS_APPROX(m3, m1*m2);
76 
77  // now construct a matrix with prescribed determinant
78  m1.setZero();
79  for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
80  RealScalar absdet = abs(m1.diagonal().prod());
81  m3 = qr.householderQ(); // get a unitary
82  m1 = m3 * m1 * m3;
83  qr.compute(m1);
84  VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
85  // This test is tricky if the determinant becomes too small.
86  // Since we generate random numbers with magnitude rrange [0,1], the average determinant is 0.5^size
87  VERIFY_IS_MUCH_SMALLER_THAN( abs(absdet-qr.absDeterminant()), numext::maxi(RealScalar(pow(0.5,size)),numext::maxi<RealScalar>(abs(absdet),abs(qr.absDeterminant()))) );
88 
89 }
90 
91 template<typename MatrixType> void qr_verify_assert()
92 {
93  MatrixType tmp;
94 
101 }
102 
103 void 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 }
HouseholderQR & compute(const EigenBase< InputType > &matrix)
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:285
HouseholderSequenceType householderQ() const
#define max(a, b)
Definition: datatypes.h:20
MatrixType m2(n_dims)
void test_qr()
Definition: qr.cpp:103
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)
Array33i a
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
internal::enable_if< !(internal::is_same< typename Derived::Scalar, ScalarExponent >::value)&&EIGEN_SCALAR_BINARY_SUPPORTED(pow, typename Derived::Scalar, ScalarExponent), const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Derived, ScalarExponent, pow) >::type pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
#define VERIFY_IS_APPROX(a, b)
Matrix3d m1
Definition: IOFormat.cpp:2
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
static const int Cols
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:335
void qr_invertible()
Definition: qr.cpp:51
#define EIGEN_TEST_MAX_SIZE
#define VERIFY_IS_UNITARY(a)
Definition: main.h:340
const Solve< HouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
MatrixType::RealScalar logAbsDeterminant() const
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
The matrix class, also used for vectors and row-vectors.
#define abs(x)
Definition: datatypes.h:17
MatrixType::RealScalar absDeterminant() const
std::ptrdiff_t j
void qr(const MatrixType &m)
Definition: qr.cpp:13
void qr_verify_assert()
Definition: qr.cpp:91
void qr_fixedsize()
Definition: qr.cpp:31
const MatrixType & matrixQR() const


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