code: def calculate_B_H(sub_network,skip_pre=False):
      
  
      network = sub_network.network
  
      if not skip_pre:
          calculate_dependent_values(network)
          find_bus_controls(sub_network)
  
      if network.sub_networks.at[sub_network.name,"carrier"] == "DC":
          attribute="r_pu_eff"
      else:
          attribute="x_pu_eff"
  
      #following leans heavily on pypower.makeBdc
  
      #susceptances
      b = 1./np.concatenate([(c.df.loc[c.ind, attribute]).values \
                             for c in sub_network.iterate_components(network.passive_branch_components)])
  
  
      if np.isnan(b).any():
          logger.warning("Warning! Some series impedances are zero - this will cause a singularity in LPF!")
      b_diag = csr_matrix((b, (r_[:len(b)], r_[:len(b)])))
  
      #incidence matrix
      sub_network.K = sub_network.incidence_matrix(busorder=sub_network.buses_o)
  
      sub_network.H = b_diag*sub_network.K.T
  
      #weighted Laplacian
      sub_network.B = sub_network.K * sub_network.H
  
  
      sub_network.p_branch_shift = -b*np.concatenate([(c.df.loc[c.ind, "phase_shift"]).values*np.pi/180. if c.name == "Transformer"
                                                      else np.zeros((len(c.ind),))
                                                      for c in sub_network.iterate_components(network.passive_branch_components)])
  
      sub_network.p_bus_shift = sub_network.K * sub_network.p_branch_shift
docs: Calculate B and H matrices for AC or DC sub-networks.