Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. # n is an integer, the number of quantization steps of the 4-tuple [w, x, y, z]
  2. def quantize(w, x, y, z, n) :
  3. if z == None :
  4. z = 1.0 - w - x - y
  5. #assert w + x + y + z == 1
  6. n = int(n)
  7.  
  8. v = [w, x, y, z]
  9. vn = [a * n for a in v]
  10. vni = [int(a) for a in vn]
  11. vnf = [a - ai for a, ai in zip(vn, vni)]
  12. defect = n - sum(vni)
  13.  
  14. if defect == 0 :
  15. cutoff = 1.0
  16. else :
  17. defects = vnf[:]
  18. defects.sort() # could replace with sorting network or min heap or something
  19. cutoff = defects[-defect]
  20.  
  21. def cround(xi, xf) :
  22. if xf < cutoff :
  23. return float(xi) / n
  24. else :
  25. return float(xi + 1) / n
  26.  
  27. return [cround(ai, af) for ai, af in zip(vni, vnf)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement