fix division by zero if the matrix is exactly zero

This commit is contained in:
Gael Guennebaud 2011-02-17 19:39:57 +01:00
parent b8ef48c46d
commit 9195a224f3

View File

@ -402,6 +402,7 @@ SelfAdjointEigenSolver<MatrixType>& SelfAdjointEigenSolver<MatrixType>
// map the matrix coefficients to [-1:1] to avoid over- and underflow.
RealScalar scale = matrix.cwiseAbs().maxCoeff();
if(scale==Scalar(0)) scale = 1;
mat = matrix / scale;
m_subdiag.resize(n-1);
internal::tridiagonalization_inplace(mat, diag, m_subdiag, computeEigenvectors);
@ -471,12 +472,17 @@ namespace internal {
template<int StorageOrder,typename RealScalar, typename Scalar, typename Index>
static void tridiagonal_qr_step(RealScalar* diag, RealScalar* subdiag, Index start, Index end, Scalar* matrixQ, Index n)
{
// NOTE this version avoids over & underflow, however since the matrix is prescaled, overflow cannot occur,
// and underflows should be meaningless anyway. So I don't any reason to enable this version, but I keep
// it here for reference:
// RealScalar td = (diag[end-1] - diag[end])*RealScalar(0.5);
// RealScalar e = subdiag[end-1];
// RealScalar mu = diag[end] - (e / (td + (td>0 ? 1 : -1))) * (e / hypot(td,e));
RealScalar td = (diag[end-1] - diag[end])*RealScalar(0.5);
RealScalar e2 = abs2(subdiag[end-1]);
RealScalar mu = diag[end] - e2 / (td + (td>0 ? 1 : -1) * sqrt(td*td + e2));
RealScalar x = diag[start] - mu;
RealScalar z = subdiag[start];
for (Index k = start; k < end; ++k)
{
JacobiRotation<RealScalar> rot;
@ -490,6 +496,7 @@ static void tridiagonal_qr_step(RealScalar* diag, RealScalar* subdiag, Index sta
diag[k+1] = rot.s() * sdk + rot.c() * dkp1;
subdiag[k] = rot.c() * sdk - rot.s() * dkp1;
if (k > start)
subdiag[k - 1] = rot.c() * subdiag[k-1] - rot.s() * z;