kusano 7d535a
function failed = trysuperlu(A);
kusano 7d535a
% TRYSUPERLU : Test the Matlab interface to superlu.
kusano 7d535a
%
kusano 7d535a
% failed = trysuperlu;  
kusano 7d535a
%          This runs several tests on superlu, using a matrix with the
kusano 7d535a
%          structure of "smallmesh".  It returns a list of failed tests.
kusano 7d535a
%
kusano 7d535a
% failed = trysuperlu(A);
kusano 7d535a
%          This just times the factorization of A.
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 SUPERLU on ' time]);
kusano 7d535a
disp(' ');
kusano 7d535a
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 to factor the given input matrix.
kusano 7d535a
    ntest=ntest+1;
kusano 7d535a
    fprintf('Matrix size: %d by %d with %d nonzeros.\n',n,m,nnz(A));
kusano 7d535a
    r = trytime(A);
kusano 7d535a
    if r > tol, disp('*** FAILED ***'); failed = [failed ntest]; end;
kusano 7d535a
    return;
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
% With no input argument, try several tests on the small mesh.
kusano 7d535a
kusano 7d535a
PivPostA = sprandn(A);
kusano 7d535a
A = (n+1)*speye(n) - spones(A); % Diagonally dominant
kusano 7d535a
[t,post] = etree(A'*A);
kusano 7d535a
kusano 7d535a
if post == [1:n], disp('note: column etree already in postorder'), end;
kusano 7d535a
kusano 7d535a
NoPivPostA = A(post,:);
kusano 7d535a
NoPivNoPostA = A(post,post);
kusano 7d535a
PivNoPostA = sprandn(NoPivNoPostA);
kusano 7d535a
p = randperm(n);
kusano 7d535a
pinv = zeros(1,n);
kusano 7d535a
pinv(p) = 1:n;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, no pivot, no postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(NoPivNoPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, no pivot, no postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(NoPivNoPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, no pivot, no postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try2(NoPivNoPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, pivot, no postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(PivNoPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, pivot, no postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(PivNoPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, pivot, no postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try2(PivNoPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, no pivot, postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(NoPivPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, no pivot, postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(NoPivPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, no pivot, postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try2(NoPivPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, pivot, postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(PivPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, pivot, postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(PivPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm 0, pivot, postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try2(PivPostA,0);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, no pivot, no postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(NoPivNoPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, no pivot, no postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(NoPivNoPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, no pivot, no postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try2(NoPivNoPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, pivot, no postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(PivNoPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, pivot, no postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(PivNoPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, pivot, no postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try2(PivNoPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, no pivot, postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(NoPivPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, no pivot, postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(NoPivPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, no pivot, postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try2(NoPivPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, pivot, postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(PivPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, pivot, postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(PivPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given, pivot, postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try2(PivPostA(:,pinv),p);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: No input perm (colamd done internally), pivot, postorder, 4 outputs.\n',ntest);
kusano 7d535a
r = try4(PivPostA);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: No input perm, pivot, postorder, 3 outputs.\n',ntest);
kusano 7d535a
r = try3(PivPostA);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: No input perm, pivot, postorder, 2 outputs.\n',ntest);
kusano 7d535a
r = try3(PivPostA);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Input perm given as matrix, pivot, postorder, 4 outputs.\n',ntest);
kusano 7d535a
P = speye(n);
kusano 7d535a
P = P(p,:);
kusano 7d535a
r = try4(PivPostA*P,P);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
kusano 7d535a
kusano 7d535a
ntest=ntest+1;
kusano 7d535a
fprintf('Test %d: Empty matrix.\n',ntest);
kusano 7d535a
[L,U,PROW,PCOL] = superlu([]);
kusano 7d535a
disp(' ');
kusano 7d535a
if max([size(L) size(U) size(PROW) size(PCOL)])
kusano 7d535a
    L, U, PROW, PCOL
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 LU 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
pmmd = colamd(A);
kusano 7d535a
A = A(:,pmmd);
kusano 7d535a
r = trytime(A);
kusano 7d535a
if r > tol, disp('*** FAILED ***'), disp(' '), failed = [failed ntest]; end;
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;