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 int &iters,
const int &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 > FMatrixType;
67 RealScalar tol = tol_error;
68 const int maxIters = iters;
71 const int m = mat.rows();
73 VectorType p0 = rhs - mat*x;
74 VectorType r0 = precond.solve(p0);
77 if(abs(r0.norm()) < tol) {
81 VectorType w = VectorType::Zero(restart + 1);
83 FMatrixType H = FMatrixType::Zero(m, restart + 1);
84 VectorType tau = VectorType::Zero(restart + 1);
85 std::vector < JacobiRotation < Scalar > > G(restart);
90 r0.makeHouseholder(e, tau.coeffRef(0), beta);
92 H.bottomLeftCorner(m - 1, 1) = e;
94 for (
int k = 1; k <= restart; ++k) {
98 VectorType v = VectorType::Unit(m, k - 1), workspace(m);
101 for (
int i = k - 1; i >= 0; --i) {
102 v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
110 for (
int i = 0; i < k; ++i) {
111 v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
114 if (v.tail(m - k).norm() != 0.0) {
119 VectorType e(m - k - 1);
121 v.tail(m - k).makeHouseholder(e, tau.coeffRef(k), beta);
122 H.col(k).tail(m - k - 1) = e;
125 v.tail(m - k).applyHouseholderOnTheLeft(H.col(k).tail(m - k - 1), tau.coeffRef(k), workspace.data());
131 for (
int i = 0; i < k - 1; ++i) {
133 v.applyOnTheLeft(i, i + 1, G[i].adjoint());
137 if (k<m && v(k) != (Scalar) 0) {
139 G[k - 1].makeGivens(v(k - 1), v(k));
142 v.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
143 w.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
148 H.col(k - 1).head(k) = v.head(k);
150 bool stop=(k==m || abs(w(k)) < tol || iters == maxIters);
152 if (stop || k == restart) {
155 VectorType y = w.head(k);
156 H.topLeftCorner(k, k).template triangularView < Eigen::Upper > ().solveInPlace(y);
159 VectorType x_new = y(k - 1) * VectorType::Unit(m, k - 1);
162 x_new.tail(m - k + 1).applyHouseholderOnTheLeft(H.col(k - 1).tail(m - k), tau.coeffRef(k - 1), workspace.data());
164 for (
int i = k - 2; i >= 0; --i) {
165 x_new += y(i) * VectorType::Unit(m, i);
167 x_new.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
179 VectorType p1=precond.solve(p0);
182 w = VectorType::Zero(restart + 1);
183 H = FMatrixType::Zero(m, restart + 1);
184 tau = VectorType::Zero(restart + 1);
188 r0.makeHouseholder(e, tau.coeffRef(0), beta);
190 H.bottomLeftCorner(m - 1, 1) = e;
206 template<
typename _MatrixType,
207 typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
212 template<
typename _MatrixType,
typename _Preconditioner>
213 struct traits<
GMRES<_MatrixType,_Preconditioner> >
215 typedef _MatrixType MatrixType;
216 typedef _Preconditioner Preconditioner;
253 template<
typename _MatrixType,
typename _Preconditioner>
254 class GMRES :
public IterativeSolverBase<GMRES<_MatrixType,_Preconditioner> >
256 typedef IterativeSolverBase<GMRES> Base;
257 using Base::mp_matrix;
259 using Base::m_iterations;
261 using Base::m_isInitialized;
267 typedef _MatrixType MatrixType;
268 typedef typename MatrixType::Scalar Scalar;
269 typedef typename MatrixType::Index Index;
270 typedef typename MatrixType::RealScalar RealScalar;
271 typedef _Preconditioner Preconditioner;
288 template<
typename MatrixDerived>
289 explicit GMRES(
const EigenBase<MatrixDerived>& A) : Base(A.derived()), m_restart(30) {}
307 template<
typename Rhs,
typename Guess>
308 inline const internal::solve_retval_with_guess<GMRES, Rhs, Guess>
311 eigen_assert(m_isInitialized &&
"GMRES is not initialized.");
312 eigen_assert(Base::rows()==b.rows()
313 &&
"GMRES::solve(): invalid number of rows of the right hand side matrix b");
314 return internal::solve_retval_with_guess
315 <
GMRES, Rhs, Guess>(*
this, b.derived(), x0);
319 template<
typename Rhs,
typename Dest>
320 void _solveWithGuess(
const Rhs& b, Dest& x)
const
323 for(
int j=0; j<b.cols(); ++j)
325 m_iterations = Base::maxIterations();
326 m_error = Base::m_tolerance;
328 typename Dest::ColXpr xj(x,j);
329 if(!internal::gmres(*mp_matrix, b.col(j), xj, Base::m_preconditioner, m_iterations, m_restart, m_error))
332 m_info = failed ? NumericalIssue
333 : m_error <= Base::m_tolerance ? Success
335 m_isInitialized =
true;
339 template<
typename Rhs,
typename Dest>
340 void _solve(
const Rhs& b, Dest& x)
const
343 if(x.squaredNorm() == 0)
return;
344 _solveWithGuess(b,x);
354 template<
typename _MatrixType,
typename _Preconditioner,
typename Rhs>
355 struct solve_retval<GMRES<_MatrixType, _Preconditioner>, Rhs>
356 : solve_retval_base<GMRES<_MatrixType, _Preconditioner>, Rhs>
358 typedef GMRES<_MatrixType, _Preconditioner> Dec;
359 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
361 template<typename Dest>
void evalTo(Dest& dst)
const
363 dec()._solve(rhs(),dst);
371 #endif // EIGEN_GMRES_H
GMRES(const EigenBase< MatrixDerived > &A)
Definition: GMRES.h:289
const internal::solve_retval_with_guess< GMRES, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: GMRES.h:309
GMRES()
Definition: GMRES.h:276
void set_restart(const int restart)
Definition: GMRES.h:300
A GMRES solver for sparse square problems.
Definition: GMRES.h:208
int get_restart()
Definition: GMRES.h:295