16#include "./InternalHeaderCheck.h"
60template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
61bool gmres(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
const Preconditioner& precond, Index& iters,
62 const Index& restart_,
typename Dest::RealScalar& tol_error) {
63 using RealScalar =
typename Dest::RealScalar;
64 using Scalar =
typename Dest::Scalar;
65 using VectorType = Matrix<Scalar, Dynamic, 1>;
66 using FMatrixType = Matrix<Scalar, Dynamic, Dynamic, ColMajor>;
68 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
70 if (rhs.norm() <= considerAsZero) {
77 RealScalar tol = tol_error;
78 const Index maxIters = iters;
81 const Index m = mat.rows();
84 const Index restart = numext::mini(numext::maxi(restart_, Index(1)), m);
87 VectorType p0 = rhs - mat * x;
88 VectorType r0 = precond.solve(p0);
90 const RealScalar r0Norm = r0.norm();
93 const RealScalar rhsNorm = numext::maxi((x.squaredNorm() == 0) ? r0Norm : precond.solve(rhs).norm(), considerAsZero);
94 const RealScalar threshold = numext::maxi(tol * rhsNorm, considerAsZero);
97 if (r0Norm < threshold) {
98 tol_error = r0Norm / rhsNorm;
103 FMatrixType H = FMatrixType::Zero(m, restart + 1);
104 VectorType w = VectorType::Zero(restart + 1);
105 VectorType tau = VectorType::Zero(restart + 1);
108 std::vector<JacobiRotation<Scalar> > G(restart);
111 VectorType t(m), v(m), workspace(m), x_new(m);
114 Ref<VectorType> H0_tail = H.col(0).tail(m - 1);
116 r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
119 for (Index k = 1; k <= restart; ++k) {
122 v = VectorType::Unit(m, k - 1);
126 for (Index i = k - 1; i >= 0; --i) {
127 v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
131 t.noalias() = mat * v;
132 v = precond.solve(t);
136 for (Index i = 0; i < k; ++i) {
137 v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
140 if (v.tail(m - k).norm() != 0.0 && k <= restart) {
142 Ref<VectorType> Hk_tail = H.col(k).tail(m - k - 1);
143 v.tail(m - k).makeHouseholder(Hk_tail, tau.coeffRef(k), beta);
146 v.tail(m - k).applyHouseholderOnTheLeft(Hk_tail, tau.coeffRef(k), workspace.data());
150 for (Index i = 0; i < k - 1; ++i) {
151 v.applyOnTheLeft(i, i + 1, G[i].adjoint());
154 if (k < m && v(k) != (Scalar)0) {
156 G[k - 1].makeGivens(v(k - 1), v(k));
159 v.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
160 w.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
164 H.col(k - 1).head(k) = v.head(k);
166 tol_error = numext::abs(w(k)) / rhsNorm;
167 bool stop = (k == m || tol_error < tol || iters == maxIters);
169 if (stop || k == restart) {
171 Ref<VectorType> y = w.head(k);
172 H.topLeftCorner(k, k).template triangularView<Upper>().solveInPlace(y);
176 for (Index i = k - 1; i >= 0; --i) {
179 x_new.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
190 p0.noalias() = rhs - mat * x;
191 r0 = precond.solve(p0);
199 r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
210template <
typename MatrixType_,
typename Preconditioner_ = DiagonalPreconditioner<
typename MatrixType_::Scalar> >
215template <
typename MatrixType_,
typename Preconditioner_>
216struct traits<GMRES<MatrixType_, Preconditioner_> > {
217 using MatrixType = MatrixType_;
218 using Preconditioner = Preconditioner_;
261template <
typename MatrixType_,
typename Preconditioner_>
267 using Base::m_isInitialized;
268 using Base::m_iterations;
272 Index m_restart = 30;
275 using Base::_solve_impl;
276 using MatrixType = MatrixType_;
277 using Scalar =
typename MatrixType::Scalar;
278 using RealScalar =
typename MatrixType::RealScalar;
279 using Preconditioner = Preconditioner_;
295 template <
typename MatrixDerived>
308 template <
typename Rhs,
typename Dest>
309 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
311 m_error = Base::m_tolerance;
312 bool ret = internal::gmres(matrix(), b, x, Base::m_preconditioner, m_iterations, m_restart, m_error);
A GMRES solver for sparse square problems.
Definition GMRES.h:262
GMRES(const EigenBase< MatrixDerived > &A)
Definition GMRES.h:296
Index get_restart() const
Definition GMRES.h:300
GMRES()
Definition GMRES.h:283
void set_restart(const Index restart)
Definition GMRES.h:305
IterativeSolverBase()
Definition IterativeSolverBase.h:136
Index maxIterations() const
Definition IterativeSolverBase.h:246
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34