forked from scaleracademy/linear-algebra-assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.py
302 lines (269 loc) · 9.15 KB
/
checker.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from contextlib import contextmanager
from copy import deepcopy
import numpy as np
from scipy.optimize import linprog
import solutions
TAB = '\t'
num_tests = 10000
def close(x, y):
return np.allclose(x, y, 5e-2, 5e-2, True)
@contextmanager
def debug():
print('\n\u001b[41m\u001B[30m', end='')
yield
print('\033[0m')
@contextmanager
def info():
print('\u001b[43m\u001B[30m', end='')
yield
print('\033[0m', end='')
def _generate_matrix(n, m, rank, max=3):
# generate matrix of given dimensions and rank using A=UPV
while True:
U = np.random.randint(-1, max, (n, n))
V = np.random.randint(-1, max, (m, m))
P = np.eye(n, m)
P[rank:, rank:] = 0
A = U @ P @ V
# sort the rows so that the leftmost 0's are pulled to the top
# this way, if the matrix is singular, the row reductions require row swaps
A = sorted([list(map(float, row)) for row in A])
if np.linalg.matrix_rank(A) == rank:
return A
def _generate_vector(n, max=3):
return list(map(float, np.random.randint(-1, max, (n, 1))))
def generate_non_singular_matrix(max=3):
n = np.random.randint(2, 5)
return _generate_matrix(n, n, n, max=max)
def generate_singular_matrix(max=3):
n = np.random.randint(2, 5)
m = np.random.randint(2, 5)
rank = np.random.randint(1, min(n, m))
return _generate_matrix(n, m, rank, max=max)
def test_pldu():
def test(A):
try:
P, L, D, U = solutions.pldu.pldu(deepcopy(A))
except Exception:
raise
try:
A = np.array(A, dtype=np.double)
P = np.array(P, dtype=np.double)
L = np.array(L, dtype=np.double)
D = np.array(D, dtype=np.double)
U = np.array(U, dtype=np.double)
n, m = A.shape
assert P.shape == (n, n), 'Shape of P is incorrect'
assert L.shape == (n, n), 'Shape of L is incorrect'
assert D.shape == (n, n), 'Shape of D is incorrect'
assert U.shape == (n, m), 'Shape of U is incorrect'
if np.max(np.max(np.abs(L))) < 1e5 and np.max(np.max(np.abs(D))) < 1e5:
assert close(A, P @ L @ D @ U), 'Incorrect decomposition'
assert close(np.array(sorted(map(list, P), reverse=True), dtype=np.double),
np.eye(n, dtype=np.double)), 'P is not a permutation matrix'
assert close(L, np.tril(L, -1) + np.eye(n)), 'L is not a lower triangular matrix with unit diagonal'
assert close(D, np.diag(np.diag(D))), 'D is not a diagonal matrix'
assert np.max(np.abs(np.diag(U))) <= 1.1, 'U has not been normalized to have unit diagonal'
# don't expect students to be able to fix this themselves, hence won't check for upper-triangular-ness of U
# assert close(U, U - np.tril(U, -1)), 'U is not an upper triangular matrix with unit diagonal'
except KeyboardInterrupt:
raise
except Exception as e:
with debug():
print('Verdict:', e)
print('A')
print(A.round(2))
print('P')
print(P.round(2))
print('L')
print(L.round(2))
print('D')
print(D.round(2))
print('U')
print(U.round(2))
print('reconstruction')
print((P @ L @ D @ U).round(2))
return False
return True
marks = 0
with info():
print('-' * 80)
print('Testing PLDU')
print(TAB, 'non-singular matrix:', end=' ')
for _ in range(num_tests):
A = generate_non_singular_matrix()
result = test(A)
if not result:
with info():
print(TAB, 'Failed')
break
else:
marks += 6
with info():
print(TAB, 'Success!')
print(TAB, 'bonus: singular matrix:', end=' ')
for _ in range(num_tests):
A = generate_non_singular_matrix()
result = test(A)
if not result:
with info():
print(TAB, 'Failed')
break
else:
marks += 2
with info():
print('Success! (2 bonus marks)')
with info():
print(TAB, 'Final Marks for PLDU:', marks, '/ 6')
def test_equations():
def test_solve(A, b):
try:
x = solutions.equations.solve(deepcopy(A), deepcopy(b))
except Exception:
raise
A = np.array(A, dtype=np.double)
n, m = A.shape
b = np.array(b, dtype=np.double)
Ab = np.hstack((A, b.reshape(-1, 1)))
rank_A = np.linalg.matrix_rank(A)
rank_Ab = np.linalg.matrix_rank(Ab)
try:
x = np.array(x, dtype=np.double)
if rank_A != rank_Ab:
assert x == -1, 'Incorrect result for Inconsistent system of equations'
elif rank_A < n:
assert x == -2, 'Incorrect result for Infinite solutions'
else:
assert close(A @ x, b), 'Incorrect result for Ax = b'
except KeyboardInterrupt:
raise
except Exception as e:
with debug():
print('Verdict:', e)
print('A')
print(A.round(2))
print('b')
print(b.round(2))
print('returned answer')
print(x.round(2))
print('correct answer')
print(np.linalg.solve(A, b).round(2))
return False
return True
def test_det(A):
try:
d = solutions.equations.det(deepcopy(A))
except Exception:
raise
A = np.array(A, dtype=np.double)
n, m = A.shape
try:
if n != m:
assert d == 0, 'Incorrect result for non-square matrix'
else:
assert close(d, np.linalg.det(A)), 'Incorrect result for determinant'
except KeyboardInterrupt:
raise
except Exception as e:
with debug():
print('Verdict:', e)
print('A')
print(A.round(2))
print('returned answer')
print(d)
print('correct answer')
print(np.linalg.det(A))
return False
return True
marks = 0
with info():
print('-' * 80)
print('Testing Equations')
print(TAB, 'solve Ax=b:', end=' ')
for _ in range(num_tests):
A = generate_non_singular_matrix()
b = _generate_vector(len(A))
result = test_solve(A, b)
if not result:
with info():
print(TAB, 'Failed')
break
else:
marks += 3
with info():
print(TAB, 'Success!')
with info():
print(TAB, 'determinant:', end=' ')
for _ in range(num_tests):
A = generate_non_singular_matrix()
result = test_det(A)
if not result:
with info():
print(TAB, 'Failed')
break
else:
marks += 3
with info():
print(TAB, 'Success!')
with info():
print(TAB, 'Final Marks for Equations:', marks, '/ 6')
def test_simplex():
def test(A, b, c):
try:
x = solutions.simplex.simplex(deepcopy(A), deepcopy(b), deepcopy(c))
except Exception:
raise
try:
A = np.array(A, dtype=np.double)
b = np.array(b, dtype=np.double)
c = np.array(c, dtype=np.double)
x = np.array(x, dtype=np.double)
value = np.dot(c.T, x)
result = linprog(-c, A_ub=A, b_ub=b, bounds=(0, None))
if result.x is None:
return True # infeasible, so no need to check the result
assert close(value, -result.fun), 'Suboptimal result for Simplex'
except KeyboardInterrupt:
raise
except Exception as e:
with debug():
print('Verdict:', e)
print('A')
print(A.round(2))
print('b')
print(b.round(2))
print('c')
print(c.round(2))
print('returned answer')
print(x.round(2))
print('correct answer')
print(result.x.round(2))
print('correct optimal value')
print(-result.fun)
return False
return True
marks = 0
with info():
print('-' * 80)
print('Testing Simplex: ', end=' ')
for _ in range(num_tests):
A = generate_singular_matrix()
b = _generate_vector(len(A), max=1000)
c = _generate_vector(len(A[0]))
result = test(A, b, c)
if not result:
with info():
print(TAB, 'Failed')
break
else:
marks += 6
with info():
print(TAB, 'Success!')
with info():
print(TAB, 'Final Marks for Simplex:', marks, '/ 6')
def check_all():
np.random.seed(0)
test_pldu()
test_equations()
test_simplex()
print('\033[0m')