Mercurial > hg > octave-nkf
annotate scripts/sparse/cgs.m @ 13076:59def8890992
codesprint: avoid crash after running tests
* cgs.m: Use anonymous function for test.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 03 Sep 2011 16:25:48 -0400 |
parents | 4b110dd204b9 |
children | 1bf456b4e51c |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2008-2011 Radek Salac |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
2 ## Copyright (C) 2011 Carlo de Falco |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
3 ## |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
4 ## This file is part of Octave. |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
5 ## |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
6 ## Octave is free software; you can redistribute it and/or modify it |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
7 ## under the terms of the GNU General Public License as published by |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
9 ## your option) any later version. |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
10 ## |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
11 ## Octave is distributed in the hope that it will be useful, but |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
14 ## General Public License for more details. |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
15 ## |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
16 ## You should have received a copy of the GNU General Public License |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
17 ## along with Octave; see the file COPYING. If not, see |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
18 ## <http://www.gnu.org/licenses/>. |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
19 |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
20 ## -*- texinfo -*- |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
21 ## |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
22 ## @deftypefn {Function File} {@var{x} =} cgs (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{M1}, @var{M2}, @var{x0}) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{x} =} cgs (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{P}) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{x}, @var{flag}, @var{relres}, @var{iter}, @var{resvec}] =} cgs (@var{A}, @var{b}, ...) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
25 ## |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
26 ## Solve @code{A x = b}, where @var{A} is a square matrix, using the Conjugate Gradients Squared method. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
27 ## |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
28 ## @itemize @minus |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
29 ## @item @var{rtol} is the relative tolerance, if not given or set to [] the default value 1e-6 is used. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
30 ## @item @var{maxit} the maximum number of outer iterations, if not given or set to [] the default value @code{min (20, numel (b))} is used. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
31 ## @item @var{x0} the initial guess, if not given or set to [] the default value @code{zeros (size (b))} is used. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
32 ## @end itemize |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
33 ## |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
34 ## @var{A} can be passed as a matrix or as a function handle or |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
35 ## inline function @code{f} such that @code{f(x) = A*x}. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
36 ## |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
37 ## The preconditioner @var{P} is given as @code{P = M1 * M2}. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
38 ## Both @var{M1} and @var{M2} can be passed as a matrix or as a function handle or |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
39 ## inline function @code{g} such that @code{g(x) = M1 \ x} or @code{g(x) = M2 \ x}. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
40 ## |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
41 ## If called with more than one output parameter |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
42 ## |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
43 ## @itemize @minus |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
44 ## @item @var{flag} indicates the exit status: |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
45 ## @itemize @minus |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
46 ## @item 0: iteration converged to the within the chosen tolerance |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
47 ## @item 1: the maximum number of iterations was reached before convergence |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
48 ## @item 3: the algorithm reached stagnation |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
49 ## @end itemize |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
50 ## (the value 2 is unused but skipped for compatibility). |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
51 ## @item @var{relres} is the final value of the relative residual. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
52 ## @item @var{iter} is the number of iterations performed. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
53 ## @item @var{resvec} is a vector containing the relative residual at each iteration. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
54 ## @end itemize |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
55 ## |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
56 ## @seealso{pcg,bicgstab,bicg,gmres} |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
57 ## |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
58 ## @end deftypefn |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
59 |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
60 function [x, flag, relres, iter, resvec] = cgs (A, b, tol, maxit, M1, M2, x0) |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
61 |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
62 if ((nargin >= 2) && (nargin <= 7) && isvector (full (b))) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
63 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
64 if (ischar (A)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
65 A = str2func (A); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
66 elseif (ismatrix (A)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
67 Ax = @(x) A * x; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
68 elseif (isa (A, "function_handle")) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
69 Ax = @(x) feval (A, x); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
70 else |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
71 error (["cgs: first argument is expected to "... |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
72 "be a function or a square matrix"]); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
73 endif |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
74 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
75 if ((nargin < 3) || (isempty (tol))) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
76 tol = 1e-6; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
77 endif |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
78 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
79 if ((nargin < 4) || (isempty (maxit))) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
80 maxit = min (rows (b), 20); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
81 endif |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
82 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
83 if ((nargin < 5) || isempty (M1)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
84 M1m1x = @(x) x; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
85 elseif (ischar (M1)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
86 M1m1x = str2func (M1); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
87 elseif (ismatrix (M1)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
88 M1m1x = @(x) M1 \ x; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
89 elseif (isa (M1, "function_handle")) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
90 M1m1x = @(x) feval (M1, x); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
91 else |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
92 error (["cgs: preconditioner is expected "... |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
93 "to be a function or matrix"]); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
94 endif |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
95 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
96 if ((nargin < 6) || isempty (M2)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
97 M2m1x = @(x) x; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
98 elseif (ischar (M2)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
99 M2m1x = str2func (M2); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
100 elseif (ismatrix (M2)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
101 M2m1x = @(x) M2 \ x; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
102 elseif (isa (M2, "function_handle")) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
103 M2m1x = @(x) feval (M2, x); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
104 else |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
105 error ("cgs: preconditioner is expected to be a function or matrix"); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
106 endif |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
107 |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
108 precon = @(x) M2m1x (M1m1x (x)); |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
109 |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
110 if ((nargin < 7) || (isempty (x0))) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
111 x0 = zeros (size (b)); |
9279
1673a0dc019f
fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents:
9278
diff
changeset
|
112 endif |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
113 |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
114 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
115 x = x0; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
116 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
117 res = b - Ax (x); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
118 norm_b = norm (b); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
119 ## Vector of the residual norms for each iteration. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
120 resvec = [ norm(res)/norm_b ]; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
121 ro = 0; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
122 ## Default behavior we don't reach tolerance tol within maxit iterations. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
123 flag = 1; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
124 for iter = 1 : maxit |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
125 |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
126 z = precon (res); |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
127 |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
128 ## Cache. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
129 ro_old = ro; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
130 ro = res' * z; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
131 if (iter == 1) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
132 p = z; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
133 else |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
134 beta = ro / ro_old; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
135 p = z + beta * p; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
136 endif |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
137 ## Cache. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
138 q = Ax (p); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
139 alpha = ro / (p' * q); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
140 x = x + alpha * p; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
141 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
142 res = res - alpha * q; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
143 relres = norm(res) / norm_b; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
144 resvec = [resvec;relres]; |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
145 |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
146 if (relres <= tol) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
147 ## We reach tolerance tol within maxit iterations. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
148 flag = 0; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
149 break; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
150 elseif (resvec (end) == resvec (end - 1)) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
151 ## The method stagnates. |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
152 flag = 3; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
153 break; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
154 endif |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
155 endfor; |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
156 |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
157 if (nargout < 1) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
158 if ( flag == 0 ) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
159 printf (["cgs converged at iteration %i ", |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
160 "to a solution with relative residual %e\n"],iter,relres); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
161 elseif (flag == 3) |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
162 printf (["cgs stopped at iteration %i ", |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
163 "without converging to the desired tolerance %e\n", |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
164 "because the method stagnated.\n", |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
165 "The iterate returned (number %i) has relative residual %e\n"], |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
166 iter,tol,iter,relres); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
167 else |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
168 printf (["cgs stopped at iteration %i ", |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
169 "without converging to the desired tolerance %e\n", |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
170 "because the maximum number of iterations was reached.\n", |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
171 "The iterate returned (number %i) has relative residual %e\n"], |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
172 iter,tol,iter,relres); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
173 endif |
9278 | 174 endif |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
175 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
176 else |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
177 print_usage (); |
9278 | 178 endif |
179 | |
8338
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
180 endfunction |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
181 |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
182 |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
183 |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
184 %!demo |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
185 %! % Solve system of A*x=b |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
186 %! A=[5 -1 3;-1 2 -2;3 -2 3] |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
187 %! b=[7;-1;4] |
a35bf360b919
Add the cgs and treelayout functions
Radek Salac <salac.r@gmail.com>
parents:
diff
changeset
|
188 %! [a,b,c,d,e]=cgs(A,b) |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
189 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
190 %!shared A, b, n, M |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
191 %! |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
192 %!test |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
193 %! n = 100; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
194 %! A = spdiags ([-ones(n,1) 4*ones(n,1) -ones(n,1)], -1:1, n, n); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
195 %! b = sum (A, 2); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
196 %! tol = 1e-8; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
197 %! maxit = 1000; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
198 %! M = 4*eye (n); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
199 %! [x, flag, relres, iter, resvec] = cgs (A, b, tol, maxit, M); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
200 %! assert (x, ones (size (b)), 1e-7); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
201 %! |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
202 %!test |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
203 %! tol = 1e-8; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
204 %! maxit = 15; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
205 %! |
13076
59def8890992
codesprint: avoid crash after running tests
John W. Eaton <jwe@octave.org>
parents:
13063
diff
changeset
|
206 %! [x, flag, relres, iter, resvec] = cgs (@(x) A * x, b, tol, maxit, M); |
13063
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
207 %! assert (x, ones (size (b)), 1e-7); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
208 |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
209 %!test |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
210 %! n = 100; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
211 %! tol = 1e-8; |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
212 %! a = sprand (n, n, .1); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
213 %! A = a'*a + 100 * eye (n); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
214 %! b = sum (A, 2); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
215 %! [x, flag, relres, iter, resvec] = cgs (A, b, tol, [], diag (diag (A))); |
4b110dd204b9
codesprint: Allow passing a function handle for the coefficient matrix in cgs
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11587
diff
changeset
|
216 %! assert (x, ones (size (b)), 1e-7); |