Exemplo n.º 1
0
def solve_block_jacobi(A, b, n_partitions=2, x0=None, maxiter=100):
    assert A.shape[0] == A.shape[1], "Square matrix required"

    N = len(b)
    x = parse_initial_cond(x0, N)
    A_sub = partition_mat_by_num(A, n_partitions)
    x_sub_old = partition_vec_by_num(x, n_partitions)
    b_sub = partition_vec_by_num(b, n_partitions)
    rhs_sub = dict()
    x_sub = dict()
    residual = 0.0

    for _ in xrange(maxiter):
        for i in xrange(n_partitions):  # Go through row blocks
            sum_mat_vec_results = np.zeros(len(b_sub[i]))
            for j in xrange(n_partitions):
                if i == j:
                    continue
                sum_mat_vec_results += A_sub[i, j] * x_sub_old[j]

            rhs_sub[i] = b_sub[i] - sum_mat_vec_results
            x_sub[i] = spsolve(A_sub[i, i], rhs_sub[i])

        for i in xrange(n_partitions):
            x_sub_old[i][:] = x_sub[i][:]

        x = reconstruct_vec_from_sub_vectors(x_sub)
        residual = compute_normalized_linf_residual(A, x, b)

    return x, residual
Exemplo n.º 2
0
def solve_gauss_seidel(A, b, x0=None, maxiter=100):
    N = len(b)
    L, D, U = lower_diag_upper(A)
    E = -L
    F = -U

    x = parse_initial_cond(x0, N)

    residual = 1

    for _ in xrange(maxiter):
        x = spsolve(D - E, (b + F * x))
        residual = compute_normalized_linf_residual(A, x, b)

    return x, residual
Exemplo n.º 3
0
def solve_jacobi(A, b, x0=None, maxiter=100):
    N = len(b)
    L, D, U = lower_diag_upper(A)
    E = - L
    F = - U

    D.data[:] = 1. / D.data[:]
    D_inv = D

    x = parse_initial_cond(x0, N)

    for _ in xrange(maxiter):
        x = D_inv * (b + (E + F) * x)
        residual = compute_normalized_linf_residual(A, x, b)

    return x, residual