function s = detperm(P)
% DETPERM  Compute the determinant of a permutation matrix.
% (Merely needed to compute sign in gooddet.m.)

[n m] = size(P);
if n ~= m, error('not square'), end
if n == 1, s = 1; return, end

k = find(P(1,:));        % column index of "1" in first row
if length(k) > 1, error('not a permutation matrix'), end
s = (-1)^(k-1);
for j=2:n-1
  % replace P by the minor:
  if k == 1
    P = P(2:end,2:end);
  elseif k == size(P,2)
    P = P(2:end,1:end-1);
  else
    P = P(2:end,[1:k-1, k+1:end]);
  end
  k = find(P(1,:));
  if length(k) > 1, error('not a permutation matrix'), end
  s = s * (-1)^(k-1);
end
