NiHu  2.0
MyGMRES.hpp
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2012, 2014 Kolja Brix <brix@igpm.rwth-aaachen.de>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_GMRES_H
12 #define EIGEN_GMRES_H
13 
14 namespace Eigen {
15 
16 namespace internal {
17 
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)
58 {
59 
60  using std::sqrt;
61  using std::abs;
62 
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;
67 
68  RealScalar tol0 = precond.solve(rhs).norm();
69  RealScalar tol = tol_error * tol0;
70  const int maxIters = iters;
71  iters = 0;
72 
73  const int m = mat.rows();
74 
75  VectorType p0 = rhs - mat*x;
76  VectorType r0 = precond.solve(p0);
77 
78  // is initial guess already good enough?
79  if(abs(r0.norm()) < tol) {
80  return true;
81  }
82 
83  VectorType w = VectorType::Zero(restart + 1);
84 
85  FMatrixType H = FMatrixType::Zero(m, restart + 1); // Hessenberg matrix
86  VectorType tau = VectorType::Zero(restart + 1);
87  std::vector < JacobiRotation < Scalar > > G(restart);
88 
89  // generate first Householder vector
90  VectorType e(m-1);
91  RealScalar beta;
92  r0.makeHouseholder(e, tau.coeffRef(0), beta);
93  w(0)=(Scalar) beta;
94  H.bottomLeftCorner(m - 1, 1) = e;
95 
96  for (int k = 1; k <= restart; ++k) {
97 
98  ++iters;
99 
100  VectorType v = VectorType::Unit(m, k - 1), workspace(m);
101 
102  // apply Householder reflections H_{1} ... H_{k-1} to v
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());
105  }
106 
107  // apply matrix M to v: v = mat * v;
108  VectorType t=mat*v;
109  v=precond.solve(t);
110 
111  // apply Householder reflections H_{k-1} ... H_{1} to v
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());
114  }
115 
116  if (v.tail(m - k).norm() != 0.0) {
117 
118  if (k <= restart) {
119 
120  // generate new Householder vector
121  VectorType e(m - k - 1);
122  RealScalar beta;
123  v.tail(m - k).makeHouseholder(e, tau.coeffRef(k), beta);
124  H.col(k).tail(m - k - 1) = e;
125 
126  // apply Householder reflection H_{k} to v
127  v.tail(m - k).applyHouseholderOnTheLeft(H.col(k).tail(m - k - 1), tau.coeffRef(k), workspace.data());
128 
129  }
130  }
131 
132  if (k > 1) {
133  for (int i = 0; i < k - 1; ++i) {
134  // apply old Givens rotations to v
135  v.applyOnTheLeft(i, i + 1, G[i].adjoint());
136  }
137  }
138 
139  if (k<m && v(k) != (Scalar) 0) {
140  // determine next Givens rotation
141  G[k - 1].makeGivens(v(k - 1), v(k));
142 
143  // apply Givens rotation to v and w
144  v.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
145  w.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
146  }
147 
148  // insert coefficients into upper matrix triangle
149  H.col(k - 1).head(k) = v.head(k);
150 
151  bool stop=(k==m || abs(w(k)) < tol || iters == maxIters);
152 
153  if (stop || k == restart) {
154 
155  // solve upper triangular system
156  VectorType y = w.head(k);
157  H.topLeftCorner(k, k).template triangularView < Eigen::Upper > ().solveInPlace(y);
158 
159  // use Horner-like scheme to calculate solution vector
160  VectorType x_new = y(k - 1) * VectorType::Unit(m, k - 1);
161 
162  // apply Householder reflection H_{k} to x_new
163  x_new.tail(m - k + 1).applyHouseholderOnTheLeft(H.col(k - 1).tail(m - k), tau.coeffRef(k - 1), workspace.data());
164 
165  for (int i = k - 2; i >= 0; --i) {
166  x_new += y(i) * VectorType::Unit(m, i);
167  // apply Householder reflection H_{i} to x_new
168  x_new.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
169  }
170 
171  x += x_new;
172 
173  if (stop) {
174  return true;
175  } else {
176  k=0;
177 
178  // reset data for a restart r0 = rhs - mat * x;
179  VectorType p0=mat*x;
180  VectorType p1=precond.solve(p0);
181  r0 = rhs - p1;
182 // r0_sqnorm = r0.squaredNorm();
183  w = VectorType::Zero(restart + 1);
184  H = FMatrixType::Zero(m, restart + 1);
185  tau = VectorType::Zero(restart + 1);
186 
187  // generate first Householder vector
188  RealScalar beta;
189  r0.makeHouseholder(e, tau.coeffRef(0), beta);
190  w(0)=(Scalar) beta;
191  H.bottomLeftCorner(m - 1, 1) = e;
192 
193  }
194 
195  }
196  }
197  return false;
198 }
199 
200 }// end namespace internal
201 }// end namespace Eigen
202 
203 #endif // EIGEN_GMRES_H
NiHu::bessel::H
make_complex< T >::type H(T const &z)
H^(K)_nu(z) Bessel function.
Definition: math_functions.hpp:365