function c = cond(A, p)
%COND   Condition number with respect to inversion.
%   COND(X) returns the 2-norm condition number (the ratio of the
%   largest singular value of X to the smallest).  Large condition
%   numbers indicate a nearly singular matrix.
%
%   COND(X,P) returns the condition number of X in P-norm:
%
%      NORM(X,P) * NORM(INV(X),P). 
%
%   where P = 1, 2, inf, or 'fro.' 
%
%   See also CONDEST, CONDEIG, NORM, NORMEST.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.9 $  $Date: 1997/11/21 23:38:19 $

if nargin == 1, p = 2; end

if isempty(A)
    c = 0;
    return
end
if issparse(A)
    warning('Use CONDEST instead of COND for sparse matrices.')
    c = condest(A);
    return
end

[m, n] = size(A);
if m ~= n & p ~= 2
   error('A is rectangular.  Use the 2 norm.')
end

if p == 2
   s = svd(A);
   if any(s == 0)   % Handle singular matrix
       c = Inf;
   else
       c = max(s)./min(s);
   end
else
%  We'll let NORM pick up any invalid p argument.
   c = norm(A, p) * norm(inv(A), p);
end
