def _zero_second_column(self): if self._mat[1, 1] == 0 or self._mat[2, 1] % self._mat[1, 1] == 0: self._second_exact_division() if self._mat[2, 1] < 0: self.flip_sign_row(2) r, s, t = extended_gcd(self._mat[1, 1], self._mat[2, 1]) self._set_zero(1, 2, self._mat[1, 1], self._mat[2, 1], r, s, t)
def _test_extended_gcd_n_times(self, n): for i in range(n): aa, bb = numpy.random.randint(10, size=2) + 1 r, s, t = extended_gcd(aa, bb) # print("%d = %d * (%d) + %d * (%d)" % # (r, aa, s, bb, t)) wanted = aa * s + bb * t self.assertEqual(r, 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 _zero_first_ele_in_row_i(self, i): if self._mat[i, 0] < 0: self.flip_sign_row(i) r, s, t = extended_gcd(self._mat[0, 0], self._mat[i, 0]) self._set_zero(0, i, self._mat[0, 0], self._mat[i, 0], r, s, t)