def test_set_row(self): """ Tests that set row works correctly. """ m = IntMatrix((3, 3)) m.set_row(0, [1, 1, 0]) m.set_row(1, [0, 1, 1]) m.set_row(2, [0, 1, 0]) # Checking reverse order, as zeroth position is the least significant bit. self.assertEqual(m.data[0], 0b011) self.assertEqual(m.data[1], 0b110) self.assertEqual(m.data[2], 0b010)
def test_get_column(self): """ Tests that get_column works correctly. """ m1 = IntMatrix((4, 4)) m1.set_row(0, [1, 1, 1, 0]) m1.set_row(1, [1, 0, 1, 0]) m1.set_row(2, [0, 1, 1, 0]) m1.set_row(3, [0, 0, 1, 0]) self.assertListEqual(m1.get_column(0), [1, 1, 0, 0]) self.assertListEqual(m1.get_column(1), [1, 0, 1, 0]) self.assertListEqual(m1.get_column(2), [1, 1, 1, 1]) self.assertListEqual(m1.get_column(3), [0, 0, 0, 0])
def test_rank_1(self): """ Tests that the rank is calculated correctly. The rank was verified by hand. """ # Checks rank for 0 matrix m = IntMatrix() self.assertEqual(m.rank(), 0) m1 = IntMatrix((4, 4)) m1.set_row(0, [1, 1, 1, 0]) m1.set_row(1, [1, 0, 1, 0]) m1.set_row(2, [0, 1, 1, 0]) m1.set_row(3, [0, 0, 1, 0]) self.assertEqual(m1.rank(), 3, "Wrong Rank")
def create_random_matrix(rng: RNG, matrix_size): """ Create a matrix_size*matrix_size with random entries from GF(2). :param rng: Random number generator to use when generating the matrix entries. :param matrix_size: Size of the matrix. Note the matrices are square. :return: GF2Matrix with random entries. """ m = IntMatrix((matrix_size, matrix_size)) for i in range(matrix_size): # Get a row vector of the 64 bits. # not sure why the cast to int is required, but get a type error otherwise. row_vector = [ int(i) for i in np.binary_repr(int(rng.next_64_bits()), 64) ] m.set_row(i, row_vector[0:matrix_size]) return m
def test_get_row(self): """ Tests that get row works correctly. """ m = IntMatrix((4, 4)) row1 = [1, 1, 0, 1] row2 = [0, 1, 1, 1] row3 = [0, 1, 0, 1] row4 = [1, 0, 0, 1] m.set_row(0, row1) m.set_row(1, row2) m.set_row(2, row3) m.set_row(3, row4) self.assertListEqual(m.get_row(0), row1, "Wrong value in row.") self.assertListEqual(m.get_row(1), row2, "Wrong value in row.") self.assertListEqual(m.get_row(2), row3, "Wrong value in row.") self.assertListEqual(m.get_row(3), row4, "Wrong value in row.")
def test_rank_2(self): """ Tests that the rank is calculated correctly. The rank was verified by hand. """ m2 = IntMatrix((5, 4)) m2.set_row(0, [1, 0, 1, 0]) m2.set_row(1, [1, 1, 0, 1]) m2.set_row(2, [0, 0, 1, 0]) m2.set_row(3, [1, 1, 1, 1]) m2.set_row(4, [1, 0, 0, 0]) self.assertEqual(m2.rank(), 3, "Wrong rank")
def test_mul(self): """ Tests that multiplication works correctly. """ m1 = IntMatrix((2, 3)) m1.set_row(0, [1, 0, 1]) m1.set_row(1, [0, 0, 1]) m2 = IntMatrix((3, 2)) m2.set_row(0, [1, 0]) m2.set_row(1, [1, 1]) m2.set_row(2, [0, 1]) result = m1 * m2 self.assertEqual(result.size(), (2, 2)) self.assertListEqual(result.get_row(0), [1, 1]) self.assertListEqual(result.get_row(1), [0, 1])