コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
def test_LU_example3_3x3():
    A = Matrix([[2, 6, 2],
                [-3, -8, 0],
                [4, 9, 2]])
    L, U = lu(A)
    assert (L @ U) == A
コード例 #5
0
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