Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib as plt
  2.  
  3. def run(A, f, w, eps=1e-12):
  4.     iterations = 0
  5.     f = np.matmul(A.T, f)
  6.     A = np.matmul(A.T, A)
  7.     x = np.zeros(A.shape[1], dtype=np.float64)
  8.     B = np.zeros(A.shape, dtype=np.float64)
  9.     for i in range(A.shape[0]):
  10.         B[i][i] = A[i][i] / w
  11.         for j in range(A.shape[0]):
  12.             if j < i:
  13.                 B[i][j] = A[i][j]
  14.     loss = np.sum((f - np.matmul(A, x)) ** 2) ** (1 / 2)
  15.     while loss > eps:
  16.         iterations += 1
  17.         x = np.matmul(np.linalg.inv(B), f - np.matmul(A - B, x))
  18.         loss = np.sum((f - np.matmul(A, x)) ** 2) ** (1 / 2)
  19.     return x, iterations
  20.  
  21. def execute(par, A, f, w, eps=1e-12):
  22.     x, iters = run(A, f, w, eps)
  23.     if par == "solution":
  24.         return x
  25.     if par == "iterations":
  26.         return iters
  27.  
  28. def learn_iter(A, f, eps=1e-12):
  29.     w_step = 0.1
  30.     W = np.arange(w_step, 2, w_step)
  31.     iters = []
  32.     for w in W:
  33.         iters += [execute("iterations", A, f, w, eps)]
  34.     plt.plot(W, iters)
  35.     plt.show()
  36.  
  37. def check(A, f):
  38. ## Для проверки функций я использую функции библиотеки для анализа данных numpy
  39.     return np.linalg.solve(A, f)
  40.  
  41. def main():
  42.     print("Insert the dimension of matrix")
  43.     n = int(input())
  44.    
  45.     print("\nType of input\n")
  46.     print("\t1 - File\n\t2 - Use formula")
  47.     typ = int(input())
  48.  
  49.     A = np.zeros(shape=(n, n), dtype=np.float64)
  50.     b = np.zeros(shape=(n), dtype=np.float64)
  51.     if typ == 1:
  52.         print("\nEnter filename\n")
  53.         filename = input()
  54.         with open(filename) as f:
  55.             for i in range(n):
  56.                 line = list(f.readline().split())
  57.                 for j, elem in enumerate(line[:n]):
  58.                     A[i][j] = np.float64(elem)
  59.                 b[i] = np.float64(line[n])
  60.     elif typ == 2:
  61.         print("\nEnter parametr m\n")
  62.         m = np.float64(input())
  63.         for i in range(n):
  64.             for j in range(n):
  65.                 if i == j:
  66.                     A[i][j] = n + m ** 2 + j / m + i / n
  67.                 else:
  68.                     A[i][j] = (i + j) / (m + n)
  69.             b[i] = 200 + 50 * i
  70.            
  71.     print("\nChoose operation\n")
  72.     print("\t1 - Find solution\n" +
  73.           "\t2 - Learn iterations\n")
  74.     operation = int(input())  
  75.     w = 1
  76.     if operation == 1:
  77.         print("\nEnter w\n")
  78.         w = int(input())
  79.     print("solution =\n {0}".format(execute("solution", A, b, w)))
  80.     print("check:\n {0}\n".format(check(A, b)))
  81.     if operation == 2:
  82.         learn_iter(A, b)
  83.  
  84. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement