-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterapp.m
70 lines (67 loc) · 2.81 KB
/
iterapp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function y = iterapp(op,afun,atype,afcnstr,x,varargin)
%ITERAPP Apply matrix operator to vector and error gracefully.
% ITERAPP(OP,AFUN,ATYPE,AFCNSTR,X) applies matrix operator AFUN to vector
% X. If ATYPE is 'matrix, then AFUN is a matrix and the OP is applied
% directly. OP is either 'mtimes' or 'mldivide'.
% ATYPE and AFCNSTR are used in case of error.
% ITERAPP(OP,AFUN,ATYPE,AFCNSTR,X,P1,P2,...) allows extra arguments to
% AFUN(X,P1,P2,...) although this usage is now discouraged in favor of
% using anonymous functions.
% AFUN(X,P1,P2,...,PN,TFLAG) should accept a TFLAG as its final input if
% the calling function is BICG, LSQR or QMR. TFLAG is either 'transp' or
% 'notransp' depending on whether A' OP X or A OP X is required.
% ITERAPP is designed for use by iterative methods like PCG which
% require matrix operators AFUN representing matrices A to operate on
% vectors X and return A*X and may also have operators MFUN representing
% preconditioning matrices M operate on vectors X and return M\X.
%
% See also BICG, BICGSTAB, CGS, GMRES, LSQR, MINRES, PCG, QMR, SYMMLQ.
% Copyright 1984-2008 The MathWorks, Inc.
% $Revision: 1.7.4.4 $ $Date: 2008/06/20 08:01:27 $
if strcmp(atype,'matrix')
switch lower(op)
case 'mtimes'
if (nargin >= 6) && isequal(varargin{end},'transp')
y = afun' * x;
else
y = afun * x;
end
case 'mldivide'
if (nargin >= 6) && isequal(varargin{end},'transp')
y = afun' \ x;
else
y = afun \ x;
end
otherwise
error('MATLAB:iterapp:InvalidOp', 'Invalid operation.')
end
else
try
if (nargin >= 6) && isequal(varargin{end},'notransp')
% A request for A*x coming from BICG, LSQR and QMR
try
% New syntax: we now request afun(x,P1,P2,...,PN,'notransp')
y = afun(x,varargin{:});
catch
% Old syntax: we used to accept afun(x,P1,P2,...,PN)
y = afun(x,varargin{1:end-1});
end
else
% A request for A*x
% coming from BICGSTAB, CGS, GMRES, MINRES, PCG or SYMMLQ
% with the call always afun(P1,P2,...,PN)
% or a request for A'*x coming from
% BICG, LSQR and QMR in the afun(x,P1,P2,...,PN,'transp') case
y = afun(x,varargin{:});
end
catch ME
error('MATLAB:InvalidInput', ['user supplied %s ==> %s\n' ...
'failed with the following error:\n\n%s'], ...
atype,afcnstr, ME.message);
end
if ~isvector(y) || (size(y,2) ~= 1)
error('MATLAB:MustReturnColumn', ['user supplied %s ==> %s\n' ...
'must return a column vector.'], ...
atype,afcnstr)
end
end