Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # n is an integer, the number of quantization steps of the 4-tuple [w, x, y, z]
- def quantize(w, x, y, z, n) :
- if z == None :
- z = 1.0 - w - x - y
- #assert w + x + y + z == 1
- n = int(n)
- v = [w, x, y, z]
- vn = [a * n for a in v]
- vni = [int(a) for a in vn]
- vnf = [a - ai for a, ai in zip(vn, vni)]
- defect = n - sum(vni)
- if defect == 0 :
- cutoff = 1.0
- else :
- defects = vnf[:]
- defects.sort() # could replace with sorting network or min heap or something
- cutoff = defects[-defect]
- def cround(xi, xf) :
- if xf < cutoff :
- return float(xi) / n
- else :
- return float(xi + 1) / n
- return [cround(ai, af) for ai, af in zip(vni, vnf)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement