mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-09-12 17:33:15 +08:00
Refactor compute() by splitting off two smaller private methods.
This commit is contained in:
parent
e64460d5d0
commit
ed73a195e0
@ -227,6 +227,10 @@ template<typename _MatrixType> class ComplexEigenSolver
|
|||||||
bool m_isInitialized;
|
bool m_isInitialized;
|
||||||
bool m_eigenvectorsOk;
|
bool m_eigenvectorsOk;
|
||||||
EigenvectorType m_matX;
|
EigenvectorType m_matX;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void doComputeEigenvectors(RealScalar matrixnorm);
|
||||||
|
void sortEigenvalues(bool computeEigenvectors);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -235,17 +239,28 @@ ComplexEigenSolver<MatrixType>& ComplexEigenSolver<MatrixType>::compute(const Ma
|
|||||||
{
|
{
|
||||||
// this code is inspired from Jampack
|
// this code is inspired from Jampack
|
||||||
assert(matrix.cols() == matrix.rows());
|
assert(matrix.cols() == matrix.rows());
|
||||||
const Index n = matrix.cols();
|
|
||||||
const RealScalar matrixnorm = matrix.norm();
|
|
||||||
|
|
||||||
// Step 1: Do a complex Schur decomposition, A = U T U^*
|
// Do a complex Schur decomposition, A = U T U^*
|
||||||
// The eigenvalues are on the diagonal of T.
|
// The eigenvalues are on the diagonal of T.
|
||||||
m_schur.compute(matrix, computeEigenvectors);
|
m_schur.compute(matrix, computeEigenvectors);
|
||||||
m_eivalues = m_schur.matrixT().diagonal();
|
m_eivalues = m_schur.matrixT().diagonal();
|
||||||
|
|
||||||
if(computeEigenvectors)
|
if(computeEigenvectors)
|
||||||
|
doComputeEigenvectors(matrix.norm());
|
||||||
|
sortEigenvalues(computeEigenvectors);
|
||||||
|
|
||||||
|
m_isInitialized = true;
|
||||||
|
m_eigenvectorsOk = computeEigenvectors;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename MatrixType>
|
||||||
|
void ComplexEigenSolver<MatrixType>::doComputeEigenvectors(RealScalar matrixnorm)
|
||||||
{
|
{
|
||||||
// Step 2: Compute X such that T = X D X^(-1), where D is the diagonal of T.
|
const Index n = m_eivalues.size();
|
||||||
|
|
||||||
|
// Compute X such that T = X D X^(-1), where D is the diagonal of T.
|
||||||
// The matrix X is unit triangular.
|
// The matrix X is unit triangular.
|
||||||
m_matX = EigenvectorType::Zero(n, n);
|
m_matX = EigenvectorType::Zero(n, n);
|
||||||
for(Index k=n-1 ; k>=0 ; k--)
|
for(Index k=n-1 ; k>=0 ; k--)
|
||||||
@ -268,7 +283,7 @@ ComplexEigenSolver<MatrixType>& ComplexEigenSolver<MatrixType>::compute(const Ma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3: Compute V as V = U X; now A = U T U^* = U X D X^(-1) U^* = V D V^(-1)
|
// Compute V as V = U X; now A = U T U^* = U X D X^(-1) U^* = V D V^(-1)
|
||||||
m_eivec.noalias() = m_schur.matrixU() * m_matX;
|
m_eivec.noalias() = m_schur.matrixU() * m_matX;
|
||||||
// .. and normalize the eigenvectors
|
// .. and normalize the eigenvectors
|
||||||
for(Index k=0 ; k<n ; k++)
|
for(Index k=0 ; k<n ; k++)
|
||||||
@ -277,10 +292,11 @@ ComplexEigenSolver<MatrixType>& ComplexEigenSolver<MatrixType>::compute(const Ma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_isInitialized = true;
|
|
||||||
m_eigenvectorsOk = computeEigenvectors;
|
|
||||||
|
|
||||||
// Step 4: Sort the eigenvalues
|
template<typename MatrixType>
|
||||||
|
void ComplexEigenSolver<MatrixType>::sortEigenvalues(bool computeEigenvectors)
|
||||||
|
{
|
||||||
|
const Index n = m_eivalues.size();
|
||||||
for (Index i=0; i<n; i++)
|
for (Index i=0; i<n; i++)
|
||||||
{
|
{
|
||||||
Index k;
|
Index k;
|
||||||
@ -293,10 +309,7 @@ ComplexEigenSolver<MatrixType>& ComplexEigenSolver<MatrixType>::compute(const Ma
|
|||||||
m_eivec.col(i).swap(m_eivec.col(k));
|
m_eivec.col(i).swap(m_eivec.col(k));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // EIGEN_COMPLEX_EIGEN_SOLVER_H
|
#endif // EIGEN_COMPLEX_EIGEN_SOLVER_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user