2008-06-08 15:03:23 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-06-08 15:03:23 +00:00
|
|
|
//
|
|
|
|
|
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
|
|
|
|
//
|
|
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2 of
|
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_HESSENBERGDECOMPOSITION_H
|
|
|
|
|
#define EIGEN_HESSENBERGDECOMPOSITION_H
|
|
|
|
|
|
2009-09-04 09:23:38 +02:00
|
|
|
/** \eigenvalues_module \ingroup Eigenvalues_Module
|
2008-11-22 19:51:05 +00:00
|
|
|
* \nonstableyet
|
2008-07-22 10:54:42 +00:00
|
|
|
*
|
|
|
|
|
* \class HessenbergDecomposition
|
2008-06-08 15:03:23 +00:00
|
|
|
*
|
|
|
|
|
* \brief Reduces a squared matrix to an Hessemberg form
|
|
|
|
|
*
|
|
|
|
|
* \param MatrixType the type of the matrix of which we are computing the Hessenberg decomposition
|
|
|
|
|
*
|
|
|
|
|
* This class performs an Hessenberg decomposition of a matrix \f$ A \f$ such that:
|
|
|
|
|
* \f$ A = Q H Q^* \f$ where \f$ Q \f$ is unitary and \f$ H \f$ a Hessenberg matrix.
|
|
|
|
|
*
|
|
|
|
|
* \sa class Tridiagonalization, class Qr
|
|
|
|
|
*/
|
|
|
|
|
template<typename _MatrixType> class HessenbergDecomposition
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
typedef _MatrixType MatrixType;
|
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
Size = MatrixType::RowsAtCompileTime,
|
|
|
|
|
SizeMinusOne = MatrixType::RowsAtCompileTime==Dynamic
|
2008-08-03 20:23:06 +00:00
|
|
|
? Dynamic
|
|
|
|
|
: MatrixType::RowsAtCompileTime-1
|
|
|
|
|
};
|
2008-06-08 15:03:23 +00:00
|
|
|
|
|
|
|
|
typedef Matrix<Scalar, SizeMinusOne, 1> CoeffVectorType;
|
|
|
|
|
typedef Matrix<RealScalar, Size, 1> DiagonalType;
|
|
|
|
|
typedef Matrix<RealScalar, SizeMinusOne, 1> SubDiagonalType;
|
|
|
|
|
|
2009-05-10 16:24:39 +00:00
|
|
|
typedef typename NestByValue<Diagonal<MatrixType,0> >::RealReturnType DiagonalReturnType;
|
2008-06-08 15:03:23 +00:00
|
|
|
|
2009-05-10 16:24:39 +00:00
|
|
|
typedef typename NestByValue<Diagonal<
|
|
|
|
|
NestByValue<Block<MatrixType,SizeMinusOne,SizeMinusOne> >,0 > >::RealReturnType SubDiagonalReturnType;
|
2008-06-08 15:03:23 +00:00
|
|
|
|
2008-06-14 13:02:41 +00:00
|
|
|
/** This constructor initializes a HessenbergDecomposition object for
|
|
|
|
|
* further use with HessenbergDecomposition::compute()
|
|
|
|
|
*/
|
|
|
|
|
HessenbergDecomposition(int size = Size==Dynamic ? 2 : Size)
|
|
|
|
|
: m_matrix(size,size), m_hCoeffs(size-1)
|
2008-06-08 15:03:23 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
HessenbergDecomposition(const MatrixType& matrix)
|
|
|
|
|
: m_matrix(matrix),
|
|
|
|
|
m_hCoeffs(matrix.cols()-1)
|
|
|
|
|
{
|
|
|
|
|
_compute(m_matrix, m_hCoeffs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Computes or re-compute the Hessenberg decomposition for the matrix \a matrix.
|
|
|
|
|
*
|
|
|
|
|
* This method allows to re-use the allocated data.
|
|
|
|
|
*/
|
|
|
|
|
void compute(const MatrixType& matrix)
|
|
|
|
|
{
|
|
|
|
|
m_matrix = matrix;
|
|
|
|
|
m_hCoeffs.resize(matrix.rows()-1,1);
|
|
|
|
|
_compute(m_matrix, m_hCoeffs);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-16 14:34:08 +02:00
|
|
|
/** \returns a const reference to the householder coefficients allowing to
|
2008-06-08 15:03:23 +00:00
|
|
|
* reconstruct the matrix Q from the packed data.
|
|
|
|
|
*
|
|
|
|
|
* \sa packedMatrix()
|
|
|
|
|
*/
|
2009-09-16 14:34:08 +02:00
|
|
|
const CoeffVectorType& householderCoefficients() const { return m_hCoeffs; }
|
2008-06-08 15:03:23 +00:00
|
|
|
|
2009-09-16 14:34:08 +02:00
|
|
|
/** \returns a const reference to the internal representation of the decomposition.
|
2008-06-08 15:03:23 +00:00
|
|
|
*
|
|
|
|
|
* The returned matrix contains the following information:
|
|
|
|
|
* - the upper part and lower sub-diagonal represent the Hessenberg matrix H
|
|
|
|
|
* - the rest of the lower part contains the Householder vectors that, combined with
|
|
|
|
|
* Householder coefficients returned by householderCoefficients(),
|
|
|
|
|
* allows to reconstruct the matrix Q as follow:
|
|
|
|
|
* Q = H_{N-1} ... H_1 H_0
|
|
|
|
|
* where the matrices H are the Householder transformation:
|
|
|
|
|
* H_i = (I - h_i * v_i * v_i')
|
|
|
|
|
* where h_i == householderCoefficients()[i] and v_i is a Householder vector:
|
|
|
|
|
* v_i = [ 0, ..., 0, 1, M(i+2,i), ..., M(N-1,i) ]
|
|
|
|
|
*
|
|
|
|
|
* See LAPACK for further details on this packed storage.
|
|
|
|
|
*/
|
|
|
|
|
const MatrixType& packedMatrix(void) const { return m_matrix; }
|
|
|
|
|
|
2009-08-17 17:41:01 +02:00
|
|
|
MatrixType matrixQ() const;
|
|
|
|
|
MatrixType matrixH() const;
|
2008-06-08 15:03:23 +00:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
static void _compute(MatrixType& matA, CoeffVectorType& hCoeffs);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
MatrixType m_matrix;
|
|
|
|
|
CoeffVectorType m_hCoeffs;
|
|
|
|
|
};
|
|
|
|
|
|
2008-06-14 13:02:41 +00:00
|
|
|
#ifndef EIGEN_HIDE_HEAVY_CODE
|
2008-06-08 15:03:23 +00:00
|
|
|
|
|
|
|
|
/** \internal
|
|
|
|
|
* Performs a tridiagonal decomposition of \a matA in place.
|
|
|
|
|
*
|
|
|
|
|
* \param matA the input selfadjoint matrix
|
|
|
|
|
* \param hCoeffs returned Householder coefficients
|
|
|
|
|
*
|
|
|
|
|
* The result is written in the lower triangular part of \a matA.
|
|
|
|
|
*
|
|
|
|
|
* Implemented from Golub's "Matrix Computations", algorithm 8.3.1.
|
|
|
|
|
*
|
|
|
|
|
* \sa packedMatrix()
|
|
|
|
|
*/
|
|
|
|
|
template<typename MatrixType>
|
|
|
|
|
void HessenbergDecomposition<MatrixType>::_compute(MatrixType& matA, CoeffVectorType& hCoeffs)
|
|
|
|
|
{
|
|
|
|
|
assert(matA.rows()==matA.cols());
|
|
|
|
|
int n = matA.rows();
|
2009-08-17 17:41:01 +02:00
|
|
|
Matrix<Scalar,1,Dynamic> temp(n);
|
|
|
|
|
for (int i = 0; i<n-1; ++i)
|
2008-06-08 15:03:23 +00:00
|
|
|
{
|
|
|
|
|
// let's consider the vector v = i-th column starting at position i+1
|
2009-08-17 17:41:01 +02:00
|
|
|
int remainingSize = n-i-1;
|
|
|
|
|
RealScalar beta;
|
|
|
|
|
Scalar h;
|
|
|
|
|
matA.col(i).end(remainingSize).makeHouseholderInPlace(&h, &beta);
|
|
|
|
|
matA.col(i).coeffRef(i+1) = beta;
|
2008-06-08 15:03:23 +00:00
|
|
|
hCoeffs.coeffRef(i) = h;
|
|
|
|
|
|
2009-08-17 17:41:01 +02:00
|
|
|
// Apply similarity transformation to remaining columns,
|
|
|
|
|
// i.e., compute A = H A H'
|
2009-09-03 11:39:44 +02:00
|
|
|
|
2009-08-17 17:41:01 +02:00
|
|
|
// A = H A
|
|
|
|
|
matA.corner(BottomRight, remainingSize, remainingSize)
|
|
|
|
|
.applyHouseholderOnTheLeft(matA.col(i).end(remainingSize-1), h, &temp.coeffRef(0));
|
2008-06-08 15:03:23 +00:00
|
|
|
|
2009-08-17 17:41:01 +02:00
|
|
|
// A = A H'
|
|
|
|
|
matA.corner(BottomRight, n, remainingSize)
|
|
|
|
|
.applyHouseholderOnTheRight(matA.col(i).end(remainingSize-1).conjugate(), ei_conj(h), &temp.coeffRef(0));
|
2008-06-08 15:03:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** reconstructs and returns the matrix Q */
|
|
|
|
|
template<typename MatrixType>
|
|
|
|
|
typename HessenbergDecomposition<MatrixType>::MatrixType
|
2009-08-17 17:41:01 +02:00
|
|
|
HessenbergDecomposition<MatrixType>::matrixQ() const
|
2008-06-08 15:03:23 +00:00
|
|
|
{
|
|
|
|
|
int n = m_matrix.rows();
|
2008-07-21 00:34:46 +00:00
|
|
|
MatrixType matQ = MatrixType::Identity(n,n);
|
2009-08-06 14:10:02 +02:00
|
|
|
Matrix<Scalar,1,MatrixType::ColsAtCompileTime> temp(n);
|
2008-06-08 15:03:23 +00:00
|
|
|
for (int i = n-2; i>=0; i--)
|
|
|
|
|
{
|
2009-08-17 17:41:01 +02:00
|
|
|
matQ.corner(BottomRight,n-i-1,n-i-1)
|
|
|
|
|
.applyHouseholderOnTheLeft(m_matrix.col(i).end(n-i-2), ei_conj(m_hCoeffs.coeff(i)), &temp.coeffRef(0,0));
|
2008-06-08 15:03:23 +00:00
|
|
|
}
|
|
|
|
|
return matQ;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-14 13:02:41 +00:00
|
|
|
#endif // EIGEN_HIDE_HEAVY_CODE
|
|
|
|
|
|
2008-06-08 15:03:23 +00:00
|
|
|
/** constructs and returns the matrix H.
|
|
|
|
|
* Note that the matrix H is equivalent to the upper part of the packed matrix
|
|
|
|
|
* (including the lower sub-diagonal). Therefore, it might be often sufficient
|
|
|
|
|
* to directly use the packed matrix instead of creating a new one.
|
|
|
|
|
*/
|
|
|
|
|
template<typename MatrixType>
|
|
|
|
|
typename HessenbergDecomposition<MatrixType>::MatrixType
|
2009-08-17 17:41:01 +02:00
|
|
|
HessenbergDecomposition<MatrixType>::matrixH() const
|
2008-06-08 15:03:23 +00:00
|
|
|
{
|
|
|
|
|
// FIXME should this function (and other similar) rather take a matrix as argument
|
2008-06-14 13:02:41 +00:00
|
|
|
// and fill it (to avoid temporaries)
|
2008-06-08 15:03:23 +00:00
|
|
|
int n = m_matrix.rows();
|
|
|
|
|
MatrixType matH = m_matrix;
|
2008-06-15 11:54:18 +00:00
|
|
|
if (n>2)
|
2009-07-27 13:50:23 +02:00
|
|
|
matH.corner(BottomLeft,n-2, n-2).template triangularView<LowerTriangular>().setZero();
|
2008-06-08 15:03:23 +00:00
|
|
|
return matH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_HESSENBERGDECOMPOSITION_H
|