code: def _rank(M, iszerofunc=_iszero, simplify=False):
      """Returns the rank of a matrix.
  
      Examples
      ========
  
      >>> 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 _permute_complexity_right(M, iszerofunc):
          """Permute columns with complicated elements as
          far right as they can go.  Since the ``sympy`` row reduction
          algorithms start on the left, having complexity right-shifted
          speeds things up.
  
          Returns a tuple (mat, perm) where perm is a permutation
          of the columns to perform to shift the complex columns right, and mat
          is the permuted matrix."""
  
          def complexity(i):
              # the complexity of a column will be judged by how many
              # element's zero-ness cannot be determined
              return sum(
                  1 if iszerofunc(e) is None else 0 for e in M[:, i]
              )
  
          complex = [(complexity(i), i) for i in range(M.cols)]
          perm = [j for (i, j) in sorted(complex)]
  
          return (M.permute(perm, orientation="cols"), perm)
  
      simpfunc = (
          simplify if isinstance(simplify, FunctionType) else _simplify
      )
  
      # for small matrices, we compute the rank explicitly
      # if is_zero on elements doesn't answer the question
      # for small matrices, we fall back to the full routine.
      if M.rows <= 0 or M.cols <= 0:
          return 0
  
      if M.rows <= 1 or M.cols <= 1:
          zeros = [iszerofunc(x) for x in M]
  
          if False in zeros:
              return 1
  
      if M.rows == 2 and M.cols == 2:
          zeros = [iszerofunc(x) for x in M]
  
          if False not in zeros and None not in zeros:
              return 0
  
          d = M.det()
  
          if iszerofunc(d) and False in zeros:
              return 1
          if iszerofunc(d) is False:
              return 2
  
      mat, _ = _permute_complexity_right(M, iszerofunc=iszerofunc)
      _, pivots, _ = _row_reduce(
          mat,
          iszerofunc,
          simpfunc,
          normalize_last=True,
          normalize=False,
          zero_above=False,
      )
  
      return len(pivots)
  
file_path: sympy/sympy/matrices/reductions.py
imports: from types import FunctionType
  from sympy.polys.polyerrors import CoercionFailed
  from sympy.polys.domains import ZZ, QQ
  from .utilities import _get_intermediate_simp, _iszero, _dotprodsimp, _simplify
  from .determinant import _find_reasonable_pivot
label_desc: Use image node_id 5 for calling a global function with example usage: _rank(M, iszerofunc, simplify) and returns: len, M,  perm, int, sum, int, int, int, int