<jupyter_start><jupyter_text>Gauss–Seidel method From https://en.wikipedia.org/wiki/Gauss–Seidel_method:  Gauss–Seidel methodIn numerical linear algebra, the Gauss–Seidel method, also known as the Liebmann method or the method of successive displacement, is an iterative method used to solve a linear system of equations DescriptionLet:$A\mathbf x = \mathbf b$be a square system of $n$ linear equations, where:$A=\begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix}, \qquad  \mathbf{x} = \begin{bmatrix} x_{1} \\ x_2 \\ \vdots \\ x_n \end{bmatrix} , \qquad  \mathbf{b} = \begin{bmatrix} b_{1} \\ b_2 \\ \vdots \\ b_n \end{bmatrix}.$Then $A$ can be decomposed into a diagonal matrix $L_*$, and the remainder $U$:$$A=L_*+U \qquad \text{where} \qquad L_* = \begin{bmatrix} a_{11} & 0 & \cdots & 0 \\ a_{21} & a_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix}, \quad U = \begin{bmatrix} 0 & a_{12} & \cdots & a_{1n} \\ 0 & 0 & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & 0 \end{bmatrix}.$$ $$\mathbf{x}^{(k+1)} = L_*^{-1} (\mathbf{b} - U \mathbf{x}^{(k)})$$ Equivalently: (In the following Code following equations have been used):$$ \mathbf{x}^{(k+1)} = T \mathbf{x}^{(k)} + C$$$$T = - L_*^{-1} U$$$$C = L_*^{-1} \mathbf{b}$$ Stop Condition:$$ \lVert X^{(k+1)} - X^{(k)} \rVert_2 \le 10^{-4}$$<jupyter_code>import numpy as np
def Gauss_Seidel(A,b,initial_guess):
    #Decomposing matrix A into a lower triangle matrix and a strictly upper triangle matrix:
    L = np.tril(A)
    U = np.triu(A,1)
    #Calculating the inverse of matrix L:
    L_inv = np.linalg.inv(L)
    
    #Symbol of matrix multiplication in numpy is @
    T = -L_inv@U
    C = L_inv@b
    x = initial_guess

    
    while(1):
        x_old = x
        x = T@x + C
        x_new = x
        #using norm2:
        if np.linalg.norm(x_new-x_old) < 10**(-4):
            break
    return x
A = np.matrix([[2.0,1.0],
               [5.0,7.0]])
b = np.matrix([[11.0],[13.0]])
initialGuess = np.matrix([[1.0],[1.0]])

sol = Gauss_Seidel(A,b,initialGuess)

print ('A:')
print(A)

print ('\nb:')
print(b)

print('\nSolution:')
print(sol)<jupyter_output>A:
[[2. 1.]
 [5. 7.]]

b:
[[11.]
 [13.]]

Solution:
[[ 7.11108566]
 [-3.22220404]]
{'fraction_non_alphanumeric': 0.20699708454810495, 'fraction_numerical': 0.04039983340274885, 'mean_word_length': 3.0369747899159663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '59354', 'license': 'MIT', 'n_tokens_mistral': 1015, 'n_tokens_neox': 936, 'n_words': 254, 'path': '.ipynb_checkpoints/Gauss–Seidel_Method-checkpoint.ipynb', 'repo_name': 'enazari/iterative-methods-for-solvling-linear-systems-of-equations', 'size': '2401'}