def getErrorWord(error_ratio=0.5): """ Generate a error vector according to uniform probability distribution, (error_ratio, 1 - error_ratio). """ e = np.full(7, gf.GF2(0)) if np.random.rand() > (1 - error_ratio): error_index = np.random.randint(7) e[error_index] = gf.GF2(1) return e
def asGF2(arr): """ Return a numpy array that elements are converted to GF(2). """ if type(arr) is np.ndarray: if arr.size is not 0 and type(arr.item(0)) is gf.GF2: return arr src = np.asarray(arr) ones_indice = (src == 1) output = np.full(src.shape, gf.GF2(0)) output[ones_indice] = gf.GF2(1) return output
def __str__(self): (width, height) = self.v.shape edge = '-' * (5 * width - 1) pretty = edge for row in self.v: morse = ['*' if bit == gf.GF2(1) else ' ' for bit in row] pretty = pretty + '\n' + '| ' + ' | '.join(morse) + ' |' pretty = pretty + '\n' + edge return pretty
def findErrorWord(c): """ If error exist, return a 7-vector in GF(2) which error occurred entry is 1. Otherwise, return a zero 7-vector. """ H = getParityChecker() # Parity error vector, pe pe = np.dot(H, c) index = -1 for n in range(3): if pe[n] == gf.GF2(1): index = index + 2**n e = np.full(7, gf.GF2(0)) if index == -1: return e else: e[index] = gf.GF2(1) return e
""" State vector : State when push the (2,2) button. It represent state of light puzzle. Like, --------- | * | * | --------- | * | | --------- '*' represent button is light-on else empty space is light-off. """ # Push the right-bottom button rb = state(np.array([[gf.GF2(1), gf.GF2(1)], [gf.GF2(1), gf.GF2(0)]])) # Push the left-bottom button. lb = state(np.array([[gf.GF2(1), gf.GF2(1)], [gf.GF2(0), gf.GF2(1)]])) # Push the right-top button. rt = state(np.array([[gf.GF2(1), gf.GF2(0)], [gf.GF2(1), gf.GF2(1)]])) # Push the left-top button. lt = state(np.array([[gf.GF2(0), gf.GF2(1)], [gf.GF2(1), gf.GF2(1)]])) print('State when push the (2,2) button.') print(rb) print('State when push the (2,1) button.') print(lb)
def randGF2Vec(length): output = np.full(length, gf.GF2(1)) coords = np.random.randint(low=0, high=length, size=int(length / 2)) output[coords] = gf.GF2(0) return output
def gf2toint(gf2array): """ Convert numpy array in GF(2) to a integer list. """ return [1 if n == gf.GF2(1) else 0 for n in gf2array]