diff options
author | 2010-06-10 16:39:46 +0200 | |
---|---|---|
committer | 2010-06-10 16:39:46 +0200 | |
commit | 469382407ca5d730f23788c593e71e91d24e9b89 (patch) | |
tree | 7c5423e63a9c4b01a25b0edded15cae4d7efb88c /Eigen | |
parent | d2d7465bcfcfc60e2b7350f4e4ae44d809182d39 (diff) |
* 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.
Diffstat (limited to 'Eigen')
-rw-r--r-- | Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h | 34 | ||||
-rw-r--r-- | Eigen/src/Eigenvalues/Tridiagonalization.h | 236 | ||||
-rw-r--r-- | Eigen/src/Householder/HouseholderSequence.h | 47 |
3 files changed, 197 insertions, 120 deletions
diff --git a/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h b/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h index 2878a1494..a77b96186 100644 --- a/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h +++ b/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h @@ -38,7 +38,7 @@ * * \tparam _MatrixType the type of the matrix of which we are computing the * 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 * 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 * 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 * \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. @@ -117,7 +119,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver SelfAdjointEigenSolver() : m_eivec(), m_eivalues(), - m_tridiag(), m_subdiag(), m_isInitialized(false) { } @@ -138,7 +139,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver SelfAdjointEigenSolver(Index size) : m_eivec(size, size), m_eivalues(size), - m_tridiag(size), m_subdiag(size > 1 ? size - 1 : 1), m_isInitialized(false) {} @@ -146,7 +146,7 @@ template<typename _MatrixType> class SelfAdjointEigenSolver /** \brief Constructor; computes eigendecomposition of given matrix. * * \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 * eigenvalues are computed; if false, only the eigenvalues are * computed. @@ -164,7 +164,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver SelfAdjointEigenSolver(const MatrixType& matrix, bool computeEigenvectors = true) : m_eivec(matrix.rows(), matrix.cols()), m_eivalues(matrix.cols()), - m_tridiag(matrix.rows()), m_subdiag(matrix.rows() > 1 ? matrix.rows() - 1 : 1), m_isInitialized(false) { @@ -174,7 +173,9 @@ template<typename _MatrixType> class SelfAdjointEigenSolver /** \brief Constructor; computes generalized eigendecomposition of given 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. + * Only the lower triangular part of the matrix is referenced. * \param[in] computeEigenvectors If true, both the eigenvectors and the * eigenvalues are computed; if false, only the eigenvalues are * computed. @@ -196,7 +197,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver SelfAdjointEigenSolver(const MatrixType& matA, const MatrixType& matB, bool computeEigenvectors = true) : m_eivec(matA.rows(), matA.cols()), m_eivalues(matA.cols()), - m_tridiag(matA.rows()), m_subdiag(matA.rows() > 1 ? matA.rows() - 1 : 1), m_isInitialized(false) { @@ -206,7 +206,7 @@ template<typename _MatrixType> class SelfAdjointEigenSolver /** \brief Computes eigendecomposition of given matrix. * * \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 * eigenvalues are computed; if false, only the eigenvalues are * computed. @@ -240,7 +240,9 @@ template<typename _MatrixType> class SelfAdjointEigenSolver /** \brief Computes generalized eigendecomposition of given 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. + * Only the lower triangular part of the matrix is referenced. * \param[in] computeEigenvectors If true, both the eigenvectors and the * eigenvalues are computed; if false, only the eigenvalues are * computed. @@ -386,7 +388,6 @@ template<typename _MatrixType> class SelfAdjointEigenSolver protected: MatrixType m_eivec; RealVectorType m_eivalues; - TridiagonalizationType m_tridiag; typename TridiagonalizationType::SubDiagonalType m_subdiag; ComputationInfo m_info; bool m_isInitialized; @@ -418,8 +419,6 @@ SelfAdjointEigenSolver<MatrixType>& SelfAdjointEigenSolver<MatrixType>::compute( assert(matrix.cols() == matrix.rows()); Index n = matrix.cols(); m_eivalues.resize(n,1); - if(computeEigenvectors) - m_eivec.resize(n,n); if(n==1) { @@ -432,12 +431,13 @@ SelfAdjointEigenSolver<MatrixType>& SelfAdjointEigenSolver<MatrixType>::compute( return *this; } - m_tridiag.compute(matrix); + // declare some aliases RealVectorType& diag = m_eivalues; - diag = m_tridiag.diagonal(); - m_subdiag = m_tridiag.subDiagonal(); - if (computeEigenvectors) - m_eivec = m_tridiag.matrixQ(); + MatrixType& mat = m_eivec; + + mat = matrix; + m_subdiag.resize(n-1); + ei_tridiagonalization_inplace(mat, diag, m_subdiag, computeEigenvectors); Index end = n-1; Index start = 0; @@ -487,7 +487,7 @@ SelfAdjointEigenSolver<MatrixType>& SelfAdjointEigenSolver<MatrixType>::compute( { std::swap(m_eivalues[i], m_eivalues[k+i]); 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); // compute C = inv(L) A inv(L') - MatrixType matC = matA; + MatrixType matC = matA.template selfadjointView<Lower>(); cholB.matrixL().template solveInPlace<OnTheLeft>(matC); cholB.matrixU().template solveInPlace<OnTheRight>(matC); diff --git a/Eigen/src/Eigenvalues/Tridiagonalization.h b/Eigen/src/Eigenvalues/Tridiagonalization.h index 62a607176..af970e94d 100644 --- a/Eigen/src/Eigenvalues/Tridiagonalization.h +++ b/Eigen/src/Eigenvalues/Tridiagonalization.h @@ -154,7 +154,7 @@ template<typename _MatrixType> class Tridiagonalization { m_matrix = matrix; m_hCoeffs.resize(matrix.rows()-1, 1); - _compute(m_matrix, m_hCoeffs); + ei_tridiagonalization_inplace(m_matrix, m_hCoeffs); m_isInitialized = true; return *this; } @@ -285,43 +285,6 @@ template<typename _MatrixType> class Tridiagonalization */ 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: static void _compute(MatrixType& matA, CoeffVectorType& hCoeffs); @@ -368,21 +331,36 @@ Tridiagonalization<MatrixType>::matrixT() const } /** \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 hCoeffs returned Householder coefficients + * \param[in,out] matA On input the selfadjoint matrix. Only the \b lower triangular part is referenced. + * 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. * - * \sa packedMatrix() + * \sa Tridiagonalization::packedMatrix() */ -template<typename MatrixType> -void Tridiagonalization<MatrixType>::_compute(MatrixType& matA, CoeffVectorType& hCoeffs) +template<typename MatrixType, typename CoeffVectorType> +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(); for (Index i = 0; i<n-1; ++i) { @@ -408,67 +386,139 @@ void Tridiagonalization<MatrixType>::_compute(MatrixType& matA, CoeffVectorType& } } -template<typename MatrixType> -void Tridiagonalization<MatrixType>::decomposeInPlace(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, bool extractQ) +// forward declaration, implementation at the end of this file +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(); ei_assert(mat.cols()==n && diag.size()==n && subdiag.size()==n-1); - if (n==3 && (!NumTraits<Scalar>::IsComplex) ) - { - _decomposeInPlace3x3(mat, diag, subdiag, extractQ); - } - else + ei_tridiagonalization_inplace_selector<MatrixType>::run(mat, diag, subdiag, extractQ); +} + +/** \internal + * 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) { - Tridiagonalization tridiag(mat); - diag = tridiag.diagonal(); - subdiag = tridiag.subDiagonal(); - if (extractQ) - mat = tridiag.matrixQ(); + 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 - * Optimized path for 3x3 matrices. + * Specialization for 3x3 matrices. * Especially useful for plane fitting. */ 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)); - RealScalar v1norm2 = ei_abs2(mat(0,2)); - if (ei_isMuchSmallerThan(v1norm2, RealScalar(1))) - { - diag[1] = ei_real(mat(1,1)); - diag[2] = ei_real(mat(2,2)); - subdiag[0] = ei_real(mat(0,1)); - subdiag[1] = ei_real(mat(1,2)); - if (extractQ) - mat.setIdentity(); - } - else + typedef typename MatrixType::Scalar Scalar; + typedef typename MatrixType::RealScalar RealScalar; + + template<typename DiagonalType, typename SubDiagonalType> + static void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, bool extractQ) { - 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) + diag[0] = ei_real(mat(0,0)); + RealScalar v1norm2 = ei_abs2(mat(2,0)); + if (ei_isMuchSmallerThan(v1norm2, RealScalar(1))) { - mat(0,0) = 1; - mat(0,1) = 0; - mat(0,2) = 0; - mat(1,0) = 0; - mat(1,1) = m01; - mat(1,2) = m02; - mat(2,0) = 0; - mat(2,1) = m02; - mat(2,2) = -m01; + diag[1] = ei_real(mat(1,1)); + diag[2] = ei_real(mat(2,2)); + subdiag[0] = ei_real(mat(1,0)); + subdiag[1] = ei_real(mat(2,1)); + if (extractQ) + mat.setIdentity(); + } + else + { + 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 diff --git a/Eigen/src/Householder/HouseholderSequence.h b/Eigen/src/Householder/HouseholderSequence.h index 835fe6a1c..b33fe5eeb 100644 --- a/Eigen/src/Householder/HouseholderSequence.h +++ b/Eigen/src/Householder/HouseholderSequence.h @@ -160,18 +160,45 @@ template<typename VectorsType, typename CoeffsType, int Side> class HouseholderS template<typename DestType> void evalTo(DestType& dst) const { 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, - AutoAlign|ColMajor, DestType::MaxRowsAtCompileTime, 1> temp(rows()); - for(Index k = vecs-1; k >= 0; --k) + AutoAlign|ColMajor, DestType::MaxRowsAtCompileTime, 1> temp(rows()); + 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; - 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)); + // in-place + dst.diagonal().setOnes(); + dst.template triangularView<StrictlyUpper>().setZero(); + 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)); + + // 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)); + } } } |