Advertisement
Dari_

Untitled

Nov 27th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. from math import *
  2.  
  3. global eps
  4. global n
  5.        
  6. def mistake(xk, xkp):                                #stopping criterion
  7.     norm = 0
  8.     for i in range(n):
  9.         norm += (xk[i] - xkp[i])*(xk[i] - xkp[i])
  10.     return (sqrt(norm) < eps)
  11.  
  12. eps = 0.0000001
  13.  
  14. print("input matrix range")          
  15. n = int(input())
  16. print("if you want input from keyboard, press 1,\nif you want to use formula press 2,\nif you want input from file press 3.")
  17. inp = int(input())
  18. print("input iterative parametr")
  19. u = float(input())
  20.  
  21. transp = [[0 for j in range(n)] for i in range(n)]
  22. mult = [[0 for j in range(n)] for i in range(n)]
  23. answ = [0 for i in range(n)]
  24. prev = [0 for i in range(n)]
  25. ans_col = [0 for i in range(n)]
  26. mult_ans = [0 for i in range(n)]
  27.  
  28. matrix = [[0 for j in range(n)] for i in range(n)]
  29.  
  30. if (inp == 1):                                                #from terminal
  31.     for i in range(n):
  32.         print("input", i + 1, "line")
  33.         line = list(map(float, input().split()))
  34.         for j in range(n):
  35.             matrix[i][j] = line[j]
  36.             transp[j][i] = matrix[i][j]
  37.         ans_col[i] = line[n]
  38.  
  39. elif inp == 2:                                                #using formula
  40.     for i in range(n):
  41.         for j in range(n):
  42.             if (i != j):
  43.                 matrix[i][j] = (i + j) / 65
  44.             else:
  45.                 matrix[i][j] = 275 + j / 15 + i / 50
  46.             transp[j][i] = matrix[j][i]
  47.         ans_col[i] = 750 - (i * i * i)
  48.    
  49. elif inp == 3:                                                 #from file named 'input.txt'
  50.     fin = open('input.txt', 'r')
  51.     for i in range(n):
  52.         line = list(map(float, (fin.readline()).split()))
  53.         for j in range(n):
  54.             matrix[i][j] = line[j]
  55.             transp[j][i] = matrix[i][j]
  56.         ans_col[i] = line[n]        
  57.     fin.close()
  58.  
  59. for i in range(n):                                             #multiplied matrix
  60.     for j in range(n):
  61.         mult[i][j] = 0
  62.         for k in range(n):
  63.             mult[i][j] += transp[i][k] * matrix[k][j]
  64.  
  65. for i in range(n):                                             #multiplied answers
  66.     mult_ans[i] = 0
  67.     for k in range(n):
  68.         mult_ans[i] += transp[i][k] * ans_col[k]
  69.  
  70. for i in range(n):
  71.     answ[i] = 0
  72.  
  73. count = 1
  74. for i in range(n):                                           #using method of upper relexation
  75.     prev[i] = answ[i]
  76. for i in range(n):
  77.     summa = 0
  78.     for j in range(i):
  79.         summa += (mult[i][j] * answ[j])
  80.     for j in range(i, n):
  81.         summa += (mult[i][j] * prev[j])
  82.     answ[i] = u * (mult_ans[i] - summa) / mult[i][i] + prev[i]
  83.    
  84. while (not mistake(answ, prev)):
  85.     count += 1
  86.     for i in range(n):
  87.         prev[i] = answ[i]
  88.     for i in range(n):
  89.         summa = 0
  90.         for j in range(i):
  91.             summa += (mult[i][j] * answ[j])
  92.         for j in range(i, n):
  93.             summa += (mult[i][j] * prev[j])
  94.         answ[i] = u * (mult_ans[i] - summa) / mult[i][i] + prev[i]  
  95.  
  96. for i in range(n):
  97.     print("answ[" + str(i + 1) + "] = " + str(answ[i]))
  98. print("\nIT =", count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement