code: def modularity(matrix, clusters):
      
      matrix = convert_to_adjacency_matrix(matrix)
      m = matrix.sum()
  
      if isspmatrix(matrix):
          matrix_2 = matrix.tocsr(copy=True)
      else :
          matrix_2 = matrix
  
      if is_undirected(matrix):
          expected = lambda i,j : (( matrix_2[i,:].sum() + matrix[:,i].sum() )*
                                   ( matrix[:,j].sum() + matrix_2[j,:].sum() ))
      else:
          expected = lambda i,j : ( matrix_2[i,:].sum()*matrix[:,j].sum() )
      
      delta   = delta_matrix(matrix, clusters)
      indices = np.array(delta.nonzero())
      
      Q = sum( matrix[i, j] - expected(i, j)/m for i, j in indices.T )/m
      
      return Q
docs: Compute the modularity
  
      :param matrix: The adjacency matrix
      :param clusters: The clusters returned by get_clusters
      :returns: modularity value