def test_row_iterations(): matrix = BinaryMatrix(4, [[0, 1], [2, 3], [1, 2]]) rows = matrix.rows() assert rows.__next__() == BinaryVector(4, [0, 1]) assert rows.__next__() == BinaryVector(4, [2, 3]) assert rows.__next__() == BinaryVector(4, [1, 2]) with pytest.raises(StopIteration): rows.__next__()
def decode(self, message): syndrome = self.code.syndrome_of(message) bit = self.bit_to_flip(syndrome) if bit: # To flip the bit, we addition a vector with a single 1 at the corresponding position. return message + BinaryVector(7, [bit]) else: # It is already a codeword. return message
def test_access(): vector = BinaryVector(5, [1, 3]) assert vector.element(0) == 0 assert vector.element(1) == 1 assert vector.element(2) == 0 assert vector.element(3) == 1 assert vector.element(4) == 0 with pytest.raises(IndexError): vector.element(5)
def __init__(self, code): self.code = code length = len(code) self.limit = (length - 1) / 2 self.zero_codeword = BinaryVector(length, list()) self.one_codeword = BinaryVector(length, list(range(length)))
def test_to_dense(): vector = BinaryVector(5, [1, 3]) dense = to_dense(vector) expected = np.array([0, 1, 0, 1, 0]) np.testing.assert_array_equal(dense, expected)
def test_row_access(): matrix = BinaryMatrix(4, [[0, 1], [2, 3], [1, 2]]) assert matrix.row(2) == BinaryVector(4, [1, 2]) with pytest.raises(IndexError): matrix.row(10)