kusano 7d535a
function failed = trylusolve(A,b);
kusano 7d535a
% TRYLUSOLVE : Test the Matlab interface to lusolve.
kusano 7d535a
%
kusano 7d535a
% failed = trylusolve;  
kusano 7d535a
%          This runs several tests on lusolve, using a matrix with the
kusano 7d535a
%          structure of "smallmesh".  It returns a list of failed tests.
kusano 7d535a
%
kusano 7d535a
% failed = trylusolve(A); 
kusano 7d535a
% failed = trylusolve(A,b); 
kusano 7d535a
%          This times the solution of a system with the given matrix,
kusano 7d535a
%          both with lusolve and with Matlab's builtin "\" operator.
kusano 7d535a
%          Usually, "\" will be faster for triangular or symmetric postive
kusano 7d535a
%          definite matrices, and lusolve will be faster otherwise.
kusano 7d535a
%
kusano 7d535a
% Copyright (c) 1995 by Xerox Corporation.  All rights reserved.
kusano 7d535a
% HELP COPYRIGHT for complete copyright and licensing notice.
kusano 7d535a
kusano 7d535a
disp(' ');
kusano 7d535a
disp(['Testing LUSOLVE on ' time]);
kusano 7d535a
disp(' ');
kusano 7d535a
kusano 7d535a
format compact
kusano 7d535a
resetrandoms;
kusano 7d535a
if nargin == 0
kusano 7d535a
    A = hbo('smallmesh');
kusano 7d535a
end;
kusano 7d535a
[n,m] = size(A);
kusano 7d535a
if n~=m, error('matrix must be square'), end;
kusano 7d535a
kusano 7d535a
failed = [];
kusano 7d535a
ntest = 0;
kusano 7d535a
tol = 100 * eps * n^2;
kusano 7d535a
kusano 7d535a
if nargin >= 1 % Just try a solve with the given input matrix.
kusano 7d535a
    ntest=ntest+1;
kusano 7d535a
    v = spparms;
kusano 7d535a
    spparms('spumoni',1);
kusano 7d535a
    fprintf('Matrix size: %d by %d with %d nonzeros.\n',n,m,nnz(A));
kusano 7d535a
    disp(' ');
kusano 7d535a
    if nargin == 1
kusano 7d535a
        b = rand(n,1);
kusano 7d535a
    end;
kusano 7d535a
    tic; x=A\b; t1=toc;
kusano 7d535a
    fprintf('A\\b time = %d seconds.\n',t1);
kusano 7d535a
    disp(' ');
kusano 7d535a
    tic; x=lusolve(A,b); t2=toc;
kusano 7d535a
    fprintf('LUSOLVE time = %d seconds.\n',t2);
kusano 7d535a
    disp(' ');
kusano 7d535a
    ratio = t1/t2;
kusano 7d535a
    fprintf('Ratio = %d\n',ratio);
kusano 7d535a
    residual_norm = norm(A*x-b,inf)/norm(A,inf)
kusano 7d535a
    disp(' ');
kusano 7d535a
    if residual_norm > tol
kusano 7d535a
         disp('*** FAILED ***'); failed = [failed ntest];
kusano 7d535a
    end;
kusano 7d535a
    spparms(v);
kusano 7d535a
    return;
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
% With no inputs, try several tests on the small mesh.
kusano 7d535a
kusano 7d535a
kusano 7d535a
A = sprandn(A);
kusano 7d535a
p = randperm(n);
kusano 7d535a
I = speye(n);
kusano 7d535a
P = I(p,:);
kusano 7d535a
b = rand(n,2);
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0.\n',ntest);
kusano 7d535a
x = lusolve(A,b,0);
kusano 7d535a
residual_norm = norm(A*x-b,inf)
kusano 7d535a
disp(' ');
kusano 7d535a
if residual_norm > tol 
kusano 7d535a
    disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; 
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given as vector.\n',ntest);
kusano 7d535a
x = lusolve(A,b,p);
kusano 7d535a
residual_norm = norm(A*x-b,inf)
kusano 7d535a
disp(' ');
kusano 7d535a
if residual_norm > tol
kusano 7d535a
    disp('*** FAILED ***^G'), disp(' '), failed = [failed ntest];
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given as matrix.\n',ntest);
kusano 7d535a
x = lusolve(A,b,P);
kusano 7d535a
residual_norm = norm(A*x-b,inf)
kusano 7d535a
disp(' ');
kusano 7d535a
if residual_norm > tol 
kusano 7d535a
    disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; 
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: No input perm (colamd done internally).\n',ntest);
kusano 7d535a
x = lusolve(A,b);
kusano 7d535a
residual_norm = norm(A*x-b,inf)
kusano 7d535a
disp(' ');
kusano 7d535a
if residual_norm > tol 
kusano 7d535a
    disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; 
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Empty matrix.\n',ntest);
kusano 7d535a
x = lusolve([],[]);
kusano 7d535a
disp(' ');
kusano 7d535a
% if max(size(x))
kusano 7d535a
if length(x)
kusano 7d535a
    x
kusano 7d535a
    disp('*** FAILED ***^G'), disp(' ');
kusano 7d535a
    failed = [failed ntest];
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Timing versus \\ on the 3-element airfoil matrix.\n',ntest);
kusano 7d535a
A = hbo('airfoil2');
kusano 7d535a
n = max(size(A));
kusano 7d535a
A = sprandn(A);
kusano 7d535a
b = rand(n,1);
kusano 7d535a
tic; x=A\b; t1=toc;
kusano 7d535a
fprintf('A\\b time = %d seconds.\n',t1);
kusano 7d535a
tic; x=lusolve(A,b); t2=toc;
kusano 7d535a
fprintf('LUSOLVE time = %d seconds.\n',t2);
kusano 7d535a
ratio = t1/t2;
kusano 7d535a
fprintf('Ratio = %d\n',ratio);
kusano 7d535a
disp(' ');
kusano 7d535a
if ratio < 1, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
kusano 7d535a
nfailed = length(failed);
kusano 7d535a
if nfailed
kusano 7d535a
    fprintf('\n%d tests failed.\n\n',nfailed);
kusano 7d535a
else
kusano 7d535a
    fprintf('\nAll tests passed.\n\n',nfailed);
kusano 7d535a
end;