4
/// This file is part of Rheolef.
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
8
/// Rheolef is free software; you can redistribute it and/or modify
9
/// it under the terms of the GNU General Public License as published by
10
/// the Free Software Foundation; either version 2 of the License, or
11
/// (at your option) any later version.
13
/// Rheolef is distributed in the hope that it will be useful,
14
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
/// GNU General Public License for more details.
18
/// You should have received a copy of the GNU General Public License
19
/// along with Rheolef; if not, write to the Free Software
20
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
/// =========================================================================
24
NAME: cgs - cgs-conjugate gradient method
27
"int cgs" ("const Matrix &A, Vector &x, const Vector &b",
28
| const Preconditioner &M, int &max_iter, Real &tol);
32
The simplest call to 'cgs' has the folling form:
34
| int status = cgs(a, x, b, EYE, 100, 1e-7);
38
'cgs' solves the unsymmetric linear system Ax = b
39
using the conjugate gradient squared method.
41
The return value indicates convergence within max_iter (input)
42
iterations (0), or no convergence within max_iter iterations (1).
43
Upon successful return, output arguments have the following values:
45
"x" approximate solution to Ax = b
47
"max_iter" the number of iterations performed before the tolerance was reached
49
"tol" the residual after the final iteration
53
"cg", "bicg", "bichstab", class "csr", class "vec"
57
'cgs' is an iterative template routine.
59
'cgs' follows the algorithm described on p. 26 in
61
Templates for the Solution of Linear Systems: Building Blocks for Iterative Methods,
63
R. Barrett, M. Berry, T. F. Chan, J. Demmel, J. Donato, J. Dongarra, V. Eijkhout,
64
R. Pozo, C. Romine, H. Van der Vorst,
65
SIAM, 1994, "ftp.netlib.org/templates/templates.ps".
67
The present implementation is inspired from "IML++ 1.2" iterative method library,
68
"http://math.nist.gov/iml++".
73
| Pierre.Saramito@imag.fr
74
LMC-IMAG, 38041 Grenoble cedex 9, France
83
// ========== [ method implementation ] ====================================
86
template < class Matrix, class Vector, class Preconditioner, class Real >
88
cgs(const Matrix &A, Vector &x, const Vector &b,
89
const Preconditioner &M, int &max_iter, Real &tol)
92
Real rho_1, rho_2=0, alpha, beta;
93
Vector p, phat, q, qhat, vhat, u, uhat;
102
if ((resid = norm(r) / normb) <= tol) {
108
for (int i = 1; i <= max_iter; i++) {
109
rho_1 = dot(rtilde, r);
110
if (rho_1 == Real(0)) {
111
tol = norm(r) / normb;
118
beta = rho_1 / rho_2;
120
p = u + beta * (q + beta * p);
124
alpha = rho_1 / dot(rtilde, vhat);
125
q = u - alpha * vhat;
126
uhat = M.solve(u + q);
131
if ((resid = norm(r) / normb) < tol) {
142
#endif // _SKIT_CGS_H