def test_bugs_get_snf(self): mat = IntMat3x3([-1, 1, 1, 1, -1, 1, 1, 1, -1]) ori_mat = copy.copy(mat) snf_D, snf_S, snf_T = mat.get_snf() SAT = numpy.matmul(snf_S, numpy.matmul(ori_mat.mat, snf_T)) wanted_mat = numpy.array([1, 0, 0, 0, 2, 0, 0, 0, 2]).reshape((3, 3)) numpy.testing.assert_almost_equal(SAT, wanted_mat) numpy.testing.assert_almost_equal(snf_D, wanted_mat)
def test_dead_loop_bug(self): mat = IntMat3x3([1, 0, 0, 1, 1, 0, 0, 0, 7]) # import pdb; pdb.set_trace() ori_mat = copy.copy(mat) snf_D, snf_S, snf_T = mat.get_snf() SAT = numpy.matmul(snf_S, numpy.matmul(ori_mat.mat, snf_T)) wanted_mat = numpy.array([1, 0, 0, 0, 1, 0, 0, 0, 7]).reshape((3, 3)) numpy.testing.assert_almost_equal(SAT, wanted_mat) numpy.testing.assert_almost_equal(snf_D, wanted_mat)
def test_positive_diag(self): mat = IntMat3x3([2, 0, 0, 0, 6, 0, 0, 0, -12]) ori_mat = copy.copy(mat) mat._positive_diag() wanted_mat = numpy.array([2, 0, 0, 0, 6, 0, 0, 0, 12]).reshape((3, 3)) wanted_op = numpy.array([1, 0, 0, 0, 1, 0, 0, 0, -1]).reshape((3, 3)) numpy.testing.assert_almost_equal(mat.mat, wanted_mat) numpy.testing.assert_almost_equal(mat.opL, wanted_op) # make sure operation is right, which can restore origin matrix wanted = mat.mat got = numpy.matmul(mat.opL, numpy.matmul(ori_mat.mat, mat.opR)) numpy.testing.assert_almost_equal(got, wanted)
def test_second_exact_division(self): mat = IntMat3x3([1, 0, 0, 0, 1, 0, 0, 1, 7]) ori_mat = copy.copy(mat) mat._second_exact_division() wanted_mat = numpy.array([1, 0, 0, 0, 1, 0, 0, 0, 7]).reshape((3, 3)) wanted_op = numpy.array([1, 0, 0, 0, 1, 0, 0, -1, 1]).reshape((3, 3)) numpy.testing.assert_almost_equal(mat.mat, wanted_mat) numpy.testing.assert_almost_equal(mat.opL, wanted_op) # make sure operation is right, which can restore origin matrix wanted = mat.mat got = numpy.matmul(mat.opL, numpy.matmul(ori_mat.mat, mat.opR)) numpy.testing.assert_almost_equal(got, wanted)
def test_set_zero(self): mat = IntMat3x3([3, 4, 5, 0, 1, 2, 6, 7, 8]) ori_mat = copy.copy(mat) r, s, t = extended_gcd(mat.mat[0, 0], mat.mat[2, 0]) mat._set_zero(0, 2, mat.mat[0, 0], mat.mat[2, 0], r, s, t) wanted_mat = numpy.array([3, 4, 5, 0, 1, 2, 0, -1, -2]).reshape((3, 3)) wanted_op = numpy.array([1, 0, 0, 0, 1, 0, -2, 0, 1]).reshape((3, 3)) numpy.testing.assert_almost_equal(mat.mat, wanted_mat) numpy.testing.assert_almost_equal(mat.opL, wanted_op) # make sure operation is right, which can restore origin matrix wanted = mat.mat got = numpy.matmul(mat.opL, numpy.matmul(ori_mat.mat, mat.opR)) numpy.testing.assert_almost_equal(got, wanted)
def test_det_1_bug(self): """ 需要保证左乘和右乘矩阵的行列式为1 """ mat = IntMat3x3([7, -5, -3, 3, 1, 6, -5, -5, 5]) ori_mat = copy.copy(mat) # import pdb; pdb.set_trace() snf_D, snf_S, snf_T = mat.get_snf() SAT = numpy.matmul(snf_S, numpy.matmul(ori_mat.mat, snf_T)) wanted_mat = numpy.array([1, 0, 0, 0, 1, 0, 0, 0, 500]).reshape((3, 3)) numpy.testing.assert_almost_equal(SAT, wanted_mat) numpy.testing.assert_almost_equal(snf_D, wanted_mat) numpy.testing.assert_almost_equal(numpy.linalg.det(snf_S), 1) numpy.testing.assert_almost_equal(numpy.linalg.det(snf_T), 1)
def test_snf_random(self): for i in range(100): mat = self._get_random_mat() mat = IntMat3x3(mat) ori_mat = copy.copy(mat) snf_D, snf_S, snf_T = mat.get_snf() SAT = numpy.matmul(snf_S, numpy.matmul(ori_mat.mat, snf_T)) # print("mat", ori_mat.mat) # print("snf_D", snf_D) # print("snf_S", snf_S) # print("snf_T", snf_T) # print("det S", numpy.linalg.det(snf_S)) # print("det T", numpy.linalg.det(snf_T)) numpy.testing.assert_almost_equal(SAT, snf_D) numpy.testing.assert_almost_equal(numpy.linalg.det(snf_S), 1) numpy.testing.assert_almost_equal(numpy.linalg.det(snf_T), 1)
def setUp(self): self.mat = IntMat3x3([0, 1, 2, 3, 4, 5, 6, 7, 8]) self.realmat = IntMat3x3([2, 4, 4, -6, 6, 12, 10, -4, -16])
def test_snf_diag_incremental(self): for i in range(10): mat = numpy.random.randint(100, size=9).reshape((3, 3)) mat = IntMat3x3(mat) list_diag = numpy.diagonal(mat.mat).tolist() self.assertTrue(sorted(list_diag), list_diag)
def test_snf_diag_positive(self): for i in range(10): mat = numpy.random.randint(100, size=9).reshape((3, 3)) mat = IntMat3x3(mat) self.assertTrue(numpy.all(mat.mat >= numpy.zeros_like(mat.mat)))
def test_snf_diag(self): for i in range(100): mat = numpy.random.randint(100, size=9).reshape((3, 3)) mat = IntMat3x3(mat) snf_D, _, _ = mat.get_snf() self.assertTrue(self.is_diag(snf_D))