示例#1
0
def solve(A, b, pivoting):
    # No pivoting
    if pivoting == 0:
        L, U = lu.lu_out_of_place(A)
        z = forward_substitution(L, b)
        x = back_substitution(U, z)
        return x

    # Partial pivoting
    if pivoting == 1:
        P, L, U = lu.lu_partial_pivot(A)
        z = forward_substitution(L, np.dot(P.transpose(), b))
        x = back_substitution(U, z)
        return x

    # Complete pivoting
    if pivoting == 2:
        P, Q, L, U = lu.lu_complete_pivot(A)
        z = forward_substitution(L, np.dot(P.transpose(), b))
        x = np.dot(Q.transpose(), back_substitution(U, z))
        return x

    # Rook pivoting
    if pivoting == 3:
        P, Q, L, U = lu.lu_rook_pivot(A)
        z = forward_substitution(L, np.dot(P.transpose(), b))
        x = np.dot(Q.transpose(), back_substitution(U, z))
        return x

    # Blok PP
    if pivoting == 4:
        P, L, U = lu_block.lu_partial_block(A, 32)
        z = forward_substitution(L, np.dot(P.transpose(), b))
        x = back_substitution(U, z)
        return x
示例#2
0
def inverse(A, pivoting):
    (m, n) = A.shape
    A_inverse = np.zeros((m, m))
    b = np.identity(m)

    # No pivot
    if pivoting == 0:
        L, U = lu.lu_out_of_place(A)
        for k in range(m):
            z = forward_substitution(L, b[:, k])
            x = back_substitution(U, z)
            A_inverse[:, k, np.newaxis] = x
        return A_inverse

    # Partial pivot
    if pivoting == 1:
        P, L, U = lu.lu_partial_pivot(A)
        for k in range(m):
            z = forward_substitution(L, np.dot(P.transpose(), b[:, k]))
            x = back_substitution(U, z)
            A_inverse[:, k, np.newaxis] = x
        return A_inverse

    # Complete pivot
    if pivoting == 2:
        P, Q, L, U = lu.lu_complete_pivot(A)
        for k in range(m):
            z = forward_substitution(L, np.dot(P.transpose(), b[:, k]))
            x = np.dot(Q.transpose(), back_substitution(U, z))
            A_inverse[:, k, np.newaxis] = x
        return A_inverse

    # Rook pivot
    if pivoting == 3:
        P, Q, L, U = lu.lu_rook_pivot(A)
        for k in range(m):
            z = forward_substitution(L, np.dot(P.transpose(), b[:, k]))
            x = np.dot(Q.transpose(), back_substitution(U, z))
            A_inverse[:, k, np.newaxis] = x
        return A_inverse

    # PP block
    if pivoting == 4:
        P, L, U = lu_block.lu_partial_block(A, 32)
        for k in range(m):
            z = forward_substitution(L, np.dot(P.transpose(), b[:, k]))
            x = back_substitution(U, z)
            A_inverse[:, k, np.newaxis] = x
        return A_inverse
