function ival = r8mat_is_ref ( m, n, a )

%*****************************************************************************80
%
%% R8MAT_IS_REF determines whether an R8MAT is in row echelon form.
%
%  Discussion:
%
%    An R8MAT is a matrix of real values.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    08 April 2017
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer M, N, the ow and column dimensions of 
%    the matrix.  M and N must be positive.
%
%    Input, real A(M,N), the matrix.
%
%    Output, integer IVAL:
%    -1, the matrix is not in row echelon form.
%    1, the matrix is in row echelon form.
%    2, the matrix is in reduced row echelon form.
%
  ival = 2;
  last_one = 0;

  for i = 1 : m

    this_one = n + 1;

    for j = n : -1 : 1
      if ( a(i,j) ~= 0.0 )
        this_one = j;
      end
    end

    if ( this_one <= n )

      if ( a(i,this_one) ~= 1.0 )
        ival = -1;
        return
      end

      if ( this_one <= last_one )
        ival = -1;
        return
      end

      for i2 = 1 : m
        if ( i2 ~= i )
          if ( a(i2,this_one) ~= 0.0 )
            ival = 1;
          end
        end
      end

    end

    last_one = this_one;

  end

  return
end
