-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.py
More file actions
40 lines (30 loc) · 1.21 KB
/
source.py
File metadata and controls
40 lines (30 loc) · 1.21 KB
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
37
38
39
40
def det_matrix(matrix):
'''input : a nested list square matrix
output :a float scaler
if unable to execute, raises error ? : True'''
from copy import deepcopy
m=deepcopy(matrix)
try:
if len(m)==1:
det=m[0]
return det
to_sum=[]
uniq=list(set(map(len, m)) )
if len(uniq)==1 and uniq[0]== len(m): # check is matrix square
if len(m)==2:
det= (m[0][0]*m[1][1])-(m[0][1]*m[1][0])
return det
elif len(m) > 2:
for column in range(len(m)):
ele= m[0][column]
sub=deepcopy(m)[1:]
for row in range(len(sub)):
del sub[row][column] # delete columnth element from each row of sub matrix
to_sum+=[pow(-1,column)*ele*det_matrix(sub)]
det=sum(to_sum)
return det
else:
error=TypeError('INPUT IS NOT A SQUARE MATRIX')
raise error
except TypeError:
print('INPUT IS NOT A SQUARE MATRIX')