code: def _dm_to_fmt(M, fmt):
      """Convert a matrix to the given format and return the old format."""
      old_fmt = M.rep.fmt
      if old_fmt == fmt:
          pass
      elif fmt == "dense":
          M = M.to_dense()
      elif fmt == "sparse":
          M = M.to_sparse()
      else:
          raise ValueError(f"Unknown format: {fmt}")  # pragma: no cover
      return M, old_fmt
  
file_path: sympy/sympy/polys/matrices/rref.py
imports: from sympy.polys.domains import ZZ
  from sympy.polys.matrices.sdm import SDM, sdm_irref, sdm_rref_den
  from sympy.polys.matrices.ddm import DDM
  from sympy.polys.matrices.dense import ddm_irref, ddm_irref_den
label_desc: Use image node_id 3 for calling a global function with example usage: _dm_to_fmt(M, fmt) and returns: M,  old_fmt