-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeterminant.py
179 lines (134 loc) · 3.9 KB
/
determinant.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from minor import minor
def det1x1(matrix):
return matrix[0][0]
def det2x2(matrix):
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
def det3x3(matrix):
a = matrix[0][0]
b = matrix[0][1]
c = matrix[0][2]
d = matrix[1][0]
e = matrix[1][1]
f = matrix[1][2]
g = matrix[2][0]
h = matrix[2][1]
i = matrix[2][2]
return a * e * i + b * f * g + c * d * h - c * e * g - a * f * h - b * d * i
def det4x4(matrix):
ans = 0
depth = 4
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det5x5(matrix):
ans = 0
depth = 5
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det6x6(matrix):
ans = 0
depth = 6
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det7x7(matrix):
ans = 0
depth = 7
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det8x8(matrix):
ans = 0
depth = 8
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det9x9(matrix):
ans = 0
depth = 9
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det10x10(matrix):
ans = 0
depth = 10
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det11x11(matrix):
ans = 0
depth = 11
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det12x12(matrix):
ans = 0
depth = 12
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det13x13(matrix):
ans = 0
depth = 13
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det14x14(matrix):
ans = 0
depth = 14
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det15x15(matrix):
ans = 0
depth = 15
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det16x16(matrix):
ans = 0
depth = 16
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det17x17(matrix):
ans = 0
depth = 17
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det18x18(matrix):
ans = 0
depth = 18
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
def det19x19(matrix):
ans = 0
depth = 19
row = matrix[0]
for i in range(len(matrix[0])):
ans += (-1) ** i * row[i] * det[depth - 1](minor(matrix, 0, i))
return ans
det = [0, det1x1, det2x2, det3x3, det4x4, det5x5,
det6x6, det7x7, det8x8, det9x9, det10x10,
det11x11, det12x12, det13x13, det14x14,
det15x15, det16x16, det17x17, det18x18, det19x19]
def determinant(matrix):
return det[len(matrix)](matrix)