Small size linear solver

by

 

27 May 2010 (Updated )

Linear solver of multiple 2x2 and 3x3 systems

bench_inv2inv3.m
% Script for benchmark of INV2 and INV3 
n = 100000;
m = 3; % size of the systems
A=rand(m,m,n);
RHS=rand(m,1,n);

%% Matlab for-loop inversion
tic
X=zeros(m,1,n);
for k=1:n
    X(:,:,k) = A(:,:,k)\RHS(:,:,k);
end
t1 = toc;

%% INV2 and INV3 with MTIMESX
if m==2
    tic
    X = inv2(A,RHS,'speed');
    t2 = toc;
else
    tic
    X = inv3(A,RHS,'speed');
    t2 = toc;
end

fprintf('n = %d, m = %d\n', n, m);
fprintf('For loop backslash = %g seconds\n', t1);
fprintf('         inv2/inv3 = %g seconds\n', t2);
fprintf('              Gain = %g\n', t1/t2);

Contact us