SOPT
Sparse OPTimisation
Classes | Public Member Functions | List of all members
sopt::ConjugateGradient Class Reference

Solves $Ax = b$ for $x$, given $A$ and $b$. More...

#include <conjugate_gradient.h>

Classes

struct  Diagnostic
 Values indicating how the algorithm ran. More...
 
struct  DiagnosticAndResult
 Values indicating how the algorithm ran and its result;. More...
 

Public Member Functions

 ConjugateGradient (t_uint itermax=std::numeric_limits< t_uint >::max(), t_real tolerance=1e-8)
 Creates conjugate gradient operator. More...
 
virtual ~ConjugateGradient ()
 
template<typename VECTOR , typename T1 , typename T2 >
Diagnostic operator() (VECTOR &x, Eigen::MatrixBase< T1 > const &A, Eigen::MatrixBase< T2 > const &b) const
 Computes $x$ for $Ax=b$. More...
 
template<typename VECTOR , typename T1 , typename T2 >
std::enable_if< not std::is_base_of< Eigen::EigenBase< T1 >, T1 >::value, Diagnostic >::type operator() (VECTOR &x, T1 const &A, Eigen::MatrixBase< T2 > const &b) const
 Computes $x$ for $Ax=b$. More...
 
template<typename T0 , typename A_TYPE >
DiagnosticAndResult< typename T0::Scalaroperator() (A_TYPE const &A, Eigen::MatrixBase< T0 > const &b) const
 Computes $x$ for $Ax=b$. More...
 
t_uint itermax () const
 Maximum number of iterations. More...
 
void itermax (t_uint const &itermax)
 Sets maximum number of iterations. More...
 
t_real tolerance () const
 Tolerance criteria. More...
 
void tolerance (t_real const &tolerance)
 Sets tolerance criteria. More...
 

Detailed Description

Solves $Ax = b$ for $x$, given $A$ and $b$.

Definition at line 14 of file conjugate_gradient.h.

Constructor & Destructor Documentation

◆ ConjugateGradient()

sopt::ConjugateGradient::ConjugateGradient ( t_uint  itermax = std::numeric_limits<t_uint>::max(),
t_real  tolerance = 1e-8 
)
inline

Creates conjugate gradient operator.

Parameters
[in]itermaxMaximum number of iterations. 0 means algorithm breaks only if convergence is reached.
[in]toleranceConvergence criteria

Definition at line 39 of file conjugate_gradient.h.

40  : tolerance_(tolerance), itermax_(itermax) {}
t_real tolerance() const
Tolerance criteria.
t_uint itermax() const
Maximum number of iterations.

◆ ~ConjugateGradient()

virtual sopt::ConjugateGradient::~ConjugateGradient ( )
inlinevirtual

Definition at line 41 of file conjugate_gradient.h.

41 {}

Member Function Documentation

◆ itermax() [1/2]

t_uint sopt::ConjugateGradient::itermax ( ) const
inline

Maximum number of iterations.

0 means algorithm breaks only if convergence is reached.

Definition at line 75 of file conjugate_gradient.h.

75 { return itermax_; }

◆ itermax() [2/2]

void sopt::ConjugateGradient::itermax ( t_uint const &  itermax)
inline

Sets maximum number of iterations.

0 means algorithm breaks only if convergence is reached.

Definition at line 78 of file conjugate_gradient.h.

78 { itermax_ = itermax; }

References itermax().

Referenced by itermax().

◆ operator()() [1/3]

template<typename T0 , typename A_TYPE >
DiagnosticAndResult<typename T0::Scalar> sopt::ConjugateGradient::operator() ( A_TYPE const &  A,
Eigen::MatrixBase< T0 > const &  b 
) const
inline

Computes $x$ for $Ax=b$.

Specialisation where x is constructed during call and returned. And x is a matrix rather than an array.

Definition at line 65 of file conjugate_gradient.h.

66  {
67  DiagnosticAndResult<typename T0::Scalar> result;
68  result.result = Vector<typename T0::Scalar>::Zero(b.size());
69  *static_cast<Diagnostic *>(&result) = operator()(result.result, A, b);
70  return result;
71  }
constexpr Scalar b
sopt::Vector< Scalar > Vector
Definition: inpainting.cc:28

References b, and sopt::ConjugateGradient::DiagnosticAndResult< T >::result.

◆ operator()() [2/3]

template<typename VECTOR , typename T1 , typename T2 >
Diagnostic sopt::ConjugateGradient::operator() ( VECTOR &  x,
Eigen::MatrixBase< T1 > const &  A,
Eigen::MatrixBase< T2 > const &  b 
) const
inline

Computes $x$ for $Ax=b$.

Specialization that converts A from a matrix to a functor. This convertion is only so we write the conjugate-gradient algorithm only once for A as a matrix and A as a functor. A as a functor means A can be a complex operation, e.g. an FFT or two.

Definition at line 49 of file conjugate_gradient.h.

50  {
51  return implementation(x, A, b);
52  }

References b.

◆ operator()() [3/3]

template<typename VECTOR , typename T1 , typename T2 >
std::enable_if<not std::is_base_of<Eigen::EigenBase<T1>, T1>::value, Diagnostic>::type sopt::ConjugateGradient::operator() ( VECTOR &  x,
T1 const &  A,
Eigen::MatrixBase< T2 > const &  b 
) const
inline

Computes $x$ for $Ax=b$.

Specialisation where A is a functor and b and x are matrix-like objects. This is the innermost specialization.

Definition at line 58 of file conjugate_gradient.h.

58  {
59  return implementation(x, details::wrap<typename VECTOR::PlainObject>(A), b);
60  }

References b.

◆ tolerance() [1/2]

t_real sopt::ConjugateGradient::tolerance ( ) const
inline

Tolerance criteria.

Definition at line 80 of file conjugate_gradient.h.

80 { return tolerance_; }

Referenced by main(), and tolerance().

◆ tolerance() [2/2]

void sopt::ConjugateGradient::tolerance ( t_real const &  tolerance)
inline

Sets tolerance criteria.

Definition at line 82 of file conjugate_gradient.h.

82  {
83  if (tolerance <= 0e0) throw std::domain_error("Incorrect tolerance input");
84  tolerance_ = tolerance;
85  }

References tolerance().


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