20 #ifndef SELDON_FILE_ITERATIVE_GMRES_CXX
44 #ifdef SELDON_WITH_VIRTUAL
45 template<
class T,
class Vector1>
46 int Gmres(
const VirtualMatrix<T>& A, Vector1& x,
const Vector1& b,
47 Preconditioner_Base<T>& M,
48 Iteration<
typename ClassComplexType<T>::Treal>& outer)
50 template <
class Titer,
class MatrixSparse,
class Vector1,
class Preconditioner>
51 int Gmres(
const MatrixSparse& A, Vector1& x,
const Vector1& b,
55 const int N = A.GetM();
59 typedef typename Vector1::value_type Complexe;
67 std::vector<Vector1> V(m+1, b);
79 Vector1 w(b), r(b), u(b);
81 s.Fill(zero); w.Fill(zero); r.Fill(zero); u.Fill(zero);
83 for (
int i = 0; i < m+1; i++)
86 typedef typename ClassComplexType<Complexe>::Treal Treal;
88 rotations_sin.Fill(zero);
90 rotations_cos.Fill(Treal(0));
95 outer.
MltAdd(-one, A, x, one, w);
101 Treal beta =
Norm2(r);
104 int success_init = outer.
Init(r);
105 if (success_init != 0)
128 H.Reallocate(m+1, m+1);
134 outer.
Mlt(A, V[i], u);
140 for (k = 0; k <= i; k++)
143 H.Val(k, i) = DotProdConj(V[k], w);
144 Add(-H(k,i), V[k], w);
153 Mlt(one/hi_ip1, V[i+1]);
157 for (k = 0; k < i; k++)
159 rotations_cos(k), rotations_sin(k));
165 GenRot(H.Val(i,i), hi_ip1,
166 rotations_cos(i), rotations_sin(i));
170 ApplyRot(s(i), s(i+1), rotations_cos(i), rotations_sin(i));
173 ++inner, ++outer, ++i;
175 }
while (! inner.
Finished(abs(s(i))));
178 H.Resize(i, i); s.Resize(i);
179 Solve(H, s); s.Resize(m+1);
182 for (k = 0; k < i; k++)
187 outer.
MltAdd(-one, A, x, one, w);
200 #define SELDON_FILE_ITERATIVE_GMRES_CXX
void ApplyRot(T &x, T &y, const T &c_, const T &s_)
Rotation of a point in 2-D.
int GetNumberIteration() const
Returns the number of iterations.
int GetRestart() const
Returns the restart parameter.
ClassComplexType< T >::Treal Norm2(const VectorExpression< T, E > &X)
returns 2-norm of an expression with vectors
void SetMaxNumberIteration(int max_iteration)
Changes the maximum number of iterations.
int Init(const Vector1 &r)
Initialization with the right hand side.
void Mlt(const Matrix1 &A, const Vector1 &x, Vector1 &y)
Computes y = A x.
int ErrorCode()
Returns the error code (if an error occured)
int Gmres(const MatrixSparse &A, Vector1 &x, const Vector1 &b, Preconditioner &M, Iteration< Titer > &outer)
Solves a linear system by using Generalized Minimum Residual (GMRES)
void MltAdd(const T1 &alpha, const Matrix1 &A, const Vector1 &x, const T1 &beta, Vector1 &y)
Computes y = beta y + alpha A x.
void SetNumberIteration(int nb)
Changes the number of iterations.
Class containing parameters for an iterative resolution.
bool IsInitGuess_Null() const
Returns true if the initial guess is null.
void GenRot(T &a_in, T &b_in, T &c_, T &s_)
Computation of rotation between two points.
void SetComplexReal(int n, std::complex< T > &number)
Sets a complex number to (n, 0).
bool Finished(const Vector1 &r) const
Returns true if the iterative solver has reached its end.