11#ifndef EIGEN_CONJUGATE_GRADIENT_H
12#define EIGEN_CONJUGATE_GRADIENT_H
15#include "./InternalHeaderCheck.h"
30template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
31EIGEN_DONT_INLINE
void conjugate_gradient(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
const Preconditioner& precond,
32 Index& iters,
typename Dest::RealScalar& tol_error) {
33 using RealScalar =
typename Dest::RealScalar;
34 using Scalar =
typename Dest::Scalar;
38 using VectorType =
typename Dest::PlainObject;
40 RealScalar tol = tol_error;
41 Index maxIters = iters;
45 VectorType residual = rhs - mat * x;
47 RealScalar rhsNorm = rhs.stableNorm();
54 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
55 RealScalar threshold = numext::maxi(RealScalar(tol * rhsNorm), considerAsZero);
56 RealScalar residualNorm = residual.stableNorm();
57 if (residualNorm < threshold) {
59 tol_error = residualNorm / rhsNorm;
64 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
65 residual /= residualScale;
68 threshold = internal::iterative_solver_threshold(tol, RealScalar(rhsNorm / residualScale),
69 internal::iterative_solver_residual_floor<RealScalar>());
72 p = precond.solve(residual);
74 VectorType z(n), tmp(n);
75 RealScalar absNew = numext::real(residual.dot(p));
77 while (i < maxIters) {
78 tmp.noalias() = mat * p;
80 Scalar alpha = absNew / p.dot(tmp);
81 x += (residualScale * alpha) * p;
82 residual -= alpha * tmp;
84 residualNorm = residual.stableNorm();
85 if (residualNorm < threshold)
break;
87 z = precond.solve(residual);
89 RealScalar absOld = absNew;
90 absNew = numext::real(residual.dot(z));
91 RealScalar beta = absNew / absOld;
95 tol_error = residualNorm / (rhsNorm / residualScale);
101template <
typename MatrixType_,
int UpLo_ =
Lower,
107template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
108struct traits<ConjugateGradient<MatrixType_, UpLo_, Preconditioner_> > {
109 using MatrixType = MatrixType_;
110 using Preconditioner = Preconditioner_;
163template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
169 using Base::m_isInitialized;
170 using Base::m_iterations;
174 using MatrixType = MatrixType_;
175 using Scalar =
typename MatrixType::Scalar;
176 using RealScalar =
typename MatrixType::RealScalar;
177 using Preconditioner = Preconditioner_;
179 enum { UpLo = UpLo_ };
195 template <
typename MatrixDerived>
199 template <
typename Rhs,
typename Dest>
200 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
202 using ActualMatrixType =
typename Base::ActualMatrixType;
204 TransposeInput = (!MatrixWrapper::MatrixFree) && (UpLo == (
Lower |
Upper)) && (!MatrixType::IsRowMajor) &&
205 (!NumTraits<Scalar>::IsComplex)
207 using RowMajorWrapper =
208 std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>, ActualMatrixType
const&>;
209 EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree, UpLo == (
Lower |
Upper)),
210 MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
211 using SelfAdjointWrapper =
212 std::conditional_t<UpLo == (
Lower |
Upper), RowMajorWrapper,
213 typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>;
216 m_error = Base::m_tolerance;
218 RowMajorWrapper row_mat(matrix());
219 internal::conjugate_gradient(SelfAdjointWrapper(row_mat), b, x, Base::m_preconditioner, m_iterations, m_error);
A conjugate gradient solver for sparse (or dense) self-adjoint problems.
Definition ConjugateGradient.h:164
ConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition ConjugateGradient.h:196
ConjugateGradient()
Definition ConjugateGradient.h:183
A preconditioner based on the diagonal entries.
Definition BasicPreconditioners.h:40
IterativeSolverBase()
Definition IterativeSolverBase.h:194
Index maxIterations() const
Definition IterativeSolverBase.h:307
Expression of an array as a mathematical vector or matrix.
Definition ArrayWrapper.h:120
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34