def test_reduce_to_bidiagonal(self): mat = Matrix.from_cols([[1, 2, 3], [4, 5, 6], [7, 8, 10]]) b, _, _ = reduce_to_bidiagonal(mat) utils.assert_upper_triangular(b) truncated = MatrixView.with_size( b, (0, 1), (mat.num_rows() - 1, mat.num_cols() - 1)).to_matrix() utils.assert_lower_triangular(truncated)
def test_lu(self): matrix = Matrix.from_cols([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) mat_l, mat_u = compute_lu_factorization(matrix) utils.assert_lower_triangular(mat_l) utils.assert_upper_triangular(mat_u) assert self.vector_all(utils.extract_diagonal(mat_l), 1) product = mat_l.multiply(mat_u) assert product.all_cols() == matrix.all_cols()
def test_lu_more_rows(self): mat_l = Matrix.from_cols([[1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 0, 1, 2, 3], [0, 0, 0, 1, 2], [0, 0, 0, 0, 1]]) mat_u = Matrix.from_cols([[5, 1, 2, 3, 4], [0, 4, 3, 2, 1], [0, 0, 2, 1, 0]]) matrix = mat_l.multiply(mat_u) mat_l, mat_u = compute_lu_factorization(matrix) # note LU factorization is not unique therefore we are not comparing L and U utils.assert_lower_triangular(mat_l) utils.assert_upper_triangular(mat_u) assert self.vector_all(utils.extract_diagonal(mat_l), 1) product = mat_l.multiply(mat_u) assert product.all_cols() == matrix.all_cols()
def check_svd(self, u, s, v, mat): utils.assert_orthonormal(u) utils.assert_orthonormal(v) utils.assert_lower_triangular(s) utils.assert_upper_triangular(s) diagonal = utils.extract_diagonal(s) for i in range(len(diagonal) - 1): assert diagonal[i] >= diagonal[i + 1] for elem in diagonal: assert elem >= 0 product = u.multiply(s).multiply(v.transpose()) product.print_full() mat.print_full() utils.assert_matrix_equal(product, mat)
def test_rectangular_triangular(self): utils.assert_upper_triangular( Matrix.from_cols([[4, 0], [1, 2], [3, 4]]))
def test_identity_triangular(self): identity = Matrix.identity(3) utils.assert_upper_triangular(identity) utils.assert_lower_triangular(identity)
def test_upper_triangular(self): mat = Matrix.from_cols([[1, 0, 0], [2, 3, 0], [4, 5, 6]]) utils.assert_upper_triangular(mat) with pytest.raises(AssertionError): utils.assert_upper_triangular(mat.transpose())