10 #ifndef EIGEN_CONJUGATE_GRADIENT_H
11 #define EIGEN_CONJUGATE_GRADIENT_H
26 template<
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
28 void conjugate_gradient(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
29 const Preconditioner& precond,
int& iters,
30 typename Dest::RealScalar& tol_error)
34 typedef typename Dest::RealScalar RealScalar;
35 typedef typename Dest::Scalar Scalar;
36 typedef Matrix<Scalar,Dynamic,1> VectorType;
38 RealScalar tol = tol_error;
43 VectorType residual = rhs - mat * x;
45 RealScalar rhsNorm2 = rhs.squaredNorm();
53 RealScalar threshold = tol*tol*rhsNorm2;
54 RealScalar residualNorm2 = residual.squaredNorm();
55 if (residualNorm2 < threshold)
58 tol_error = sqrt(residualNorm2 / rhsNorm2);
63 p = precond.solve(residual);
65 VectorType z(n), tmp(n);
66 RealScalar absNew = numext::real(residual.dot(p));
70 tmp.noalias() = mat * p;
72 Scalar alpha = absNew / p.dot(tmp);
74 residual -= alpha * tmp;
76 residualNorm2 = residual.squaredNorm();
77 if(residualNorm2 < threshold)
80 z = precond.solve(residual);
82 RealScalar absOld = absNew;
83 absNew = numext::real(residual.dot(z));
84 RealScalar beta = absNew / absOld;
88 tol_error = sqrt(residualNorm2 / rhsNorm2);
94 template<
typename _MatrixType,
int _UpLo=
Lower,
95 typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
100 template<
typename _MatrixType,
int _UpLo,
typename _Preconditioner>
103 typedef _MatrixType MatrixType;
104 typedef _Preconditioner Preconditioner;
157 template<
typename _MatrixType,
int _UpLo,
typename _Preconditioner>
158 class ConjugateGradient :
public IterativeSolverBase<ConjugateGradient<_MatrixType,_UpLo,_Preconditioner> >
160 typedef IterativeSolverBase<ConjugateGradient> Base;
161 using Base::mp_matrix;
163 using Base::m_iterations;
165 using Base::m_isInitialized;
167 typedef _MatrixType MatrixType;
168 typedef typename MatrixType::Scalar Scalar;
169 typedef typename MatrixType::Index Index;
170 typedef typename MatrixType::RealScalar RealScalar;
171 typedef _Preconditioner Preconditioner;
201 template<
typename Rhs,
typename Guess>
202 inline const internal::solve_retval_with_guess<ConjugateGradient, Rhs, Guess>
205 eigen_assert(m_isInitialized &&
"ConjugateGradient is not initialized.");
206 eigen_assert(Base::rows()==b.rows()
207 &&
"ConjugateGradient::solve(): invalid number of rows of the right hand side matrix b");
208 return internal::solve_retval_with_guess
213 template<
typename Rhs,
typename Dest>
214 void _solveWithGuess(
const Rhs& b, Dest& x)
const
217 m_error = Base::m_tolerance;
219 for(
int j=0; j<b.cols(); ++j)
222 m_error = Base::m_tolerance;
224 typename Dest::ColXpr xj(x,j);
225 internal::conjugate_gradient(mp_matrix->template selfadjointView<UpLo>(), b.col(j), xj,
226 Base::m_preconditioner, m_iterations, m_error);
229 m_isInitialized =
true;
234 template<
typename Rhs,
typename Dest>
235 void _solve(
const Rhs& b, Dest& x)
const
238 _solveWithGuess(b,x);
248 template<
typename _MatrixType,
int _UpLo,
typename _Preconditioner,
typename Rhs>
249 struct solve_retval<ConjugateGradient<_MatrixType,_UpLo,_Preconditioner>, Rhs>
250 : solve_retval_base<ConjugateGradient<_MatrixType,_UpLo,_Preconditioner>, Rhs>
252 typedef ConjugateGradient<_MatrixType,_UpLo,_Preconditioner> Dec;
253 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
255 template<typename Dest>
void evalTo(Dest& dst)
const
257 dec()._solve(rhs(),dst);
265 #endif // EIGEN_CONJUGATE_GRADIENT_H
A conjugate gradient solver for sparse self-adjoint problems.
Definition: ConjugateGradient.h:96
const internal::solve_retval_with_guess< ConjugateGradient, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: ConjugateGradient.h:203
Definition: Constants.h:167
ConjugateGradient()
Definition: ConjugateGradient.h:180
Definition: Eigen_Colamd.h:54
Definition: Constants.h:380
ConjugateGradient(const MatrixType &A)
Definition: ConjugateGradient.h:192
Definition: Constants.h:376
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
int maxIterations() const
Definition: IterativeSolverBase.h:136