Ax=b

From Wikimization

(Difference between revisions)
Jump to: navigation, search
(New page: =Seven ways Matlab can solve thin-matrix linear equality= When <math>b\notin\mathcal{R}(A)</math> <pre> %test backslash timing clc %clear all; close all; fclose all; slow execution by o...)
Line 6: Line 6:
%test backslash timing
%test backslash timing
clc %clear all; close all; fclose all; slow execution by order of magnitude
clc %clear all; close all; fclose all; slow execution by order of magnitude
-
A = randn(600*96000,52);
+
A = randn(1e6,52);
% spA = sparse(A);
% spA = sparse(A);
xact = randn(52,1);
xact = randn(52,1);
Line 14: Line 14:
opt1.LT = true; opt2.UT = true;
opt1.LT = true; opt2.UT = true;
 +
%form A'*A
[m n] = size(A);
[m n] = size(A);
AA = zeros(n,n);
AA = zeros(n,n);
Line 104: Line 105:
disp(['error = ' num2str(norm(x7a - xact)/norm(xact))])
disp(['error = ' num2str(norm(x7a - xact)/norm(xact))])
end
end
- 
</pre>
</pre>
 +
 +
[[http://www.convexoptimization.com/wikimization/index.php/Accumulator_Error_Feedback|<tt>csum()</tt> routine]]
 +
(with presorting) increases precision by orders of magnitude.

Revision as of 15:29, 17 December 2017

Seven ways Matlab can solve thin-matrix linear equality

When LaTeX: b\notin\mathcal{R}(A)

%test backslash timing
clc  %clear all; close all; fclose all; slow execution by order of magnitude
A = randn(1e6,52);
% spA = sparse(A);
xact = randn(52,1); 
b = A*xact;

opt.SYM = true;  opt.POSDEF = true;
opt1.LT = true;  opt2.UT = true;

%form A'*A
[m n] = size(A);
AA = zeros(n,n);
bb = zeros(n,1);
for i=1:n
   for j=1:i
      AA(i,j) = csum(A(:,i).*A(:,j));
      AA(j,i) = AA(i,j);
   end
  bb(i) = csum(A(:,i).*b);
end
spAA = sparse(AA);
for i=1:4
   if i > 1, disp(' ');disp(' '); end

%    disp('   backslash on A')
%    tic
%    x1 = A\b;
%    toc
%    disp(['error = ' num2str(norm(x1 - xact)/norm(xact))])

   disp('   backslash on A''A')
   tic
   x1a = AA\bb;
   toc
   disp(['error = ' num2str(norm(x1a - xact)/norm(xact))])

%    disp('   pinv on A')
%    tic
%    x2 = pinv(A)*b;
%    toc
%    disp(['error = ' num2str(norm(x2 - xact)/norm(xact))])

   disp('   pinv on A''A')
   tic
   x2a = pinv(AA)*bb;
   toc
   disp(['error = ' num2str(norm(x2a - xact)/norm(xact))])

%    disp('   QR on A')
%    tic
%    [c,R] = qr(spA,b,0);  
%    x3 = R\c;
%    toc
%    disp(['error = ' num2str(norm(x3 - xact)/norm(xact))])

   disp('   QR on A''A')
   tic
   [c,R] = qr(spAA,bb,0);  
   x3a = R\c;
   toc
   disp(['error = ' num2str(norm(x3a - xact)/norm(xact))])

   disp('   U\(U''\A''*b) on U=chol(A''A)')
   tic; 
   U = chol(AA); 
   x4 = U\(U'\bb); 
   toc
   disp(['error = ' num2str(norm(x4 - xact)/norm(xact))])

   disp('   linsolve on chol(A''A)')  %winner on Dec.14 2017
   tic
   U = chol(AA);
   x5 = linsolve(U, linsolve(U',bb,opt1), opt2);
   toc
   disp(['error = ' num2str(norm(x5 - xact)/norm(xact))])

%    disp('   linsolve on A')
%    tic
%    x6 = linsolve(A,b);
%    toc
%    disp(['error = ' num2str(norm(x6 - xact)/norm(xact))])

   disp('   linsolve on A''A')
   tic
   x6a = linsolve(AA,bb,opt);
   toc
   disp(['error = ' num2str(norm(x6a - xact)/norm(xact))])

%    disp('   lscov on A')
%    tic
%    x7 = lscov(A,b);
%    toc
%    disp(['error = ' num2str(norm(x7 - xact)/norm(xact))])

   disp('   lscov on A''A')
   tic
   x7a = lscov(AA,bb);
   toc
   disp(['error = ' num2str(norm(x7a - xact)/norm(xact))])
end

[csum() routine] (with presorting) increases precision by orders of magnitude.

Personal tools