55 template<
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
56 bool gmres(
const MatrixType & mat,
const Rhs & rhs, Dest & x,
const Preconditioner & precond,
57 Index &iters,
const Index &restart,
typename Dest::RealScalar & tol_error) {
62 typedef typename Dest::RealScalar RealScalar;
63 typedef typename Dest::Scalar Scalar;
67 RealScalar tol = tol_error;
68 const Index maxIters = iters;
71 const Index m = mat.rows();
74 VectorType p0 = rhs - mat*x;
75 VectorType r0 = precond.solve(p0);
77 const RealScalar r0Norm = r0.norm();
87 FMatrixType H = FMatrixType::Zero(m, restart + 1);
88 VectorType w = VectorType::Zero(restart + 1);
89 VectorType tau = VectorType::Zero(restart + 1);
92 std::vector < JacobiRotation < Scalar > > G(restart);
95 VectorType t(m), v(m), workspace(m), x_new(m);
100 r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
103 for (
Index k = 1; k <= restart; ++k)
107 v = VectorType::Unit(m, k - 1);
111 for (
Index i = k - 1; i >= 0; --i) {
112 v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
116 t.noalias() = mat * v;
117 v = precond.solve(t);
121 for (
Index i = 0; i < k; ++i) {
122 v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
125 if (v.tail(m - k).norm() != 0.0)
131 v.tail(m - k).makeHouseholder(Hk_tail, tau.coeffRef(k), beta);
134 v.tail(m - k).applyHouseholderOnTheLeft(Hk_tail, tau.coeffRef(k), workspace.data());
140 for (
Index i = 0; i < k - 1; ++i)
143 v.applyOnTheLeft(i, i + 1, G[i].adjoint());
147 if (k<m && v(k) != (Scalar) 0)
150 G[k - 1].makeGivens(v(k - 1), v(k));
153 v.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
154 w.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
158 H.col(k-1).head(k) = v.head(k);
160 tol_error = abs(w(k)) / r0Norm;
161 bool stop = (k==m || tol_error < tol || iters == maxIters);
163 if (stop || k == restart)
167 H.topLeftCorner(k, k).template triangularView <Upper>().solveInPlace(y);
171 for (
Index i = k - 1; i >= 0; --i)
175 x_new.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
189 p0.noalias() = rhs - mat*x;
190 r0 = precond.solve(p0);
198 r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
210 template<
typename _MatrixType,
216 template<
typename _MatrixType,
typename _Preconditioner>
219 typedef _MatrixType MatrixType;
220 typedef _Preconditioner Preconditioner;
259 template<
typename _MatrixType,
typename _Preconditioner>
265 using Base::m_iterations;
267 using Base::m_isInitialized;
273 using Base::_solve_impl;
274 typedef _MatrixType MatrixType;
275 typedef typename MatrixType::Scalar Scalar;
276 typedef typename MatrixType::RealScalar RealScalar;
277 typedef _Preconditioner Preconditioner;
294 template<
typename MatrixDerived>
309 template<
typename Rhs,
typename Dest>
310 void _solve_with_guess_impl(
const Rhs& b, Dest& x)
const 313 for(
Index j=0; j<b.cols(); ++j)
315 m_iterations = Base::maxIterations();
316 m_error = Base::m_tolerance;
318 typename Dest::ColXpr xj(x,j);
319 if(!
internal::gmres(matrix(), b.col(j), xj, Base::m_preconditioner, m_iterations, m_restart, m_error))
323 : m_error <= Base::m_tolerance ?
Success 325 m_isInitialized =
true;
329 template<
typename Rhs,
typename Dest>
334 _solve_with_guess_impl(b,x.derived());
343 #endif // EIGEN_GMRES_H A preconditioner based on the digonal entries.
Definition: BasicPreconditioners.h:36
GMRES()
Default constructor.
Definition: GMRES.h:282
Index get_restart()
Get the number of iterations after that a restart is performed.
Definition: GMRES.h:301
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
EIGEN_DEVICE_FUNC RealScalar squaredNorm() const
Definition: Dot.h:92
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:28
The provided data did not satisfy the prerequisites.
Definition: Constants.h:434
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
GMRES(const EigenBase< MatrixDerived > &A)
Initialize the solver with matrix A for further Ax=b solving.
Definition: GMRES.h:295
Computation was successful.
Definition: Constants.h:432
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:190
void set_restart(const Index restart)
Set the number of iterations after that a restart is performed.
Definition: GMRES.h:306
Definition: BandTriangularSolver.h:13
A GMRES solver for sparse square problems.
Definition: GMRES.h:212
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:143
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Iterative procedure did not converge.
Definition: Constants.h:436
Definition: ForwardDeclarations.h:17
bool gmres(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond, Index &iters, const Index &restart, typename Dest::RealScalar &tol_error)
Generalized Minimal Residual Algorithm based on the Arnoldi algorithm implemented with Householder re...
Definition: GMRES.h:56