function value = is_rref ( A )

%*****************************************************************************80
%
%% is_rref() determines if a matrix is in reduced row echelon format.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    15 December 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    A(m,n): a matrix.
%
%  Output:
%
%    is_ref: True if A is in reduced row echelon form.
%
  [ m, n ] = size ( A );

  c = 0;

  for r = 1 : m
%
%  Increment the first legal column for next pivot.
%
    c = c + 1;
%
%  Search for pivot p in this row.
%  If none, set p = n + 1.
%
    p = n + 1;
    for j = 1 : n
      if ( A(r,j) ~= 0.0 )
        p = j;
        break;
      end
    end
%
%  If p == n + 1, go to next row.
%
    if ( p == n + 1 )
      continue;
    end
%
%  If p is too early, fail.
%
    if ( p < c )
      value = false;
      return
    end
%
%  Accept p as new c.
%
    c = p;
%
%  If A(r,c) is not 1, fail
%
    if ( A(r,c) ~= 1.0 )
      value = false;
      return
    end
%
%  If A(r,c) is not the only nonzero in column c, fail
%
    for i = 1 : m
      if ( i ~= r )
        if ( A(i,c) ~= 0.0 )
          value = false;
          return
        end
      end
    end

  end

  value = true;

  return
end

