code: def _kamada_kawai_costfn(pos_vec, np, invdist, meanweight, dim):
      # Cost-function and gradient for Kamada-Kawai layout algorithm
      nNodes = invdist.shape[0]
      pos_arr = pos_vec.reshape((nNodes, dim))
  
      delta = pos_arr[:, np.newaxis, :] - pos_arr[np.newaxis, :, :]
      nodesep = np.linalg.norm(delta, axis=-1)
      direction = np.einsum(
          "ijk,ij->ijk", delta, 1 / (nodesep + np.eye(nNodes) * 1e-3)
      )
  
      offset = nodesep * invdist - 1.0
      offset[np.diag_indices(nNodes)] = 0
  
      cost = 0.5 * np.sum(offset**2)
      grad = np.einsum(
          "ij,ij,ijk->ik", invdist, offset, direction
      ) - np.einsum("ij,ij,ijk->jk", invdist, offset, direction)
  
      # Additional parabolic term to encourage mean position to be near origin:
      sumpos = np.sum(pos_arr, axis=0)
      cost += 0.5 * meanweight * np.sum(sumpos**2)
      grad += meanweight * sumpos
  
      return (cost, grad.ravel())
  
imports: import networkx
  from networkx.utils import np_random_state
label_desc: Use image node_id 11 for calling a global function with example usage: _kamada_kawai_costfn(pos_vec, np, invdist, meanweight, dim) and returns: cost,  grad