1 ## Copyright (C) 2008-2012 Radek Salac
2 ## Copyright (C) 2012 Carlo de Falco
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
22 ## @deftypefn {Function File} {@var{x} =} cgs (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{M1}, @var{M2}, @var{x0})
23 ## @deftypefnx {Function File} {@var{x} =} cgs (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{P})
24 ## @deftypefnx {Function File} {[@var{x}, @var{flag}, @var{relres}, @var{iter}, @var{resvec}] =} cgs (@var{A}, @var{b}, @dots{})
25 ## Solve @code{A x = b}, where @var{A} is a square matrix, using the
26 ## Conjugate Gradients Squared method.
29 ## @item @var{rtol} is the relative tolerance, if not given or set to []
30 ## the default value 1e-6 is used.
32 ## @item @var{maxit} the maximum number of outer iterations, if not
33 ## given or set to [] the default value @code{min (20, numel (b))} is
36 ## @item @var{x0} the initial guess, if not given or set to [] the
37 ## default value @code{zeros (size (b))} is used.
40 ## @var{A} can be passed as a matrix or as a function handle or
41 ## inline function @code{f} such that @code{f(x) = A*x}.
43 ## The preconditioner @var{P} is given as @code{P = M1 * M2}.
44 ## Both @var{M1} and @var{M2} can be passed as a matrix or as a function
45 ## handle or inline function @code{g} such that @code{g(x) = M1 \ x} or
46 ## @code{g(x) = M2 \ x}.
48 ## If called with more than one output parameter
51 ## @item @var{flag} indicates the exit status:
53 ## @item 0: iteration converged to the within the chosen tolerance
55 ## @item 1: the maximum number of iterations was reached before convergence
57 ## @item 3: the algorithm reached stagnation
59 ## (the value 2 is unused but skipped for compatibility).
61 ## @item @var{relres} is the final value of the relative residual.
63 ## @item @var{iter} is the number of iterations performed.
65 ## @item @var{resvec} is a vector containing the relative residual at
69 ## @seealso{pcg, bicgstab, bicg, gmres}
72 function [x, flag, relres, iter, resvec] = cgs (A, b, tol, maxit, M1, M2, x0)
74 if (nargin >= 2 && nargin <= 7 && isvector (full (b)))
80 elseif (isa (A, "function_handle"))
81 Ax = @(x) feval (A, x);
83 error (["cgs: first argument is expected to "...
84 "be a function or a square matrix"]);
87 if (nargin < 3 || isempty (tol))
91 if (nargin < 4 || isempty (maxit))
92 maxit = min (rows (b), 20);
95 if (nargin < 5 || isempty (M1))
98 M1m1x = str2func (M1);
99 elseif (ismatrix (M1))
101 elseif (isa (M1, "function_handle"))
102 M1m1x = @(x) feval (M1, x);
104 error ("cgs: preconditioner is expected to be a function or matrix");
107 if (nargin < 6 || isempty (M2))
110 M2m1x = str2func (M2);
111 elseif (ismatrix (M2))
113 elseif (isa (M2, "function_handle"))
114 M2m1x = @(x) feval (M2, x);
116 error ("cgs: preconditioner is expected to be a function or matrix");
119 precon = @(x) M2m1x (M1m1x (x));
121 if (nargin < 7 || isempty (x0))
122 x0 = zeros (size (b));
130 ## Vector of the residual norms for each iteration.
131 resvec = norm (res) / norm_b;
133 ## Default behavior we don't reach tolerance tol within maxit iterations.
150 alpha = ro / (p' * q);
153 res = res - alpha * q;
154 relres = norm (res) / norm_b;
155 resvec = [resvec; relres];
158 ## We reach tolerance tol within maxit iterations.
161 elseif (resvec (end) == resvec (end - 1))
162 ## The method stagnates.
170 printf ("cgs converged at iteration %i to a solution with relative residual %e\n",
173 printf (["cgs stopped at iteration %i without converging to the desired tolerance %e\n",
174 "because the method stagnated.\n",
175 "The iterate returned (number %i) has relative residual %e\n"],
176 iter, tol, iter, relres);
178 printf (["cgs stopped at iteration %i without converging to the desired tolerance %e\n",
179 "because the maximum number of iterations was reached.\n",
180 "The iterate returned (number %i) has relative residual %e\n"],
181 iter, tol, iter, relres);
194 %! % Solve system of A*x=b
195 %! A=[5 -1 3;-1 2 -2;3 -2 3]
197 %! [a,b,c,d,e]=cgs(A,b)
203 %! A = spdiags ([-ones(n,1) 4*ones(n,1) -ones(n,1)], -1:1, n, n);
208 %! [x, flag, relres, iter, resvec] = cgs (A, b, tol, maxit, M);
209 %! assert (x, ones (size (b)), 1e-7);
215 %! [x, flag, relres, iter, resvec] = cgs (@(x) A * x, b, tol, maxit, M);
216 %! assert (x, ones (size (b)), 1e-7);
221 %! a = sprand (n, n, .1);
222 %! A = a'*a + 100 * eye (n);
224 %! [x, flag, relres, iter, resvec] = cgs (A, b, tol, [], diag (diag (A)));
225 %! assert (x, ones (size (b)), 1e-7);