11#ifndef EIGEN_LEAST_SQUARE_CONJUGATE_GRADIENT_H
12#define EIGEN_LEAST_SQUARE_CONJUGATE_GRADIENT_H
15#include "./InternalHeaderCheck.h"
30template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
31EIGEN_DONT_INLINE
void least_square_conjugate_gradient(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
32 const Preconditioner& precond, Index& iters,
33 typename Dest::RealScalar& tol_error) {
34 using RealScalar =
typename Dest::RealScalar;
35 using Scalar =
typename Dest::Scalar;
36 using VectorType = Matrix<Scalar, Dynamic, 1>;
38 RealScalar tol = tol_error;
39 Index maxIters = iters;
41 Index m = mat.rows(), n = mat.cols();
43 VectorType residual = rhs - mat * x;
44 VectorType normal_residual = mat.adjoint() * residual;
45 VectorType normal_rhs = mat.adjoint() * rhs;
47 RealScalar rhsNorm = normal_rhs.stableNorm();
54 RealScalar threshold = tol * rhsNorm;
55 RealScalar residualNorm = normal_residual.stableNorm();
56 if (residualNorm == 0 || residualNorm < threshold) {
58 tol_error = residualNorm / rhsNorm;
63 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
64 normal_residual /= residualScale;
65 threshold /= residualScale;
68 p = precond.solve(normal_residual);
70 VectorType z(n), tmp(m);
71 RealScalar absNew = numext::real(normal_residual.dot(p));
73 while (i < maxIters) {
74 tmp.noalias() = mat * p;
76 Scalar alpha = absNew / tmp.squaredNorm();
77 x += (residualScale * alpha) * p;
78 residual -= (residualScale * alpha) * tmp;
79 normal_residual.noalias() = mat.adjoint() * residual;
80 normal_residual /= residualScale;
82 residualNorm = normal_residual.stableNorm();
83 if (residualNorm < threshold)
break;
85 z = precond.solve(normal_residual);
87 RealScalar absOld = absNew;
88 absNew = numext::real(normal_residual.dot(z));
89 RealScalar beta = absNew / absOld;
93 tol_error = residualNorm / (rhsNorm / residualScale);
99template <
typename MatrixType_,
105template <
typename MatrixType_,
typename Preconditioner_>
106struct traits<LeastSquaresConjugateGradient<MatrixType_, Preconditioner_> > {
107 using MatrixType = MatrixType_;
108 using Preconditioner = Preconditioner_;
151template <
typename MatrixType_,
typename Preconditioner_>
153 :
public IterativeSolverBase<LeastSquaresConjugateGradient<MatrixType_, Preconditioner_> > {
158 using Base::m_isInitialized;
159 using Base::m_iterations;
163 using MatrixType = MatrixType_;
164 using Scalar =
typename MatrixType::Scalar;
165 using RealScalar =
typename MatrixType::RealScalar;
166 using Preconditioner = Preconditioner_;
182 template <
typename MatrixDerived>
186 template <
typename Rhs,
typename Dest>
187 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
189 m_error = Base::m_tolerance;
191 internal::least_square_conjugate_gradient(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error);
IterativeSolverBase()
Definition IterativeSolverBase.h:136
Index maxIterations() const
Definition IterativeSolverBase.h:246
Jacobi preconditioner for LeastSquaresConjugateGradient.
Definition BasicPreconditioners.h:122
A conjugate gradient solver for sparse (or dense) least-square problems.
Definition LeastSquareConjugateGradient.h:153
LeastSquaresConjugateGradient()
Definition LeastSquareConjugateGradient.h:170
LeastSquaresConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition LeastSquareConjugateGradient.h:183
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34