-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.py
272 lines (234 loc) · 7.58 KB
/
algo.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import numpy as np
from enum import Enum
class State(Enum):
NO_ZERO = 1
ZERO_ROW_COL = 2
ZERO_ROW = 3
ZERO_COL = 4
ZERO_EL = 5
TEMPORARY_SKIPPING_COL = 6
START = 50
END = 51
UNDETERMINED = 100
class Action(Enum):
INCREMENT_ROW_COL = 1
INCREMENT_ROW = 2
INCREMENT_COL = 3
FIND_NONZERO_EL = 4
INCREMENT_ROW_AND_RECALL_COL = 5
START = 50
END = 51
UNDETERMINED = 100
class Data():
def __init__(
self,
mat: np.matrix, row: np.matrix, col: np.matrix, CnRm: np.matrix, remainder: np.matrix,
m: int, n: int, element: float,
mat_idx: int, remainder_idx: int
) -> None:
self.mat = mat
self.row = row
self.col = col
self.CnRm = CnRm
self.remainder = remainder
self.m = m
self.n = n
self.element = element
self.mat_idx = mat_idx
self.remainder_idx = remainder_idx
mat: np.matrix
row: np.matrix
col: np.matrix
CnRm: np.matrix
remainder: np.matrix
m: int
n: int
element: float
mat_idx: int
remainder_idx: int
def view(self):
print(f'{self.mat_idx=}')
print(f'{self.remainder_idx=}')
print(f'{self.mat=}')
print(f'{self.row=}')
print(f'{self.col=}')
print(f'{self.CnRm=}')
print(f'{self.remainder=}')
print(f'{self.m=}')
print(f'{self.n=}')
print(f'{self.element=}')
class Step():
def __init__(
self, number: int, state: State, action: Action, data: Data
) -> None:
self.number = number
self.state = state
self.action = action
self.data = data
# Discription
number: int
state: int = State.UNDETERMINED
action: int = Action.UNDETERMINED
# Data
data: Data
# Viewing function
def view(self):
print('===================================')
print(f'{self.number=}')
print(f'{self.state=}')
print(f'{self.action=}')
self.data.view()
class Decomposition():
original_matrix: np.matrix
steps = []
# A = CR
rows = []
cols = []
indices = []
# A = LU
L = np.matrix
U = np.matrix
# The decomposition algorithm
# A = CR
def decom(mat: np.matrix, m: int, n: int, save_n: int, goto_save: bool, saves: Decomposition) -> Decomposition:
# Get the matrix's dimensions
num_of_row = mat.shape[0]
num_of_col = mat.shape[1]
row_m = mat[m, :]
col_n = mat[:, n]
element_mn = mat[m, n]
if element_mn != 0:
row_m = row_m / element_mn
CnRm = np.matmul(col_n, row_m)
remainder = mat - CnRm
# If the remainder is a zero matrix
# -> The matrix has been decomposed
if not remainder.any():
# Save answer
saves.rows.append(row_m)
saves.cols.append(col_n)
saves.indices.append([m, n])
# Save step
saves.steps.append(Step(
saves.steps[-1].number + 1,
State.END,
Action.END,
Data(
mat, row_m, col_n, CnRm, remainder,
m, n, element_mn,
saves.steps[-1].data.remainder_idx,
saves.steps[-1].data.remainder_idx + 1,
)
))
return saves # End of recursion
else:
# Reaching the last row and column without decomposing the matrix
if m == num_of_row - 1 and n == num_of_col - 1:
print("Something went wrong?")
return
# Save answer
saves.rows.append(row_m)
saves.cols.append(col_n)
saves.indices.append([m, n])
# Continue decomposing
if goto_save:
# Save step
saves.steps.append(Step(
saves.steps[-1].number + 1,
State.TEMPORARY_SKIPPING_COL,
Action.INCREMENT_ROW_AND_RECALL_COL,
Data(
mat, row_m, col_n, CnRm, remainder,
m, n, element_mn,
saves.steps[-1].data.remainder_idx,
saves.steps[-1].data.remainder_idx + 1,
)
))
return decom(mat=remainder, m=m+1, n=save_n, save_n=0, goto_save=False, saves=saves)
# Else (not goto_save)
saves.steps.append(Step(
saves.steps[-1].number + 1,
State.NO_ZERO,
Action.INCREMENT_ROW_COL,
Data(
mat, row_m, col_n, CnRm, remainder,
m, n, element_mn,
saves.steps[-1].data.remainder_idx,
saves.steps[-1].data.remainder_idx + 1,
)
))
return decom(mat=remainder, m=m+1, n=n+1, save_n=0, goto_save=False, saves=saves)
# element_mn == 0
else:
CnRm = np.matmul(col_n, row_m)
remainder = mat - CnRm
# If row_m AND col_n are zero-vectors
if (
(not row_m.any()) and
(not col_n.any())
):
saves.steps.append(Step(
saves.steps[-1].number + 1,
State.ZERO_ROW_COL,
Action.INCREMENT_ROW_COL,
Data(
mat, row_m, col_n, CnRm, remainder,
m, n, 0.0,
saves.steps[-1].data.remainder_idx,
saves.steps[-1].data.remainder_idx,
)
))
return decom(mat=mat, m=m+1, n=n+1, save_n=0, goto_save=False, saves=saves)
# If only row_m is a zero-vector
if not row_m.any():
saves.steps.append(Step(
saves.steps[-1].number + 1,
State.ZERO_ROW,
Action.INCREMENT_ROW,
Data(
mat, row_m, col_n, CnRm, remainder,
m, n, element_mn,
saves.steps[-1].data.remainder_idx,
saves.steps[-1].data.remainder_idx,
)
))
return decom(mat=mat, m=m + 1, n=n, save_n=0, goto_save=False, saves=saves)
# If only col_n is a zero-vector
if not col_n.any():
saves.steps.append(Step(
saves.steps[-1].number + 1,
State.ZERO_COL,
Action.INCREMENT_COL,
Data(
mat, row_m, col_n, CnRm, remainder,
m, n, element_mn,
saves.steps[-1].data.remainder_idx,
saves.steps[-1].data.remainder_idx,
)
))
return decom(mat=mat, m=m, n=n + 1, save_n=0, goto_save=False, saves=saves)
# None of the above -> Only element_mn is zero
# A[m][n] = 0
# Find element A[m][n + a] != 0
temp = n
while mat[m, temp] == 0:
temp += 1
if temp == num_of_col: # Exceeds number of columns -> This row is a zero-vector
break
# Found
# -> Pick out C(n + a) and Rm for the decomposition,
# instead of Cn and Rm,
# and remember Cn
# Afterward, continue with the next row - Cn and R(m + 1)
saves.steps.append(Step(
saves.steps[-1].number + 1,
State.ZERO_EL,
Action.FIND_NONZERO_EL,
Data(
mat, row_m, col_n, None, None,
m, n, element_mn,
saves.steps[-1].data.remainder_idx,
saves.steps[-1].data.remainder_idx,
)
))
return decom(mat=mat, m=m, n=temp, save_n=n, goto_save=True, saves=saves)