* Make HouseholderSequence::evalTo works in place

* Clean a bit the Triadiagonalization making sure it the inplace
  function really works inplace ;), and that only the lower
   triangular part of the matrix is referenced.
* Remove the Tridiagonalization member object of SelfAdjointEigenSolver
  exploiting the in place capability of HouseholdeSequence.
* Update unit test to check SelfAdjointEigenSolver only consider
  the lower triangular part.
This commit is contained in:
Gael Guennebaud 2010-06-10 16:39:46 +02:00
parent d2d7465bcf
commit 469382407c
4 changed files with 208 additions and 126 deletions

View File

@ -38,7 +38,7 @@
* *
* \tparam _MatrixType the type of the matrix of which we are computing the * \tparam _MatrixType the type of the matrix of which we are computing the
* eigendecomposition; this is expected to be an instantiation of the Matrix * eigendecomposition; this is expected to be an instantiation of the Matrix
* class template. Currently, only real matrices are supported. * class template.
* *
* A matrix \f$ A \f$ is selfadjoint if it equals its adjoint. For real * A matrix \f$ A \f$ is selfadjoint if it equals its adjoint. For real
* matrices, this means that the matrix is symmetric: it equals its * matrices, this means that the matrix is symmetric: it equals its
@ -55,6 +55,8 @@
* faster and more accurate than the general purpose eigenvalue algorithms * faster and more accurate than the general purpose eigenvalue algorithms
* implemented in EigenSolver and ComplexEigenSolver. * implemented in EigenSolver and ComplexEigenSolver.
* *
* Only the \b lower \b triangular \b part of the input matrix is referenced.
*
* This class can also be used to solve the generalized eigenvalue problem * This class can also be used to solve the generalized eigenvalue problem
* \f$ Av = \lambda Bv \f$. In this case, the matrix \f$ A \f$ should be * \f$ Av = \lambda Bv \f$. In this case, the matrix \f$ A \f$ should be
* selfadjoint and the matrix \f$ B \f$ should be positive definite. * selfadjoint and the matrix \f$ B \f$ should be positive definite.
@ -117,7 +119,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
SelfAdjointEigenSolver() SelfAdjointEigenSolver()
: m_eivec(), : m_eivec(),
m_eivalues(), m_eivalues(),
m_tridiag(),
m_subdiag(), m_subdiag(),
m_isInitialized(false) m_isInitialized(false)
{ } { }
@ -138,7 +139,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
SelfAdjointEigenSolver(Index size) SelfAdjointEigenSolver(Index size)
: m_eivec(size, size), : m_eivec(size, size),
m_eivalues(size), m_eivalues(size),
m_tridiag(size),
m_subdiag(size > 1 ? size - 1 : 1), m_subdiag(size > 1 ? size - 1 : 1),
m_isInitialized(false) m_isInitialized(false)
{} {}
@ -146,7 +146,7 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
/** \brief Constructor; computes eigendecomposition of given matrix. /** \brief Constructor; computes eigendecomposition of given matrix.
* *
* \param[in] matrix Selfadjoint matrix whose eigendecomposition is to * \param[in] matrix Selfadjoint matrix whose eigendecomposition is to
* be computed. * be computed. Only the lower triangular part of the matrix is referenced.
* \param[in] computeEigenvectors If true, both the eigenvectors and the * \param[in] computeEigenvectors If true, both the eigenvectors and the
* eigenvalues are computed; if false, only the eigenvalues are * eigenvalues are computed; if false, only the eigenvalues are
* computed. * computed.
@ -164,7 +164,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
SelfAdjointEigenSolver(const MatrixType& matrix, bool computeEigenvectors = true) SelfAdjointEigenSolver(const MatrixType& matrix, bool computeEigenvectors = true)
: m_eivec(matrix.rows(), matrix.cols()), : m_eivec(matrix.rows(), matrix.cols()),
m_eivalues(matrix.cols()), m_eivalues(matrix.cols()),
m_tridiag(matrix.rows()),
m_subdiag(matrix.rows() > 1 ? matrix.rows() - 1 : 1), m_subdiag(matrix.rows() > 1 ? matrix.rows() - 1 : 1),
m_isInitialized(false) m_isInitialized(false)
{ {
@ -174,7 +173,9 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
/** \brief Constructor; computes generalized eigendecomposition of given matrix pencil. /** \brief Constructor; computes generalized eigendecomposition of given matrix pencil.
* *
* \param[in] matA Selfadjoint matrix in matrix pencil. * \param[in] matA Selfadjoint matrix in matrix pencil.
* Only the lower triangular part of the matrix is referenced.
* \param[in] matB Positive-definite matrix in matrix pencil. * \param[in] matB Positive-definite matrix in matrix pencil.
* Only the lower triangular part of the matrix is referenced.
* \param[in] computeEigenvectors If true, both the eigenvectors and the * \param[in] computeEigenvectors If true, both the eigenvectors and the
* eigenvalues are computed; if false, only the eigenvalues are * eigenvalues are computed; if false, only the eigenvalues are
* computed. * computed.
@ -196,7 +197,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
SelfAdjointEigenSolver(const MatrixType& matA, const MatrixType& matB, bool computeEigenvectors = true) SelfAdjointEigenSolver(const MatrixType& matA, const MatrixType& matB, bool computeEigenvectors = true)
: m_eivec(matA.rows(), matA.cols()), : m_eivec(matA.rows(), matA.cols()),
m_eivalues(matA.cols()), m_eivalues(matA.cols()),
m_tridiag(matA.rows()),
m_subdiag(matA.rows() > 1 ? matA.rows() - 1 : 1), m_subdiag(matA.rows() > 1 ? matA.rows() - 1 : 1),
m_isInitialized(false) m_isInitialized(false)
{ {
@ -206,7 +206,7 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
/** \brief Computes eigendecomposition of given matrix. /** \brief Computes eigendecomposition of given matrix.
* *
* \param[in] matrix Selfadjoint matrix whose eigendecomposition is to * \param[in] matrix Selfadjoint matrix whose eigendecomposition is to
* be computed. * be computed. Only the lower triangular part of the matrix is referenced.
* \param[in] computeEigenvectors If true, both the eigenvectors and the * \param[in] computeEigenvectors If true, both the eigenvectors and the
* eigenvalues are computed; if false, only the eigenvalues are * eigenvalues are computed; if false, only the eigenvalues are
* computed. * computed.
@ -240,7 +240,9 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
/** \brief Computes generalized eigendecomposition of given matrix pencil. /** \brief Computes generalized eigendecomposition of given matrix pencil.
* *
* \param[in] matA Selfadjoint matrix in matrix pencil. * \param[in] matA Selfadjoint matrix in matrix pencil.
* Only the lower triangular part of the matrix is referenced.
* \param[in] matB Positive-definite matrix in matrix pencil. * \param[in] matB Positive-definite matrix in matrix pencil.
* Only the lower triangular part of the matrix is referenced.
* \param[in] computeEigenvectors If true, both the eigenvectors and the * \param[in] computeEigenvectors If true, both the eigenvectors and the
* eigenvalues are computed; if false, only the eigenvalues are * eigenvalues are computed; if false, only the eigenvalues are
* computed. * computed.
@ -386,7 +388,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver
protected: protected:
MatrixType m_eivec; MatrixType m_eivec;
RealVectorType m_eivalues; RealVectorType m_eivalues;
TridiagonalizationType m_tridiag;
typename TridiagonalizationType::SubDiagonalType m_subdiag; typename TridiagonalizationType::SubDiagonalType m_subdiag;
ComputationInfo m_info; ComputationInfo m_info;
bool m_isInitialized; bool m_isInitialized;
@ -418,8 +419,6 @@ SelfAdjointEigenSolver<MatrixType>& SelfAdjointEigenSolver<MatrixType>::compute(
assert(matrix.cols() == matrix.rows()); assert(matrix.cols() == matrix.rows());
Index n = matrix.cols(); Index n = matrix.cols();
m_eivalues.resize(n,1); m_eivalues.resize(n,1);
if(computeEigenvectors)
m_eivec.resize(n,n);
if(n==1) if(n==1)
{ {
@ -432,12 +431,13 @@ SelfAdjointEigenSolver<MatrixType>& SelfAdjointEigenSolver<MatrixType>::compute(
return *this; return *this;
} }
m_tridiag.compute(matrix); // declare some aliases
RealVectorType& diag = m_eivalues; RealVectorType& diag = m_eivalues;
diag = m_tridiag.diagonal(); MatrixType& mat = m_eivec;
m_subdiag = m_tridiag.subDiagonal();
if (computeEigenvectors) mat = matrix;
m_eivec = m_tridiag.matrixQ(); m_subdiag.resize(n-1);
ei_tridiagonalization_inplace(mat, diag, m_subdiag, computeEigenvectors);
Index end = n-1; Index end = n-1;
Index start = 0; Index start = 0;
@ -487,7 +487,7 @@ SelfAdjointEigenSolver<MatrixType>& SelfAdjointEigenSolver<MatrixType>::compute(
{ {
std::swap(m_eivalues[i], m_eivalues[k+i]); std::swap(m_eivalues[i], m_eivalues[k+i]);
if(computeEigenvectors) if(computeEigenvectors)
m_eivec.col(i).swap(m_eivec.col(k+i)); m_eivec.col(i).swap(m_eivec.col(k+i));
} }
} }
} }
@ -507,7 +507,7 @@ compute(const MatrixType& matA, const MatrixType& matB, bool computeEigenvectors
LLT<MatrixType> cholB(matB); LLT<MatrixType> cholB(matB);
// compute C = inv(L) A inv(L') // compute C = inv(L) A inv(L')
MatrixType matC = matA; MatrixType matC = matA.template selfadjointView<Lower>();
cholB.matrixL().template solveInPlace<OnTheLeft>(matC); cholB.matrixL().template solveInPlace<OnTheLeft>(matC);
cholB.matrixU().template solveInPlace<OnTheRight>(matC); cholB.matrixU().template solveInPlace<OnTheRight>(matC);

View File

@ -154,7 +154,7 @@ template<typename _MatrixType> class Tridiagonalization
{ {
m_matrix = matrix; m_matrix = matrix;
m_hCoeffs.resize(matrix.rows()-1, 1); m_hCoeffs.resize(matrix.rows()-1, 1);
_compute(m_matrix, m_hCoeffs); ei_tridiagonalization_inplace(m_matrix, m_hCoeffs);
m_isInitialized = true; m_isInitialized = true;
return *this; return *this;
} }
@ -285,43 +285,6 @@ template<typename _MatrixType> class Tridiagonalization
*/ */
const SubDiagonalReturnType subDiagonal() const; const SubDiagonalReturnType subDiagonal() const;
/** \brief Performs a full decomposition in place
*
* \param[in,out] mat On input, the selfadjoint matrix whose tridiagonal
* decomposition is to be computed. On output, the orthogonal matrix Q
* in the decomposition if \p extractQ is true.
* \param[out] diag The diagonal of the tridiagonal matrix T in the
* decomposition.
* \param[out] subdiag The subdiagonal of the tridiagonal matrix T in
* the decomposition.
* \param[in] extractQ If true, the orthogonal matrix Q in the
* decomposition is computed and stored in \p mat.
*
* Compute the tridiagonal matrix of \p mat in place. The tridiagonal
* matrix T is passed to the output parameters \p diag and \p subdiag. If
* \p extractQ is true, then the orthogonal matrix Q is passed to \p mat.
*
* The vectors \p diag and \p subdiag are not resized. The function
* assumes that they are already of the correct size. The length of the
* vector \p diag should equal the number of rows in \p mat, and the
* length of the vector \p subdiag should be one left.
*
* This implementation contains an optimized path for real 3-by-3 matrices
* which is especially useful for plane fitting.
*
* \note Notwithstanding the name, the current implementation copies
* \p mat to a temporary matrix and uses that matrix to compute the
* decomposition.
*
* Example (this uses the same matrix as the example in
* Tridiagonalization(const MatrixType&)):
* \include Tridiagonalization_decomposeInPlace.cpp
* Output: \verbinclude Tridiagonalization_decomposeInPlace.out
*
* \sa Tridiagonalization(const MatrixType&), compute()
*/
static void decomposeInPlace(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, bool extractQ = true);
protected: protected:
static void _compute(MatrixType& matA, CoeffVectorType& hCoeffs); static void _compute(MatrixType& matA, CoeffVectorType& hCoeffs);
@ -368,21 +331,36 @@ Tridiagonalization<MatrixType>::matrixT() const
} }
/** \internal /** \internal
* Performs a tridiagonal decomposition of \a matA in place. * Performs a tridiagonal decomposition of the selfadjoint matrix \a matA in-place.
* *
* \param matA the input selfadjoint matrix * \param[in,out] matA On input the selfadjoint matrix. Only the \b lower triangular part is referenced.
* \param hCoeffs returned Householder coefficients * On output, the strict upper part is left unchanged, and the lower triangular part
* represents the T and Q matrices in packed format has detailed below.
* \param[out] hCoeffs returned Householder coefficients (see below)
* *
* The result is written in the lower triangular part of \a matA. * On output, the tridiagonal selfadjoint matrix T is stored in the diagonal
* and lower sub-diagonal of the matrix \a matA.
* The unitary matrix Q is represented in a compact way as a product of
* Householder reflectors \f$ H_i \f$ such that:
* \f$ Q = H_{N-1} \ldots H_1 H_0 \f$.
* The Householder reflectors are defined as
* \f$ H_i = (I - h_i v_i v_i^T) \f$
* where \f$ h_i = hCoeffs[i]\f$ is the \f$ i \f$th Householder coefficient and
* \f$ v_i \f$ is the Householder vector defined by
* \f$ v_i = [ 0, \ldots, 0, 1, matA(i+2,i), \ldots, matA(N-1,i) ]^T \f$.
* *
* Implemented from Golub's "Matrix Computations", algorithm 8.3.1. * Implemented from Golub's "Matrix Computations", algorithm 8.3.1.
* *
* \sa packedMatrix() * \sa Tridiagonalization::packedMatrix()
*/ */
template<typename MatrixType> template<typename MatrixType, typename CoeffVectorType>
void Tridiagonalization<MatrixType>::_compute(MatrixType& matA, CoeffVectorType& hCoeffs) void ei_tridiagonalization_inplace(MatrixType& matA, CoeffVectorType& hCoeffs)
{ {
assert(matA.rows()==matA.cols()); ei_assert(matA.rows()==matA.cols());
ei_assert(matA.rows()==hCoeffs.size()+1);
typedef typename MatrixType::Index Index;
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::RealScalar RealScalar;
Index n = matA.rows(); Index n = matA.rows();
for (Index i = 0; i<n-1; ++i) for (Index i = 0; i<n-1; ++i)
{ {
@ -408,67 +386,139 @@ void Tridiagonalization<MatrixType>::_compute(MatrixType& matA, CoeffVectorType&
} }
} }
template<typename MatrixType> // forward declaration, implementation at the end of this file
void Tridiagonalization<MatrixType>::decomposeInPlace(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, bool extractQ) template<typename MatrixType, int Size=MatrixType::ColsAtCompileTime>
struct ei_tridiagonalization_inplace_selector;
/** \brief Performs a full tridiagonalization in place
*
* \param[in,out] mat On input, the selfadjoint matrix whose tridiagonal
* decomposition is to be computed. Only the lower triangular part referenced.
* The rest is left unchanged. On output, the orthogonal matrix Q
* in the decomposition if \p extractQ is true.
* \param[out] diag The diagonal of the tridiagonal matrix T in the
* decomposition.
* \param[out] subdiag The subdiagonal of the tridiagonal matrix T in
* the decomposition.
* \param[in] extractQ If true, the orthogonal matrix Q in the
* decomposition is computed and stored in \p mat.
*
* Computes the tridiagonal decomposition of the selfadjoint matrix \p mat in place
* such that \f$ mat = Q T Q^* \f$ where \f$ Q \f$ is unitary and \f$ T \f$ a real
* symmetric tridiagonal matrix.
*
* The tridiagonal matrix T is passed to the output parameters \p diag and \p subdiag. If
* \p extractQ is true, then the orthogonal matrix Q is passed to \p mat. Otherwise the lower
* part of the matrix \p mat is destroyed.
*
* The vectors \p diag and \p subdiag are not resized. The function
* assumes that they are already of the correct size. The length of the
* vector \p diag should equal the number of rows in \p mat, and the
* length of the vector \p subdiag should be one left.
*
* This implementation contains an optimized path for 3-by-3 matrices
* which is especially useful for plane fitting.
*
* \note Currently, it requires two temporary vectors to hold the intermediate
* Householder coefficients, and to reconstruct the matrix Q from the Householder
* reflectors.
*
* Example (this uses the same matrix as the example in
* Tridiagonalization::Tridiagonalization(const MatrixType&)):
* \include Tridiagonalization_decomposeInPlace.cpp
* Output: \verbinclude Tridiagonalization_decomposeInPlace.out
*
* \sa class Tridiagonalization
*/
template<typename MatrixType, typename DiagonalType, typename SubDiagonalType>
void ei_tridiagonalization_inplace(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, bool extractQ)
{ {
typedef typename MatrixType::Index Index;
Index n = mat.rows(); Index n = mat.rows();
ei_assert(mat.cols()==n && diag.size()==n && subdiag.size()==n-1); ei_assert(mat.cols()==n && diag.size()==n && subdiag.size()==n-1);
if (n==3 && (!NumTraits<Scalar>::IsComplex) ) ei_tridiagonalization_inplace_selector<MatrixType>::run(mat, diag, subdiag, extractQ);
{
_decomposeInPlace3x3(mat, diag, subdiag, extractQ);
}
else
{
Tridiagonalization tridiag(mat);
diag = tridiag.diagonal();
subdiag = tridiag.subDiagonal();
if (extractQ)
mat = tridiag.matrixQ();
}
} }
/** \internal /** \internal
* Optimized path for 3x3 matrices. * General full tridiagonalization
*/
template<typename MatrixType, int Size>
struct ei_tridiagonalization_inplace_selector
{
typedef typename Tridiagonalization<MatrixType>::CoeffVectorType CoeffVectorType;
typedef typename Tridiagonalization<MatrixType>::HouseholderSequenceType HouseholderSequenceType;
typedef typename MatrixType::Index Index;
template<typename DiagonalType, typename SubDiagonalType>
static void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, bool extractQ)
{
CoeffVectorType hCoeffs(mat.cols()-1);
ei_tridiagonalization_inplace(mat,hCoeffs);
diag = mat.diagonal().real();
subdiag = mat.template diagonal<-1>().real();
if(extractQ)
mat = HouseholderSequenceType(mat, hCoeffs.conjugate(), false, mat.rows() - 1, 1);
}
};
/** \internal
* Specialization for 3x3 matrices.
* Especially useful for plane fitting. * Especially useful for plane fitting.
*/ */
template<typename MatrixType> template<typename MatrixType>
void Tridiagonalization<MatrixType>::_decomposeInPlace3x3(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, bool extractQ) struct ei_tridiagonalization_inplace_selector<MatrixType,3>
{ {
diag[0] = ei_real(mat(0,0)); typedef typename MatrixType::Scalar Scalar;
RealScalar v1norm2 = ei_abs2(mat(0,2)); typedef typename MatrixType::RealScalar RealScalar;
if (ei_isMuchSmallerThan(v1norm2, RealScalar(1)))
template<typename DiagonalType, typename SubDiagonalType>
static void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, bool extractQ)
{ {
diag[1] = ei_real(mat(1,1)); diag[0] = ei_real(mat(0,0));
diag[2] = ei_real(mat(2,2)); RealScalar v1norm2 = ei_abs2(mat(2,0));
subdiag[0] = ei_real(mat(0,1)); if (ei_isMuchSmallerThan(v1norm2, RealScalar(1)))
subdiag[1] = ei_real(mat(1,2));
if (extractQ)
mat.setIdentity();
}
else
{
RealScalar beta = ei_sqrt(ei_abs2(mat(0,1))+v1norm2);
RealScalar invBeta = RealScalar(1)/beta;
Scalar m01 = mat(0,1) * invBeta;
Scalar m02 = mat(0,2) * invBeta;
Scalar q = RealScalar(2)*m01*mat(1,2) + m02*(mat(2,2) - mat(1,1));
diag[1] = ei_real(mat(1,1) + m02*q);
diag[2] = ei_real(mat(2,2) - m02*q);
subdiag[0] = beta;
subdiag[1] = ei_real(mat(1,2) - m01 * q);
if (extractQ)
{ {
mat(0,0) = 1; diag[1] = ei_real(mat(1,1));
mat(0,1) = 0; diag[2] = ei_real(mat(2,2));
mat(0,2) = 0; subdiag[0] = ei_real(mat(1,0));
mat(1,0) = 0; subdiag[1] = ei_real(mat(2,1));
mat(1,1) = m01; if (extractQ)
mat(1,2) = m02; mat.setIdentity();
mat(2,0) = 0; }
mat(2,1) = m02; else
mat(2,2) = -m01; {
RealScalar beta = ei_sqrt(ei_abs2(mat(1,0)) + v1norm2);
RealScalar invBeta = RealScalar(1)/beta;
Scalar m01 = ei_conj(mat(1,0)) * invBeta;
Scalar m02 = ei_conj(mat(2,0)) * invBeta;
Scalar q = RealScalar(2)*m01*ei_conj(mat(2,1)) + m02*(mat(2,2) - mat(1,1));
diag[1] = ei_real(mat(1,1) + m02*q);
diag[2] = ei_real(mat(2,2) - m02*q);
subdiag[0] = beta;
subdiag[1] = ei_real(ei_conj(mat(2,1)) - m01 * q);
if (extractQ)
{
mat << 1, 0, 0,
0, m01, m02,
0, m02, -m01;
}
} }
} }
} };
/** \internal
* Trivial specialization for 1x1 matrices
*/
template<typename MatrixType>
struct ei_tridiagonalization_inplace_selector<MatrixType,1>
{
typedef typename MatrixType::Scalar Scalar;
template<typename DiagonalType, typename SubDiagonalType>
static void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType&, bool extractQ)
{
diag(0,0) = ei_real(mat(0,0));
if(extractQ)
mat(0,0) = Scalar(1);
}
};
#endif // EIGEN_TRIDIAGONALIZATION_H #endif // EIGEN_TRIDIAGONALIZATION_H

