<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
from numpy.linalg import norm, cond, solve, inv
from scipy.linalg import hilbert<jupyter_output><empty_output><jupyter_text>Методы общая схема итерационного метода<jupyter_code>def iterational_method(alpha,beta,x0,eps): #общая схема итерационного метода
    num_of_iters = 1
    x1 = alpha @ x0 + beta
    while (norm(x1-x0)>eps and num_of_iters < 500):
        x0 = x1
        x1 = alpha @ x0 + beta
        num_of_iters += 1
    return x1, num_of_iters<jupyter_output><empty_output><jupyter_text>метод Зейделя<jupyter_code>def seidel_method(a,b,x0,eps): 
    n, m = a.shape[0], a.shape[1]
    l,r,d = [np.zeros([n,m]) for _ in range(3)]
    for i in range(n):
        for j in range(m):
            if i > j:
                l[i,j] = a[i,j]
            elif i < j:
                r[i,j] = a[i,j]
            else:
                d[i,j] = a[i,j]
    beta = inv(d+l)
    return iterational_method(-beta@r,beta@x0,x0,eps)<jupyter_output><empty_output><jupyter_text>метод простых итераций<jupyter_code>def simple_iterational_method(a,b,x0,eps): 
    alpha = np.zeros([a.shape[0],a.shape[1]])
    beta = np.zeros(b.shape[0])
    for i in range(alpha.shape[0]):
        for j in range(alpha.shape[1]):
            if i != j:
                alpha[i,j] = -a[i,j]/a[i,i]
                beta[i] = b[i]/a[i,i]
    return iterational_method(alpha,beta,x0,eps)<jupyter_output><empty_output><jupyter_text>тесты<jupyter_code>matrix1 = np.array([[-401.64, 200.12], 
               [21200.72,  -601.76]]) 

matrix2 = np.array([[-400.94, 200.02],
               [1200.12, -600.96]])
matrix3 = np.array([[1,2],
              [2,3]])

matrix4 = np.array([
    [0.1,0.2,0.3],
    [0.3,0.3,0.3],
    [0.3,0.9,0.7]])<jupyter_output><empty_output><jupyter_text>Решение<jupyter_code>array = [matrix1,matrix2, matrix3, matrix4,hilbert(4),hilbert(5)]
X = pd.DataFrame(columns=['eps=10^(-4),||x_eps-x||','eps=10^(-4),iters',
                          'eps=10^(-8),||x_eps-x||', 'eps=10^(-8),iters',
                          'eps=10^(-12),||x_eps-x||','eps=10^(-12),iters'])

Y = pd.DataFrame(columns=['eps=10^(-4),||x_eps-x||','eps=10^(-4),iters',
                          'eps=10^(-8),||x_eps-x||', 'eps=10^(-8),iters',
                          'eps=10^(-12),||x_eps-x||','eps=10^(-12),iters'])


for matrix in array:
    b = np.random.uniform(-100,100,size=matrix.shape[0])
    x = solve(matrix,b)
    row_X,row_Y = [],[]
    for i in range(4,13,4):
        x_seidel,seidel_iters = seidel_method(matrix,b,b,10**(-i))
        x_iter,iter_iters = simple_iterational_method(matrix,b,b,10**(-i))
        row_X.extend([norm(x_seidel-x),seidel_iters])
        row_Y.extend([norm(x_iter-x),iter_iters])
    X = X.append(pd.Series(row_X,index=X.columns),True)
    Y = Y.append(pd.Series(row_Y,index=Y.columns),True)
X.index = ['matrix1','matrix2','matrix3', 'matrix4','hilbert(4)','hilbert(5)']

X
Y.index = ['matrix1','matrix2','matrix3', 'matrix4','hilbert(4)','hilbert(5)']

Y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19732550554468362, 'fraction_numerical': 0.055446836268754074, 'mean_word_length': 3.1445945945945946, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '317142', 'license': 'Apache-2.0', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1321, 'n_words': 171, 'path': 'method4.ipynb', 'repo_name': 'ancka019/ComputationsMethods6sem', 'size': '3066'}