def test_LU_example4_4x3(): A = Matrix([[1, 0, 2, 3], [2, -1, 3, 6], [1, 4, 4, 0]]) L, U = lu(A) assert (L @ U) == A L, U = lu(A.T) assert (L @ U) == A.T
def test_LU_example1_2x2(): A = Matrix([[3, 1], [4, 2]]) L, U = lu(A) assert L == Matrix([[1, 0], [4/3, 1]]) assert U == Matrix([[3, 1], [0, 0.6666666666666667]]) assert (L @ U) == A
def lu_inv(A): """ Calculating matrix inverse using LU-factorization: A = LU A @ A_inv = I LU @ A_inv = I L (U @ A_inv) = I (U @ A_inv) = L_inv A_inv = L_inv @ U_inv :param A: matrix :return: """ nrow, ncol = A.shape if nrow != ncol: print("Matrix is not invertible") return None L, U = lu(A) I = eye(nrow) Y = solve(L, I) A_inv = solve(U, Y) return A_inv
def test_LU_example3_3x3(): A = Matrix([[2, 6, 2], [-3, -8, 0], [4, 9, 2]]) L, U = lu(A) assert (L @ U) == A
def test_LU_example2_3x3_zero_row(): A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) L, U = lu(A) assert (L @ U) == A