55template<
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
56bool 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;
64 typedef Matrix < Scalar, Dynamic, 1 > VectorType;
65 typedef Matrix < Scalar, Dynamic, Dynamic, ColMajor> FMatrixType;
67 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
69 if(rhs.norm() <= considerAsZero)
76 RealScalar tol = tol_error;
77 const Index maxIters = iters;
80 const Index m = mat.rows();
83 VectorType p0 = rhs - mat*x;
84 VectorType r0 = precond.solve(p0);
86 const RealScalar r0Norm = r0.norm();
96 FMatrixType H = FMatrixType::Zero(m, restart + 1);
97 VectorType w = VectorType::Zero(restart + 1);
98 VectorType tau = VectorType::Zero(restart + 1);
101 std::vector < JacobiRotation < Scalar > > G(restart);
104 VectorType t(m), v(m), workspace(m), x_new(m);
107 Ref<VectorType> H0_tail = H.col(0).tail(m - 1);
109 r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
112 for (
Index k = 1; k <= restart; ++k)
116 v = VectorType::Unit(m, k - 1);
120 for (
Index i = k - 1; i >= 0; --i) {
121 v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
125 t.noalias() = mat * v;
126 v = precond.solve(t);
130 for (
Index i = 0; i < k; ++i) {
131 v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
134 if (v.tail(m - k).norm() != 0.0)
139 Ref<VectorType> Hk_tail = H.col(k).tail(m - k - 1);
140 v.tail(m - k).makeHouseholder(Hk_tail, tau.coeffRef(k), beta);
143 v.tail(m - k).applyHouseholderOnTheLeft(Hk_tail, tau.coeffRef(k), workspace.data());
149 for (
Index i = 0; i < k - 1; ++i)
152 v.applyOnTheLeft(i, i + 1, G[i].adjoint());
156 if (k<m && v(k) != (Scalar) 0)
159 G[k - 1].makeGivens(v(k - 1), v(k));
162 v.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
163 w.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
167 H.col(k-1).head(k) = v.head(k);
169 tol_error =
abs(w(k)) / r0Norm;
170 bool stop = (k==m || tol_error < tol || iters == maxIters);
172 if (stop || k == restart)
175 Ref<VectorType> y = w.head(k);
176 H.topLeftCorner(k, k).template triangularView <Upper>().solveInPlace(y);
180 for (
Index i = k - 1; i >= 0; --i)
184 x_new.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
198 p0.noalias() = rhs - mat*x;
199 r0 = precond.solve(p0);
207 r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
219template<
typename _MatrixType,
220 typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
225template<
typename _MatrixType,
typename _Preconditioner>
226struct traits<GMRES<_MatrixType,_Preconditioner> >
228 typedef _MatrixType MatrixType;
229 typedef _Preconditioner Preconditioner;
268template<
typename _MatrixType,
typename _Preconditioner>
274 using Base::m_iterations;
276 using Base::m_isInitialized;
282 using Base::_solve_impl;
283 typedef _MatrixType MatrixType;
284 typedef typename MatrixType::Scalar Scalar;
285 typedef typename MatrixType::RealScalar RealScalar;
286 typedef _Preconditioner Preconditioner;
303 template<
typename MatrixDerived>
318 template<
typename Rhs,
typename Dest>
319 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const
322 m_error = Base::m_tolerance;
323 bool ret = internal::gmres(matrix(), b, x, Base::m_preconditioner, m_iterations, m_restart, m_error);
325 : m_error <= Base::m_tolerance ?
Success
A GMRES solver for sparse square problems.
Definition: GMRES.h:270
GMRES()
Definition: GMRES.h:291
GMRES(const EigenBase< MatrixDerived > &A)
Definition: GMRES.h:304
void set_restart(const Index restart)
Definition: GMRES.h:315
Index get_restart()
Definition: GMRES.h:310
Index maxIterations() const
Namespace containing all symbols from the Eigen library.
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index