Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > Class Template Reference

A conjugate gradient solver for sparse (or dense) self-adjoint problems. More...

+ Inheritance diagram for Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >:

Public Types

enum  { UpLo }
 
typedef MatrixType_ MatrixType
 
typedef Preconditioner_ Preconditioner
 
typedef MatrixType::RealScalar RealScalar
 
typedef MatrixType::Scalar Scalar
 
- Public Types inherited from Eigen::IterativeSolverBase< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > >
enum  
 
typedef internal::traits< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > >::MatrixType MatrixType
 
typedef internal::traits< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > >::Preconditioner Preconditioner
 
typedef MatrixType::RealScalar RealScalar
 
typedef MatrixType::Scalar Scalar
 
typedef MatrixType::StorageIndex StorageIndex
 

Public Member Functions

template<typename Rhs , typename Dest >
void _solve_vector_with_guess_impl (const Rhs &b, Dest &x) const
 
 ConjugateGradient ()
 
template<typename MatrixDerived >
 ConjugateGradient (const EigenBase< MatrixDerived > &A)
 
 ~ConjugateGradient ()
 
- Public Member Functions inherited from Eigen::IterativeSolverBase< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > >
void _solve_impl (const Rhs &b, Dest &x) const
 
std::enable_if_t< Rhs::ColsAtCompileTime!=1 &&DestDerived::ColsAtCompileTime!=1 > _solve_with_guess_impl (const Rhs &b, MatrixBase< DestDerived > &aDest) const
 
std::enable_if_t< Rhs::ColsAtCompileTime==1||DestDerived::ColsAtCompileTime==1 > _solve_with_guess_impl (const Rhs &b, MatrixBase< DestDerived > &dest) const
 
void _solve_with_guess_impl (const Rhs &b, SparseMatrixBase< DestDerived > &aDest) const
 
ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > & analyzePattern (const EigenBase< MatrixDerived > &A)
 
EIGEN_CONSTEXPR Index cols () const EIGEN_NOEXCEPT
 
ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > & compute (const EigenBase< MatrixDerived > &A)
 
ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > & derived ()
 
const ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > & derived () const
 
RealScalar error () const
 
ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > & factorize (const EigenBase< MatrixDerived > &A)
 
ComputationInfo info () const
 
Index iterations () const
 
 IterativeSolverBase ()
 
 IterativeSolverBase (const EigenBase< MatrixDerived > &A)
 
 IterativeSolverBase (IterativeSolverBase &&)=default
 
Index maxIterations () const
 
Preconditionerpreconditioner ()
 
const Preconditionerpreconditioner () const
 
EIGEN_CONSTEXPR Index rows () const EIGEN_NOEXCEPT
 
ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > & setMaxIterations (Index maxIters)
 
ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > & setTolerance (const RealScalar &tolerance)
 
const SolveWithGuess< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >, Rhs, Guess > solveWithGuess (const MatrixBase< Rhs > &b, const Guess &x0) const
 
RealScalar tolerance () const
 
 ~IterativeSolverBase ()
 
- Public Member Functions inherited from Eigen::SparseSolverBase< Derived >
Derived & derived ()
 
const Derived & derived () const
 
template<typename Rhs >
const Solve< Derived, Rhs > solve (const MatrixBase< Rhs > &b) const
 
template<typename Rhs >
const Solve< Derived, Rhs > solve (const SparseMatrixBase< Rhs > &b) const
 
 SparseSolverBase ()
 
 SparseSolverBase (SparseSolverBase &&other)
 
 ~SparseSolverBase ()
 

Private Types

typedef IterativeSolverBase< ConjugateGradientBase
 

Private Member Functions

const ActualMatrixTypematrix () const
 

Private Attributes

RealScalar m_error
 
ComputationInfo m_info
 
Index m_iterations
 

Additional Inherited Members

- Protected Types inherited from Eigen::IterativeSolverBase< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > >
typedef MatrixWrapper::ActualMatrixType ActualMatrixType
 
