HessenbergDecomposition.h
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) 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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template<typename MatrixType> struct HessenbergDecompositionMatrixHReturnType;
21 template<typename MatrixType>
22 struct traits<HessenbergDecompositionMatrixHReturnType<MatrixType> >
23 {
24  typedef MatrixType ReturnType;
25 };
26 
27 }
28 
59 template<typename MatrixType_> class HessenbergDecomposition
60 {
61  public:
62 
64  typedef MatrixType_ MatrixType;
65 
66  enum {
67  Size = MatrixType::RowsAtCompileTime,
69  Options = MatrixType::Options,
70  MaxSize = MatrixType::MaxRowsAtCompileTime,
72  };
73 
75  typedef typename MatrixType::Scalar Scalar;
76  typedef Eigen::Index Index;
77 
85 
88 
89  typedef internal::HessenbergDecompositionMatrixHReturnType<MatrixType> MatrixHReturnType;
90 
103  : m_matrix(size,size),
104  m_temp(size),
105  m_isInitialized(false)
106  {
107  if(size>1)
108  m_hCoeffs.resize(size-1);
109  }
110 
120  template<typename InputType>
122  : m_matrix(matrix.derived()),
123  m_temp(matrix.rows()),
124  m_isInitialized(false)
125  {
126  if(matrix.rows()<2)
127  {
128  m_isInitialized = true;
129  return;
130  }
131  m_hCoeffs.resize(matrix.rows()-1,1);
133  m_isInitialized = true;
134  }
135 
153  template<typename InputType>
155  {
156  m_matrix = matrix.derived();
157  if(matrix.rows()<2)
158  {
159  m_isInitialized = true;
160  return *this;
161  }
162  m_hCoeffs.resize(matrix.rows()-1,1);
164  m_isInitialized = true;
165  return *this;
166  }
167 
182  {
183  eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
184  return m_hCoeffs;
185  }
186 
216  const MatrixType& packedMatrix() const
217  {
218  eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
219  return m_matrix;
220  }
221 
237  {
238  eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
239  return HouseholderSequenceType(m_matrix, m_hCoeffs.conjugate())
240  .setLength(m_matrix.rows() - 1)
241  .setShift(1);
242  }
243 
265  {
266  eigen_assert(m_isInitialized && "HessenbergDecomposition is not initialized.");
267  return MatrixHReturnType(*this);
268  }
269 
270  private:
271 
272  typedef Matrix<Scalar, 1, Size, int(Options) | int(RowMajor), 1, MaxSize> VectorType;
274  static void _compute(MatrixType& matA, CoeffVectorType& hCoeffs, VectorType& temp);
275 
276  protected:
281 };
282 
295 template<typename MatrixType>
297 {
298  eigen_assert(matA.rows()==matA.cols());
299  Index n = matA.rows();
300  temp.resize(n);
301  for (Index i = 0; i<n-1; ++i)
302  {
303  // let's consider the vector v = i-th column starting at position i+1
304  Index remainingSize = n-i-1;
305  RealScalar beta;
306  Scalar h;
307  matA.col(i).tail(remainingSize).makeHouseholderInPlace(h, beta);
308  matA.col(i).coeffRef(i+1) = beta;
309  hCoeffs.coeffRef(i) = h;
310 
311  // Apply similarity transformation to remaining columns,
312  // i.e., compute A = H A H'
313 
314  // A = H A
315  matA.bottomRightCorner(remainingSize, remainingSize)
316  .applyHouseholderOnTheLeft(matA.col(i).tail(remainingSize-1), h, &temp.coeffRef(0));
317 
318  // A = A H'
319  matA.rightCols(remainingSize)
320  .applyHouseholderOnTheRight(matA.col(i).tail(remainingSize-1), numext::conj(h), &temp.coeffRef(0));
321  }
322 }
323 
324 namespace internal {
325 
341 template<typename MatrixType> struct HessenbergDecompositionMatrixHReturnType
342 : public ReturnByValue<HessenbergDecompositionMatrixHReturnType<MatrixType> >
343 {
344  public:
349  HessenbergDecompositionMatrixHReturnType(const HessenbergDecomposition<MatrixType>& hess) : m_hess(hess) { }
350 
356  template <typename ResultType>
357  inline void evalTo(ResultType& result) const
358  {
359  result = m_hess.packedMatrix();
360  Index n = result.rows();
361  if (n>2)
362  result.bottomLeftCorner(n-2, n-2).template triangularView<Lower>().setZero();
363  }
364 
365  Index rows() const { return m_hess.packedMatrix().rows(); }
366  Index cols() const { return m_hess.packedMatrix().cols(); }
367 
368  protected:
369  const HessenbergDecomposition<MatrixType>& m_hess;
370 };
371 
372 } // end namespace internal
373 
374 } // end namespace Eigen
375 
376 #endif // EIGEN_HESSENBERGDECOMPOSITION_H
int n
#define eigen_assert(x)
Definition: Macros.h:902
MatrixXf matA(2, 2)
Matrix< float, 1, Dynamic > MatrixType
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
Reduces a square matrix to Hessenberg form by an orthogonal similarity transformation.
Matrix< Scalar, SizeMinusOne, 1, Options &~RowMajor, MaxSizeMinusOne, 1 > CoeffVectorType
Type for vector of Householder coefficients.
const CoeffVectorType & householderCoefficients() const
Returns the Householder coefficients.
HessenbergDecomposition(Index size=Size==Dynamic ? 2 :Size)
Default constructor; the decomposition will be computed later.
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
HessenbergDecomposition & compute(const EigenBase< InputType > &matrix)
Computes Hessenberg decomposition of given matrix.
HessenbergDecomposition(const EigenBase< InputType > &matrix)
Constructor; computes Hessenberg decomposition of given matrix.
static void _compute(MatrixType &matA, CoeffVectorType &hCoeffs, VectorType &temp)
MatrixHReturnType matrixH() const
Constructs the Hessenberg matrix H in the decomposition.
HouseholderSequenceType matrixQ() const
Reconstructs the orthogonal matrix Q in the decomposition.
const MatrixType & packedMatrix() const
Returns the internal representation of the decomposition.
HouseholderSequence< MatrixType, internal::remove_all_t< typename CoeffVectorType::ConjugateReturnType > > HouseholderSequenceType
Return type of matrixQ()
internal::HessenbergDecompositionMatrixHReturnType< MatrixType > MatrixHReturnType
NumTraits< Scalar >::Real RealScalar
Matrix< Scalar, 1, Size, int(Options)|int(RowMajor), 1, MaxSize > VectorType
MatrixType_ MatrixType
Synonym for the template parameter MatrixType_.
Sequence of Householder reflections acting on subspaces with decreasing size.
constexpr void resize(Index rows, Index cols)
constexpr Scalar & coeffRef(Index rowId, Index colId)
@ RowMajor
Definition: Constants.h:323
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
const int Dynamic
Definition: Constants.h:24
Derived & derived()
Definition: EigenBase.h:48
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:62
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231