code: def impose_collapse(pairs, samples, weights):
      """collapse the weight and position of each pair (i,j) in pairs
  
      Collapse is defined as weight[j] += weight[i] and weights[i] = 0,
      with samples[j] = samples[i].
  
      Inputs:
          samples -- a list of sample points
          weights -- a list of sample weights
          pairs -- set of tuples of indices (i,j) where collapse occurs
  
      Examples:
          >>> impose_collapse({(0,1),(0,2)},[1,2,3,4,5],[.2,.2,.2,.2,.2])
          ([1.5999999999999996, 1.5999999999999996, 1.5999999999999996, 4.5999999999999996, 5.5999999999999996], [0.6000000000000001, 0.0, 0.0, 0.2, 0.2])
  
          >>> impose_collapse({(0,1),(3,4)},[1,2,3,4,5],[.2,.2,.2,.2,.2])
          ([1.3999999999999999, 1.3999999999999999, 3.3999999999999999, 4.4000000000000004, 4.4000000000000004], [0.4, 0.0, 0.2, 0.4, 0.0])
  
      Notes: is 'mean-preserving' for samples and 'norm-preserving' for weights
      """
      samples, weights = list(samples), list(
          weights
      )  # don't edit inputs
      m = mean(samples, weights)
      # allow negative indexing
      pairs = zip(
          *tuple(
              tuple(len(weights) + i if i < 0 else i for i in j)
              for j in zip(*pairs)
          )
      )
      # XXX: any vectorized way to do this?
      from mystic.tools import connected
  
      pairs = connected(pairs)
      for i, j in pairs.items():
          v = weights[i]
          for k in j:
              v += weights[k]
              weights[k] = type(v)(0.0)
              samples[k] = samples[i]
          weights[i] = v
      return impose_mean(m, samples, weights), weights
  
imports: from mystic.math.stats import *
  from mystic.math.samples import *
  from mystic.math.integrate import *
  from mystic.symbolic import generate_solvers, generate_constraint, solve
  from mystic.symbolic import generate_conditions, generate_penalty
  from mystic.math import almostEqual
  from mystic.math.distance import Lnorm
  from numpy import asarray
label_desc: Use image node_id 52 for calling a global function with example usage: impose_collapse(pairs, samples, weights) and returns: impose_mean,  weights