10 #ifndef EIGEN_CONJUGATE_GRADIENT_H 11 #define EIGEN_CONJUGATE_GRADIENT_H 26 template<
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
28 void conjugate_gradient(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
29 const Preconditioner& precond,
int& iters,
30 typename Dest::RealScalar& tol_error)
34 typedef typename Dest::RealScalar RealScalar;
38 RealScalar tol = tol_error;
43 VectorType residual = rhs - mat * x;
45 RealScalar rhsNorm2 = rhs.squaredNorm();
53 RealScalar threshold = tol*tol*rhsNorm2;
54 RealScalar residualNorm2 = residual.squaredNorm();
55 if (residualNorm2 < threshold)
58 tol_error = sqrt(residualNorm2 / rhsNorm2);
63 p = precond.solve(residual);
65 VectorType z(n), tmp(n);
66 RealScalar absNew = numext::real(residual.dot(p));
70 tmp.noalias() = mat * p;
72 Scalar alpha = absNew / p.dot(tmp);
74 residual -= alpha * tmp;
76 residualNorm2 = residual.squaredNorm();
77 if(residualNorm2 < threshold)
80 z = precond.solve(residual);
82 RealScalar absOld = absNew;
83 absNew = numext::real(residual.dot(z));
84 RealScalar beta = absNew / absOld;
88 tol_error = sqrt(residualNorm2 / rhsNorm2);
94 template<
typename _MatrixType,
int _UpLo=
Lower,
95 typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
100 template<
typename _MatrixType,
int _UpLo,
typename _Preconditioner>
103 typedef _MatrixType MatrixType;
104 typedef _Preconditioner Preconditioner;
146 template<
typename _MatrixType,
int _UpLo,
typename _Preconditioner>
150 using Base::mp_matrix;
152 using Base::m_iterations;
154 using Base::m_isInitialized;
156 typedef _MatrixType MatrixType;
158 typedef typename MatrixType::Index Index;
159 typedef typename MatrixType::RealScalar RealScalar;
160 typedef _Preconditioner Preconditioner;
181 template<
typename MatrixDerived>
191 template<
typename Rhs,
typename Guess>
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");
203 template<
typename Rhs,
typename Dest>
204 void _solveWithGuess(
const Rhs& b, Dest& x)
const 209 >::type MatrixWrapperType;
210 m_iterations = Base::maxIterations();
211 m_error = Base::m_tolerance;
213 for(
int j=0; j<b.cols(); ++j)
215 m_iterations = Base::maxIterations();
216 m_error = Base::m_tolerance;
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);
222 m_isInitialized =
true;
227 template<
typename Rhs,
typename Dest>
228 void _solve(
const Rhs& b, Dest& x)
const 231 _solveWithGuess(b,x);
241 template<
typename _MatrixType,
int _UpLo,
typename _Preconditioner,
typename Rhs>
246 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
248 template<
typename Dest>
void evalTo(Dest& dst)
const 250 dec()._solve(rhs(),dst);
258 #endif // EIGEN_CONJUGATE_GRADIENT_H Definition: gtest_unittest.cc:5031
const internal::solve_retval_with_guess< ConjugateGradient, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: ConjugateGradient.h:193
Definition: ForwardDeclarations.h:124
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
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
ConjugateGradient()
Default constructor.
Definition: ConjugateGradient.h:169
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:26
View matrix as an upper triangular matrix.
Definition: Constants.h:169
Definition: SparseSolve.h:86
Computation was successful.
Definition: Constants.h:376
Definition: BandTriangularSolver.h:13
Definition: ForwardDeclarations.h:125
View matrix as a lower triangular matrix.
Definition: Constants.h:167
ConjugateGradient(const EigenBase< MatrixDerived > &A)
Initialize the solver with matrix A for further Ax=b solving.
Definition: ConjugateGradient.h:182
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:21
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Iterative procedure did not converge.
Definition: Constants.h:380
Definition: ForwardDeclarations.h:17
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48