Created
June 5, 2015 04:03
-
-
Save kmill/efc428652b0e2cf79ef9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment