function a_det = rref_determinant ( A )

%*****************************************************************************80
%
%% rref_determinant() uses reduced row echelon form (RREF) to compute a determinant.
%
%  Discussion:
%
%    The procedure will fail if A is not square.
%
%    This is simply a demonstration of how RREF can be used to compute
%    the determinant.  But:
%    * there are better ways to compute the inverse, including MATLAB's
%      d = det ( A )
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    04 December 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(N,N), a square, invertible matrix.
%
%  Output:
%
%    real A_DET: the determinant.
%
  [ m, n ] = size ( A );

  if ( m ~= n )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'rref_determinant(): Fatal error!\n' );
    fprintf ( 1, '  Input matrix is not square.\n' );
    error ( 'rref_determinant(): Fatal error!\n' );
  end
%
%  Do an RREF on A.
%
  [ arref, k ] = rref ( A );
%
%  Compute product of diagonal entries.
%
  a_det = 1.0;
  for i = 1 : n
    a_det = a_det * arref(i,i);
  end

  return
end

