def itransform(entry, direction, width, x): """ Inverse transform - we simply reverse the operations in transform. """ assert x < 2**width assert entry < 2**width return utils.lrot(x, direction + 1, width) ^ entry
def itransform(entry, direction, width, x): """ Inverse transform - we simply reverse the operations in transform. """ assert x < 2**width assert entry < 2**width return utils.lrot(x, direction+1, width)^entry
def hilbert_index(dimension, order, p): h, e, d = 0, 0, 0 for i in range(order): l = 0 for x in range(dimension): b = utils.bitrange(p[dimension - x - 1], order, i, i + 1) l |= b << x l = transform(e, d, dimension, l) w = utils.igraycode(l) e = e ^ utils.lrot(entry(w), d + 1, dimension) d = (d + direction(w, dimension) + 1) % dimension h = (h << dimension) | w return h
def hilbert_index(dimension, order, p): # Hamilton's paper initialises d to 0, which appears to be an error. h, e, d = 0, 0, 1 for i in range(order): l = 0 for x in range(dimension): b = utils.bitrange(p[x], order, i, i+1) l |= b<<x l = transform(e, d, dimension, l) w = utils.igraycode(l) e = e ^ utils.lrot(entry(w), d+1, dimension) d = (d + direction(w, dimension) + 1)%dimension h = (h<<dimension)|w return h
def hilbert_point(dimension, order, h): """ Convert an index on the Hilbert curve of the specified dimension and order to a set of point coordinates. """ # The bit widths in this function are: # p[*] - order # h - order*dimension # l - dimension # e - dimension hwidth = order*dimension e, d = 0, 0 p = [0]*dimension for i in range(order): w = utils.bitrange(h, hwidth, i*dimension, i*dimension+dimension) l = utils.graycode(w) l = itransform(e, d, dimension, l) for j in range(dimension): b = utils.bitrange(l, dimension, j, j+1) p[j] = utils.setbit(p[j], order, i, b) e = e ^ utils.lrot(entry(w), d+1, dimension) d = (d + direction(w, dimension) + 1)%dimension return p
def hilbert_point(dimension, order, h): """ Convert an index on the Hilbert curve of the specified dimension and order to a set of point coordinates. """ # The bit widths in this function are: # p[*] - order # h - order*dimension # l - dimension # e - dimension hwidth = order * dimension e, d = 0, 0 p = [0] * dimension for i in range(order): w = utils.bitrange(h, hwidth, i * dimension, i * dimension + dimension) l = utils.graycode(w) l = itransform(e, d, dimension, l) for j in range(dimension): b = utils.bitrange(l, dimension, j, j + 1) p[j] = utils.setbit(p[j], order, i, b) e = e ^ utils.lrot(entry(w), d + 1, dimension) d = (d + direction(w, dimension) + 1) % dimension return p
def rotpair(self, left, right, i, width): assert utils.rrot(left, i, width) == right assert utils.lrot(right, i, width) == left assert utils.lrot(left, i, width) == utils.rrot(left, width-i, width)
def rotpair(self, left, right, i, width): assert utils.rrot(left, i, width) == right assert utils.lrot(right, i, width) == left assert utils.lrot(left, i, width) == utils.rrot(left, width - i, width)