Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import *
- global eps
- global n
- def mistake(xk, xkp): #stopping criterion
- norm = 0
- for i in range(n):
- norm += (xk[i] - xkp[i])*(xk[i] - xkp[i])
- return (sqrt(norm) < eps)
- eps = 0.0000001
- print("input matrix range")
- n = int(input())
- 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.")
- inp = int(input())
- print("input iterative parametr")
- u = float(input())
- transp = [[0 for j in range(n)] for i in range(n)]
- mult = [[0 for j in range(n)] for i in range(n)]
- answ = [0 for i in range(n)]
- prev = [0 for i in range(n)]
- ans_col = [0 for i in range(n)]
- mult_ans = [0 for i in range(n)]
- matrix = [[0 for j in range(n)] for i in range(n)]
- if (inp == 1): #from terminal
- for i in range(n):
- print("input", i + 1, "line")
- line = list(map(float, input().split()))
- for j in range(n):
- matrix[i][j] = line[j]
- transp[j][i] = matrix[i][j]
- ans_col[i] = line[n]
- elif inp == 2: #using formula
- for i in range(n):
- for j in range(n):
- if (i != j):
- matrix[i][j] = (i + j) / 65
- else:
- matrix[i][j] = 275 + j / 15 + i / 50
- transp[j][i] = matrix[j][i]
- ans_col[i] = 750 - (i * i * i)
- elif inp == 3: #from file named 'input.txt'
- fin = open('input.txt', 'r')
- for i in range(n):
- line = list(map(float, (fin.readline()).split()))
- for j in range(n):
- matrix[i][j] = line[j]
- transp[j][i] = matrix[i][j]
- ans_col[i] = line[n]
- fin.close()
- for i in range(n): #multiplied matrix
- for j in range(n):
- mult[i][j] = 0
- for k in range(n):
- mult[i][j] += transp[i][k] * matrix[k][j]
- for i in range(n): #multiplied answers
- mult_ans[i] = 0
- for k in range(n):
- mult_ans[i] += transp[i][k] * ans_col[k]
- for i in range(n):
- answ[i] = 0
- count = 1
- for i in range(n): #using method of upper relexation
- prev[i] = answ[i]
- for i in range(n):
- summa = 0
- for j in range(i):
- summa += (mult[i][j] * answ[j])
- for j in range(i, n):
- summa += (mult[i][j] * prev[j])
- answ[i] = u * (mult_ans[i] - summa) / mult[i][i] + prev[i]
- while (not mistake(answ, prev)):
- count += 1
- for i in range(n):
- prev[i] = answ[i]
- for i in range(n):
- summa = 0
- for j in range(i):
- summa += (mult[i][j] * answ[j])
- for j in range(i, n):
- summa += (mult[i][j] * prev[j])
- answ[i] = u * (mult_ans[i] - summa) / mult[i][i] + prev[i]
- for i in range(n):
- print("answ[" + str(i + 1) + "] = " + str(answ[i]))
- print("\nIT =", count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement