ConjugateGradient.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
29 template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
30 EIGEN_DONT_INLINE void conjugate_gradient(const MatrixType& mat, const Rhs& rhs, Dest& x, const Preconditioner& precond,
31  Index& iters, typename Dest::RealScalar& tol_error) {
32  typedef typename Dest::RealScalar RealScalar;
33  typedef typename Dest::Scalar Scalar;
35 
36  RealScalar tol = tol_error;
37  Index maxIters = iters;
38 
39  Index n = mat.cols();
40 
41  VectorType residual = rhs - mat * x; // initial residual
42 
43  RealScalar rhsNorm2 = rhs.squaredNorm();
44  if (rhsNorm2 == 0) {
45  x.setZero();
46  iters = 0;
47  tol_error = 0;
48  return;
49  }
50  const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
51  RealScalar threshold = numext::maxi(RealScalar(tol * tol * rhsNorm2), considerAsZero);
52  RealScalar residualNorm2 = residual.squaredNorm();
53  if (residualNorm2 < threshold) {
54  iters = 0;
55  tol_error = numext::sqrt(residualNorm2 / rhsNorm2);
56  return;
57  }
58 
59  VectorType p(n);
60  p = precond.solve(residual); // initial search direction
61 
62  VectorType z(n), tmp(n);
63  RealScalar absNew = numext::real(residual.dot(p)); // the square of the absolute value of r scaled by invM
64  Index i = 0;
65  while (i < maxIters) {
66  tmp.noalias() = mat * p; // the bottleneck of the algorithm
67 
68  Scalar alpha = absNew / p.dot(tmp); // the amount we travel on dir
69  x += alpha * p; // update solution
70  residual -= alpha * tmp; // update residual
71 
72  residualNorm2 = residual.squaredNorm();
73  if (residualNorm2 < threshold) break;
74 
75  z = precond.solve(residual); // approximately solve for "A z = residual"
76 
77  RealScalar absOld = absNew;
78  absNew = numext::real(residual.dot(z)); // update the absolute value of r
79  RealScalar beta = absNew / absOld; // calculate the Gram-Schmidt value used to create the new search direction
80  p = z + beta * p; // update search direction
81  i++;
82  }
83  tol_error = numext::sqrt(residualNorm2 / rhsNorm2);
84  iters = i;
85 }
86 
87 } // namespace internal
88 
89 template <typename MatrixType_, int UpLo_ = Lower,
90  typename Preconditioner_ = DiagonalPreconditioner<typename MatrixType_::Scalar> >
91 class ConjugateGradient;
92 
93 namespace internal {
94 
95 template <typename MatrixType_, int UpLo_, typename Preconditioner_>
96 struct traits<ConjugateGradient<MatrixType_, UpLo_, Preconditioner_> > {
97  typedef MatrixType_ MatrixType;
98  typedef Preconditioner_ Preconditioner;
99 };
100 
101 } // namespace internal
102 
151 template <typename MatrixType_, int UpLo_, typename Preconditioner_>
152 class ConjugateGradient : public IterativeSolverBase<ConjugateGradient<MatrixType_, UpLo_, Preconditioner_> > {
154  using Base::m_error;
155  using Base::m_info;
156  using Base::m_isInitialized;
157  using Base::m_iterations;
158  using Base::matrix;
159 
160  public:
161  typedef MatrixType_ MatrixType;
162  typedef typename MatrixType::Scalar Scalar;
164  typedef Preconditioner_ Preconditioner;
165 
166  enum { UpLo = UpLo_ };
167 
168  public:
171 
182  template <typename MatrixDerived>
184 
186 
188  template <typename Rhs, typename Dest>
189  void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const {
190  typedef typename Base::MatrixWrapper MatrixWrapper;
191  typedef typename Base::ActualMatrixType ActualMatrixType;
192  enum {
193  TransposeInput = (!MatrixWrapper::MatrixFree) && (UpLo == (Lower | Upper)) && (!MatrixType::IsRowMajor) &&
195  };
196  typedef std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>, ActualMatrixType const&>
197  RowMajorWrapper;
198  EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree, UpLo == (Lower | Upper)),
199  MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
200  typedef std::conditional_t<UpLo == (Lower | Upper), RowMajorWrapper,
201  typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>
202  SelfAdjointWrapper;
203 
206 
207  RowMajorWrapper row_mat(matrix());
210  }
211 
212  protected:
213 };
214 
215 } // end namespace Eigen
216 
217 #endif // EIGEN_CONJUGATE_GRADIENT_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define EIGEN_DONT_INLINE
Definition: Macros.h:853
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
float * p
Definition: Tutorial_Map_using.cpp:9
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
A conjugate gradient solver for sparse (or dense) self-adjoint problems.
Definition: ConjugateGradient.h:152
ComputationInfo m_info
Definition: IterativeSolverBase.h:389
IterativeSolverBase< ConjugateGradient > Base
Definition: ConjugateGradient.h:153
MatrixType::Scalar Scalar
Definition: ConjugateGradient.h:162
MatrixType::RealScalar RealScalar
Definition: ConjugateGradient.h:163
ConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition: ConjugateGradient.h:183
RealScalar m_error
Definition: IterativeSolverBase.h:387
ConjugateGradient()
Definition: ConjugateGradient.h:170
Index m_iterations
Definition: IterativeSolverBase.h:388
~ConjugateGradient()
Definition: ConjugateGradient.h:185
void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const
Definition: ConjugateGradient.h:189
MatrixType_ MatrixType
Definition: ConjugateGradient.h:161
const ActualMatrixType & matrix() const
Definition: IterativeSolverBase.h:374
Preconditioner_ Preconditioner
Definition: ConjugateGradient.h:164
@ UpLo
Definition: ConjugateGradient.h:166
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:124
internal::generic_matrix_wrapper< MatrixType > MatrixWrapper
Definition: IterativeSolverBase.h:371
Index maxIterations() const
Definition: IterativeSolverBase.h:251
ComputationInfo m_info
Definition: IterativeSolverBase.h:389
MatrixWrapper::ActualMatrixType ActualMatrixType
Definition: IterativeSolverBase.h:372
RealScalar m_error
Definition: IterativeSolverBase.h:387
Preconditioner m_preconditioner
Definition: IterativeSolverBase.h:382
Index m_iterations
Definition: IterativeSolverBase.h:388
bool m_isInitialized
Definition: SparseSolverBase.h:110
RealScalar m_tolerance
Definition: IterativeSolverBase.h:385
ConjugateGradient< MatrixType_, UpLo_, Preconditioner_ > & derived()
Definition: SparseSolverBase.h:76
const ActualMatrixType & matrix() const
Definition: IterativeSolverBase.h:374
Index cols() const
Definition: SparseMatrix.h:161
Definition: IterativeSolverBase.h:53
float real
Definition: datatypes.h:10
#define min(a, b)
Definition: datatypes.h:22
@ Lower
Definition: Constants.h:211
@ Upper
Definition: Constants.h:213
@ Success
Definition: Constants.h:440
@ NoConvergence
Definition: Constants.h:444
RealScalar alpha
Definition: level1_cplx_impl.h:151
Scalar beta
Definition: level2_cplx_impl.h:36
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
@ Rhs
Definition: TensorContractionMapper.h:20
constexpr bool check_implication(bool a, bool b)
Definition: Meta.h:740
EIGEN_DONT_INLINE void conjugate_gradient(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond, Index &iters, typename Dest::RealScalar &tol_error)
Definition: ConjugateGradient.h:30
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:926
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sqrt(const float &x)
Definition: arch/SSE/MathFunctions.h:69
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
unsigned Preconditioner
----------------------—Domain Properties------------------------—
Definition: space_time_oscillating_cylinder.cc:725
Definition: Eigen_Colamd.h:49
list x
Definition: plotDoE.py:28
Type
Type of JSON value.
Definition: rapidjson.h:513
Definition: EigenBase.h:33
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: ForwardDeclarations.h:21
Definition: fft_test_shared.h:66