10 #ifndef EIGEN_CONJUGATE_GRADIENT_H
11 #define EIGEN_CONJUGATE_GRADIENT_H
26 template<
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
29 const Preconditioner& precond,
Index& iters,
30 typename Dest::RealScalar& tol_error)
34 typedef typename Dest::RealScalar RealScalar;
35 typedef typename Dest::Scalar Scalar;
38 RealScalar tol = tol_error;
39 Index maxIters = iters;
43 VectorType residual = rhs - mat * x;
45 RealScalar rhsNorm2 = rhs.squaredNorm();
54 RealScalar threshold =
numext::maxi(RealScalar(tol*tol*rhsNorm2),considerAsZero);
55 RealScalar residualNorm2 = residual.squaredNorm();
56 if (residualNorm2 < threshold)
59 tol_error =
sqrt(residualNorm2 / rhsNorm2);
64 p = precond.solve(residual);
66 VectorType z(n), tmp(n);
71 tmp.noalias() = mat * p;
73 Scalar alpha = absNew / p.dot(tmp);
75 residual -= alpha * tmp;
77 residualNorm2 = residual.squaredNorm();
78 if(residualNorm2 < threshold)
81 z = precond.solve(residual);
83 RealScalar absOld = absNew;
85 RealScalar beta = absNew / absOld;
89 tol_error =
sqrt(residualNorm2 / rhsNorm2);
95 template<
typename _MatrixType,
int _UpLo=
Lower,
96 typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
97 class ConjugateGradient;
101 template<
typename _MatrixType,
int _UpLo,
typename _Preconditioner>
157 template<
typename _MatrixType,
int _UpLo,
typename _Preconditioner>
168 typedef typename MatrixType::Scalar
Scalar;
191 template<
typename MatrixDerived>
197 template<
typename Rhs,
typename Dest>
203 TransposeInput = (!MatrixWrapper::MatrixFree)
205 && (!MatrixType::IsRowMajor)
213 >::
type SelfAdjointWrapper;
218 RowMajorWrapper row_mat(
matrix());
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
Definition: ArrayCwiseUnaryOps.h:52
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Definition: ArrayCwiseUnaryOps.h:187
EIGEN_DEVICE_FUNC RealReturnType real() const
Definition: CommonCwiseUnaryOps.h:100
#define EIGEN_DONT_INLINE
Definition: Macros.h:940
#define EIGEN_IMPLIES(a, b)
Definition: Macros.h:1315
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
A conjugate gradient solver for sparse (or dense) self-adjoint problems.
Definition: ConjugateGradient.h:159
@ UpLo
Definition: ConjugateGradient.h:173
ComputationInfo m_info
Definition: IterativeSolverBase.h:438
_Preconditioner Preconditioner
Definition: ConjugateGradient.h:170
IterativeSolverBase< ConjugateGradient > Base
Definition: ConjugateGradient.h:160
void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const
Definition: ConjugateGradient.h:198
MatrixType::Scalar Scalar
Definition: ConjugateGradient.h:168
RealScalar m_error
Definition: IterativeSolverBase.h:436
ConjugateGradient()
Definition: ConjugateGradient.h:179
~ConjugateGradient()
Definition: ConjugateGradient.h:194
Index m_iterations
Definition: IterativeSolverBase.h:437
MatrixType::RealScalar RealScalar
Definition: ConjugateGradient.h:169
ConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition: ConjugateGradient.h:192
_MatrixType MatrixType
Definition: ConjugateGradient.h:167
const ActualMatrixType & matrix() const
Definition: IterativeSolverBase.h:419
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:144
internal::generic_matrix_wrapper< MatrixType > MatrixWrapper
Definition: IterativeSolverBase.h:416
Index maxIterations() const
Definition: IterativeSolverBase.h:281
ComputationInfo m_info
Definition: IterativeSolverBase.h:438
MatrixWrapper::ActualMatrixType ActualMatrixType
Definition: IterativeSolverBase.h:417
RealScalar m_error
Definition: IterativeSolverBase.h:436
Preconditioner m_preconditioner
Definition: IterativeSolverBase.h:431
Index m_iterations
Definition: IterativeSolverBase.h:437
bool m_isInitialized
Definition: SparseSolverBase.h:119
RealScalar m_tolerance
Definition: IterativeSolverBase.h:434
ConjugateGradient< _MatrixType, _UpLo, _Preconditioner > & derived()
Definition: SparseSolverBase.h:79
const ActualMatrixType & matrix() const
Definition: IterativeSolverBase.h:419
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:145
Definition: IterativeSolverBase.h:47
@ Lower
Definition: Constants.h:209
@ Upper
Definition: Constants.h:211
@ Success
Definition: Constants.h:442
@ NoConvergence
Definition: Constants.h:446
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:571
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:28
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:1091
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
Definition: document.h:416
Type
Type of JSON value.
Definition: rapidjson.h:644
Definition: EigenBase.h:30
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:233
_MatrixType MatrixType
Definition: ConjugateGradient.h:104
_Preconditioner Preconditioner
Definition: ConjugateGradient.h:105
Definition: ForwardDeclarations.h:17