code: def _inv_DM(dM, cancel=True):
      """Calculates the inverse using ``DomainMatrix``.
  
      See Also
      ========
  
      inv
      inverse_ADJ
      inverse_GE
      inverse_CH
      inverse_LDL
      sympy.polys.matrices.domainmatrix.DomainMatrix.inv
      """
      m, n = dM.shape
      if m != n:
          raise NonSquareMatrixError(
              "A Matrix must be square to invert."
          )
  
      try:
          dMi, den = dM.inv_den()
      except DMNonInvertibleMatrixError:
          raise NonInvertibleMatrixError(
              "Matrix det == 0; not invertible."
          )
  
      if cancel:
          # Convert to field and cancel with the denominator.
          if not dMi.domain.is_Field:
              dMi = dMi.to_field()
          Mi = (dMi / den).to_Matrix()
      else:
          # Convert to Matrix and divide without cancelling
          Mi = dMi.to_Matrix() / dMi.domain.to_sympy(den)
  
      return Mi
  
file_path: sympy/sympy/matrices/inverse.py
imports: from sympy.polys.matrices.exceptions import DMNonInvertibleMatrixError
  from sympy.polys.domains import EX
  from .exceptions import MatrixError, NonSquareMatrixError, NonInvertibleMatrixError
  from .utilities import _iszero
label_desc: Use image node_id 13 for calling a global function with example usage: _inv_DM(dM, cancel) and returns: Mi