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 
39  Matrix<Scalar,Rows,Cols> r = qr.matrixQR();
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 
94  VERIFY_RAISES_ASSERT(qr.matrixQR())
95  VERIFY_RAISES_ASSERT(qr.solve(tmp))
96  VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp))
97  VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp))
98  VERIFY_RAISES_ASSERT(qr.householderQ())
99  VERIFY_RAISES_ASSERT(qr.absDeterminant())
100  VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
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 }
VERIFY_IS_MUCH_SMALLER_THAN
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
qr
void qr(const MatrixType &m)
Definition: qr.cpp:14
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
m1
Matrix3d m1
Definition: IOFormat.cpp:2
log
const EIGEN_DEVICE_FUNC LogReturnType log() const
Definition: ArrayCwiseUnaryOps.h:128
CALL_SUBTEST_11
#define CALL_SUBTEST_11(FUNC)
Definition: split_test_helper.h:64
CALL_SUBTEST_9
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
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
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
Eigen::HouseholderQR::householderQ
HouseholderSequenceType householderQ() const
Definition: HouseholderQR.h:163
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
CALL_SUBTEST_10
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58
Eigen::HouseholderQR::matrixQR
const MatrixType & matrixQR() const
Definition: HouseholderQR.h:172
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
qr_fixedsize
void qr_fixedsize()
Definition: qr.cpp:32
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
ceres::pow
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
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
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::ArrayBase::pow
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)
Definition: GlobalFunctions.h:143
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
solverbase.h
qr_verify_assert
void qr_verify_assert()
Definition: qr.cpp:89
main.h
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
CALL_SUBTEST_12
#define CALL_SUBTEST_12(FUNC)
Definition: split_test_helper.h:70
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
qr_invertible
void qr_invertible()
Definition: qr.cpp:48
abs
#define abs(x)
Definition: datatypes.h:17
Eigen::numext::maxi
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: Eigen/src/Core/MathFunctions.h:1093
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
CALL_SUBTEST_8
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
max
#define max(a, b)
Definition: datatypes.h:20
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:158
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(qr)
Definition: qr.cpp:103
STATIC_CHECK
#define STATIC_CHECK(COND)
Definition: main.h:397
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::HouseholderQR< MatrixType >
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 Tue Jun 25 2024 03:02:25