def upper_triangle(mat_a_vec_b): m_row, n_col = matrix.shape(mat_a_vec_b) for i_pivot in range(m_row - 1): pivot = float(mat_a_vec_b[i_pivot][i_pivot]) for j_row in range(i_pivot + 1, m_row): ratio = -(mat_a_vec_b[j_row][i_pivot] / pivot) matrix.row_mul_add(mat_a_vec_b, j_row, i_pivot, ratio, i_pivot)
def test_row_mac_03(self): det_before = m.det(self.mat_g) m.row_mul_add(self.mat_g, 0, 2, 0.5) det_after = m.det(self.mat_g) self.mat_exp = [[0, -0.5, -1.0], [0, 1, 2], [-2, -1, 0]] self.assertSequenceEqual(self.mat_g, self.mat_exp) self.assertEqual(det_before, det_after)
def test_row_mac_00(self): m.row_mul_add(self.mat_i, 0, 1, 1) self.mat_exp = [ [1, 1], [0, 1], ] self.assertSequenceEqual(self.mat_i, self.mat_exp)
def test_row_mac_02(self): det_before = m.det(self.mat_g) m.row_mul_add(self.mat_g, 1, 2, -1) det_after = m.det(self.mat_g) self.mat_exp = [[1, 0, -1], [2, 2, 2], [-2, -1, 0]] self.assertSequenceEqual(self.mat_g, self.mat_exp) self.assertEqual(det_before, det_after)
def test_row_mac_01(self): det_before = m.det(self.mat_i) m.row_mul_add(self.mat_i, 1, 0, 0.5) det_after = m.det(self.mat_i) self.mat_exp = [ [1, 0], [0.5, 1.0], ] self.assertSequenceEqual(self.mat_i, self.mat_exp) self.assertEqual(det_before, det_after)
def gauss_jordan(mat_a): mat_ai = generate_mat_ai(mat_a) for i_pivot in range(len(mat_ai)): matrix.row_mul_scalar(mat_ai, i_pivot, 1.0 / mat_ai[i_pivot][i_pivot]) for j_row in range(len(mat_ai)): if i_pivot != j_row: matrix.row_mul_add(mat_ai, j_row, i_pivot, -mat_ai[j_row][i_pivot]) inv_mat = matrix.alloc_mat( len(mat_ai), len(mat_ai), ) for i in range(len(mat_ai)): for j in range(len(mat_ai)): inv_mat[i][j] = mat_ai[i][len(mat_ai) + j] del mat_ai[:] del mat_ai return inv_mat