Пример #1
0
def problem5():
    logging.basicConfig(filename='problem5.log', level=logging.DEBUG)

    logging.info('Starting problem 5')

    lmin = 13
    lmax = 13

    matrix = [my_toeplitz(2**l) for l in range(lmin, lmax + 1)]
    b = [ones(2**l) for l in range(lmin, lmax + 1)]
    x0 = [zeros(2**l) for l in range(lmin, lmax + 1)]
    preconditioner = [my_preconditioner(2**l) for l in range(lmin, lmax + 1)]
    max_iterations = 1000

    chotime = process_time()
    L = cho_factor(matrix[0], lower=True)
    x = cho_solve(L, b[0])
    print('Dimension {}: Cholesky method in {}s'.format(
        2**13,
        process_time() - chotime))

    pcg_time = process_time()
    result_preconditioned_conjugate_gradient = (
        preconditioned_conjugate_gradient(matrix[0], x0[0], b[0],
                                          preconditioner[0], max_iterations))
    pcg_time = process_time() - pcg_time

    print('Dimension {}: PCG {} iterations in {}s'.format(
        2**13, result_preconditioned_conjugate_gradient[1], pcg_time))

    logging.info('Ending problem 5')
Пример #2
0
def problem4():
    logging.basicConfig(filename='problem4.log', level=logging.DEBUG)

    logging.info('Starting problem 4')

    lmin = 10
    lmax = 13

    matrix = [my_toeplitz(2**l) for l in range(lmin, lmax + 1)]
    preconditioner = [my_preconditioner(2**l) for l in range(lmin, lmax + 1)]
    precond_matrix = [
        dot(inv(preconditioner[i]), matrix[i]) for i in range(len(matrix))
    ]

    matrix_eig = [eigvals(m) for m in matrix]
    precond_matrix_eig = [eigvals(m) for m in precond_matrix]

    matrix_cond = [
        max(matrix_eig[i]) / min(matrix_eig[i]) for i in range(len(matrix_eig))
    ]
    precond_matrix_cond = [
        max(precond_matrix_eig[i]) / min(precond_matrix_eig[i])
        for i in range(len(precond_matrix_eig))
    ]

    logging.info('Ending problem 4')

    mateig = plt.plot([2**i for i in range(lmin, lmax + 1)],
                      matrix_cond,
                      label='CondT')
    pmateig = plt.plot([2**i for i in range(lmin, lmax + 1)],
                       precond_matrix_cond,
                       label='CondPT')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('Condition')
    mateig_line = mpatches.Patch(color='blue', label='$T_n$')
    pmateig_line = mpatches.Patch(color='green', label='$P_n^{-1}T_n$')
    plt.legend(handles=[mateig_line, pmateig_line], loc=0)
    plt.savefig('4condition')

    plt.close()

    mateig = plt.plot([2**i for i in range(lmin, lmax + 1)],
                      log10(matrix_cond),
                      label='CondT')
    pmateig = plt.plot([2**i for i in range(lmin, lmax + 1)],
                       log10(precond_matrix_cond),
                       label='CondPT')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('$Log_{10}(Condition)$')
    mateig_line = mpatches.Patch(color='blue', label='$T_n$')
    pmateig_line = mpatches.Patch(color='green', label='$P_n^{-1}T_n$')
    plt.legend(handles=[mateig_line, pmateig_line], loc=0)
    plt.savefig('4logcondition')
Пример #3
0
def problem2():
    logging.basicConfig(filename='problem2.log', level=logging.DEBUG)

    logging.info('Starting problem 2')

    lmin = 9
    lmax = 13

    matrix = [my_toeplitz(2**l) for l in range(lmin, lmax + 1)]
    b = [ones(2**l) for l in range(lmin, lmax + 1)]
    x0 = [zeros(2**l) for l in range(lmin, lmax + 1)]
    max_iterations = 1000

    gd_time = []
    cg_time = []

    result_conjugate_gradient = []
    result_gradient_descent = []

    for i in range(len(matrix)):
        logging.info('Starting caclulation for matrix of size {}'.format(i))

        cg_time.append(process_time())
        result_conjugate_gradient.append(
            conjugate_gradient(matrix[i], x0[i], b[i], max_iterations))
        cg_time[-1] = process_time() - cg_time[-1]

        gd_time.append(process_time())
        result_gradient_descent.append(
            gradient_descent(matrix[i], x0[i], b[i], max_iterations))
        gd_time[-1] = process_time() - gd_time[-1]

        logging.info('Ending calculation for matrix of size {}'.format(i))

    logging.info('Ending problem 2')

    for i in range(lmin, lmax + 1):
        print('Dimension {}: GD {} iterations in {}s, CG {} iterations in {}s'.
              format(2**i, result_gradient_descent[i - lmin][1],
                     gd_time[i - lmin], result_conjugate_gradient[i - lmin][1],
                     cg_time[i - lmin]))

    gd_plot = plt.plot([2**i for i in range(lmin, lmax + 1)],
                       log10(gd_time),
                       label='Gradient Descent')
    cg_plot = plt.plot([2**i for i in range(lmin, lmax + 1)],
                       log10(cg_time),
                       label='Conjugate Gradient')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('$Log_{10}(time (s))$')
    gd_line = mpatches.Patch(color='blue', label='Gradient Descent')
    cg_line = mpatches.Patch(color='green', label='Conjugate Gradient')
    plt.legend(handles=[gd_line, cg_line], loc=0)
    plt.savefig('2logtime')

    plt.close()

    gd_plot = plt.plot([2**i for i in range(lmin, lmax + 1)],
                       gd_time,
                       label='Gradient Descent')
    cg_plot = plt.plot([2**i for i in range(lmin, lmax + 1)],
                       cg_time,
                       label='Conjugate Gradient')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('Time (s)')
    gd_line = mpatches.Patch(color='blue', label='Gradient Descent')
    cg_line = mpatches.Patch(color='green', label='Conjugate Gradient')
    plt.legend(handles=[gd_line, cg_line], loc=0)
    plt.savefig('2time')

    plt.close()

    gd_plot = plt.plot([2**i for i in range(lmin, lmax + 1)],
                       log10([
                           result_gradient_descent[i][1]
                           for i in range(len(result_gradient_descent))
                       ]),
                       label='Gradient Descent')
    cg_plot = plt.plot([2**i for i in range(lmin, lmax + 1)],
                       log10([
                           result_conjugate_gradient[i][1]
                           for i in range(len(result_conjugate_gradient))
                       ]),
                       label='Conjugate Gradient')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('$Log_{10}(iterations)$')
    gd_line = mpatches.Patch(color='blue', label='Gradient Descent')
    cg_line = mpatches.Patch(color='green', label='Conjugate Gradient')
    plt.legend(handles=[gd_line, cg_line], loc=0)
    plt.savefig('2logiterations')

    plt.close()

    gd_plot = plt.plot([2**i for i in range(lmin, lmax + 1)], [
        result_gradient_descent[i][1]
        for i in range(len(result_gradient_descent))
    ],
                       label='Gradient Descent')
    cg_plot = plt.plot([2**i for i in range(lmin, lmax + 1)], [
        result_conjugate_gradient[i][1]
        for i in range(len(result_conjugate_gradient))
    ],
                       label='Conjugate Gradient')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('Iterations')
    gd_line = mpatches.Patch(color='blue', label='Gradient Descent')
    cg_line = mpatches.Patch(color='green', label='Conjugate Gradient')
    plt.legend(handles=[gd_line, cg_line], loc=0)
    plt.savefig('2iterations')
