-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterminat.py
More file actions
36 lines (24 loc) · 970 Bytes
/
Copy pathdeterminat.py
File metadata and controls
36 lines (24 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def determinant(matrix):
"""
Calculates the determinant of a matrix using a recursive algorithm.
"""
# Base case: 1x1 matrix
if len(matrix) == 1:
return matrix[0][0]
# Base case: 2x2 matrix
if len(matrix) == 2:
return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]
det = 0
# Iterate over the first row of the matrix
for j in range(len(matrix)):
# Calculate the submatrix by removing the first row and j-th column
submatrix = [[matrix[i][k] for k in range(len(matrix)) if k != j] for i in range(1, len(matrix))]
# Add or subtract the product of the first element and the determinant of the submatrix
det += (-1)**j * matrix[0][j] * determinant(submatrix)
return det
def display(matrix):
if(len(matrix)==len(matrix[0])):
det=determinant(matrix)
print(det)
else :
print("it should be square matrix!!")