kusano 7d535a
function x = lusolve(A,b,Pcol)
kusano 7d535a
% LUSOLVE : Solve linear systems by supernodal LU factorization.
kusano 7d535a
% 
kusano 7d535a
%  x = lusolve(A, b) returns the solution to the linear system A*x = b,
kusano 7d535a
%      using a supernodal LU factorization that is faster than Matlab's 
kusano 7d535a
%      builtin LU.  This m-file just calls a mex routine to do the work.
kusano 7d535a
%
kusano 7d535a
%  By default, A is preordered by column minimum degree before factorization.
kusano 7d535a
%  Optionally, the user can supply a desired column ordering:
kusano 7d535a
%
kusano 7d535a
%  x = lusolve(A, b, pcol) uses pcol as a column permutation.  
kusano 7d535a
%      It still returns x = A\b, but it factors A(:,pcol) (if pcol is a 
kusano 7d535a
%      permutation vector) or A*Pcol (if Pcol is a permutation matrix).
kusano 7d535a
%       
kusano 7d535a
%  x = lusolve(A, b, 0) suppresses the default minimum degree ordering;
kusano 7d535a
%      that is, it forces the identity permutation on columns.
kusano 7d535a
%
kusano 7d535a
%  See also SUPERLU.
kusano 7d535a
%
kusano 7d535a
% John Gilbert, 6 April 1995
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
kusano 7d535a
[m,n] = size(A);
kusano 7d535a
if m ~= n 
kusano 7d535a
    error('matrix must be square'); 
kusano 7d535a
end;
kusano 7d535a
[mb,nb] = size(b);
kusano 7d535a
if mb ~= n 
kusano 7d535a
    error('right-hand side must have same row dimension as matrix');
kusano 7d535a
end;
kusano 7d535a
if n == 0
kusano 7d535a
    x = [];
kusano 7d535a
    return;
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
% As necessary, compute the column permutation, and
kusano 7d535a
% convert it from a permutation vector to a permutation matrix
kusano 7d535a
% to fit the internal data structures of mexlusolve.
kusano 7d535a
if nargin < 3 
kusano 7d535a
    Pcol = colamd(A);
kusano 7d535a
end;
kusano 7d535a
if isempty(Pcol) | Pcol == 0
kusano 7d535a
    Pcol = speye(n); 
kusano 7d535a
end;
kusano 7d535a
if min(size(Pcol)) == 1
kusano 7d535a
    Pcol = sparse(1:n,Pcol,1,n,n);
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
% Make sure the matrices are sparse and the vector is full.
kusano 7d535a
if ~issparse(A)
kusano 7d535a
    A = sparse(A);
kusano 7d535a
end;
kusano 7d535a
if issparse(b)
kusano 7d535a
    b = full(b);
kusano 7d535a
end;
kusano 7d535a
if ~issparse(Pcol)
kusano 7d535a
    Pcol = sparse(Pcol);
kusano 7d535a
end;
kusano 7d535a
kusano 7d535a
x = mexlusolve(A,b,Pcol);