code: def mat_to_laplacian(mat, normalized):
      
      if sps.issparse(mat):
          if np.all(mat.diagonal()>=0): # Check diagonal
              if np.all((mat-sps.diags(mat.diagonal())).data <= 0): # Check off-diagonal elements
                  return mat
      else:
          if np.all(np.diag(mat)>=0): # Check diagonal
              if np.all(mat - np.diag(mat) <= 0): # Check off-diagonal elements
                  return mat
      deg = np.squeeze(np.asarray(mat.sum(axis=1)))
      if sps.issparse(mat):
          L = sps.diags(deg) - mat
      else:
          L = np.diag(deg) - mat
      if not normalized:
          return L
      with np.errstate(divide='ignore'):
          sqrt_deg = 1.0 / np.sqrt(deg)
      sqrt_deg[sqrt_deg==np.inf] = 0
      if sps.issparse(mat):
          sqrt_deg_mat = sps.diags(sqrt_deg)
      else:
          sqrt_deg_mat = np.diag(sqrt_deg)
      return sqrt_deg_mat.dot(L).dot(sqrt_deg_mat)
docs: Converts a sparse or dence adjacency matrix to Laplacian.
      
      Parameters
      ----------
      mat : obj
          Input adjacency matrix. If it is a Laplacian matrix already, return it.
      normalized : bool
          Whether to use normalized Laplacian.
          Normalized and unnormalized Laplacians capture different properties of graphs, e.g. normalized Laplacian spectrum can determine whether a graph is bipartite, but not the number of its edges. We recommend using normalized Laplacian.
  
      Returns
      -------
      obj
          Laplacian of the input adjacency matrix
  
      Examples
      --------
      >>> mat_to_laplacian(numpy.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]), False)
      [[ 2, -1, -1], [-1,  2, -1], [-1, -1,  2]]