typedef SparseSolverBase< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > > Base
 
typedef internal::generic_matrix_wrapper< MatrixTypeMatrixWrapper
 
- Protected Member Functions inherited from Eigen::IterativeSolverBase< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > >
void grab (const InputType &A)
 
void init ()
 
const ActualMatrixTypematrix () const
 
- Protected Attributes inherited from Eigen::IterativeSolverBase< ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > >
bool m_analysisIsOk
 
RealScalar m_error
 
bool m_factorizationIsOk
 
ComputationInfo m_info
 
bool m_isInitialized
 
Index m_iterations
 
MatrixWrapper m_matrixWrapper
 
Index m_maxIterations
 
Preconditioner m_preconditioner
 
RealScalar m_tolerance
 
- Protected Attributes inherited from Eigen::SparseSolverBase< Derived >
bool m_isInitialized
 

Detailed Description

template<typename MatrixType_, int UpLo_, typename Preconditioner_>
class Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >

A conjugate gradient solver for sparse (or dense) self-adjoint problems.

This class allows to solve for A.x = b linear problems using an iterative conjugate gradient algorithm. The matrix A must be selfadjoint. The matrix A and the vectors x and b can be either dense or sparse.

Template Parameters
MatrixType_the type of the matrix A, can be a dense or a sparse matrix.
UpLo_the triangular part that will be used for the computations. It can be Lower, Upper, or Lower|Upper in which the full matrix entries will be considered. Default is Lower, best performance is Lower|Upper.
Preconditioner_the type of the preconditioner. Default is DiagonalPreconditioner

This class follows the sparse solver concept .

The maximal number of iterations and tolerance value can be controlled via the setMaxIterations() and setTolerance() methods. The defaults are the size of the problem for the maximal number of iterations and NumTraits<Scalar>::epsilon() for the tolerance.

The tolerance corresponds to the relative residual error: |Ax-b|/|b|

Performance: Even though the default value of UpLo_ is Lower, significantly higher performance is achieved when using a complete matrix and Lower|Upper as the UpLo_ template parameter. Moreover, in this case multi-threading can be exploited if the user code is compiled with OpenMP enabled. See Eigen and multi-threading for details.

This class can be used as the direct solver classes. Here is a typical usage example:

int n = 10000;
VectorXd x(n), b(n);
SparseMatrix<double> A(n,n);
// fill A and b
ConjugateGradient<SparseMatrix<double>, Lower|Upper> cg;
cg.compute(A);
x = cg.solve(b);
std::cout << "#iterations: " << cg.iterations() << std::endl;
std::cout << "estimated error: " << cg.error() << std::endl;
// update b, and solve again
x = cg.solve(b);
Array< int, 3, 1 > b
int n
MatrixXcf A
@ Lower
Definition: Constants.h:211
@ Upper
Definition: Constants.h:213
Matrix< double, Dynamic, 1 > VectorXd
DynamicĂ—1 vector of type double.
Definition: Matrix.h:502

By default the iterations start with x=0 as an initial guess of the solution. One can control the start using the solveWithGuess() method.

ConjugateGradient can also be used in a matrix-free context, see the following example .

See also
class LeastSquaresConjugateGradient, class SimplicialCholesky, DiagonalPreconditioner, IdentityPreconditioner

Definition at line 158 of file ConjugateGradient.h.

Member Typedef Documentation

◆ Base

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
typedef IterativeSolverBase<ConjugateGradient> Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::Base
private

Definition at line 160 of file ConjugateGradient.h.

◆ MatrixType

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
typedef MatrixType_ Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::MatrixType

Definition at line 167 of file ConjugateGradient.h.

◆ Preconditioner

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
typedef Preconditioner_ Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::Preconditioner

Definition at line 170 of file ConjugateGradient.h.

◆ RealScalar

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
typedef MatrixType::RealScalar Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::RealScalar