View File

@ -160,18 +160,45 @@ template<typename VectorsType, typename CoeffsType, int Side> class HouseholderS
template<typename DestType> void evalTo(DestType& dst) const template<typename DestType> void evalTo(DestType& dst) const
{ {
Index vecs = m_actualVectors; Index vecs = m_actualVectors;
dst.setIdentity(rows(), rows()); // FIXME find a way to pass this temporary if the user want to
Matrix<Scalar, DestType::RowsAtCompileTime, 1, Matrix<Scalar, DestType::RowsAtCompileTime, 1,
AutoAlign|ColMajor, DestType::MaxRowsAtCompileTime, 1> temp(rows()); AutoAlign|ColMajor, DestType::MaxRowsAtCompileTime, 1> temp(rows());
for(Index k = vecs-1; k >= 0; --k) if( ei_is_same_type<typename ei_cleantype<VectorsType>::type,DestType>::ret
&& ei_extract_data(dst) == ei_extract_data(m_vectors))
{ {
Index cornerSize = rows() - k - m_shift; // in-place
if(m_trans) dst.diagonal().setOnes();
dst.bottomRightCorner(cornerSize, cornerSize) dst.template triangularView<StrictlyUpper>().setZero();
.applyHouseholderOnTheRight(essentialVector(k), m_coeffs.coeff(k), &temp.coeffRef(0)); for(Index k = vecs-1; k >= 0; --k)
else {
dst.bottomRightCorner(cornerSize, cornerSize) Index cornerSize = rows() - k - m_shift;
.applyHouseholderOnTheLeft(essentialVector(k), m_coeffs.coeff(k), &temp.coeffRef(0)); if(m_trans)
dst.bottomRightCorner(cornerSize, cornerSize)
.applyHouseholderOnTheRight(essentialVector(k), m_coeffs.coeff(k), &temp.coeffRef(0));
else
dst.bottomRightCorner(cornerSize, cornerSize)
.applyHouseholderOnTheLeft(essentialVector(k), m_coeffs.coeff(k), &temp.coeffRef(0));
// clear the off diagonal vector
dst.col(k).tail(rows()-k-1).setZero();
}
// clear the remaining columns if needed
for(Index k = 0; k<cols()-vecs ; ++k)
dst.col(k).tail(rows()-k-1).setZero();
}
else
{
dst.setIdentity(rows(), rows());
for(Index k = vecs-1; k >= 0; --k)
{
Index cornerSize = rows() - k - m_shift;
if(m_trans)
dst.bottomRightCorner(cornerSize, cornerSize)
.applyHouseholderOnTheRight(essentialVector(k), m_coeffs.coeff(k), &temp.coeffRef(0));
else
dst.bottomRightCorner(cornerSize, cornerSize)
.applyHouseholderOnTheLeft(essentialVector(k), m_coeffs.coeff(k), &temp.coeffRef(0));
}
} }
} }

