householder.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) 2009-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
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 householder(const MatrixType& m)
14 {
15  static bool even = true;
16  even = !even;
17  /* this test covers the following files:
18  Householder.h
19  */
20  Index rows = m.rows();
21  Index cols = m.cols();
22 
23  typedef typename MatrixType::Scalar Scalar;
24  typedef typename NumTraits<Scalar>::Real RealScalar;
29  typedef Matrix<Scalar, Dynamic, 1> HCoeffsVectorType;
30 
32 
34  Scalar* tmp = &_tmp.coeffRef(0,0);
35 
36  Scalar beta;
37  RealScalar alpha;
38  EssentialVectorType essential;
39 
40  VectorType v1 = VectorType::Random(rows), v2;
41  v2 = v1;
42  v1.makeHouseholder(essential, beta, alpha);
43  v1.applyHouseholderOnTheLeft(essential,beta,tmp);
44  VERIFY_IS_APPROX(v1.norm(), v2.norm());
45  if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(v1.tail(rows-1).norm(), v1.norm());
46  v1 = VectorType::Random(rows);
47  v2 = v1;
48  v1.applyHouseholderOnTheLeft(essential,beta,tmp);
49  VERIFY_IS_APPROX(v1.norm(), v2.norm());
50 
51  MatrixType m1(rows, cols),
52  m2(rows, cols);
53 
54  v1 = VectorType::Random(rows);
55  if(even) v1.tail(rows-1).setZero();
56  m1.colwise() = v1;
57  m2 = m1;
58  m1.col(0).makeHouseholder(essential, beta, alpha);
59  m1.applyHouseholderOnTheLeft(essential,beta,tmp);
60  VERIFY_IS_APPROX(m1.norm(), m2.norm());
61  if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m1.block(1,0,rows-1,cols).norm(), m1.norm());
63  VERIFY_IS_APPROX(numext::real(m1(0,0)), alpha);
64 
65  v1 = VectorType::Random(rows);
66  if(even) v1.tail(rows-1).setZero();
67  SquareMatrixType m3(rows,rows), m4(rows,rows);
68  m3.rowwise() = v1.transpose();
69  m4 = m3;
70  m3.row(0).makeHouseholder(essential, beta, alpha);
71  m3.applyHouseholderOnTheRight(essential,beta,tmp);
72  VERIFY_IS_APPROX(m3.norm(), m4.norm());
73  if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m3.block(0,1,rows,rows-1).norm(), m3.norm());
75  VERIFY_IS_APPROX(numext::real(m3(0,0)), alpha);
76 
77  // test householder sequence on the left with a shift
78 
79  Index shift = internal::random<Index>(0, std::max<Index>(rows-2,0));
80  Index brows = rows - shift;
81  m1.setRandom(rows, cols);
82  HBlockMatrixType hbm = m1.block(shift,0,brows,cols);
84  m2 = m1;
85  m2.block(shift,0,brows,cols) = qr.matrixQR();
86  HCoeffsVectorType hc = qr.hCoeffs().conjugate();
88  hseq.setLength(hc.size()).setShift(shift);
89  VERIFY(hseq.length() == hc.size());
90  VERIFY(hseq.shift() == shift);
91 
92  MatrixType m5 = m2;
93  m5.block(shift,0,brows,cols).template triangularView<StrictlyLower>().setZero();
94  VERIFY_IS_APPROX(hseq * m5, m1); // test applying hseq directly
95  m3 = hseq;
96  VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating hseq to a dense matrix, then applying
97 
98  SquareMatrixType hseq_mat = hseq;
99  SquareMatrixType hseq_mat_conj = hseq.conjugate();
100  SquareMatrixType hseq_mat_adj = hseq.adjoint();
101  SquareMatrixType hseq_mat_trans = hseq.transpose();
102  SquareMatrixType m6 = SquareMatrixType::Random(rows, rows);
103  VERIFY_IS_APPROX(hseq_mat.adjoint(), hseq_mat_adj);
104  VERIFY_IS_APPROX(hseq_mat.conjugate(), hseq_mat_conj);
105  VERIFY_IS_APPROX(hseq_mat.transpose(), hseq_mat_trans);
106  VERIFY_IS_APPROX(hseq_mat * m6, hseq_mat * m6);
107  VERIFY_IS_APPROX(hseq_mat.adjoint() * m6, hseq_mat_adj * m6);
108  VERIFY_IS_APPROX(hseq_mat.conjugate() * m6, hseq_mat_conj * m6);
109  VERIFY_IS_APPROX(hseq_mat.transpose() * m6, hseq_mat_trans * m6);
110  VERIFY_IS_APPROX(m6 * hseq_mat, m6 * hseq_mat);
111  VERIFY_IS_APPROX(m6 * hseq_mat.adjoint(), m6 * hseq_mat_adj);
112  VERIFY_IS_APPROX(m6 * hseq_mat.conjugate(), m6 * hseq_mat_conj);
113  VERIFY_IS_APPROX(m6 * hseq_mat.transpose(), m6 * hseq_mat_trans);
114 
115  // test householder sequence on the right with a shift
116 
117  TMatrixType tm2 = m2.transpose();
119  rhseq.setLength(hc.size()).setShift(shift);
120  VERIFY_IS_APPROX(rhseq * m5, m1); // test applying rhseq directly
121  m3 = rhseq;
122  VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating rhseq to a dense matrix, then applying
123 }
124 
126 {
127  for(int i = 0; i < g_repeat; i++) {
128  CALL_SUBTEST_1( householder(Matrix<double,2,2>()) );
129  CALL_SUBTEST_2( householder(Matrix<float,2,3>()) );
130  CALL_SUBTEST_3( householder(Matrix<double,3,5>()) );
131  CALL_SUBTEST_4( householder(Matrix<float,4,4>()) );
132  CALL_SUBTEST_5( householder(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
133  CALL_SUBTEST_6( householder(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
134  CALL_SUBTEST_7( householder(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
135  CALL_SUBTEST_8( householder(Matrix<double,1,1>()) );
136  }
137 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
#define max(a, b)
Definition: datatypes.h:20
float real
Definition: datatypes.h:10
Index length() const
Returns the length of the Householder sequence.
Vector v2
Vector v1
HouseholderSequence & setLength(Index length)
Sets the length of the Householder sequence.
MatrixType m2(n_dims)
void test_householder()
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
const HCoeffsType & hCoeffs() const
HouseholderSequence transpose() const
Transpose of the Householder sequence.
HouseholderQR< MatrixXf > qr(A)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
Sequence of Householder reflections acting on subspaces with decreasing size.
#define VERIFY_IS_APPROX(a, b)
void householder(const MatrixType &m)
Definition: householder.cpp:13
Matrix3d m1
Definition: IOFormat.cpp:2
ConjugateReturnType conjugate() const
Complex conjugate of the Householder sequence.
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
RealScalar alpha
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:34
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:335
ConjugateReturnType adjoint() const
Adjoint (conjugate transpose) of the Householder sequence.
#define VERIFY(a)
Definition: main.h:325
#define EIGEN_TEST_MAX_SIZE
Householder QR decomposition of a matrix.
EIGEN_DEVICE_FUNC const ImagReturnType imag() const
Index shift() const
Returns the shift of the Householder sequence.
The matrix class, also used for vectors and row-vectors.
const MatrixType & matrixQR() const
v setZero(3)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:11