Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def det(matrix):
- n = len(matrix)
- def add_line(index1, index2, k): # matrix[index1] - matrix[index2]*k
- for i in range(n):
- matrix[index1][i]+=matrix[index2][i]*k
- for i in range(n):
- first_index = -1;
- for j in range(i,n):
- if(matrix[i][j]!=0):
- first_index = j;
- break;
- if first_index==-1:
- return 0
- if first_index>i:
- add_line(i,first_index,1)
- for j in range(i+1, n):
- if(matrix[j][i]!=0):
- add_line(j,i,-matrix[j][i]/matrix[i][i])
- ans = 1
- for i in range(n):
- ans*=matrix[i][i]
- return ans
- dimCorrect = False
- input_dim = 0
- while True:
- print("Введите размерность матрицы")
- input_dim = input()
- try:
- input_dim = int(input_dim)
- if input_dim>0:
- break
- except Exception as e:
- continue
- print("Ошибка")
- matrix = [0 for _ in range(input_dim)]
- for i in range(input_dim):
- input_line = []
- while True:
- print("Введите ",int(i+1)," строку матрицы")
- try:
- input_line = list(map(float,input().split()))
- if len(input_line) == input_dim:
- lineCorrect = True
- break
- except Exception as e:
- continue
- print("Ошибка")
- matrix[i] = input_line
- print("Определитель матрицы равен: ", det(matrix))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement