0001
0002 function [x,rho,eta,F] = cgls_mod(A,b,k,reorth,s)
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 fudge_thr = 1e-4;
0036
0037
0038 if (k < 1), error('Number of steps k must be positive'), end
0039 if (nargin==3), reorth = 0; end
0040
0041 if (nargout==4 & nargin<4), error('Too few input arguments'), end
0042 if (reorth<0 | reorth>1), error('Illegal reorth'), end
0043 [m,n] = size(A);
0044
0045 if (reorth==1), ATr = zeros(n,k+1); end
0046 if (nargout > 1)
0047 eta = zeros(k,1); rho = eta;
0048 end
0049 F=0;
0050 if (nargin==5)
0051 F = zeros(n,k); Fd = zeros(n,1); s2 = s.^2;
0052 end
0053
0054
0055 x = zeros(n,1);
0056 d = A'*b;
0057 r = b;
0058 normr2 = d'*d;
0059 if (reorth==1), ATr(:,1) = d/norm(d); end
0060
0061
0062 for j=1:k
0063
0064
0065 Ad = A*d; alpha = normr2/(Ad'*Ad);
0066 x = x + alpha*d;
0067 r = r - alpha*Ad;
0068 s = A'*r;
0069
0070
0071 if (reorth==1)
0072 for i=1:j, s = s - (ATr(:,i)'*s)*ATr(:,i); end
0073 ATr(:,j+1) = s/norm(s);
0074 end
0075
0076
0077 normr2_new = s'*s;
0078 beta = normr2_new/normr2;
0079 normr2 = normr2_new;
0080 d = s + beta*d;
0081
0082
0083
0084 if (nargout>1), rho(j) = norm(r); end
0085 if (nargout>2), eta(j) = norm(x); end
0086
0087
0088 if (nargin==5)
0089 if (j==1)
0090 F(:,1) = alpha*s2;
0091 Fd = s2 - s2.*F(:,1) + beta*s2;
0092 else
0093 F(:,j) = F(:,j-1) + alpha*Fd;
0094 Fd = s2 - s2.*F(:,j) + beta*Fd;
0095 end
0096 if (j > 2)
0097 f = find(abs(F(:,j-1)-1) < fudge_thr & abs(F(:,j-2)-1) < fudge_thr);
0098 if ~isempty(f), F(f,j) = ones(length(f),1); end
0099 end
0100 end
0101
0102 end