code: def calculate_PTDF(sub_network,skip_pre=False):
      
  
      if not skip_pre:
          calculate_B_H(sub_network)
  
      #calculate inverse of B with slack removed
  
      n_pvpq = len(sub_network.pvpqs)
      index = np.r_[:n_pvpq]
  
      I = csc_matrix((np.ones(n_pvpq), (index, index)))
  
      B_inverse = spsolve(sub_network.B[1:, 1:],I)
  
      #exception for two-node networks, where B_inverse is a 1d array
      if issparse(B_inverse):
          B_inverse = B_inverse.toarray()
      elif B_inverse.shape == (1,):
          B_inverse = B_inverse.reshape((1,1))
  
      #add back in zeroes for slack
      B_inverse = np.hstack((np.zeros((n_pvpq,1)),B_inverse))
      B_inverse = np.vstack((np.zeros(n_pvpq+1),B_inverse))
  
      sub_network.PTDF = sub_network.H*B_inverse
docs: Calculate the Power Transfer Distribution Factor (PTDF) for
      sub_network.
  
      Sets sub_network.PTDF as a (dense) numpy array.
  
      Parameters
      ----------
      sub_network : pypsa.SubNetwork
      skip_pre: bool, default False
          Skip the preliminary steps of computing topology, calculating dependent values,
          finding bus controls and computing B and H.