Definition at line 169 of file ConjugateGradient.h.

◆ Scalar

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
typedef MatrixType::Scalar Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::Scalar

Definition at line 168 of file ConjugateGradient.h.

Member Enumeration Documentation

◆ anonymous enum

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
anonymous enum
Enumerator
UpLo 

Definition at line 172 of file ConjugateGradient.h.

172  {
173  UpLo = UpLo_
174  };

Constructor & Destructor Documentation

◆ ConjugateGradient() [1/2]

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::ConjugateGradient ( )
inline

Default constructor.

Definition at line 179 of file ConjugateGradient.h.

179 : Base() {}
IterativeSolverBase< ConjugateGradient > Base

◆ ConjugateGradient() [2/2]

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
template<typename MatrixDerived >
Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::ConjugateGradient ( const EigenBase< MatrixDerived > &  A)
inlineexplicit

Initialize the solver with matrix A for further Ax=b solving.

This constructor is a shortcut for the default constructor followed by a call to compute().

Warning
this class stores a reference to the matrix A as well as some precomputed values that depend on it. Therefore, if A is changed this class becomes invalid. Call compute() to update it with the new matrix A, or modify a copy of A.

Definition at line 192 of file ConjugateGradient.h.

192 : Base(A.derived()) {}

◆ ~ConjugateGradient()

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::~ConjugateGradient ( )
inline

Definition at line 194 of file ConjugateGradient.h.

194 {}

Member Function Documentation

◆ _solve_vector_with_guess_impl()

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
template<typename Rhs , typename Dest >
void Eigen::ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ >::_solve_vector_with_guess_impl ( const Rhs &  b,
Dest &  x 
) const
inline

Definition at line 198 of file ConjugateGradient.h.

199  {
200  typedef typename Base::MatrixWrapper MatrixWrapper;
201  typedef typename Base::ActualMatrixType ActualMatrixType;
202  enum {
203  TransposeInput = (!MatrixWrapper::MatrixFree)
204  && (UpLo==(Lower|Upper))
205  && (!MatrixType::IsRowMajor)
207  };
208  typedef std::conditional_t<TransposeInput,Transpose<const ActualMatrixType>, ActualMatrixType const&> RowMajorWrapper;
209  EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree,UpLo==(Lower|Upper)),MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
210  typedef std::conditional_t<UpLo==(Lower|Upper),
211  RowMajorWrapper,
212  typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type
213  > SelfAdjointWrapper;
214 
217 
218  RowMajorWrapper row_mat(matrix());
221  }
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
const ActualMatrixType & matrix() const
internal::generic_matrix_wrapper< MatrixType > MatrixWrapper
MatrixWrapper::ActualMatrixType ActualMatrixType
@ Success
Definition: Constants.h:446
@ NoConvergence
Definition: Constants.h:450
constexpr bool check_implication(bool a, bool b)
Definition: Meta.h:579
EIGEN_DONT_INLINE void conjugate_gradient(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond, Index &iters, typename Dest::RealScalar &tol_error)

◆ matrix()

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
const ActualMatrixType& Eigen::IterativeSolverBase< Derived >::matrix
inlineprivate

Definition at line 424 of file IterativeSolverBase.h.

Member Data Documentation

◆ m_error

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
RealScalar Eigen::IterativeSolverBase< Derived >::m_error
mutableprivate

Definition at line 441 of file IterativeSolverBase.h.

◆ m_info

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
ComputationInfo Eigen::IterativeSolverBase< Derived >::m_info
mutableprivate

Definition at line 443 of file IterativeSolverBase.h.

◆ m_iterations

template<typename MatrixType_ , int UpLo_, typename Preconditioner_ >
Index Eigen::IterativeSolverBase< Derived >::m_iterations
mutableprivate

Definition at line 442 of file IterativeSolverBase.h.


The documentation for this class was generated from the following file: