# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

def gauss_seidel_method(mat_aum, x, e):
    mat_A = [lin[0:-1] for lin in mat_aum]
    mat_b = [lin[-1] for lin in mat_aum]
    
    iteracoes = 0
    erros = [0]*len(x)
    x_geral = []
    
    x_geral.append(x.copy())
        
    # Primeira iteração é diferente porque precisa ter a primeira matriz erros ainda
    for i in range(0, len(mat_A)):
        aii = mat_A[i][i]
        bi = mat_b[i]
        soma_parenteses = bi

        for j in range(0, len(mat_A[i])):
            if j != i:
                soma_parenteses -= mat_A[i][j]*x[j]

        x[i] = (1/aii)*soma_parenteses
        
    iteracoes += 1
    x_geral.append(x.copy())
    
    for i in range(0, len(x_geral[-1])):
        erros[i] = abs(x_geral[-1][i] - x_geral[len(x_geral)-2][i])
    
    while max(erros) > e:
        for i in range(0, len(mat_A)):
            aii = mat_A[i][i]
            bi = mat_b[i]
            soma_parenteses = bi

            for j in range(0, len(mat_A[i])):
                if j != i:
                    soma_parenteses -= mat_A[i][j]*x[j]

            x[i] = (1/aii)*soma_parenteses
        
        x_geral.append(x.copy())
            
        for i in range(0, len(x_geral[-1])):
            erros[i] = abs(x_geral[-1][i] - x_geral[len(x_geral)-2][i])
            
        iteracoes += 1
    
    return (iteracoes, x_geral)


def sassenfeld_criterion(mat_aum):
    mat_A = [lin[0:-1] for lin in mat_aum]
    beta = []
    
    for i in range(0, len(mat_A)):
        bi = 0

        for j in range(0, len(mat_A[i])):
            if j != i:
                if len(beta) >= (j+1):
                    bi += abs(mat_A[i][j])*beta[j]
                else:
                    bi += abs(mat_A[i][j])

        beta.append(bi/abs(mat_A[i][i]))
    
    if max(beta) < 1:
        return (True, beta)
    else:
        return (False, beta)



exemplo_aula = [
    [1, 1, 3],
    [1, -3, -3]
]

gauss_seidel_method(exemplo_aula, [0, 0], 0.03)



StarcoderdataJupyterScriptsDedupFiltered