-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeterminant.py
More file actions
36 lines (33 loc) · 765 Bytes
/
Copy pathDeterminant.py
File metadata and controls
36 lines (33 loc) · 765 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
34
35
36
import numpy as np
def getminor(mat,temp,n,p,q):
i,j = 0,0
for row in range(n):
for col in range(n):
if (row!=p and col!=q):
temp[i][j] = mat[row][col]
j += 1
if (j==n-1):
j = 0
i = i+1
return temp
def determinant(mat,n):
D = 0
sign = 1
temp = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
if (n==1):
return mat[0][0]
for f in range(n):
temp = getminor(mat,temp,n,0,f)
D += sign*mat[0][f]*determinant(temp,n-1)
sign = -sign
return D
N = 4
matrix = [[1, 0, 2, -1],
[3, 0, 0, 5 ],
[2, 1, 4, -3],
[1, 0, 5, 0 ]]
matrix = np.array(matrix)
print("Determinant of the matrix is : {}".format(determinant(matrix, N)))