Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import *
- global eps
- def deter(count, initial_matrix): #computation of determinant
- matrix = [[0 for j in range(count)] for i in range(count)]
- for i in range(count):
- for j in range(count):
- matrix[i][j] = initial_matrix[i][j]
- det = 1
- for k in range(count): #bringing matrix to the
- for i in range(k, count): #upper-traingular form
- tmp = matrix[i][k]
- if (fabs(tmp) > eps):
- for j in range(count):
- matrix[i][j] = matrix[i][j] / tmp
- det *= tmp
- if (i != k):
- for j in range(count):
- matrix[i][j] = matrix[i][j] - matrix[k][j]
- for i in range(count): #composition of
- det *= matrix[i][i] #diagonal elements
- return det
- def inverse(count, initial_matrix, temporary): #computation of inverse matrix
- matrix = [[0 for j in range(count)] for i in range(count)]
- for i in range(count):
- for j in range(count):
- matrix[i][j] = initial_matrix[i][j]
- for i in range(count):
- for j in range(count):
- if (i == j):
- temporary[i][j] = 1
- for k in range(count): #bringing matrix to the
- for i in range(k, count): #upper-traingular form
- tmp = matrix[i][k]
- if (fabs(tmp) > eps):
- for j in range(count):
- matrix[i][j] = matrix[i][j] / tmp
- temporary[i][j] = temporary[i][j] / tmp
- if (i != k):
- for j in range(count):
- matrix[i][j] = matrix[i][j] - matrix[k][j]
- temporary[i][j] = temporary[i][j] - temporary[k][j]
- for k in range(count - 1, -1, -1): #bringing matrix to the
- for i in range(count - 1, -1, -1): #bottom-traingular form
- tmp = matrix[i][k]
- if (fabs(tmp) > eps):
- for j in range(count):
- matrix[i][j] = matrix[i][j] / tmp
- temporary[i][j] = temporary[i][j] / tmp
- if (i != k):
- for j in range(count):
- matrix[i][j] = matrix[i][j] - matrix[k][j]
- temporary[i][j] = temporary[i][j] - temporary[k][j]
- def conditionality(count, initial_matrix): #computation of conditionality
- matrix = [[0 for j in range(count)] for i in range(count)]
- for i in range(count):
- for j in range(count):
- matrix[i][j] = initial_matrix[i][j]
- conditionality1 = 0 #conditionality of matrix
- for i in range(count):
- summa = 0
- for j in range(count):
- summa += fabs(matrix[i][j])
- if (not(i) or summa > conditionality1):
- conditionality1 = summa
- temporary = [[0 for j in range(count)] for i in range(count)]
- inverse(count, initial_matrix, temporary)
- conditionality2 = 0 #conditionality of inverse matrix
- for i in range(count):
- summa = 0
- for j in range(count):
- summa += fabs(temporary[i][j])
- if (not(i) or summa > conditionality2):
- conditionality2 = summa
- return conditionality2 * conditionality1
- def gauss(count, initial_matrix, x): #gauss method
- matrix = [[0 for j in range(count + 1)] for i in range(count)]
- for i in range(count):
- for j in range(count + 1):
- matrix[i][j] = initial_matrix[i][j]
- for k in range(count): #reduction to trangular form
- for i in range(k, count):
- tmp = matrix[i][k]
- if (fabs(tmp) > eps):
- for j in range(count + 1):
- matrix[i][j] = matrix[i][j] / tmp
- if (i != k):
- for j in range(count + 1):
- matrix[i][j] = matrix[i][j] - matrix[k][j]
- for k in range(count-1, -1, -1): #computation answers x[i]
- x[k] = matrix[k][count]
- for i in range(k):
- matrix[i][count] = matrix[i][count] - matrix[i][k] * x[k]
- def modified_gauss(count, initial_matrix, x): #modified gauss method
- matrix = [[0 for j in range(count + 1)] for i in range(count)]
- for i in range(count):
- for j in range(count + 1):
- matrix[i][j] = initial_matrix[i][j]
- for k in range(count):
- max_elem = fabs(matrix[k][k])
- max_index = k
- for i in range(k + 1, count):
- if (max_elem < fabs(matrix[i][k])): #selection of maximum element
- max_elem = fabs(matrix[i][k])
- max_index = i
- if (max_elem < eps):
- return
- for j in range(count + 1):
- tmp = matrix[k][j]
- matrix[k][j] = matrix[max_index][j]
- matrix[max_index][j] = tmp
- for i in range(k, count): #reduction to trangular form
- tmp = matrix[i][k]
- if (fabs(tmp) > eps):
- for j in range(count + 1):
- matrix[i][j] = matrix[i][j] / tmp
- if (i != k):
- for j in range(count + 1):
- matrix[i][j] = matrix[i][j] - matrix[k][j]
- for k in range(count - 1, -1, -1): #computation answers x[i]
- x[k] = matrix[k][count]
- for i in range(k):
- matrix[i][count] = matrix[i][count] - matrix[i][k] * x[k]
- eps = 0.000001
- print("input matrix range")
- count = int(input())
- x = [0 for j in range(count)]
- 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())
- if inp == 1: #from terminal
- matrix = []
- for i in range(count):
- print("input", i + 1, "line")
- matrix.append(list(map(float, input().split())))
- elif inp == 2: #using formula
- matrix = [[0 for j in range(count + 1)] for i in range(count)]
- for i in range(count):
- for j in range(count):
- if (i != j):
- matrix[i][j] = (i + j) / 65
- else:
- matrix[i][j] = 275 + j / 15 + i / 50
- matrix[i][count] = 750 - i * i * i
- elif inp == 3: #from file named 'input.txt'
- fin = open('input.txt', 'r')
- matrix = []
- for i in range(count): #schitat matrix
- matrix.append(list(map(float, (fin.readline()).split())))
- fin.close()
- print("\n")
- det = deter(count, matrix)
- print("determinant D =", det)
- if (det == 0):
- print("degenerate matrix\n")
- else:
- print("M =", conditionality(count, matrix), "\n")
- print("START OF GAUSS METHOD\n")
- gauss(count, matrix, x)
- for i in range(count):
- print("x[" + str(i + 1) + "] = " + str(x[i]))
- print("")
- print("END OF GAUSS METHOD\n")
- print("START OF MODIFIED GAUSS METHOD\n")
- modified_gauss(count, matrix, x)
- for i in range(count):
- print("x[" + str(i + 1) + "] = " + str(x[i]))
- print("")
- print("END OF MODIFIED GAUSS METHOD\n")
- print("INVERSE MATRIX\n")
- temporary = [[0 for j in range(count)] for i in range(count)]
- inverse(count, matrix, temporary)
- for elem in temporary:
- for el in elem:
- print(el, end = " ")
- print("")
- print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement