0001 clear;
0002
0003 N = 1000;
0004 S = 300;
0005
0006 rseed = sum(100*clock);
0007
0008
0009
0010 RandStream.setDefaultStream(RandStream('mt19937ar','seed',rseed));
0011
0012 A = (randn(N,S));
0013 AtA = A'*A;
0014 z = sign(randn(S,1));
0015
0016
0017
0018 iAtA = inv(A'*A);
0019 delx_mil = iAtA*z;
0020 t_mil = 0;
0021
0022
0023 [Q R] = qr(A,0);
0024
0025
0026 delx_qr = R\(R'\z);
0027 t_qr = 0;
0028
0029
0030 [Q0 R0] = qr(A);
0031 delx_qrM = 0;
0032 t_qrM = 0;
0033
0034
0035 mil = 1;
0036 qr_giv = 1;
0037 qrM = 0;
0038 error = 1;
0039
0040 Gamma = [1:S]';
0041 Gamma_new = Gamma;
0042
0043 for ii = S-1:-1:2
0044
0045 Gamma = Gamma_new;
0046 out_i = floor(rand(1,1)*(ii))+1;
0047 out_x = Gamma(out_i);
0048
0049
0050 swap_shift = 0;
0051 if swap_shift
0052
0053 Gamma(out_i) = Gamma(end);
0054
0055 new_order = [1:out_i-1, ii+1, out_i+1:ii, out_i]';
0056 else
0057
0058 out_loc = Gamma==out_x;
0059 Gamma = Gamma(~out_loc);
0060 Gamma = [Gamma; out_x];
0061 new_order = [1:out_i-1, out_i+1:ii+1, out_i]';
0062 end
0063 Gamma_new = Gamma(1:end-1);
0064
0065 if error
0066 AtA = (A(:,Gamma(1:end-1))'*A(:,Gamma(1:end-1)));
0067 delx = AtA\z(Gamma(1:end-1));
0068 end
0069
0070
0071 if mil
0072 tic; t = cputime;
0073 iAtA = iAtA(new_order,new_order);
0074 Si = 1/iAtA(ii+1,ii+1);
0075 at = [iAtA(1:ii,ii+1)*sqrt(Si); 1/sqrt(Si)];
0076 ab = at(1:ii);
0077 alpha = at'*z(Gamma);
0078 delx_mil = delx_mil(new_order);
0079 delx_mil = delx_mil(1:ii)-alpha*ab;
0080 iAtA = iAtA(1:ii,1:ii)-ab*ab';
0081
0082 t1 = cputime-t;
0083 t1 = toc;
0084 t_mil = t_mil+t1;
0085 end
0086
0087
0088 if qr_giv
0089
0090 R_old = R; Q_old = Q;
0091 Q = Q';
0092 tic; t = cputime;
0093 R = [R(:,new_order)];
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 for j = out_i:ii
0106 i = j+1;
0107 [c, s] = givens(R(j,j), R(i,j));
0108 R([j i],j:end) = [c -s; s c]*R([j i],j:end);
0109 Q([j i],:) = [c -s; s c]*Q([j i],:);
0110 end
0111 ri = R\[zeros(ii,1); 1];
0112 delx_qr = delx_qr(new_order);
0113 delx_qr_simple = delx_qr-(ri'*z(Gamma))*ri;
0114 delx_qr = delx_qr_simple(1:end-1);
0115
0116 R = R(1:ii,1:ii);
0117
0118
0119
0120 t1 = cputime-t;
0121 t1 = toc;
0122 t_qr = t_qr+t1;
0123 R = triu(R); Q = Q(1:ii,:); Q = Q';
0124
0125 end
0126
0127
0128 if qrM
0129 tic; t = cputime;
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142 [Q0, R0] = qrdelete(Q0,R0, out_i, 'col');
0143 R0t = R0';
0144 delx_qrM = (R0\(R0t\z(Gamma(1:end-1))));
0145 t1 = cputime-t;
0146 t1 = toc;
0147 t_qrM = t_qrM+t1;
0148 end
0149
0150 end
0151
0152 if error
0153 fprintf('iter=%d, t_mil=%3.4g, t_qr=%3.4g, t_qrM=%3.4g, errors: mil=%3.4g, qr=%3.4g, qrM=%3.4g.\n',ii, t_mil, t_qr, t_qrM, norm(delx-delx_mil), norm(delx-delx_qr), norm(delx-delx_qrM));
0154 else
0155 fprintf('iter=%d, t_mil=%3.4g, t_qr=%3.4g, t_qrM=%3.4g.\n',ii, t_mil, t_qr, t_qrM);
0156 end