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 #include "solverbase.h"
13 
14 template<typename MatrixType> void qr(const MatrixType& m)
15 {
16  Index rows = m.rows();
17  Index cols = m.cols();
18 
19  typedef typename MatrixType::Scalar Scalar;
21 
22  MatrixType a = MatrixType::Random(rows,cols);
24 
25  MatrixQType q = qrOfA.householderQ();
27 
28  MatrixType r = qrOfA.matrixQR().template triangularView<Upper>();
29  VERIFY_IS_APPROX(a, qrOfA.householderQ() * r);
30 }
31 
32 template<typename MatrixType, int Cols2> void qr_fixedsize()
33 {
34  enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
35  typedef typename MatrixType::Scalar Scalar;
38 
40  // FIXME need better way to construct trapezoid
41  for(int i = 0; i < Rows; i++) for(int j = 0; j < Cols; j++) if(i>j) r(i,j) = Scalar(0);
42 
43  VERIFY_IS_APPROX(m1, qr.householderQ() * r);
44 
45  check_solverbase<Matrix<Scalar,Cols,Cols2>, Matrix<Scalar,Rows,Cols2> >(m1, qr, Rows, Cols, Cols2);
46 }
47 
48 template<typename MatrixType> void qr_invertible()
49 {
50  using std::log;
51  using std::abs;
52  using std::pow;
53  using std::max;
55  typedef typename MatrixType::Scalar Scalar;
56 
57  STATIC_CHECK(( internal::is_same<typename HouseholderQR<MatrixType>::StorageIndex,int>::value ));
58 
59  int size = internal::random<int>(10,50);
60 
61  MatrixType m1(size, size), m2(size, size), m3(size, size);
62  m1 = MatrixType::Random(size,size);
63 
65  {
66  // let's build a matrix more stable to inverse
67  MatrixType a = MatrixType::Random(size,size*4);
68  m1 += a * a.adjoint();
69  }
70 
72 
73  check_solverbase<MatrixType, MatrixType>(m1, qr, size, size, size);
74 
75  // now construct a matrix with prescribed determinant
76  m1.setZero();
77  for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
78  RealScalar absdet = abs(m1.diagonal().prod());
79  m3 = qr.householderQ(); // get a unitary
80  m1 = m3 * m1 * m3;
81  qr.compute(m1);
82  VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
83  // This test is tricky if the determinant becomes too small.
84  // Since we generate random numbers with magnitude range [0,1], the average determinant is 0.5^size
85  VERIFY_IS_MUCH_SMALLER_THAN( abs(absdet-qr.absDeterminant()), numext::maxi(RealScalar(pow(0.5,size)),numext::maxi<RealScalar>(abs(absdet),abs(qr.absDeterminant()))) );
86 
87 }
88 
89 template<typename MatrixType> void qr_verify_assert()
90 {
91  MatrixType tmp;
92 
96  VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp))
97  VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp))
101 }
102 
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))) );
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
130 }
Matrix3f m
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SolverBase.h:106
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define CALL_SUBTEST_12(FUNC)
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
#define CALL_SUBTEST_9(FUNC)
#define max(a, b)
Definition: datatypes.h:20
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
#define CALL_SUBTEST_3(FUNC)
MatrixType m2(n_dims)
const MatrixType & matrixQR() const
#define CALL_SUBTEST_7(FUNC)
#define STATIC_CHECK(COND)
Definition: main.h:397
#define CALL_SUBTEST_11(FUNC)
MatrixType::RealScalar absDeterminant() const
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_pow_op< typename Derived::Scalar, typename ExponentDerived::Scalar >, const Derived, const ExponentDerived > pow(const Eigen::ArrayBase< Derived > &x, const Eigen::ArrayBase< ExponentDerived > &exponents)
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
EIGEN_DEVICE_FUNC const LogReturnType log() const
#define CALL_SUBTEST_10(FUNC)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
#define VERIFY_IS_APPROX(a, b)
#define CALL_SUBTEST_1(FUNC)
Matrix3d m1
Definition: IOFormat.cpp:2
ConstTransposeReturnType transpose() const
Definition: SolverBase.h:121
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
MatrixType::RealScalar logAbsDeterminant() const
#define CALL_SUBTEST_8(FUNC)
EIGEN_DEVICE_FUNC const Scalar & q
HouseholderSequenceType householderQ() const
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
static const int Cols
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
void qr_invertible()
Definition: qr.cpp:48
#define CALL_SUBTEST_5(FUNC)
#define EIGEN_TEST_MAX_SIZE
#define VERIFY_IS_UNITARY(a)
Definition: main.h:395
#define CALL_SUBTEST_2(FUNC)
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.
AdjointReturnType adjoint() const
Definition: SolverBase.h:140
#define abs(x)
Definition: datatypes.h:17
EIGEN_DECLARE_TEST(qr)
Definition: qr.cpp:103
std::ptrdiff_t j
void qr(const MatrixType &m)
Definition: qr.cpp:14
void qr_verify_assert()
Definition: qr.cpp:89
void qr_fixedsize()
Definition: qr.cpp:32


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