示例#3
0
def precision_test(minsize, maxsize, step, repeat):
    plot_data  = []
    y_sp       = []
    y_partial  = []
    y_complete = []
    y_rook     = []
    y_block    = []
    for index, i in enumerate(range(minsize, maxsize, step)):
        test_sp       = []
        test_my       = []
        test_lu       = []
        test_partial  = []
        test_complete = []
        test_rook     = []
        test_block    = []
        for j in range(repeat):
            # A = np.random.rand(i, i)
            # Ab = np.random.rand(i, 1)
            # pos_def = tests.generate_pos_dif(i, 1, 1000)
            A = np.random.randint(-1000, 1000, size=(i, i))   # change
           # Ab = np.random.randint(-1000, 1000, size=(i, 1))  # change

            # x = sp.solve(A, Ab)
            # Anew = np.dot(A, x)
            # test_sp += [norm_2(Ab, Anew)]
            #
            # x = solve.solve(A, Ab, 2)
            # Anew = np.dot(A, x)
            # test_block += [norm_2(Ab, Anew)]

            # invers = sp.inv(A)
            # Asp = np.dot(A, invers)
            # dif_matrix = Asp - np.identity(Asp.shape[0])
            # test_sp += [np.sum(np.abs(dif_matrix))]

            # invers = solve.inverse(A, 2)
            # Asp = np.dot(A, invers)
            # dif_matrix = Asp - np.identity(Asp.shape[0])
            # test_my += [np.sum(np.abs(dif_matrix))]

            # x = sp.inv(A)
            # Amy = np.dot(A, x)
            # test_sp += [norm_2(np.identity(i), Amy)]
            #
            # # x = lu.lu_partial_pivot(A, Ab, 1)
            # # Amy = np.dot(A, x)
            # # test_partial += [norm_2(Ab, Amy)]
            #
            # x = solve.inverse(A, 4)
            # Amy = np.dot(A, x)
            # test_block += [norm_2(np.identity(i), Amy)]

            #
            # x = solve.solve(A, Ab, 3)
            # Amy = np.dot(A, x)
            # test_rook += [norm_2(Ab, Amy)]
            #
            # x = solve.solve(A, Ab, 4)
            # Amy = np.dot(A, x)
            # test_block += [norm_2(Ab, Amy)]

            P, L, U = sp.lu(A)
            Amy = np.dot(P, np.dot(L, U))
            test_sp += [norm_1(A, Amy)]

            P, L, U = lu.lu_partial_pivot(A)
            Amy = np.dot(P, np.dot(L, U))
            test_partial += [norm_1(A, Amy)]

            P, Q, L, U = lu.lu_complete_pivot(A)
            Amy = np.dot(np.dot(P, np.dot(L, U)), Q)
            test_complete += [norm_1(A, Amy)]

            P, Q, L, U = lu.lu_rook_pivot(A)
            Amy = np.dot(np.dot(P, np.dot(L, U)), Q)
            test_rook += [norm_1(A, Amy)]

            P, L, U = lu_block.lu_partial_block(A, 32)
            Amy = np.dot(P, np.dot(L, U))
            test_block += [norm_1(A, Amy)]

            # U = sp.cholesky(pos_def)
            # Amy = np.dot(U.transpose(), U)
            # test_sp += [norm_2(pos_def, Amy)]
            #
            # U = cholesky.cholesky_block(pos_def, 32)
            # Amy = np.dot(U.transpose(), U)
            # test_partial += [norm_2(pos_def, Amy)]

            # L2, U2 = lu_square.lu_in_place(A)
            # lu = np.dot(L2, U2)
            # dif_matrix = lu - A
            # test_lu += [np.sum(np.abs(dif_matrix))]

            # P3, L3, U3 = lu_square.lu_partial_pivot(A)
            # partial = np.dot(P3, np.dot(L3, U3))
            # dif_matrix = partial - A
            # test_partial += [np.sum(np.abs(dif_matrix))]
            #
            # P4, Q4, L4, U4 = lu_square.lu_complete_pivot(A)
            # complete = np.dot(np.dot(P4, np.dot(L4, U4)), Q4)
            # dif_matrix = complete - A
            # test_complete += [np.sum(np.abs(dif_matrix))]

            # P5, Q5, L5, U5 = lu_square.lu_rook_pivot(A)
            # rook = np.dot(np.dot(P5, np.dot(L5, U5)), Q5)
            # dif_matrix = rook - A
            # test_rook += [np.sum(np.abs(dif_matrix))]
            #

            print i, j

        plot_data += [{
            'y': test_sp,
            'type':'box',
            'marker':{'color': 'black'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_partial,
            'type':'box',
            'marker':{'color': 'blue'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_complete,
            'type':'box',
            'marker':{'color': 'red'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_rook,
            'type':'box',
            'marker':{'color': 'green'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_block,
            'type':'box',
            'marker':{'color': 'purple'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        # sp_avg = [np.sum(test_sp) / len(test_sp)]
        # y += [sp_avg][0]
        # plot_data += [{
        #      'y': sp_avg,
        #      'type':'box',
        #      'marker':{'color': 'green'},
        #      'name': str(i) + 'x' + str(i)
        #      }]

        # print plot_data
        # xi = np.arange(len(avg))

        y_sp += [np.sum(test_sp) / len(test_sp)]
        y_partial += [np.sum(test_partial) / len(test_partial)]
        y_complete += [np.sum(test_complete) / len(test_complete)]
        y_rook += [np.sum(test_rook) / len(test_rook)]
        y_block += [np.sum(test_block) / len(test_block)]

    print np.polyfit(range(len(y_sp)), y_sp, 1, full=True)
    print np.polyfit(range(len(y_sp)), y_sp, 2, full=True)
    print np.polyfit(range(len(y_partial)), y_partial, 1, full=True)
    print np.polyfit(range(len(y_partial)), y_partial, 2, full=True)
    print np.polyfit(range(len(y_complete)), y_complete, 1, full=True)
    print np.polyfit(range(len(y_complete)), y_complete, 2, full=True)
    print np.polyfit(range(len(y_rook)), y_rook, 1, full=True)
    print np.polyfit(range(len(y_rook)), y_rook, 2, full=True)
    print np.polyfit(range(len(y_block)), y_block, 1, full=True)
    print np.polyfit(range(len(y_block)), y_block, 2, full=True)

    url = py.plot(plot_data, filename='precision')
示例#4
0
def benchmark_test(minsize, maxsize, step, repeat):
    plot_data = []
    y_sp       = []
    y_block  = []
    y_no = []
    for i in range(minsize, maxsize, step):
        test_sp = []
        test_no = []
        test_partial = []
        test_complete = []
        test_rook = []
        test_block = []

        for j in range(repeat):
            # pos_def = tests.generate_pos_dif(i, 1, 1000)
            A = np.random.randint(-1000, 1000, size=(i, i))   # change
            # Ab = np.random.randint(-1000, 1000, size=(i, 1))  # change

            time_start = time.clock()
            sp.lu(A)
            test_sp += [time.clock() - time_start]

            time_start = time.clock()
            lu.lu_in_place(A)
            test_no += [time.clock() - time_start]

            time_start = time.clock()
            lu.lu_partial_pivot(A)
            test_partial += [time.clock() - time_start]

            time_start = time.clock()
            lu.lu_complete_pivot(A)
            test_complete += [time.clock() - time_start]

            time_start = time.clock()
            lu.lu_rook_pivot(A)
            test_rook += [time.clock() - time_start]

            time_start = time.clock()
            lu_block.lu_partial_block(A, 32)
            test_block += [time.clock() - time_start]


            # time_start = time.clock()
            # lu.lu_partial_pivot(A)
            # test_no += [time.clock() - time_start]

            print i, j

        plot_data += [{
            'y': test_sp,
            'type':'box',
            'marker':{'color': 'black'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_no,
            'type':'box',
            'marker':{'color': 'grey'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_partial,
            'type':'box',
            'marker':{'color': 'blue'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_complete,
            'type':'box',
            'marker':{'color': 'red'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_rook,
            'type':'box',
            'marker':{'color': 'green'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]

        plot_data += [{
            'y': test_block,
            'type':'box',
            'marker':{'color': 'purple'},
            'name': str(i) + 'x' + str(i),
            'boxpoints': False
            }]
            # plot_data += [{
            #     'y': test_no,
            #     'type':'box',
            #     'marker':{'color': 'blue'},
            #     'name': str(i) + 'x' + str(i),
            #     'boxpoints': False
            #     }]
            #
            # y_sp += [np.sum(test_sp) / len(test_sp)]
            # y_block += [np.sum(test_block) / len(test_block)]
            # y_no += [np.sum(test_no) / len(test_no)]

    # print np.polyfit(range(len(y_sp)), y_sp, 1, full=True)
    # print np.polyfit(range(len(y_sp)), y_sp, 2, full=True)
    # print np.polyfit(range(len(y_block)), y_block, 1, full=True)
    # print np.polyfit(range(len(y_block)), y_block, 2, full=True)
    # print np.polyfit(range(len(y_no)), y_no, 1, full=True)
    # print np.polyfit(range(len(y_no)), y_no, 2, full=True)


    url = py.plot(plot_data, filename='Benchmark')
示例#5
0
 def test_lu_partial(self):
     rand_int_matrix = np.random.randint(-1000, 1000, size=(1000, 1000))
     np.array_equal(lu.lu_partial_pivot(rand_int_matrix)[0].transpose(), sp.lu(rand_int_matrix)[0])