mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-04-21 00:59:36 +08:00
Improve robustness of 2x2 eigenvalue with shifting and scaling
This commit is contained in:
parent
7f7e84aa36
commit
95113cb15c
@ -740,10 +740,14 @@ struct direct_selfadjoint_eigenvalues<SolverType,2,false>
|
||||
EigenvectorsType& eivecs = solver.m_eivec;
|
||||
VectorType& eivals = solver.m_eivalues;
|
||||
|
||||
// map the matrix coefficients to [-1:1] to avoid over- and underflow.
|
||||
Scalar scale = mat.cwiseAbs().maxCoeff();
|
||||
scale = numext::maxi(scale,Scalar(1));
|
||||
MatrixType scaledMat = mat / scale;
|
||||
// Shift the matrix to the mean eigenvalue and map the matrix coefficients to [-1:1] to avoid over- and underflow.
|
||||
Scalar shift = mat.trace() / Scalar(2);
|
||||
MatrixType scaledMat = mat;
|
||||
scaledMat.coeffRef(0,1) = mat.coeff(1,0);
|
||||
scaledMat.diagonal().array() -= shift;
|
||||
Scalar scale = scaledMat.cwiseAbs().maxCoeff();
|
||||
if(scale > Scalar(0))
|
||||
scaledMat /= scale;
|
||||
|
||||
// Compute the eigenvalues
|
||||
computeRoots(scaledMat,eivals);
|
||||
@ -778,6 +782,7 @@ struct direct_selfadjoint_eigenvalues<SolverType,2,false>
|
||||
|
||||
// Rescale back to the original size.
|
||||
eivals *= scale;
|
||||
eivals.array() += shift;
|
||||
|
||||
solver.m_info = Success;
|
||||
solver.m_isInitialized = true;
|
||||
|
Loading…
x
Reference in New Issue
Block a user