-
Notifications
You must be signed in to change notification settings - Fork 0
/
minres_for_IP.m
352 lines (323 loc) · 8.9 KB
/
minres_for_IP.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
function [x,flag,relres,iter,resvec,resveccg] = minres_for_IP(funAx,b,tol,maxit,funPrec,M2,x0,varargin)
% From Mathwork code updated by MK for POEMA and Interior Point algorithm
%MINRES Minimum Residual Method.
% Copyright 1984-2017 The MathWorks, Inc.
if (nargin < 2)
error(message('MATLAB:minres:NotEnoughInputs'));
end
ma = size(b,1);
n = ma;
% Assign default values to unspecified parameters
if (nargin < 3) || isempty(tol)
tol = 1e-6;
end
warned = 0;
if tol <= eps
warning(message('MATLAB:minres:tooSmallTolerance'));
warned = 1;
tol = eps;
elseif tol >= 1
warning(message('MATLAB:minres:tooBigTolerance'));
warned = 1;
tol = 1-eps;
end
if (nargin < 4) || isempty(maxit)
maxit = min(n,20);
end
maxit = max(maxit, 1);
% Check for all zero right hand side vector => all zero solution
n2b = norm(b); % Norm of rhs vector, b
if (n2b == 0) % if rhs vector is all zeros
x = zeros(n,1); % then solution is all zeros
flag = 0; % a valid solution has been obtained
relres = 0; % the relative residual is actually 0/0
iter = 0; % no iterations need be performed
resvec = 0; % resvec(1) = norm(b-A*x) = norm(0)
if nargout >= 6 % resveccg(1) = norm(b-A*xcg) = norm(0)
resveccg = 0;
end
if (nargout < 2)
itermsg('minres',tol,maxit,0,flag,iter,NaN);
end
return
end
if ((nargin >= 7) && ~isempty(x0))
if ~isequal(size(x0),[n,1])
error(message('MATLAB:minres:WrongInitGuessSize', n));
else
x = x0;
end
else
x = zeros(n,1);
end
if ((nargin > 7) && strcmp(atype,'matrix') && ...
strcmp(m1type,'matrix') && strcmp(m2type,'matrix'))
error(message('MATLAB:minres:TooManyInputs'));
end
% Set up for the method
flag = 1;
iter = 0;
xmin = x; % Iterate which has minimal residual so far
imin = 0; % Iteration at which xmin was computed
tolb = tol * n2b; % Relative tolerance
%r = b - iterapp('mtimes',afun,atype,afcnstr,x,varargin{:});
r = b - funAx(x);
normr = norm(r); % Norm of residual
if (normr <= tolb) % Initial guess is a good enough solution
flag = 0;
relres = normr / n2b;
resvec = normr;
if nargout >= 6
resveccg = normr;
end
if (nargout < 2)
itermsg('minres',tol,maxit,0,flag,iter,relres);
end
return
end
resvec = zeros(maxit+1,1); % Preallocate vector for MINRES residuals
resvec(1) = normr; % resvec(1) = norm(b-A*x0)
if nargout >= 6
resveccg = zeros(maxit+2,1); % Preallocate vector for CG residuals
resveccg(1) = normr; % resveccg(1) = norm(b-A*x0)
end
normrmin = normr; % Norm of minimum residual
vold = r;
u = funPrec(vold);
if ~all(isfinite(u))
flag = 2;
relres = normr / n2b;
resvec = resvec(1);
if nargout >= 6, resveccg = resveccg(1); end
if nargout < 2
itermsg('minres',tol,maxit,0,flag,iter,relres);
end
return
end
v = u;
beta1 = vold' * v;
if (beta1 <= 0)
flag = 5;
relres = normr / n2b;
resvec = resvec(1);
if nargout >= 6, resveccg = resveccg(1); end
if nargout < 2
itermsg('minres',tol,maxit,0,flag,iter,relres);
end
return
end
beta1 = sqrt(beta1);
snprod = beta1;
vv = v / beta1;
% v = iterapp('mtimes',afun,atype,afcnstr,vv,varargin{:});
v = funAx(vv);
Amvv = v;
alpha = vv' * v;
v = v - (alpha/beta1) * vold;
% Local reorthogonalization
numer = vv' * v;
denom = vv' * vv;
v = v - (numer/denom) * vv;
volder = vold;
vold = v;
u = funPrec(vold);
if ~all(isfinite(u))
flag = 2;
relres = normr / n2b;
resvec = resvec(1);
if nargout >= 6, resveccg = resveccg(1); end
if nargout < 2
itermsg('minres',tol,maxit,0,flag,iter,relres);
end
return
end
v = u;
betaold = beta1;
beta = vold' * v;
if (beta < 0)
flag = 5;
relres = normr / n2b;
resvec = resvec(1);
if nargout >= 6, resveccg = resveccg(1); end
if nargout < 2
itermsg('minres',tol,maxit,0,flag,iter,relres);
end
return
end
iter = 1;
beta = sqrt(beta);
gammabar = alpha;
epsilon = 0;
deltabar = beta;
gamma = sqrt(gammabar^2 + beta^2);
mold = zeros(n,1);
Amold = mold;
m = vv / gamma;
Am = Amvv / gamma;
cs = gammabar / gamma;
sn = beta / gamma;
x = x + snprod * cs * m;
snprodold = snprod;
snprod = snprod * sn;
% This recurrence produces CG iterates.
% Enable the following statement to see xcg.
%xcg = x + snprod * (sn/cs) * m;
r = r - snprodold * cs * Am;
normr = norm(r);
resvec(2,1) = normr;
if nargout >= 6
if (cs == 0)
% It's possible that this cs value is zero (CG iterate does not exist)
normrcg = Inf;
else
rcg = r - snprod*(sn/cs)*Am;
normrcg = norm(rcg);
end
resveccg(2,1) = normrcg;
end
% Check for convergence after first step.
if normr <= tolb
flag = 0;
relres = normr / n2b;
resvec = resvec(1:2);
if nargout >= 6, resveccg = resveccg(1:2); end
if (nargout < 2)
itermsg('minres',tol,maxit,1,flag,iter,relres);
end
return
end
stag = 0; % stagnation of the method
moresteps = 0;
maxmsteps = min([floor(n/50),5,n-maxit]);
maxmsteps = maxit/1;
maxstagsteps = 3;
% loop over maxit iterations (unless convergence or failure)
for ii = 2 : maxit
vv = v * (1/beta);
% v = iterapp('mtimes',afun,atype,afcnstr,vv,varargin{:});
v = funAx(vv);
Amolder = Amold;
Amold = Am;
Am = v;
v = v - (beta / betaold) * volder;
alpha = vv' * v;
v = v - (alpha / beta) * vold;
volder = vold;
vold = v;
u = funPrec(vold);
if ~all(isfinite(u))
flag = 2;
break
end
v = u;
betaold = beta;
beta = vold' * v;
if (beta < 0)
flag = 5;
break
end
beta = sqrt(beta);
delta = cs * deltabar + sn * alpha;
molder = mold;
mold = m;
m = vv - delta * mold - epsilon * molder;
Am = Am - delta * Amold - epsilon * Amolder;
gammabar = sn * deltabar - cs * alpha;
epsilon = sn * beta;
deltabar = - cs * beta;
gamma = sqrt(gammabar^2 + beta^2);
m = m / gamma;
Am = Am / gamma;
cs = gammabar / gamma;
sn = beta / gamma;
% Check for stagnation of the method
if (snprod*cs == 0) || (abs(snprod*cs)*norm(m) < eps*norm(x))
% increment the number of consecutive iterates which are the same
stag = stag + 1;
else
stag = 0;
end
x = x + (snprod * cs) * m;
snprodold = snprod;
snprod = snprod * sn;
% This recurrence produces CG iterates.
% Enable the following statement to see xcg.
%xcg = x + snprod * (sn/cs) * m;
normr = abs(snprod);
resvec(ii+1,1) = normr;
if nargout >= 6
% It's possible that this cs value is zero (CG iterate does not exist).
if (cs == 0)
normrcg = Inf;
else
rcg = r - snprod*(sn/cs)*Am;
normrcg = norm(rcg);
end
resveccg(ii+2,1) = normrcg;
end
% check for convergence
if (normr <= tolb || stag >= maxstagsteps || moresteps)
% double check residual norm is less than tolerance
% r = b - iterapp('mtimes',afun,atype,afcnstr,x,varargin{:});
r = b - funAx(x);
normr = norm(r);
resvec(ii+1,1) = normr;
if (normr <= tolb)
flag = 0;
iter = ii;
break
else
if stag >= maxstagsteps && moresteps == 0
stag = 0;
end
moresteps = moresteps + 1;
if moresteps >= maxmsteps
if ~warned
warning(message('MATLAB:minres:tooSmallTolerance'));
end
flag = 3;
iter = ii;
break;
end
end
end
if (normr < normrmin) % update minimal norm quantities
normrmin = normr;
xmin = x;
imin = ii;
end
if (stag >= maxstagsteps) % 3 iterates are the same
flag = 3;
break
end
end % for ii = 1 : maxit
if isempty(ii)
ii = 1;
end
% returned solution is first with minimal residual
if (flag == 0)
relres = normr / n2b;
else
% r_comp = b - iterapp('mtimes',afun,atype,afcnstr,xmin,varargin{:});
r_comp = b - funAx(xmin);
if norm(r_comp) <= normr
x = xmin;
iter = imin;
relres = norm(r_comp) / n2b;
else
iter = ii;
relres = normr / n2b;
end
end
% truncate the zeros from resvec
if ((flag <= 1) || (flag == 3))
resvec = resvec(1:ii+1);
if nargout >= 6, resveccg = resveccg(1:ii+2); end
else
resvec = resvec(1:ii);
if nargout >= 6, resveccg = resveccg(1:ii+1); end
end
% only display a message if the output flag is not used
if (nargout < 2)
itermsg('minres',tol,maxit,ii,flag,iter,relres);
end