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)
63 typedef typename Dest::RealScalar RealScalar;
64 typedef typename Dest::Scalar Scalar;
65 typedef Matrix < Scalar, Dynamic, 1 > VectorType;
66 typedef Matrix < Scalar, Dynamic, Dynamic > FMatrixType;
68 RealScalar tol0 = precond.solve(rhs).norm();
69 RealScalar tol = tol_error * tol0;
70 const int maxIters = iters;
73 const int m = mat.rows();
75 VectorType p0 = rhs - mat*x;
76 VectorType r0 = precond.solve(p0);
79 if(abs(r0.norm()) < tol) {
83 VectorType w = VectorType::Zero(restart + 1);
85 FMatrixType
H = FMatrixType::Zero(m, restart + 1);
86 VectorType tau = VectorType::Zero(restart + 1);
87 std::vector < JacobiRotation < Scalar > > G(restart);
92 r0.makeHouseholder(e, tau.coeffRef(0), beta);
94 H.bottomLeftCorner(m - 1, 1) = e;
96 for (
int k = 1; k <= restart; ++k) {
100 VectorType v = VectorType::Unit(m, k - 1), workspace(m);
103 for (
int i = k - 1; i >= 0; --i) {
104 v.tail(m - i).applyHouseholderOnTheLeft(
H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
112 for (
int i = 0; i < k; ++i) {
113 v.tail(m - i).applyHouseholderOnTheLeft(
H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
116 if (v.tail(m - k).norm() != 0.0) {
121 VectorType e(m - k - 1);
123 v.tail(m - k).makeHouseholder(e, tau.coeffRef(k), beta);
124 H.col(k).tail(m - k - 1) = e;
127 v.tail(m - k).applyHouseholderOnTheLeft(
H.col(k).tail(m - k - 1), tau.coeffRef(k), workspace.data());
133 for (
int i = 0; i < k - 1; ++i) {
135 v.applyOnTheLeft(i, i + 1, G[i].adjoint());
139 if (k<m && v(k) != (Scalar) 0) {
141 G[k - 1].makeGivens(v(k - 1), v(k));
144 v.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
145 w.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
149 H.col(k - 1).head(k) = v.head(k);
151 bool stop=(k==m || abs(w(k)) < tol || iters == maxIters);
153 if (stop || k == restart) {
156 VectorType y = w.head(k);
157 H.topLeftCorner(k, k).template triangularView < Eigen::Upper > ().solveInPlace(y);
160 VectorType x_new = y(k - 1) * VectorType::Unit(m, k - 1);
163 x_new.tail(m - k + 1).applyHouseholderOnTheLeft(
H.col(k - 1).tail(m - k), tau.coeffRef(k - 1), workspace.data());
165 for (
int i = k - 2; i >= 0; --i) {
166 x_new += y(i) * VectorType::Unit(m, i);
168 x_new.tail(m - i).applyHouseholderOnTheLeft(
H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
180 VectorType p1=precond.solve(p0);
183 w = VectorType::Zero(restart + 1);
184 H = FMatrixType::Zero(m, restart + 1);
185 tau = VectorType::Zero(restart + 1);
189 r0.makeHouseholder(e, tau.coeffRef(0), beta);
191 H.bottomLeftCorner(m - 1, 1) = e;
203 #endif // EIGEN_GMRES_H