Beispiel #1
0
def test_est_bon_preconditionneur():
    A = generate_SPDSM(18, 18)
    T_dense = facto_cholesky_dense(A)
    T_incomplete = facto_cholesky_incomplete(A)

    print("A =", A)

    print("cond(A) = ", np.linalg.cond(A, np.inf))
    #T_dense
    print(
        "cond(T_dense) = ",
        np.linalg.cond(
            np.transpose(np.linalg.inv(T_dense)) * np.linalg.inv(T_dense) * A,
            np.inf))
    print("est_bon_preconditionneur(T_dense,A)",
          est_bon_preconditionneur(T_dense, A),
          sep=" = ")
    #T_incomplete
    print(
        "cond(T_incomplète) = ",
        np.linalg.cond(
            np.transpose(np.linalg.inv(T_incomplete)) *
            np.linalg.inv(T_incomplete) * A, np.inf))
    print("est_bon_preconditionneur(T_incomplete, A)",
          est_bon_preconditionneur(T_incomplete, A),
          sep=" = ")
Beispiel #2
0
def probleme_radiateur_centre(N, T_radiateur):
    '''
    probleme_radiateur_centre(...)

    returns the coefficients of the matrix T of the problem of heat diffusion in a 2D plan of size N by N with a radiator in the center. The temperature of the radiator is T_radiateur.

    Parameters
    ----------

    N : size of the 2D plan

    T_radiateur : the temperature of the radiator

    Returns
    -------

    out : the matrix T of heat diffusion result

    '''

    A, b = genere_matrice_N(N)
    b[N // 2 * N + N // 2, 0] = -T_radiateur
    A_oppose = -1 * A
    b_oppose = -1 * b
    L = facto_cholesky_incomplete(A_oppose)
    x = solve(L, np.transpose(L), b_oppose)

    return x.reshape([N, N])
Beispiel #3
0
def probleme_mur_chaud(N, T_mur_chaud):
    '''
    probleme_mur_chaud(...)

    returns the coefficients of the matrix T of the problem of heat diffusion in a 2D plan of size N by N with a warm wall in north position. The temperature of the warm wall is T_mur_chaud.

    Parameters
    ----------

    N : size of the 2D plan

    T_mur_chaud : the temperature of the warm wall

    Returns
    -------

    out : the matrix T of heat diffusion

    '''

    A, b = genere_matrice_N(N)
    b[:N, 0] = -T_mur_chaud
    A_oppose = -1 * A
    b_oppose = -1 * b
    L = facto_cholesky_incomplete(A_oppose)
    x = solve(L, np.transpose(L), b_oppose)

    return x.reshape([N, N])
Beispiel #4
0
def conjugate_gradient_preconditioned(A, b, x):
    '''
    conjugate_gradient_preconditioned(...)

    returns the solution to the equation Ax = b

    Parameters
    ----------

    A : positive definite square matrix
    b : vector solution of the equation Ax = b
    x : initial estimation of the searched vector

    Returns
    -------

    out : The vector x, solution to the equation Ax = b

    Time complexity
    ---------------

    inferior to n**3

    Space complexity
    ----------------

    O(n**2)

    '''
    r = b - prod(A, x)  # The inverse of the gradient of f
    T = facto_cholesky_incomplete(A)  # Incomplete Cholesky preconditionner
    Tt = trans(T)

    z = solve(T, Tt, r)
    p = z  # First direction of the being built base
    rsold = prod(trans(r), z)

    for i in range(1, 10**6):
        Ap = prod(A, p)
        alpha = rsold / (prod(trans(p), Ap)
                         )  # Coordinate of the solution in the base
        x = x + alpha * p  # Variable that converges towards the solution
        r = r - alpha * Ap  # New gradient
        z = solve(T, Tt, r)
        rsnew = prod(trans(r), z)
        if np.sqrt(rsnew[0][0]) < 10**(-10):
            break

        p = z + (rsnew / rsold) * p  # New direction
        rsold = rsnew
    return x
Beispiel #5
0
    def conjugate_gradient_preconditioned(A, b, x):
        r = b - prod(A, x)
        T = facto_cholesky_incomplete(A)
        Tt = trans(T)

        z = solve(T, Tt, r)
        p = z
        rsold = prod(trans(r), z)

        for i in range(1, 10**6):
            Ap = prod(A, p)
            alpha = rsold / (prod(trans(p), Ap))
            x = x + alpha * p
            r = r - alpha * Ap
            z = solve(T, Tt, r)
            rsnew = prod(trans(r), z)
            if np.sqrt(rsnew[0][0]) < 10**(-10):
                break

            p = z + (rsnew / rsold) * p
            rsold = rsnew
        return x, i
Beispiel #6
0
    res = prod(A, x)

    flag = 0
    for k in range(len(A)):
        if (np.absolute(res[k][0] - b[k][0]) > threshold):
            flag += 1

    if (flag == 0):
        print('Test conjugate_gradient : \033[32mPASSED\033[0m')
    else:
        print('Test conjugate_gradient : \033[31mFAILED\033[0m')

    print("\nstructural_test__triangular_solve_down : ")

    T = facto_cholesky_incomplete(A)
    x = triangular_solve_down(T, b)

    res = prod(T, x)

    flag = 0
    for k in range(len(T)):
        if (np.absolute(res[k][0] - b[k][0]) > threshold):
            flag += 1

    if (flag == 0):
        print('Test triangular_solve_down : \033[32mPASSED\033[0m')
    else:
        print('Test triangular_solve_down : \033[31mFAILED\033[0m')

    print("\nstructural_test__triangular_solve_up : ")