annotate scripts/sparse/qmr.m @ 20164:df437a52bcaf stable

doc: Update more docstrings to have one sentence summary as first line. Reviewed miscellaneous, sparse, strings in scripts directory. * scripts/miscellaneous/bzip2.m, scripts/miscellaneous/citation.m, scripts/miscellaneous/compare_versions.m, scripts/miscellaneous/computer.m, scripts/miscellaneous/debug.m, scripts/miscellaneous/dir.m, scripts/miscellaneous/edit.m, scripts/miscellaneous/error_ids.m, scripts/miscellaneous/fileattrib.m, scripts/miscellaneous/fullfile.m, scripts/miscellaneous/genvarname.m, scripts/miscellaneous/gzip.m, scripts/miscellaneous/mkoctfile.m, scripts/miscellaneous/news.m, scripts/miscellaneous/open.m, scripts/miscellaneous/parseparams.m, scripts/miscellaneous/recycle.m, scripts/miscellaneous/run.m, scripts/miscellaneous/swapbytes.m, scripts/miscellaneous/tar.m, scripts/miscellaneous/tmpnam.m, scripts/miscellaneous/unpack.m, scripts/miscellaneous/what.m, scripts/sparse/bicg.m, scripts/sparse/bicgstab.m, scripts/sparse/cgs.m, scripts/sparse/colperm.m, scripts/sparse/eigs.m, scripts/sparse/etreeplot.m, scripts/sparse/gmres.m, scripts/sparse/gplot.m, scripts/sparse/ichol.m, scripts/sparse/ilu.m, scripts/sparse/pcg.m, scripts/sparse/pcr.m, scripts/sparse/qmr.m, scripts/sparse/spaugment.m, scripts/sparse/spconvert.m, scripts/sparse/spdiags.m, scripts/sparse/spfun.m, scripts/sparse/spones.m, scripts/sparse/sprandsym.m, scripts/sparse/spstats.m, scripts/sparse/spy.m, scripts/sparse/svds.m, scripts/sparse/treelayout.m, scripts/sparse/treeplot.m, scripts/strings/base2dec.m, scripts/strings/bin2dec.m, scripts/strings/blanks.m, scripts/strings/cstrcat.m, scripts/strings/deblank.m, scripts/strings/dec2base.m, scripts/strings/dec2bin.m, scripts/strings/dec2hex.m, scripts/strings/findstr.m, scripts/strings/hex2dec.m, scripts/strings/index.m, scripts/strings/isletter.m, scripts/strings/isstrprop.m, scripts/strings/mat2str.m, scripts/strings/ostrsplit.m, scripts/strings/regexptranslate.m, scripts/strings/rindex.m, scripts/strings/str2num.m, scripts/strings/strcat.m, scripts/strings/strchr.m, scripts/strings/strjoin.m, scripts/strings/strjust.m, scripts/strings/strmatch.m, scripts/strings/strsplit.m, scripts/strings/strtok.m, scripts/strings/strtrim.m, scripts/strings/strtrunc.m, scripts/strings/substr.m, scripts/strings/untabify.m, scripts/time/datenum.m: Update more docstrings to have one sentence summary as first line.
author Rik <rik@octave.org>
date Mon, 04 May 2015 14:22:02 -0700
parents e51473fdb622
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19597
diff changeset
1 ## Copyright (C) 2014-2015 Nathan Podlich
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
2 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
4 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published by
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
8 ## (at your option) any later version.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
9 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful,
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
13 ## GNU General Public License for more details.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
14 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
16 ## along with Octave; If not, see <http://www.gnu.org/licenses/>.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
17
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
18 ## -*- texinfo -*-
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
19 ## @deftypefn {Function File} {@var{x} =} qmr (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{M1}, @var{M2}, @var{x0})
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
20 ## @deftypefnx {Function File} {@var{x} =} qmr (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{P})
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
21 ## @deftypefnx {Function File} {[@var{x}, @var{flag}, @var{relres}, @var{iter}, @var{resvec}] =} qmr (@var{A}, @var{b}, @dots{})
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
22 ## Solve @code{A x = b} using the Quasi-Minimal Residual iterative method
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
23 ## (without look-ahead).
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
24 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
25 ## @itemize @minus
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
26 ## @item @var{rtol} is the relative tolerance, if not given or set to [] the
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
27 ## default value 1e-6 is used.
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
28 ##
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
29 ## @item @var{maxit} the maximum number of outer iterations, if not given or
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
30 ## set to [] the default value @code{min (20, numel (b))} is used.
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
31 ##
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
32 ## @item @var{x0} the initial guess, if not given or set to [] the default
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
33 ## value @code{zeros (size (b))} is used.
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
34 ## @end itemize
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
35 ##
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
36 ## @var{A} can be passed as a matrix or as a function handle or inline
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
37 ## function @code{f} such that @code{f(x, "notransp") = A*x} and
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
38 ## @code{f(x, "transp") = A'*x}.
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
39 ##
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
40 ## The preconditioner @var{P} is given as @code{P = M1 * M2}. Both @var{M1}
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
41 ## and @var{M2} can be passed as a matrix or as a function handle or inline
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
42 ## function @code{g} such that @code{g(x, "notransp") = M1 \ x} or
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
43 ## @code{g(x, "notransp") = M2 \ x} and @code{g(x, "transp") = M1' \ x} or
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20101
diff changeset
44 ## @code{g(x, "transp") = M2' \ x}.
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
45 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
46 ## If called with more than one output parameter
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
47 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
48 ## @itemize @minus
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
49 ## @item @var{flag} indicates the exit status:
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
50 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
51 ## @itemize @minus
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
52 ## @item 0: iteration converged to the within the chosen tolerance
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
53 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
54 ## @item 1: the maximum number of iterations was reached before convergence
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
55 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
56 ## @item 3: the algorithm reached stagnation
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
57 ## @end itemize
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
58 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
59 ## (the value 2 is unused but skipped for compatibility).
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
60 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
61 ## @item @var{relres} is the final value of the relative residual.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
62 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
63 ## @item @var{iter} is the number of iterations performed.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
64 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
65 ## @item @var{resvec} is a vector containing the residual norms at each
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
66 ## iteration.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
67 ## @end itemize
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
68 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
69 ## References:
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
70 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
71 ## @enumerate
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
72 ## @item
20098
21dbaf135667 doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 19854
diff changeset
73 ## @nospell{R. Freund and N. Nachtigal}, @cite{QMR: a quasi-minimal residual
21dbaf135667 doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 19854
diff changeset
74 ## method for non-Hermitian linear systems}, @nospell{Numerische Mathematik},
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
75 ## 1991, 60, pp. 315-339.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
76 ##
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
77 ## @item
20101
e51473fdb622 doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20098
diff changeset
78 ## @nospell{ R. Barrett, M. Berry, T. Chan, J. Demmel, J. Donato, J. Dongarra},
e51473fdb622 doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20098
diff changeset
79 ## @nospell{ V. Eijkhour, R. Pozo, C. Romine, and H. van der Vorst},
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
80 ## @cite{Templates for the solution of linear systems: Building blocks
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
81 ## for iterative methods}, SIAM, 2nd ed., 1994.
19356
3ea57d22d9eb qmr.m: Add missing "@end enumerate" in docstring.
Rik <rik@octave.org>
parents: 19354
diff changeset
82 ## @end enumerate
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
83 ##
19854
0a3ca546d7fc doc: Add qmr to documentation.
Rik <rik@octave.org>
parents: 19833
diff changeset
84 ## @seealso{bicg, bicgstab, cgs, gmres, pcg}
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
85 ## @end deftypefn
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
86
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
87 ## Author: Nathan Podlich <nathan.podlich@gmail.com>
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
88
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
89 function [x, flag, relres, iter, resvec] = qmr (A, b, tol, maxit, M1, M2, x0)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
90
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
91 if (nargin >= 2 && isvector (full (b)))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
92
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
93 if (ischar (A))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
94 fun = str2func (A);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
95 Ax = @(x) feval (fun, x, "notransp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
96 Atx = @(x) feval (fun, x, "transp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
97 elseif (isa (A, "function_handle"))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
98 Ax = @(x) feval (A, x, "notransp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
99 Atx = @(x) feval (A, x, "transp");
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
100 elseif (isnumeric (A) && ismatrix (A))
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
101 Ax = @(x) A * x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
102 Atx = @(x) A' * x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
103 else
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
104 error (["qmr: first argument is expected to " ...
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
105 "be a function or a square matrix"]);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
106 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
107
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
108 if (nargin < 3 || isempty (tol))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
109 tol = 1e-6;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
110 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
111
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
112 if (nargin < 4 || isempty (maxit))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
113 maxit = min (rows (b), 20);
19725
5f2c0ca0ef51 Ensure that numbers passed to integer *printf format codes are integers (bug #44245).
Rik <rik@octave.org>
parents: 19700
diff changeset
114 else
5f2c0ca0ef51 Ensure that numbers passed to integer *printf format codes are integers (bug #44245).
Rik <rik@octave.org>
parents: 19700
diff changeset
115 maxit = fix (maxit);
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
116 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
117
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
118 if (nargin < 5 || isempty (M1))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
119 M1m1x = @(x, ignore) x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
120 M1tm1x = M1m1x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
121 elseif (ischar (M1))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
122 fun = str2func (M1);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
123 M1m1x = @(x) feval (fun, x, "notransp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
124 M1tm1x = @(x) feval (fun, x, "transp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
125 elseif (isa (M1, "function_handle"))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
126 M1m1x = @(x) feval (M1, x, "notransp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
127 M1tm1x = @(x) feval (M1, x, "transp");
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
128 elseif (isnumeric (M1) && ismatrix (M1))
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
129 M1m1x = @(x) M1 \ x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
130 M1tm1x = @(x) M1' \ x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
131 else
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
132 error (["qmr: preconditioner is expected to " ...
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
133 "be a function or matrix"]);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
134 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
135
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
136 if (nargin < 6 || isempty (M2))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
137 M2m1x = @(x, ignore) x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
138 M2tm1x = M2m1x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
139 elseif (ischar (M2))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
140 fun = str2func (M2);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
141 M2m1x = @(x) feval (fun, x, "notransp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
142 M2tm1x = @(x) feval (fun, x, "transp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
143 elseif (isa (M2, "function_handle"))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
144 M2m1x = @(x) feval (M2, x, "notransp");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
145 M2tm1x = @(x) feval (M2, x, "transp");
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
146 elseif (isnumeric (M2) && ismatrix (M2))
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
147 M2m1x = @(x) M2 \ x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
148 M2tm1x = @(x) M2' \ x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
149 else
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
150 error (["qmr: preconditioner is expected to " ...
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
151 "be a function or matrix"]);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
152 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
153
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
154
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
155 if (nargin < 7 || isempty (x0))
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
156 x = zeros (size (b));
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
157 else
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
158 x = x0;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
159 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
160
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
161 r = b - Ax (x);
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19356
diff changeset
162
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
163 bnorm = norm (b);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
164 res0 = norm (r);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
165 if (nargout > 4)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
166 resvec(1) = res0;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
167 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
168 vt = r;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
169
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
170 y = M1m1x (vt);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
171
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
172 rho0 = norm (y);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
173 wt = r;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
174
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
175 z = M2tm1x (wt);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
176
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
177 xi1 = norm (z);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
178 gamma0 = 1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
179 eta0 = -1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
180 flag = 1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
181 for iter=1:1:maxit
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
182 ## If rho0 == 0 or xi1 == 0, method fails.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
183 v = vt / rho0;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
184 y = y / rho0;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
185 w = wt / xi1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
186 z = z / xi1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
187
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19725
diff changeset
188 delta1 = z' * y; # If delta1 == 0, method fails.
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
189
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
190 yt = M2m1x (y);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
191 zt = M1tm1x (z);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
192
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
193 if (iter == 1)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
194 p = yt;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
195 q = zt;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
196 else
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
197 p = yt - (xi1*delta1/eps0) * p;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
198 q = zt - (rho0*delta1/eps0) * q;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
199 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
200 pt = Ax (p);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
201
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19725
diff changeset
202 eps0 = q' * pt; # If eps0 == 0, method fails.
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19725
diff changeset
203 beta1 = eps0 / delta1; # If beta1 == 0, method fails.
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
204 vt = pt - beta1 * v;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
205
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
206 y = M1m1x (vt);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
207 rho1 = norm(y);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
208 wt = Atx (q) - beta1 * w;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
209 z = M2tm1x (wt);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
210
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
211 xi1 = norm(z);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
212 theta1 = rho1 / (gamma0 * abs(beta1));
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19725
diff changeset
213 gamma1 = 1 / sqrt(1 + theta1^2); # If gamma1 == 0, method fails.
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
214 eta1 = -eta0 * rho0 * gamma1^2 / (beta1 * gamma0^2);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
215
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
216 if (iter == 1)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
217 d = eta1 * p;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
218 s = eta1 * pt;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
219 else
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
220 d = eta1 * p + (theta0*gamma1)^2 * d;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
221 s = eta1 * pt + (theta0 * gamma1)^2 * s;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
222 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
223 x += d;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
224 r -= s;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
225
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
226 res1 = norm (r) / bnorm;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
227 if (nargout > 4)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
228 resvec(iter + 1, 1) = norm (r);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
229 end
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19356
diff changeset
230
19354
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
231 if (res1 < tol)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
232 ## Convergence achieved.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
233 flag = 0;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
234 break;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
235 elseif (res0 <= res1)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
236 ## Stagnation encountered.
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
237 flag = 3;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
238 break;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
239 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
240 theta0 = theta1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
241 eta0 = eta1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
242 gamma0 = gamma1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
243 rho0 = rho1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
244 endfor
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
245
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
246 relres = res1;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
247 if (flag == 1)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
248 if (nargout < 2)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
249 printf ("qmr stopped at iteration %i ", iter);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
250 printf ("without converging to the desired tolerance %e\n", tol);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
251 printf ("because the maximum number of iterations was reached. ");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
252 printf ("The iterate returned (number %i) has ", maxit);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
253 printf ("relative residual %e\n", res1);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
254 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
255 elseif (flag == 3)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
256 if (nargout < 2)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
257 printf ("qmr stopped at iteration %i ", iter);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
258 printf (" without converging to the desired tolerance %e\n", tol);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
259 printf ("because the method stagnated.\n");
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
260 printf ("The iterate returned (number %i) ", iter);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
261 printf ("has relative residual %e\n", res1);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
262 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
263 elseif (nargout < 2)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
264 printf ("qmr converged at iteration %i ", iter);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
265 printf ("to a solution with relative residual %e\n", res1);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
266 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
267 else
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
268 print usage();
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
269 endif
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
270 endfunction
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
271
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
272
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
273 %!demo
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
274 %! % Solve system of A*x=b
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
275 %! A = [5 -1 3;-1 2 -2;3 -2 3];
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
276 %! b = [7;-1;4];
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
277 %! [x, flag, relres, iter, resvec] = qmr (A, b)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
278
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
279 %!test
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
280 %! n = 100;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
281 %! A = spdiags ([-2*ones(n,1) 4*ones(n,1) -ones(n,1)], -1:1, n, n);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
282 %! b = sum (A, 2);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
283 %! tol = 1e-8;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
284 %! maxit = 15;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
285 %! M1 = spdiags ([ones(n,1)/(-2) ones(n,1)],-1:0, n, n);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
286 %! M2 = spdiags ([4*ones(n,1) -ones(n,1)], 0:1, n, n);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
287 %! [x, flag, relres, iter, resvec] = qmr (A, b, tol, maxit, M1, M2);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
288 %! assert (x, ones (size (b)), 1e-7);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
289
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
290 %!function y = afun (x, t, a)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
291 %! switch (t)
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
292 %! case "notransp"
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
293 %! y = a * x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
294 %! case "transp"
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
295 %! y = a' * x;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
296 %! endswitch
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
297 %!endfunction
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
298 %!
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
299 %!test
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
300 %! n = 100;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
301 %! A = spdiags ([-2*ones(n,1) 4*ones(n,1) -ones(n,1)], -1:1, n, n);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
302 %! b = sum (A, 2);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
303 %! tol = 1e-8;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
304 %! maxit = 15;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
305 %! M1 = spdiags ([ones(n,1)/(-2) ones(n,1)],-1:0, n, n);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
306 %! M2 = spdiags ([4*ones(n,1) -ones(n,1)], 0:1, n, n);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
307 %!
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
308 %! [x, flag, relres, iter, resvec] = qmr (@(x, t) afun (x, t, A),
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
309 %! b, tol, maxit, M1, M2);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
310 %! assert (x, ones (size (b)), 1e-7);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
311
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
312 %!test
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
313 %! n = 100;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
314 %! tol = 1e-8;
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
315 %! a = sprand (n, n, .1);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
316 %! A = a' * a + 100 * eye (n);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
317 %! b = sum (A, 2);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
318 %! [x, flag, relres, iter, resvec] = qmr (A, b, tol, [], diag (diag (A)));
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
319 %! assert (x, ones (size (b)), 1e-7);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
320
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
321 %!test
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
322 %! A = [1 + 1i, 1 + 1i; 2 - 1i, 2 + 1i];
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
323 %! b = A * [1; 1];
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
324 %! [x, flag, relres, iter, resvec] = qmr (A, b);
cc6000ccd627 New function qmr (patch #8569).
Nathan Podlich <nathan.podlich@gmail.com>
parents:
diff changeset
325 %! assert (x, [1; 1], 1e-6);