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 
33  Matrix<Scalar, EIGEN_SIZE_MAX(MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime), 1> _tmp((std::max)(rows,cols));
34  Scalar* tmp = &_tmp.coeffRef(0,0);
35 
36  Scalar beta;
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  // reconstruct householder matrix:
52  SquareMatrixType id, H1, H2;
53  id.setIdentity(rows, rows);
54  H1 = H2 = id;
56  vv << Scalar(1), essential;
57  H1.applyHouseholderOnTheLeft(essential, beta, tmp);
58  H2.applyHouseholderOnTheRight(essential, beta, tmp);
59  VERIFY_IS_APPROX(H1, H2);
60  VERIFY_IS_APPROX(H1, id - beta * vv*vv.adjoint());
61 
63  m2(rows, cols);
64 
65  v1 = VectorType::Random(rows);
66  if(even) v1.tail(rows-1).setZero();
67  m1.colwise() = v1;
68  m2 = m1;
69  m1.col(0).makeHouseholder(essential, beta, alpha);
70  m1.applyHouseholderOnTheLeft(essential,beta,tmp);
71  VERIFY_IS_APPROX(m1.norm(), m2.norm());
72  if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m1.block(1,0,rows-1,cols).norm(), m1.norm());
75 
76  v1 = VectorType::Random(rows);
77  if(even) v1.tail(rows-1).setZero();
78  SquareMatrixType m3(rows,rows), m4(rows,rows);
79  m3.rowwise() = v1.transpose();
80  m4 = m3;
81  m3.row(0).makeHouseholder(essential, beta, alpha);
82  m3.applyHouseholderOnTheRight(essential.conjugate(),beta,tmp);
83  VERIFY_IS_APPROX(m3.norm(), m4.norm());
84  if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m3.block(0,1,rows,rows-1).norm(), m3.norm());
87 
88  // test householder sequence on the left with a shift
89 
90  Index shift = internal::random<Index>(0, std::max<Index>(rows-2,0));
91  Index brows = rows - shift;
92  m1.setRandom(rows, cols);
93  HBlockMatrixType hbm = m1.block(shift,0,brows,cols);
95  m2 = m1;
96  m2.block(shift,0,brows,cols) = qr.matrixQR();
97  HCoeffsVectorType hc = qr.hCoeffs().conjugate();
99  hseq.setLength(hc.size()).setShift(shift);
100  VERIFY(hseq.length() == hc.size());
101  VERIFY(hseq.shift() == shift);
102 
103  MatrixType m5 = m2;
104  m5.block(shift,0,brows,cols).template triangularView<StrictlyLower>().setZero();
105  VERIFY_IS_APPROX(hseq * m5, m1); // test applying hseq directly
106  m3 = hseq;
107  VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating hseq to a dense matrix, then applying
108 
109  SquareMatrixType hseq_mat = hseq;
110  SquareMatrixType hseq_mat_conj = hseq.conjugate();
111  SquareMatrixType hseq_mat_adj = hseq.adjoint();
112  SquareMatrixType hseq_mat_trans = hseq.transpose();
113  SquareMatrixType m6 = SquareMatrixType::Random(rows, rows);
114  VERIFY_IS_APPROX(hseq_mat.adjoint(), hseq_mat_adj);
115  VERIFY_IS_APPROX(hseq_mat.conjugate(), hseq_mat_conj);
116  VERIFY_IS_APPROX(hseq_mat.transpose(), hseq_mat_trans);
117  VERIFY_IS_APPROX(hseq * m6, hseq_mat * m6);
118  VERIFY_IS_APPROX(hseq.adjoint() * m6, hseq_mat_adj * m6);
119  VERIFY_IS_APPROX(hseq.conjugate() * m6, hseq_mat_conj * m6);
120  VERIFY_IS_APPROX(hseq.transpose() * m6, hseq_mat_trans * m6);
121  VERIFY_IS_APPROX(m6 * hseq, m6 * hseq_mat);
122  VERIFY_IS_APPROX(m6 * hseq.adjoint(), m6 * hseq_mat_adj);
123  VERIFY_IS_APPROX(m6 * hseq.conjugate(), m6 * hseq_mat_conj);
124  VERIFY_IS_APPROX(m6 * hseq.transpose(), m6 * hseq_mat_trans);
125 
126  // test householder sequence on the right with a shift
127 
128  TMatrixType tm2 = m2.transpose();
130  rhseq.setLength(hc.size()).setShift(shift);
131  VERIFY_IS_APPROX(rhseq * m5, m1); // test applying rhseq directly
132  m3 = rhseq;
133  VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating rhseq to a dense matrix, then applying
134 }
135 
137 {
138  for(int i = 0; i < g_repeat; i++) {
143  CALL_SUBTEST_5( householder(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
144  CALL_SUBTEST_6( householder(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
145  CALL_SUBTEST_7( householder(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
147  }
148 }
VERIFY_IS_MUCH_SMALLER_THAN
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
alpha
RealScalar alpha
Definition: level1_cplx_impl.h:147
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Eigen::HouseholderSequence::conjugate
ConjugateReturnType conjugate() const
Complex conjugate of the Householder sequence.
Definition: HouseholderSequence.h:245
Eigen::HouseholderSequence::setLength
EIGEN_DEVICE_FUNC HouseholderSequence & setLength(Index length)
Sets the length of the Householder sequence.
Definition: HouseholderSequence.h:443
m1
Matrix3d m1
Definition: IOFormat.cpp:2
real
float real
Definition: datatypes.h:10
vv
static const VectorValues vv
Definition: testHybridGaussianConditional.cpp:44
EIGEN_SIZE_MAX
#define EIGEN_SIZE_MAX(a, b)
Definition: Macros.h:1310
Eigen::HouseholderSequence::setShift
EIGEN_DEVICE_FUNC HouseholderSequence & setShift(Index shift)
Sets the shift of the Householder sequence.
Definition: HouseholderSequence.h:461
beta
double beta(double a, double b)
Definition: beta.c:61
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
hc
Vector3d hc
Definition: Tridiagonalization_householderCoefficients.cpp:5
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
id
static const Similarity3 id
Definition: testSimilarity3.cpp:44
householder
void householder(const MatrixType &m)
Definition: householder.cpp:13
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(householder)
Definition: householder.cpp:136
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
imag
const EIGEN_DEVICE_FUNC ImagReturnType imag() const
Definition: CommonCwiseUnaryOps.h:109
Eigen::Matrix::coeffRef
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:175
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
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
Eigen::HouseholderSequence::adjoint
AdjointReturnType adjoint() const
Adjoint (conjugate transpose) of the Householder sequence.
Definition: HouseholderSequence.h:266
Eigen::HouseholderSequence::transpose
TransposeReturnType transpose() const
Transpose of the Householder sequence.
Definition: HouseholderSequence.h:236
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
m3
static const DiscreteKey m3(M(3), 2)
qr
HouseholderQR< MatrixXf > qr(A)
main.h
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
v2
Vector v2
Definition: testSerializationBase.cpp:39
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
VectorType
Definition: FFTW.cpp:65
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
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
v1
Vector v1
Definition: testSerializationBase.cpp:38
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::HouseholderQR
Householder QR decomposition of a matrix.
Definition: ForwardDeclarations.h:273
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
Eigen::HouseholderSequence
Sequence of Householder reflections acting on subspaces with decreasing size.
Definition: ForwardDeclarations.h:282


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:25