Exemple #1
0
def matrix_QR_factorization_householder(matrix):
    matrix_R = [[matrix[j][i] for i in range(len(matrix[0]))]
                for j in range(len(matrix))]  #HA starts by being A
    matrix_Q = [[0 for j in range(len(matrix))] for k in range(len(matrix))
                ]  #Q starts by being the identity matrix

    for j in range(len(matrix)):
        matrix_Q[j][j] = 1
    for i in range(len(matrix[0]) - 1):
        a = [
            matrix_R[j][i] for j in range(len(matrix_R))
        ]  #a is the i'th column of matrix_R, with 0s inserted above the diagonal
        for j in range(i):
            a[j] = 0
        r = [0] * len(
            a
        )  #r is just a 0 vector, with the i'th column being the magnitude of a
        r[i] = vector_2norm(a)
        u = vector_add(a, vector_scal_mult(-1, r))
        i_ = [[0 for j in range(len(matrix))] for k in range(len(matrix))]
        for j in range(len(matrix)):
            i_[j][j] = 1
        if vector_dot(u, u) != 0:
            h = matrix_add(i_,
                           matrix_scal_mult(-2 / vector_dot(u, u),
                                            matrix_outer(u, u)))  #computer h_i
        else:  #if u ends up being the 0 matrix, then h is just the identity matrix
            h = i_
        matrix_Q = matrix_mult(matrix_Q, h)
        matrix_R = matrix_mult(h, matrix_R)

    return [matrix_Q, matrix_R]
Exemple #2
0
def matrix_rayleigh_quotient_iteration(matrix,
                                       tol,
                                       max_iter,
                                       getIterCount=False):
    error = tol * 10
    count = 0
    v_i = matrix[0]
    v_i = vector_scal_mult(1 / vector_2norm(v_i), v_i)
    eigenvaluenew = matrix_mult(matrix_transpose(convert_vec_mat(v_i)),
                                matrix_mult(matrix,
                                            convert_vec_mat(v_i)))[0][0]
    I = [[int(i == j) for i in range(len(matrix))] for j in range(len(matrix))]

    while error > tol and count < max_iter:
        eigenvalueold = eigenvaluenew
        v_i = matrix_solve_LU(
            matrix_add(matrix, matrix_scal_mult(-eigenvaluenew, I)), v_i)
        v_i = vector_scal_mult(1 / vector_2norm(v_i), v_i)
        eigenvaluenew = matrix_mult(matrix_transpose(convert_vec_mat(v_i)),
                                    matrix_mult(matrix,
                                                convert_vec_mat(v_i)))[0][0]
        error = abs(eigenvaluenew - eigenvalueold)
        count += 1
    if getIterCount == True:
        return eigenvaluenew, count
    else:
        return eigenvaluenew
def matrix_inverse_power_iteration(matrix,
                                   alpha,
                                   tol,
                                   max_iter,
                                   getIterCount=False):
    error = tol * 10
    count = 0
    eigenvaluenew = 0
    I = [[-int(i == j) * alpha for i in range(len(matrix))]
         for j in range(len(matrix))]
    v_i = matrix[0]
    while error > tol and count < max_iter:

        eigenvalueold = eigenvaluenew
        v_i = matrix_solve_LU(matrix_add(matrix, I), v_i)
        v_i = vector_scal_mult(1 / vector_2norm(v_i), v_i)
        eigenvaluenew = matrix_mult(matrix_transpose(convert_vec_mat(v_i)),
                                    matrix_mult(matrix,
                                                convert_vec_mat(v_i)))[0][0]
        count += 1
        error = abs(eigenvaluenew - eigenvalueold)
    if getIterCount == True:
        return eigenvaluenew, count
    else:
        return eigenvaluenew