View File

@ -50,10 +50,12 @@ template<typename MatrixType> void selfadjointeigensolver(const MatrixType& m)
MatrixType a = MatrixType::Random(rows,cols); MatrixType a = MatrixType::Random(rows,cols);
MatrixType a1 = MatrixType::Random(rows,cols); MatrixType a1 = MatrixType::Random(rows,cols);
MatrixType symmA = a.adjoint() * a + a1.adjoint() * a1; MatrixType symmA = a.adjoint() * a + a1.adjoint() * a1;
symmA.template triangularView<StrictlyUpper>().setZero();
MatrixType b = MatrixType::Random(rows,cols); MatrixType b = MatrixType::Random(rows,cols);
MatrixType b1 = MatrixType::Random(rows,cols); MatrixType b1 = MatrixType::Random(rows,cols);
MatrixType symmB = b.adjoint() * b + b1.adjoint() * b1; MatrixType symmB = b.adjoint() * b + b1.adjoint() * b1;
symmB.template triangularView<StrictlyUpper>().setZero();
SelfAdjointEigenSolver<MatrixType> eiSymm(symmA); SelfAdjointEigenSolver<MatrixType> eiSymm(symmA);
// generalized eigen pb // generalized eigen pb
@ -62,6 +64,9 @@ template<typename MatrixType> void selfadjointeigensolver(const MatrixType& m)
#ifdef HAS_GSL #ifdef HAS_GSL
if (ei_is_same_type<RealScalar,double>::ret) if (ei_is_same_type<RealScalar,double>::ret)
{ {
// restore symmA and symmB.
symmA = MatrixType(symmA.template selfadjointView<Lower>());
symmB = MatrixType(symmB.template selfadjointView<Lower>());
typedef GslTraits<Scalar> Gsl; typedef GslTraits<Scalar> Gsl;
typename Gsl::Matrix gEvec=0, gSymmA=0, gSymmB=0; typename Gsl::Matrix gEvec=0, gSymmA=0, gSymmB=0;
typename GslTraits<RealScalar>::Vector gEval=0; typename GslTraits<RealScalar>::Vector gEval=0;
@ -103,7 +108,7 @@ template<typename MatrixType> void selfadjointeigensolver(const MatrixType& m)
#endif #endif
VERIFY_IS_EQUAL(eiSymm.info(), Success); VERIFY_IS_EQUAL(eiSymm.info(), Success);
VERIFY((symmA * eiSymm.eigenvectors()).isApprox( VERIFY((symmA.template selfadjointView<Lower>() * eiSymm.eigenvectors()).isApprox(
eiSymm.eigenvectors() * eiSymm.eigenvalues().asDiagonal(), largerEps)); eiSymm.eigenvectors() * eiSymm.eigenvalues().asDiagonal(), largerEps));
VERIFY_IS_APPROX(symmA.template selfadjointView<Lower>().eigenvalues(), eiSymm.eigenvalues()); VERIFY_IS_APPROX(symmA.template selfadjointView<Lower>().eigenvalues(), eiSymm.eigenvalues());
@ -113,12 +118,12 @@ template<typename MatrixType> void selfadjointeigensolver(const MatrixType& m)
// generalized eigen problem Ax = lBx // generalized eigen problem Ax = lBx
VERIFY_IS_EQUAL(eiSymmGen.info(), Success); VERIFY_IS_EQUAL(eiSymmGen.info(), Success);
VERIFY((symmA * eiSymmGen.eigenvectors()).isApprox( VERIFY((symmA.template selfadjointView<Lower>() * eiSymmGen.eigenvectors()).isApprox(
symmB * (eiSymmGen.eigenvectors() * eiSymmGen.eigenvalues().asDiagonal()), largerEps)); symmB.template selfadjointView<Lower>() * (eiSymmGen.eigenvectors() * eiSymmGen.eigenvalues().asDiagonal()), largerEps));
MatrixType sqrtSymmA = eiSymm.operatorSqrt(); MatrixType sqrtSymmA = eiSymm.operatorSqrt();
VERIFY_IS_APPROX(symmA, sqrtSymmA*sqrtSymmA); VERIFY_IS_APPROX(MatrixType(symmA.template selfadjointView<Lower>()), sqrtSymmA*sqrtSymmA);
VERIFY_IS_APPROX(sqrtSymmA, symmA*eiSymm.operatorInverseSqrt()); VERIFY_IS_APPROX(sqrtSymmA, symmA.template selfadjointView<Lower>()*eiSymm.operatorInverseSqrt());
MatrixType id = MatrixType::Identity(rows, cols); MatrixType id = MatrixType::Identity(rows, cols);
VERIFY_IS_APPROX(id.template selfadjointView<Lower>().operatorNorm(), RealScalar(1)); VERIFY_IS_APPROX(id.template selfadjointView<Lower>().operatorNorm(), RealScalar(1));