$darkmode
Eigen  5.0.1-dev
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 
11 #ifndef EIGEN_HESSENBERGDECOMPOSITION_H
12 #define EIGEN_HESSENBERGDECOMPOSITION_H
13 
14 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 namespace internal {
20 
21 template <typename MatrixType>
23 template <typename MatrixType>
24 struct traits<HessenbergDecompositionMatrixHReturnType<MatrixType>> {
25  typedef MatrixType ReturnType;
26 };
27 
28 } // namespace internal
29 
60 template <typename MatrixType_>
62  public:
64  typedef MatrixType_ MatrixType;
65 
66  enum {
67  Size = MatrixType::RowsAtCompileTime,
68  SizeMinusOne = Size == Dynamic ? Dynamic : Size - 1,
69  Options = internal::traits<MatrixType>::Options,
70  MaxSize = MatrixType::MaxRowsAtCompileTime,
71  MaxSizeMinusOne = MaxSize == Dynamic ? Dynamic : MaxSize - 1
72  };
73 
75  typedef typename MatrixType::Scalar Scalar;
76  typedef Eigen::Index Index;
77 
85 
89 
91 
103  explicit HessenbergDecomposition(Index size = Size == Dynamic ? 2 : Size)
104  : m_matrix(size, size), m_temp(size), m_isInitialized(false) {
105  if (size > 1) m_hCoeffs.resize(size - 1);
106  }
107 
117  template <typename InputType>
119  : m_matrix(matrix.derived()), m_temp(matrix.rows()), m_isInitialized(false) {
120  if (matrix.rows() < 2) {
121  m_isInitialized = true;
122  return;
123  }
124  m_hCoeffs.resize(matrix.rows() - 1, 1);
125  _compute(m_matrix, m_hCoeffs, m_temp);
126  m_isInitialized = true;
127  }
128 
146  template <typename InputType>
148  m_matrix = matrix.derived();
149  if (matrix.rows() < 2) {
150  m_isInitialized = true;
151  return *this;
152  }
153  m_hCoeffs.resize(matrix.rows() - 1, 1);
154  _compute(m_matrix, m_hCoeffs, m_temp);
155  m_isInitialized = true;
156  return *this;
157  }
158 
173  eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
174  return m_hCoeffs;
175  }
176 
206  const MatrixType& packedMatrix() const {
207  eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
208  return m_matrix;
209  }
210 
226  eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
227  return HouseholderSequenceType(m_matrix, m_hCoeffs.conjugate()).setLength(m_matrix.rows() - 1).setShift(1);
228  }
229 
251  eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
252  return MatrixHReturnType(*this);
253  }
254 
255  private:
257  typedef typename NumTraits<Scalar>::Real RealScalar;
258  static void _compute(MatrixType& matA, CoeffVectorType& hCoeffs, VectorType& temp);
259 
260  protected:
261  MatrixType m_matrix;
262  CoeffVectorType m_hCoeffs;
263  VectorType m_temp;
264  bool m_isInitialized;
265 };
266 
279 template <typename MatrixType>
280 void HessenbergDecomposition<MatrixType>::_compute(MatrixType& matA, CoeffVectorType& hCoeffs, VectorType& temp) {
281  eigen_assert(matA.rows() == matA.cols());
282  Index n = matA.rows();
283  temp.resize(n);
284  for (Index i = 0; i < n - 1; ++i) {
285  // let's consider the vector v = i-th column starting at position i+1
286  Index remainingSize = n - i - 1;
287  RealScalar beta;
288  Scalar h;
289  matA.col(i).tail(remainingSize).makeHouseholderInPlace(h, beta);
290  matA.col(i).coeffRef(i + 1) = beta;
291  hCoeffs.coeffRef(i) = h;
292 
293  // Apply similarity transformation to remaining columns,
294  // i.e., compute A = H A H'
295 
296  // A = H A
297  matA.bottomRightCorner(remainingSize, remainingSize)
298  .applyHouseholderOnTheLeft(matA.col(i).tail(remainingSize - 1), h, &temp.coeffRef(0));
299 
300  // A = A H'
301  matA.rightCols(remainingSize)
302  .applyHouseholderOnTheRight(matA.col(i).tail(remainingSize - 1), numext::conj(h), &temp.coeffRef(0));
303  }
304 }
305 
306 namespace internal {
307 
323 template <typename MatrixType>
324 struct HessenbergDecompositionMatrixHReturnType
325  : public ReturnByValue<HessenbergDecompositionMatrixHReturnType<MatrixType>> {
326  public:
332 
338  template <typename ResultType>
339  inline void evalTo(ResultType& result) const {
340  result = m_hess.packedMatrix();
341  Index n = result.rows();
342  if (n > 2) result.bottomLeftCorner(n - 2, n - 2).template triangularView<Lower>().setZero();
343  }
344 
345  Index rows() const { return m_hess.packedMatrix().rows(); }
346  Index cols() const { return m_hess.packedMatrix().cols(); }
347 
348  protected:
349  const HessenbergDecomposition<MatrixType>& m_hess;
350 };
351 
352 } // end namespace internal
353 
354 } // end namespace Eigen
355 
356 #endif // EIGEN_HESSENBERGDECOMPOSITION_H
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition: HessenbergDecomposition.h:75
HouseholderSequenceType matrixQ() const
Reconstructs the orthogonal matrix Q in the decomposition.
Definition: HessenbergDecomposition.h:225
constexpr void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:268
MatrixHReturnType matrixH() const
Constructs the Hessenberg matrix H in the decomposition.
Definition: HessenbergDecomposition.h:250
HessenbergDecomposition & compute(const EigenBase< InputType > &matrix)
Computes Hessenberg decomposition of given matrix.
Definition: HessenbergDecomposition.h:147
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
MatrixType_ MatrixType
Synonym for the template parameter MatrixType_.
Definition: HessenbergDecomposition.h:64
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
Eigen::Index Index
Definition: HessenbergDecomposition.h:76
void evalTo(ResultType &result) const
Hessenberg matrix in decomposition.
Definition: HessenbergDecomposition.h:339
constexpr Index rows() const noexcept
Definition: EigenBase.h:59
Definition: EigenBase.h:33
Sequence of Householder reflections acting on subspaces with decreasing size.
Definition: ForwardDeclarations.h:445
HessenbergDecompositionMatrixHReturnType(const HessenbergDecomposition< MatrixType > &hess)
Constructor.
Definition: HessenbergDecomposition.h:331
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
constexpr Derived & derived()
Definition: EigenBase.h:49
const CoeffVectorType & householderCoefficients() const
Returns the Householder coefficients.
Definition: HessenbergDecomposition.h:172
Expression type for return value of HessenbergDecomposition::matrixH()
Definition: HessenbergDecomposition.h:22
Matrix< Scalar, SizeMinusOne, 1, Options &~RowMajor, MaxSizeMinusOne, 1 > CoeffVectorType
Type for vector of Householder coefficients.
Definition: HessenbergDecomposition.h:84
HouseholderSequence< MatrixType, internal::remove_all_t< typename CoeffVectorType::ConjugateReturnType > > HouseholderSequenceType
Return type of matrixQ()
Definition: HessenbergDecomposition.h:88
Reduces a square matrix to Hessenberg form by an orthogonal similarity transformation.
Definition: HessenbergDecomposition.h:61
HessenbergDecomposition(const EigenBase< InputType > &matrix)
Constructor; computes Hessenberg decomposition of given matrix.
Definition: HessenbergDecomposition.h:118
HessenbergDecomposition(Index size=Size==Dynamic ? 2 :Size)
Default constructor; the decomposition will be computed later.
Definition: HessenbergDecomposition.h:103
const int Dynamic
Definition: Constants.h:25
const MatrixType & packedMatrix() const
Returns the internal representation of the decomposition.
Definition: HessenbergDecomposition.h:206