function check = r8st_check ( m, n, nz_num, row, col )

%*****************************************************************************80
%
%% r8st_check() checks that a r8st matrix data structure is properly sorted.
%
%  Discussion:
%
%    This routine assumes that the data structure has been sorted,
%    so that the entries of ROW are ascending sorted, and that the
%    entries of COL are ascending sorted, within the group of entries
%    that have a common value of ROW.
%
%    The r8st storage format stores the row, column and value of each nonzero
%    entry of a sparse matrix.
%
%    The r8st format is used by CSPARSE ("sparse triplet"), SLAP
%    ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 July 2007
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, N, the number of rows and columns of
%    the matrix.
%
%    integer NZ_NUM, the number of nonzero elements in
%    the matrix.
%
%    integer ROW(NZ_NUM), COL(NZ_NUM), the row and
%    column indices of the nonzero elements.
%
%  Output:
%
%    logical CHECK, is TRUE if the matrix is properly defined.
%
  check = true;
%
%  Check 1 <= ROW(*) <= M.
%
  for k = 1 : nz_num

    if ( row(k) < 1 | m < row(k) )
      check = false;
      return
    end

  end
%
%  Check 1 <= COL(*) <= N.
%
  for k = 1 : nz_num

    if ( col(k) < 1 | n < col(k) )
      check = false;
      return
    end

  end
%
%  Check that ROW(K) <= ROW(K+1).
%
  for k = 1 : nz_num - 1

    if ( row(k+1) < row(k) )
      check = false;
      return
    end

  end
%
%  Check that, if ROW(K) == ROW(K+1), that COL(K) < COL(K+1).
%
  for k = 1 : nz_num - 1

    if ( row(k) == row(k+1) )
      if ( col(k+1) <= col(k) )
        check = false;
        return
      end
    end

  end

  return
end
