diff options
author | Desire NUENTSA <desire.nuentsa_wakam@inria.fr> | 2013-03-20 16:15:18 +0100 |
---|---|---|
committer | Desire NUENTSA <desire.nuentsa_wakam@inria.fr> | 2013-03-20 16:15:18 +0100 |
commit | da6219b19dd92231cd0afe380ae4880b62bfe88d (patch) | |
tree | 36babeb2074be95e585fa450b571c351d76fe2bd /Eigen/src/IterativeLinearSolvers/ConjugateGradient.h | |
parent | 22460edb49885a60672f1ab29e71c6dd7f89d197 (diff) |
Bug567 : Fix iterative solvers to immediately return when the initial guess is the true solution and for trivial solution
Diffstat (limited to 'Eigen/src/IterativeLinearSolvers/ConjugateGradient.h')
-rw-r--r-- | Eigen/src/IterativeLinearSolvers/ConjugateGradient.h | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/Eigen/src/IterativeLinearSolvers/ConjugateGradient.h b/Eigen/src/IterativeLinearSolvers/ConjugateGradient.h index eadf711e5..00b5647c6 100644 --- a/Eigen/src/IterativeLinearSolvers/ConjugateGradient.h +++ b/Eigen/src/IterativeLinearSolvers/ConjugateGradient.h @@ -41,21 +41,29 @@ void conjugate_gradient(const MatrixType& mat, const Rhs& rhs, Dest& x, int n = mat.cols(); VectorType residual = rhs - mat * x; //initial residual - VectorType p(n); - - p = precond.solve(residual); //initial search direction - VectorType z(n), tmp(n); - RealScalar absNew = internal::real(residual.dot(p)); // the square of the absolute value of r scaled by invM RealScalar rhsNorm2 = rhs.squaredNorm(); - // Check Zero right hand side - if(!rhsNorm2) + if(rhsNorm2 == 0) { x.setZero(); + iters = 0; + tol_error = 0; return; } - RealScalar residualNorm2 = 0; RealScalar threshold = tol*tol*rhsNorm2; + RealScalar residualNorm2 = residual.squaredNorm(); + if (residualNorm2 < threshold) + { + iters = 0; + tol_error = sqrt(residualNorm2 / rhsNorm2); + return; + } + + VectorType p(n); + p = precond.solve(residual); //initial search direction + + VectorType z(n), tmp(n); + RealScalar absNew = internal::real(residual.dot(p)); // the square of the absolute value of r scaled by invM int i = 0; while(i < maxIters) { |