Handle zero right hand side in CG and GMRES

This commit is contained in:
Desire NUENTSA 2013-03-20 11:22:45 +01:00
parent 9bfeeba1c5
commit 4107b371e3
2 changed files with 8 additions and 1 deletions

View File

@ -48,6 +48,12 @@ void conjugate_gradient(const MatrixType& mat, const Rhs& rhs, Dest& x,
VectorType z(n), tmp(n); VectorType z(n), tmp(n);
RealScalar absNew = internal::real(residual.dot(p)); // the square of the absolute value of r scaled by invM RealScalar absNew = internal::real(residual.dot(p)); // the square of the absolute value of r scaled by invM
RealScalar rhsNorm2 = rhs.squaredNorm(); RealScalar rhsNorm2 = rhs.squaredNorm();
// Check Zero right hand side
if(!rhsNorm2)
{
x.setZero();
return;
}
RealScalar residualNorm2 = 0; RealScalar residualNorm2 = 0;
RealScalar threshold = tol*tol*rhsNorm2; RealScalar threshold = tol*tol*rhsNorm2;
int i = 0; int i = 0;

View File

@ -348,7 +348,8 @@ public:
template<typename Rhs,typename Dest> template<typename Rhs,typename Dest>
void _solve(const Rhs& b, Dest& x) const void _solve(const Rhs& b, Dest& x) const
{ {
x.setZero(); x = b;
if(!x.squaredNorm()) return; // Check Zero right hand side
_solveWithGuess(b,x); _solveWithGuess(b,x);
} }