'Returns the rank of a matrix
>>> from sympy import Matrix
>>> from sympy.abc import x
>>> m = Matrix([[1, 2], [x, 1 - 1/x]])
>>> m.rank()
2
>>> n = Matrix(3, 3, range(1, 10))
>>> n.rank()
2'

def rank(self, iszerofunc=_iszero, simplify=False):

    simpfunc = (simplify if isinstance(simplify, FunctionType) else _simplify)
    if ((self.rows <= 0) or (self.cols <= 0)):
        return 0
    if ((self.rows <= 1) or (self.cols <= 1)):
        zeros = [iszerofunc(x) for x in self]
        if (False in zeros):
            return 1
    if ((self.rows == 2) and (self.cols == 2)):
        zeros = [iszerofunc(x) for x in self]
        if ((not (False in zeros)) and (not (None in zeros))):
            return 0
        det = self.det()
        if (iszerofunc(det) and (False in zeros)):
            return 1
        if (iszerofunc(det) is False):
            return 2
    (mat, _) = self._permute_complexity_right(iszerofunc=iszerofunc)
    (echelon_form, pivots, swaps) = mat._eval_echelon_form(iszerofunc=iszerofunc, simpfunc=simpfunc)
    return len(pivots)
