7 #ifndef TBCI_SOLVER_GMRES_H
8 #define TBCI_SOLVER_GMRES_H
10 #include "tbci/basics.h"
21 template < typename SysMatrix, typename SysVector >
27 for (
int i = k;
i >= 0; --
i) {
29 for (
int j =
i - 1; j >= 0; --j)
30 y(j) -=
h(j,
i) *
y(
i);
33 for (
int j = 0; j <= k; ++j)
49 INST(template <typename T>
class NN
friend \
50 void GeneratePlaneRotation(
const T &,
const double &,
double &,
T &);)
53 inline void GeneratePlaneRotation(
const T &dx,
const double &
dy,
double &
cs,
T &sn)
64 const double rel =
fabssqr(dx/dy);
65 const double ynorm = 1.0 /
MATH__ sqrt (1.0 + rel);
79 INST(template <typename T>
class NN
friend \
80 void ApplyPlaneRotation(
T &,
T &,
const double &,
const T &);)
83 inline void ApplyPlaneRotation(
T &dx,
T &dy,
const double &cs,
const T &sn)
86 dy = cs * dy - sn * dx;
139 template < typename T, typename SysMatrix, typename SysVector >
140 int GMRES (
const SysMatrix &A, SysVector &x,
const SysVector &
b,
142 unsigned int &max_iter,
double &tol)
144 const unsigned int dim = A.rows();
146 m = dim/8+max_iter/4;
147 if ((
unsigned)m > max_iter)
149 SysVector s(m+1), sn(m+1), w(dim), r(dim);
158 r = M.
solve(b - A * x);
160 const double rel_tol = tol*normb;
162 double beta = r.fabs();
164 if (beta <= rel_tol) {
175 SysVector *V =
new SysVector[m+1];
176 for (j = 0; j <= (unsigned)m; ++j)
180 while (j <= max_iter) {
183 s.fill ((
T)0.0); s(0) = (
T) beta;
186 for (
int i = 0; (
i < m) && (j <= max_iter); ++
i, ++j) {
191 for (k = 0; k <=
i; k++) {
192 H(k, i) =
dot (V[k], w);
199 const REGISTER double normw = w.fabs();
212 for (k = 0; k <
i; k++)
213 ApplyPlaneRotation(H(k,i), H(k+1,i),
cs(k), sn(k));
215 GeneratePlaneRotation(H(i,i), normw,
cs(i), sn(i));
216 ApplyPlaneRotation(H(i,i), H(i+1,i),
cs(i), sn(i));
217 ApplyPlaneRotation(s(i), s(i+1),
cs(i), sn(i));
227 if (beta <= rel_tol) {
232 Update(x, m - 1, H, s, V);
233 r = M.
solve(b - A * x);
const Vector< T > const Vector< T > & x
Abstract base class for all Preconditioners.
const Vector< T > const Vector< T > const Vector< T > int T h
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
int conj(const int arg)
conj for elementary types
const Vector< T > const Vector< T > const Vector< T > int T T & err
T dot(const T &a1, const T &a2)
NAMESPACE_TBCI Vector< T > class NN friend void Update(Vector< T > &, int, BdMatrix< T > &, Vector< T > &, Vector< T > *)
virtual TVector< T > solve(const Vector< T > &) const =0
const Vector< T > Vector< T > Vector< T > Vector< T > & y
double fabssqr(const double a)