import numpy as np

def jacobi(A, b, tol=1e-10, max_iter=1000):
    """
    Resuelve sistemas de ecuaciones lineales Ax = b usando el metodo de Jacobi

    Parametros:
        A: Matriz de coeficientes.
        b: Vector de terminos independientes.
        tol: Tolerancia para la convergencia.
        max_iter: Maximo numero de iteraciones.

    Retorna:
        x: Solucion aproximada.
    """
    n = len(b)
    x = np.zeros(n)
    x_new = np.zeros(n)


    for k in range(max_iter):
        for i in range(n):
            x_new[i]= (b[i] - sum(A[i,j] * x[j] for j in range(n) if j != i))/A[i,i]

        if np.linalg.norm(x_new - x) < tol:
            print(f"Total de iteraciones necesitadas: {k}")
            return x_new
        
        x = x_new.copy()
    
    print(f"No se pudo converger en {max_iter} iteraciones.")
    return x

A = np.array([[3,-0.1,-0.2],[0.1,7,-0.3],[0.3,-0.2,10]], dtype=float)
b = np.array([7.85,-19.3,71.4], dtype=float)

ejemplo_jacobi =  jacobi(A,b)
print(f"Solucion por gauss seidel: {ejemplo_jacobi}")