Example #1
0
def matrix_QR_factorization_mod(matrix):

    u_vectors = [[matrix[i][j] for i in range(len(matrix))]
                 for j in range(len(matrix[0]))]
    for j in range(len(matrix[0])):
        for i in range(j):
            u_vectors[j] = vector_add(
                u_vectors[j],
                vector_scal_mult(-1 * vector_dot(u_vectors[j], u_vectors[i]),
                                 u_vectors[i]))
        u_vectors[j] = vector_scal_mult(1 / vector_2norm(u_vectors[j]),
                                        u_vectors[j])

    matrix_Q = [[u_vectors[i][j] for i in range(len(u_vectors))]
                for j in range(len(u_vectors[0]))]
    matrix_R = matrix_mult(matrix_transpose(matrix_Q), matrix)

    return [matrix_Q, matrix_R]
Example #2
0
def matrix_solve_gauss_seidel_test():
    matrix1 = gen_rand_matrix(100)
    matrix2 = gen_sym_matrix(100)
    matrix3 = gen_diagdom_matrix(100)
    matrix4 = gen_diagdom_sym_matrix(100)
    matrices = [matrix1, matrix2, matrix3, matrix4]
    vector = [1] * len(matrix1)
    for i in range(len(matrices)):
        b = matrix_mult(matrices[i], convert_vec_mat(vector))
        starttime = time.time()
        solution = matrix_solve_gauss_seidel(matrices[i],
                                             convert_vec_mat(b),
                                             0.00001,
                                             1000,
                                             getIterCount=True)
        print("Time: " + str(time.time() - starttime))
        print("# of iterations: " + str(solution[1]))
        print(solution[0])
Example #3
0
def matrix_solve_gauss_seidel(matrix,vector_b,tol,max_iter,getIterCount=False):
    xnew = [0 for i in range(len(matrix))]
    error = tol * 10
    count = 0
    while error > tol and count < max_iter:
        # print(count) used for testing
        # print(error) used for testing
        count += 1
        xold = xnew.copy()
        xnew = vector_b.copy()
        for i in range(len(matrix)):
            for j in range(i):
                xnew[i] = xnew[i] - matrix[i][j]*xold[j]
            for j in range(i+1, len(xnew)):
                xnew[i] = xnew[i] - matrix[i][j]*xold[j]
            xnew[i] = xnew[i] / matrix[i][i]
            xold[i] = xnew[i]
        error = vector_2norm(vector_add(convert_vec_mat(matrix_mult(matrix,convert_vec_mat(xnew))),vector_scal_mult(-1,vector_b)))
    if getIterCount == True:
        return xnew,count
    else:
        return xnew
Example #4
0
def hilbert_matrix_QR_test():

    hilbert_matrix = [0] * 11
    Q_matrix = [0] * 11

    # Create the hilbert matrices and their corresponding Q factorization matrix.
    for n in range(1, 11):
        hilbert_matrix[n] = [[1 / (1 + i + j) for i in range(n)]
                             for j in range(n)]  # Hilbert Matrix generator
        Q_matrix[n] = matrix_QR_factorization(hilbert_matrix[n])[0]

    # Print the Q-factorized hilbert matrix, as well as Q^t*Q
    for i in range(4, 11, 2):  # 4, 6, 8, 10
        print("Given a " + str(i) + "-sized Hilbert matrix, its Q matrix is:")
        for j in range(i):
            print(Q_matrix[i][j])

        print("Q^t*Q should result in the identity matrix:")
        temp_matrix = matrix_mult(Q_matrix[i], matrix_transpose(Q_matrix[i]))
        for j in range(i):
            print(temp_matrix[j])

        print()  # Print a new line for organization purposes.
Example #5
0
def hilbert_matrix_steepest_descent_test():

    selection = [4, 8, 16, 32]
    hilbert_matrix = []
    solution = []

    # Create the hilbert matrices and their corresponding Q factorization matrix.
    for n in selection:
        hilbert_matrix.append([[1 / (1 + i + j) for i in range(n)]
                               for j in range(n)])  # Hilbert Matrix generator
        solution.append(
            matrix_solve_steepest_descent(hilbert_matrix[-1], [1] * n, 0.0001,
                                          10000))

    for i in range(0, len(selection)):  # 4, 6, 8, 10
        #print(matrix_mult(hilbert_matrix[i],convert_vec_mat(solution[i])))
        print("Absolute error for " + str(selection[i]) + "x" +
              str(selection[i]) + ": " + str(
                  abs_error_2norm([1] * selection[i],
                                  convert_vec_mat(
                                      matrix_mult(hilbert_matrix[i],
                                                  convert_vec_mat(
                                                      solution[i]))))))
        print(solution[i])
Example #6
0
def matrix_solve_least_square(matrix, vector):
    matrix_At_A = matrix_mult(matrix_transpose(matrix), matrix)
    vector_At_b = matrix_mult(matrix_transpose(matrix),
                              convert_vec_mat(vector))
    solution = matrix_solve(matrix_At_A, convert_vec_mat(vector_At_b))
    return solution
Example #7
0
def matrix_solve_least_squares_QR(matrix,vector):
    (matrix_Q, matrix_R) = matrix_QR_factorization_mod(matrix)
    matrix_Qtb = matrix_mult(matrix_transpose(matrix_Q), convert_vec_mat(vector))
    solution = matrix_solve_upper_tri(matrix_R, convert_vec_mat(matrix_Qtb))
    return solution