12#ifndef EIGEN_HESSENBERGDECOMPOSITION_H
13#define EIGEN_HESSENBERGDECOMPOSITION_H
16#include "./InternalHeaderCheck.h"
22template <
typename MatrixType>
24template <
typename MatrixType>
26 using ReturnType =
typename MatrixType::PlainObject;
29template <
typename MatrixType,
typename CoeffVectorType,
typename WorkspaceType>
30void hessenberg_decomposition_inplace(MatrixType& matA, CoeffVectorType& hCoeffs, WorkspaceType& temp);
64template <
typename MatrixType_>
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
79 using Scalar =
typename MatrixType::Scalar;
108 : m_matrix(size, size), m_temp(size), m_isInitialized(false) {
109 if (size > 1) m_hCoeffs.resize(size - 1);
121 template <
typename InputType>
123 : m_matrix(matrix.derived()), m_temp(matrix.rows()), m_isInitialized(false) {
135 template <
typename InputType>
137 : m_matrix(matrix.derived()), m_temp(matrix.rows()), m_isInitialized(false) {
158 template <
typename InputType>
179 eigen_assert(m_isInitialized &&
"HessenbergDecomposition is not initialized.");
213 eigen_assert(m_isInitialized &&
"HessenbergDecomposition is not initialized.");
232 eigen_assert(m_isInitialized &&
"HessenbergDecomposition is not initialized.");
257 eigen_assert(m_isInitialized &&
"HessenbergDecomposition is not initialized.");
258 return MatrixHReturnType(*
this);
262 using VectorType =
Matrix<Scalar, 1, Size, int(Options) | int(
RowMajor), 1, MaxSize>;
263 using RealScalar =
typename NumTraits<Scalar>::Real;
265 void computeInPlace() {
266 if (m_matrix.rows() < 2) {
267 m_isInitialized =
true;
270 m_hCoeffs.
resize(m_matrix.rows() - 1, 1);
271 internal::hessenberg_decomposition_inplace(m_matrix, m_hCoeffs, m_temp);
272 m_isInitialized =
true;
279 bool m_isInitialized;
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();
307 const RealScalar matrixScale = n > 1 ? RealScalar(matA.realView().cwiseAbs().maxCoeff()) : RealScalar(0);
308 for (Index i = 0; i < n - 1; ++i) {
310 Index remainingSize = n - i - 1;
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)();
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 =
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;
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;
343 if (negligibleTail) {
345 beta = numext::real(householder.coeff(0));
346 householder.tail(remainingSize - 1).setZero();
348 householder.makeHouseholderInPlace(h, beta);
350 matA.col(i).coeffRef(i + 1) = beta;
351 hCoeffs.coeffRef(i) = h;
357 matA.bottomRightCorner(remainingSize, remainingSize)
358 .applyHouseholderOnTheLeft(matA.col(i).tail(remainingSize - 1), h, &temp.coeffRef(0));
361 matA.rightCols(remainingSize)
362 .applyHouseholderOnTheRight(matA.col(i).tail(remainingSize - 1), numext::conj(h), &temp.coeffRef(0));
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);
387 Index firstReflector = 0;
388 while (firstReflector < n - 1 && numext::is_exactly_zero(hCoeffs.coeff(firstReflector))) ++firstReflector;
389 if (firstReflector == n - 1) {
391 matQ.setIdentity(n, n);
393 HouseholderSequenceType(matA, hCoeffs.conjugate()).setLength(n - 1).setShift(1).evalTo(matQ, temp);
396 if (n > 2) matA.bottomLeftCorner(n - 2, n - 2).template triangularView<Lower>().setZero();
414template <
typename MatrixType>
416 :
public ReturnByValue<HessenbergDecompositionMatrixHReturnType<MatrixType>> {
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();
436 Index rows()
const {
return m_hess.
packedMatrix().rows(); }
437 Index cols()
const {
return m_hess.
packedMatrix().cols(); }
440 const HessenbergDecomposition<MatrixType>& m_hess;
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