Eigen  3.2.7
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ConjugateGradient.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011 Gael Guennebaud <[email protected]>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CONJUGATE_GRADIENT_H
11 #define EIGEN_CONJUGATE_GRADIENT_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
26 template<typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
27 EIGEN_DONT_INLINE
28 void conjugate_gradient(const MatrixType& mat, const Rhs& rhs, Dest& x,
29  const Preconditioner& precond, int& iters,
30  typename Dest::RealScalar& tol_error)
31 {
32  using std::sqrt;
33  using std::abs;
34  typedef typename Dest::RealScalar RealScalar;
35  typedef typename Dest::Scalar Scalar;
36  typedef Matrix<Scalar,Dynamic,1> VectorType;
37 
38  RealScalar tol = tol_error;
39  int maxIters = iters;
40 
41  int n = mat.cols();
42 
43  VectorType residual = rhs - mat * x; //initial residual
44 
45  RealScalar rhsNorm2 = rhs.squaredNorm();
46  if(rhsNorm2 == 0)
47  {
48  x.setZero();
49  iters = 0;
50  tol_error = 0;
51  return;
52  }
53  RealScalar threshold = tol*tol*rhsNorm2;
54  RealScalar residualNorm2 = residual.squaredNorm();
55  if (residualNorm2 < threshold)
56  {
57  iters = 0;
58  tol_error = sqrt(residualNorm2 / rhsNorm2);
59  return;
60  }
61 
62  VectorType p(n);
63  p = precond.solve(residual); //initial search direction
64 
65  VectorType z(n), tmp(n);
66  RealScalar absNew = numext::real(residual.dot(p)); // the square of the absolute value of r scaled by invM
67  int i = 0;
68  while(i < maxIters)
69  {
70  tmp.noalias() = mat * p; // the bottleneck of the algorithm
71 
72  Scalar alpha = absNew / p.dot(tmp); // the amount we travel on dir
73  x += alpha * p; // update solution
74  residual -= alpha * tmp; // update residue
75 
76  residualNorm2 = residual.squaredNorm();
77  if(residualNorm2 < threshold)
78  break;
79 
80  z = precond.solve(residual); // approximately solve for "A z = residual"
81 
82  RealScalar absOld = absNew;
83  absNew = numext::real(residual.dot(z)); // update the absolute value of r
84  RealScalar beta = absNew / absOld; // calculate the Gram-Schmidt value used to create the new search direction
85  p = z + beta * p; // update search direction
86  i++;
87  }
88  tol_error = sqrt(residualNorm2 / rhsNorm2);
89  iters = i;
90 }
91 
92 }
93 
94 template< typename _MatrixType, int _UpLo=Lower,
95  typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
97 
98 namespace internal {
99 
100 template< typename _MatrixType, int _UpLo, typename _Preconditioner>
101 struct traits<ConjugateGradient<_MatrixType,_UpLo,_Preconditioner> >
102 {
103  typedef _MatrixType MatrixType;
104  typedef _Preconditioner Preconditioner;
105 };
106 
107 }
108 
146 template< typename _MatrixType, int _UpLo, typename _Preconditioner>
147 class ConjugateGradient : public IterativeSolverBase<ConjugateGradient<_MatrixType,_UpLo,_Preconditioner> >
148 {
149  typedef IterativeSolverBase<ConjugateGradient> Base;
150  using Base::mp_matrix;
151  using Base::m_error;
152  using Base::m_iterations;
153  using Base::m_info;
154  using Base::m_isInitialized;
155 public:
156  typedef _MatrixType MatrixType;
157  typedef typename MatrixType::Scalar Scalar;
158  typedef typename MatrixType::Index Index;
159  typedef typename MatrixType::RealScalar RealScalar;
160  typedef _Preconditioner Preconditioner;
161 
162  enum {
163  UpLo = _UpLo
164  };
165 
166 public:
167 
170 
181  template<typename MatrixDerived>
182  explicit ConjugateGradient(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {}
183 
184  ~ConjugateGradient() {}
185 
191  template<typename Rhs,typename Guess>
192  inline const internal::solve_retval_with_guess<ConjugateGradient, Rhs, Guess>
193  solveWithGuess(const MatrixBase<Rhs>& b, const Guess& x0) const
194  {
195  eigen_assert(m_isInitialized && "ConjugateGradient is not initialized.");
196  eigen_assert(Base::rows()==b.rows()
197  && "ConjugateGradient::solve(): invalid number of rows of the right hand side matrix b");
198  return internal::solve_retval_with_guess
199  <ConjugateGradient, Rhs, Guess>(*this, b.derived(), x0);
200  }
201 
203  template<typename Rhs,typename Dest>
204  void _solveWithGuess(const Rhs& b, Dest& x) const
205  {
206  typedef typename internal::conditional<UpLo==(Lower|Upper),
207  const MatrixType&,
209  >::type MatrixWrapperType;
210  m_iterations = Base::maxIterations();
211  m_error = Base::m_tolerance;
212 
213  for(int j=0; j<b.cols(); ++j)
214  {
215  m_iterations = Base::maxIterations();
216  m_error = Base::m_tolerance;
217 
218  typename Dest::ColXpr xj(x,j);
219  internal::conjugate_gradient(MatrixWrapperType(*mp_matrix), b.col(j), xj, Base::m_preconditioner, m_iterations, m_error);
220  }
221 
222  m_isInitialized = true;
223  m_info = m_error <= Base::m_tolerance ? Success : NoConvergence;
224  }
225 
227  template<typename Rhs,typename Dest>
228  void _solve(const Rhs& b, Dest& x) const
229  {
230  x.setZero();
231  _solveWithGuess(b,x);
232  }
233 
234 protected:
235 
236 };
237 
238 
239 namespace internal {
240 
241 template<typename _MatrixType, int _UpLo, typename _Preconditioner, typename Rhs>
242 struct solve_retval<ConjugateGradient<_MatrixType,_UpLo,_Preconditioner>, Rhs>
243  : solve_retval_base<ConjugateGradient<_MatrixType,_UpLo,_Preconditioner>, Rhs>
244 {
245  typedef ConjugateGradient<_MatrixType,_UpLo,_Preconditioner> Dec;
246  EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
247 
248  template<typename Dest> void evalTo(Dest& dst) const
249  {
250  dec()._solve(rhs(),dst);
251  }
252 };
253 
254 } // end namespace internal
255 
256 } // end namespace Eigen
257 
258 #endif // EIGEN_CONJUGATE_GRADIENT_H
ConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition: ConjugateGradient.h:182
Definition: Constants.h:167
ConjugateGradient()
Definition: ConjugateGradient.h:169
const internal::solve_retval_with_guess< ConjugateGradient, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: ConjugateGradient.h:193
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:49
A conjugate gradient solver for sparse self-adjoint problems.
Definition: ConjugateGradient.h:96
Definition: Constants.h:169
Definition: EigenBase.h:26
Definition: Constants.h:380
Definition: Constants.h:376
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:21
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
int maxIterations() const
Definition: IterativeSolverBase.h:141