Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
HessenbergDecomposition.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_HESSENBERGDECOMPOSITION_H
13#define EIGEN_HESSENBERGDECOMPOSITION_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22template <typename MatrixType>
24template <typename MatrixType>
25struct traits<HessenbergDecompositionMatrixHReturnType<MatrixType>> {
26 using ReturnType = typename MatrixType::PlainObject;
27};
28
29template <typename MatrixType, typename CoeffVectorType, typename WorkspaceType>
30void hessenberg_decomposition_inplace(MatrixType& matA, CoeffVectorType& hCoeffs, WorkspaceType& temp);
31
32} // namespace internal
33
64template <typename MatrixType_>
66 public:
68 using MatrixType = MatrixType_;
69
70 enum {
71 Size = MatrixType::RowsAtCompileTime,
72 SizeMinusOne = Size == Dynamic ? Dynamic : Size - 1,
73 Options = internal::plain_object_options<MatrixType>::value,
74 MaxSize = MatrixType::MaxRowsAtCompileTime,
75 MaxSizeMinusOne = MaxSize == Dynamic ? Dynamic : MaxSize - 1
76 };
77
79 using Scalar = typename MatrixType::Scalar;
80 using Index = Eigen::Index;
81
89
93
95
107 explicit HessenbergDecomposition(Index size = Size == Dynamic ? 2 : Size)
108 : m_matrix(size, size), m_temp(size), m_isInitialized(false) {
109 if (size > 1) m_hCoeffs.resize(size - 1);
110 }
111
121 template <typename InputType>
123 : m_matrix(matrix.derived()), m_temp(matrix.rows()), m_isInitialized(false) {
124 computeInPlace();
125 }
126
135 template <typename InputType>
137 : m_matrix(matrix.derived()), m_temp(matrix.rows()), m_isInitialized(false) {
138 computeInPlace();
139 }
140
158 template <typename InputType>
160 m_matrix = matrix.derived();
161 computeInPlace();
162 return *this;
163 }
164
179 eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
180 return m_hCoeffs;
181 }
182
212 const MatrixType& packedMatrix() const {
213 eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
214 return m_matrix;
215 }
216
232 eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
233 return HouseholderSequenceType(m_matrix, m_hCoeffs.conjugate()).setLength(m_matrix.rows() - 1).setShift(1);
234 }
235
256 MatrixHReturnType matrixH() const {
257 eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
258 return MatrixHReturnType(*this);
259 }
260
261 private:
262 using VectorType = Matrix<Scalar, 1, Size, int(Options) | int(RowMajor), 1, MaxSize>;
263 using RealScalar = typename NumTraits<Scalar>::Real;
264
265 void computeInPlace() {
266 if (m_matrix.rows() < 2) {
267 m_isInitialized = true;
268 return;
269 }
270 m_hCoeffs.resize(m_matrix.rows() - 1, 1);
271 internal::hessenberg_decomposition_inplace(m_matrix, m_hCoeffs, m_temp);
272 m_isInitialized = true;
273 }
274
275 protected:
276 MatrixType m_matrix;
277 CoeffVectorType m_hCoeffs;
278 VectorType m_temp;
279 bool m_isInitialized;
280};
281
282namespace internal {
283
299template <typename MatrixType, typename CoeffVectorType, typename WorkspaceType>
300void hessenberg_decomposition_inplace(MatrixType& matA, CoeffVectorType& hCoeffs, WorkspaceType& temp) {
301 using Scalar = typename MatrixType::Scalar;
302 using RealScalar = typename NumTraits<Scalar>::Real;
303 EIGEN_INTERNAL_ASSERT_SQUARE(matA);
304 Index n = matA.rows();
305 temp.resize(n);
306 // The largest component of A, the scale against which a tail is negligible below.
307 const RealScalar matrixScale = n > 1 ? RealScalar(matA.realView().cwiseAbs().maxCoeff()) : RealScalar(0);
308 for (Index i = 0; i < n - 1; ++i) {
309 // let's consider the vector v = i-th column starting at position i+1
310 Index remainingSize = n - i - 1;
311 RealScalar beta;
312 Scalar h;
313 auto householder = matA.col(i).tail(remainingSize);
314 const RealScalar tailSqNorm =
315 remainingSize == 1 ? RealScalar(0) : householder.tail(remainingSize - 1).unwind().squaredNorm();
316 const RealScalar tol = (std::numeric_limits<RealScalar>::min)();
317 // Preserve negligible subdiagonal entries instead of rotating them into much larger matrix coefficients. The
318 // latter can erase small eigenvalues through cancellation even though the reflector itself is accurate.
319 // Negligible means |tail|_inf <= sqrt(tol) * m and |Im head| <= sqrt(tol) * m, for the largest component m of the
320 // entries the reflector touches, A(i+1:n, i:n) and A(:, i+1:n); for callers that scale A to O(1) (RealSchur,
321 // ComplexSchur) and a matrix without separated blocks that is the absolute test |tail|^2 <= tol. The tests on
322 // tailSqNorm and |Im head| against the largest component of A are a cheap prefilter, as the local scale is at most
323 // that; it saves the two scans for the local scale on an input that is already Hessenberg with a complex
324 // subdiagonal. The tailSqNorm test underflows for entries below sqrt(tol).
325 bool negligibleTail = false;
326 const RealScalar headImag = numext::abs(numext::imag(householder.coeff(0)));
327 if (tailSqNorm <= tol * matrixScale * matrixScale && headImag <= numext::sqrt(tol) * matrixScale &&
328 (numext::isfinite)(matrixScale)) {
329 const RealScalar tailMax =
330 remainingSize == 1
331 ? RealScalar(0)
332 : RealScalar(householder.tail(remainingSize - 1).unwind().realView().cwiseAbs().maxCoeff());
333 if (numext::is_exactly_zero(tailMax) && numext::is_exactly_zero(headImag)) {
334 negligibleTail = true; // the reflector would be the identity
335 } else {
336 const RealScalar localScale = numext::maxi(
337 RealScalar(matA.bottomRightCorner(remainingSize, remainingSize + 1).realView().cwiseAbs().maxCoeff()),
338 RealScalar(matA.rightCols(remainingSize).realView().cwiseAbs().maxCoeff()));
339 const RealScalar threshold = numext::sqrt(tol) * localScale;
340 negligibleTail = tailMax <= threshold && headImag <= threshold;
341 }
342 }
343 if (negligibleTail) {
344 h = Scalar(0);
345 beta = numext::real(householder.coeff(0));
346 householder.tail(remainingSize - 1).setZero();
347 } else {
348 householder.makeHouseholderInPlace(h, beta);
349 }
350 matA.col(i).coeffRef(i + 1) = beta;
351 hCoeffs.coeffRef(i) = h;
352
353 // Apply similarity transformation to remaining columns,
354 // i.e., compute A = H A H'
355
356 // A = H A
357 matA.bottomRightCorner(remainingSize, remainingSize)
358 .applyHouseholderOnTheLeft(matA.col(i).tail(remainingSize - 1), h, &temp.coeffRef(0));
359
360 // A = A H'
361 matA.rightCols(remainingSize)
362 .applyHouseholderOnTheRight(matA.col(i).tail(remainingSize - 1), numext::conj(h), &temp.coeffRef(0));
363 }
364}
365
378template <typename MatrixType, typename CoeffVectorType, typename WorkspaceType, typename MatrixQType>
379void hessenberg_decomposition_inplace(MatrixType& matA, CoeffVectorType& hCoeffs, WorkspaceType& temp,
380 MatrixQType& matQ, bool computeQ) {
381 using HouseholderSequenceType =
382 HouseholderSequence<MatrixType, remove_all_t<typename CoeffVectorType::ConjugateReturnType>>;
383 const Index n = matA.rows();
384 hCoeffs.resize(n - 1);
385 hessenberg_decomposition_inplace(matA, hCoeffs, temp);
386 if (computeQ) {
387 Index firstReflector = 0;
388 while (firstReflector < n - 1 && numext::is_exactly_zero(hCoeffs.coeff(firstReflector))) ++firstReflector;
389 if (firstReflector == n - 1) {
390 // All tau_i = 0: Q = I, without allocating block Householder factors.
391 matQ.setIdentity(n, n);
392 } else {
393 HouseholderSequenceType(matA, hCoeffs.conjugate()).setLength(n - 1).setShift(1).evalTo(matQ, temp);
394 }
395 }
396 if (n > 2) matA.bottomLeftCorner(n - 2, n - 2).template triangularView<Lower>().setZero();
397}
398
414template <typename MatrixType>
416 : public ReturnByValue<HessenbergDecompositionMatrixHReturnType<MatrixType>> {
417 public:
423
429 template <typename ResultType>
430 inline void evalTo(ResultType& result) const {
431 result = m_hess.packedMatrix();
432 Index n = result.rows();
433 if (n > 2) result.bottomLeftCorner(n - 2, n - 2).template triangularView<Lower>().setZero();
434 }
435
436 Index rows() const { return m_hess.packedMatrix().rows(); }
437 Index cols() const { return m_hess.packedMatrix().cols(); }
438
439 protected:
440 const HessenbergDecomposition<MatrixType>& m_hess;
441};
442
443} // end namespace internal
444
445} // end namespace Eigen
446
447#endif // EIGEN_HESSENBERGDECOMPOSITION_H
Reduces a square matrix to Hessenberg form by an orthogonal similarity transformation.
Definition HessenbergDecomposition.h:65
HessenbergDecomposition(EigenBase< InputType > &matrix)
Constructor for inplace decomposition .
Definition HessenbergDecomposition.h:136
HessenbergDecomposition(Index size=Size==Dynamic ? 2 :Size)
Default constructor; the decomposition will be computed later.
Definition HessenbergDecomposition.h:107
HouseholderSequence< MatrixType, internal::remove_all_t< typename CoeffVectorType::ConjugateReturnType > > HouseholderSequenceType
Return type of matrixQ()
Definition HessenbergDecomposition.h:91
HessenbergDecomposition(const EigenBase< InputType > &matrix)
Constructor; computes Hessenberg decomposition of given matrix.
Definition HessenbergDecomposition.h:122
MatrixType_ MatrixType
Synonym for the template parameter MatrixType_.
Definition HessenbergDecomposition.h:68
const MatrixType & packedMatrix() const
Returns the internal representation of the decomposition.
Definition HessenbergDecomposition.h:212
MatrixHReturnType matrixH() const
Constructs the Hessenberg matrix H in the decomposition.
Definition HessenbergDecomposition.h:256
HouseholderSequenceType matrixQ() const
Reconstructs the orthogonal matrix Q in the decomposition.
Definition HessenbergDecomposition.h:231
typename MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition HessenbergDecomposition.h:79
Eigen::Index Index
Definition HessenbergDecomposition.h:80
HessenbergDecomposition & compute(const EigenBase< InputType > &matrix)
Computes Hessenberg decomposition of given matrix.
Definition HessenbergDecomposition.h:159
Matrix< Scalar, SizeMinusOne, 1, Options &~RowMajor, MaxSizeMinusOne, 1 > CoeffVectorType
Type for vector of Householder coefficients.
Definition HessenbergDecomposition.h:88
const CoeffVectorType & householderCoefficients() const
Returns the Householder coefficients.
Definition HessenbergDecomposition.h:178
Sequence of Householder reflections acting on subspaces with decreasing size.
Definition HouseholderSequence.h:155
HouseholderSequence & setLength(Index length)
Sets the length of the Householder sequence.
Definition HouseholderSequence.h:590
HouseholderSequence & setShift(Index shift)
Sets the shift of the Householder sequence.
Definition HouseholderSequence.h:606
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
constexpr void resize(Index rows, Index cols)
Definition PlainObjectBase.h:295
@ RowMajor
Definition Constants.h:321
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50
Expression type for return value of HessenbergDecomposition::matrixH()
Definition HessenbergDecomposition.h:416
void evalTo(ResultType &result) const
Hessenberg matrix in decomposition.
Definition HessenbergDecomposition.h:430
HessenbergDecompositionMatrixHReturnType(const HessenbergDecomposition< MatrixType > &hess)
Constructor.
Definition HessenbergDecomposition.h:422