Пример #4
0
def problem3():
    logging.basicConfig(filename='problem3.log', level=logging.DEBUG)

    logging.info('Starting problem 3')

    lmin = 6
    lmax = 10

    matrix = [my_toeplitz(2**l) for l in range(lmin, lmax + 1)]
    b = [ones(2**l) for l in range(lmin, lmax + 1)]
    x0 = [zeros(2**l) for l in range(lmin, lmax + 1)]
    preconditioner = [my_preconditioner(2**l) for l in range(lmin, lmax + 1)]

    max_iterations = 1000

    pcg_time = []

    result_preconditioned_conjugate_gradient = []

    for i in range(len(matrix)):
        logging.info('Starting caclulation for matrix of size {}'.format(i))

        pcg_time.append(process_time())
        result_preconditioned_conjugate_gradient.append(
            preconditioned_conjugate_gradient(matrix[i], x0[i], b[i],
                                              preconditioner[i],
                                              max_iterations))
        pcg_time[-1] = process_time() - pcg_time[-1]

        logging.info('Ending calculation for matrix of size {}'.format(i))

    logging.info('Ending problem 3')

    for i in range(lmin, lmax + 1):
        print('Dimension {}: PCG {} iterations in {}s'.format(
            2**i, result_preconditioned_conjugate_gradient[i - lmin][1],
            pcg_time[i - lmin]))

    pcg_plot = plt.plot([2**i for i in range(lmin, lmax + 1)],
                        log10(pcg_time),
                        label='Preconditioned Conjugate Gradient',
                        color='red')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('$Log_{10}(time (s))$')
    pcg_line = mpatches.Patch(color='red',
                              label='Preconditioned Conjugate Gradient')
    plt.legend(handles=[pcg_line], loc=0)
    plt.savefig('3logtime')

    plt.close()

    pcg_plot = plt.plot([2**i for i in range(lmin, lmax + 1)],
                        pcg_time,
                        label='Preconditioned Conjugate Gradient',
                        color='red')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('Time (s)')
    pcg_line = mpatches.Patch(color='red',
                              label='Preconditioned Conjugate Gradient')
    plt.legend(handles=[pcg_line], loc=0)
    plt.savefig('3time')

    plt.close()

    pcg_plot = plt.plot(
        [2**i for i in range(lmin, lmax + 1)],
        log10([
            result_preconditioned_conjugate_gradient[i][1]
            for i in range(len(result_preconditioned_conjugate_gradient))
        ]),
        label='Preconditioned Conjugate Gradient',
        color='red')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('$Log_{10}(iterations)$')
    pcg_line = mpatches.Patch(color='red',
                              label='Preconditioned Conjugate Gradient')
    plt.legend(handles=[pcg_line], loc=0)
    plt.savefig('3logiterations')

    plt.close()

    pcg_plot = plt.plot([2**i for i in range(lmin, lmax + 1)], [
        result_preconditioned_conjugate_gradient[i][1]
        for i in range(len(result_preconditioned_conjugate_gradient))
    ],
                        label='Conjugate Gradient',
                        color='red')
    plt.xlabel('Matrix size (n)')
    plt.ylabel('Iterations')
    pcg_line = mpatches.Patch(color='red',
                              label='Preconditioned Conjugate Gradient')
    plt.legend(handles=[pcg_line], loc=0)
    plt.savefig('3iterations')
Пример #5
0
from numpy import flipud, insert, append, zeros, multiply, split
from numpy.fft import rfft, irfft


def precompute_g(T):
    return rfft(append(T[:, 0], insert(flipud(T[0, 1:]), 0, 0)))


def fftmul(g, x):
    h = multiply(g, rfft(append(x, zeros(len(x)))))
    return split(irfft(h), 2)[0]


if __name__ == '__main__':
    from numpy import ones
    from project3.my_toeplitz import my_toeplitz
    fftmul(my_toeplitz(5), ones(5))