Exemple #1
0
def test_LUdecomp():
    testmat = Matrix([[0, 2, 5, 3], [3, 3, 7, 4], [8, 4, 0, 2], [-2, 6, 3, 4]])
    L, U, p = testmat.LUdecomposition()
    assert L.is_lower()
    assert U.is_upper()
    assert (L * U).permuteBkwd(p) - testmat == zeros(4)

    testmat = Matrix([[6, -2, 7, 4], [0, 3, 6, 7], [1, -2, 7, 4],
                      [-9, 2, 6, 3]])
    L, U, p = testmat.LUdecomposition()
    assert L.is_lower()
    assert U.is_upper()
    assert (L * U).permuteBkwd(p) - testmat == zeros(4)

    x, y, z = Symbol('x'), Symbol('y'), Symbol('z')
    M = Matrix(((1, x, 1), (2, y, 0), (y, 0, z)))
    L, U, p = M.LUdecomposition()
    assert L.is_lower()
    assert U.is_upper()
    assert (L * U).permuteBkwd(p) - M == zeros(3)

    # test FF LUdecomp
    M = Matrix([[1, 3, 3], [3, 2, 6], [3, 2, 2]])
    P, L, Dee, U = M.LUdecompositionFF()
    assert P * M == L * Dee.inv() * U

    M = Matrix([[1, 2, 3, 4], [3, -1, 2, 3], [3, 1, 3, -2], [6, -1, 0, 2]])
    P, L, Dee, U = M.LUdecompositionFF()
    assert P * M == L * Dee.inv() * U