def matrix_solve_steepest_descent(matrix,
                                  vector_b,
                                  tol,
                                  max_iter,
                                  getIterCount=False):
    error = tol * 10
    count = 0
    x_i = vector_b.copy()  #x_0
    while error > tol and count < max_iter:
        #print(count)# used for testing
        #print(error)# used for testing
        count += 1
        residual = vector_add(
            vector_b,
            vector_scal_mult(
                -1, convert_vec_mat(matrix_mult(matrix,
                                                convert_vec_mat(x_i)))))
        d1 = vector_dot(residual, residual)
        d2 = vector_dot(
            residual,
            convert_vec_mat(matrix_mult(matrix, convert_vec_mat(residual))))
        alpha = d1 / d2
        x_i = vector_add(x_i, vector_scal_mult(alpha, residual))
        error = vector_2norm(residual)
    if getIterCount == True:
        return x_i, count
    else:
        return x_i
Ejemplo n.º 2
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]
Ejemplo n.º 3
0
def matrix_solve_conjugate_gradient(matrix,
                                    vector_b,
                                    tol,
                                    max_iter,
                                    getIterCount=False):
    error = tol * 10
    count = 0
    x_i = vector_b.copy()  # x_0
    residual = vector_add(
        vector_b,
        vector_scal_mult(
            -1, convert_vec_mat(matrix_mult(matrix, convert_vec_mat(x_i)))))
    direction = residual
    d1new = vector_dot(residual, residual)

    while error > tol and count < max_iter:
        s = convert_vec_mat(matrix_mult(matrix, convert_vec_mat(direction)))
        d1old = d1new
        alpha = d1old / vector_dot(direction, s)
        x_i = vector_add(x_i, vector_scal_mult(alpha, direction))
        residual = vector_add(residual, vector_scal_mult(-alpha, s))
        d1new = vector_dot(residual, residual)
        direction = vector_add(residual,
                               vector_scal_mult(d1new / d1old, direction))
        count += 1
        error = vector_2norm(residual)
    if getIterCount == True:
        return x_i, count
    else:
        return x_i
Ejemplo n.º 4
0
def matrix_QR_factorization(matrix):
    matrix_columns = [[matrix[i][j] for i in range(len(matrix))] for j in range(len(matrix[0]))]
    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(matrix_columns[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]