-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dimension_problems.py
380 lines (325 loc) · 12.4 KB
/
Dimension_problems.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# version code 9913d98afecb+
coursera = 1
# Please fill out this stencil and submit using the provided submission script.
from vecutil import list2vec
from GF2 import one
from solver import solve
from matutil import listlist2mat, coldict2mat
from mat import Mat
from vec import Vec
from The_Basis_problems import vec2rep
from The_Basis_problems import exchange
from The_Basis_problems import is_superfluous
from The_Basis_problems import is_independent
from independence import rank
from The_Basis_problems import rep2vec
from matutil import mat2coldict
from matutil import mat2rowdict
from vecutil import zero_vec
from matutil import identity
## 1: (Problem 1) Iterative Exchange Lemma
w0 = list2vec([1,0,0])
w1 = list2vec([0,1,0])
w2 = list2vec([0,0,1])
v0 = list2vec([1,2,3])
v1 = list2vec([1,3,3])
v2 = list2vec([0,3,3])
# Fill in exchange_S1 and exchange_S2
# with appropriate lists of 3 vectors
exchange_S0 = [w0, w1, w2]
exchange_S1 = [v0, w1, w2]
exchange_S2 = [v0, v1, w2]
exchange_S3 = [v0, v1, v2]
## 2: (Problem 2) Another Iterative Exchange Lemma
w0 = list2vec([0,one,0])
w1 = list2vec([0,0,one])
w2 = list2vec([one,one,one])
v0 = list2vec([one,0,one])
v1 = list2vec([one,0,0])
v2 = list2vec([one,one,0])
exchange_2_S0 = [w0, w1, w2]
exchange_2_S1 = [w0, v0, w2]
exchange_2_S2 = [v1, v0, w2]
exchange_2_S3 = [v0, v1, v2]
## 3: (Problem 3) Morph Lemma Coding
def morph(S, B):
'''
Input:
- S: a list of distinct Vecs
- B: a list of linearly independent Vecs all in Span S
Output: a list of pairs of vectors to inject and eject (see problem description)
Example:
>>> # This is how our morph works. Yours may yield different results.
>>> # Note: Make a copy of S to modify instead of modifying S itself.
>>> from vecutil import list2vec
>>> from vec import Vec
>>> S = [list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]]
>>> B = [list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]]
>>> D = {0, 1, 2}
>>> morph(S, B) == [(Vec(D,{0: 1, 1: 1, 2: 0}), Vec(D,{0: 1, 1: 0, 2: 0})), (Vec(D,{0: 0, 1: 1, 2: 1}), Vec(D,{0: 0, 1: 1, 2: 0})), (Vec(D,{0: 1, 1: 0, 2: 1}), Vec(D,{0: 0, 1: 0, 2: 1}))]
True
>>> S == [list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]]
True
>>> B == [list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]]
True
>>> from GF2 import one
>>> D = {0, 1, 2, 3, 4, 5, 6, 7}
>>> S = [Vec(D,{1: one, 2: one, 3: one, 4: one}), Vec(D,{1: one, 3: one}), Vec(D,{0: one, 1: one, 3: one, 5: one, 6: one}), Vec(D,{3: one, 4: one}), Vec(D,{3: one, 5: one, 6: one})]
>>> B = [Vec(D,{2: one, 4: one}), Vec(D,{0: one, 1: one, 2: one, 3: one, 4: one, 5: one, 6: one}), Vec(D,{0: one, 1: one, 2: one, 5: one, 6: one})]
>>> sol = morph(S, B)
>>> sol == [(B[0],S[0]), (B[1],S[2]), (B[2],S[3])] or sol == [(B[0],S[1]), (B[1],S[2]), (B[2],S[3])]
True
>>> # Should work the same regardless of order of S
>>> from random import random
>>> sol = morph(sorted(S, key=lambda x:random()), B)
>>> sol == [(B[0],S[0]), (B[1],S[2]), (B[2],S[3])] or sol == [(B[0],S[1]), (B[1],S[2]), (B[2],S[3])]
True
'''
morphed_S = list(S)
A = list(())
ret = list()
for b in B:
w = exchange(morphed_S, A, b)
morphed_S.remove(w)
morphed_S.append(b)
A.append(b)
ret.append((b, w))
return ret
## 4: (Problem 4) Row and Column Rank Practice
# Please express each solution as a list of Vecs
row_space_1 = [list2vec([1,2,0]), list2vec([0,2,1])]
col_space_1 = [list2vec([1,0]), list2vec([0,1])]
row_space_2 = [list2vec([1,4,0,0]), list2vec([0,2,2,0]), list2vec([0,0,1,1])]
col_space_2 = [list2vec([1,0,0]), list2vec([0,2,1]), list2vec([0,0,1])]
row_space_3 = [list2vec([1])]
col_space_3 = [list2vec([1,2,3])]
row_space_4 = [list2vec([1,0]), list2vec([2,1])]
col_space_4 = [list2vec([1,2,3]), list2vec([0,1,4])]
##: (Problem 5) Subset Basis
def subset_basis(T):
'''
Input:
- T: a set of Vecs
Output:
- set S containing Vecs from T that is a basis for Span T.
Examples:
The following tests use the procedure is_independent, provided in module independence
>>> from vec import Vec
>>> from independence import is_independent
>>> a0 = Vec({'a','b','c','d'}, {'a':1})
>>> a1 = Vec({'a','b','c','d'}, {'b':1})
>>> a2 = Vec({'a','b','c','d'}, {'c':1})
>>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3})
>>> sb = subset_basis({a0, a1, a2, a3})
>>> len(sb)
3
>>> all(v in [a0, a1, a2, a3] for v in sb)
True
>>> is_independent(sb)
True
>>> b0 = Vec({0,1,2,3},{0:2,1:2,3:4})
>>> b1 = Vec({0,1,2,3},{0:1,1:1})
>>> b2 = Vec({0,1,2,3},{2:3,3:4})
>>> b3 = Vec({0,1,2,3},{3:3})
>>> sb = subset_basis({b0, b1, b2, b3})
>>> len(sb)
3
>>> all(v in [b0, b1, b2, b3] for v in sb)
True
>>> is_independent(sb)
True
>>> D = {'a','b','c','d'}
>>> c0, c1, c2, c3, c4 = Vec(D,{'d': one, 'c': one}), Vec(D,{'d': one, 'a': one, 'c': one, 'b': one}), Vec(D,{'a': one}), Vec(D,{}), Vec(D,{'d': one, 'a': one, 'b': one})
>>> subset_basis({c0,c1,c2,c3,c4}) == {c0,c1,c2,c4}
True
'''
S = set(T)
for s in S.copy():
if is_superfluous(S, s):
S.remove(s)
return S
##: (Problem 6) Superset Basis Lemma in Python
def superset_basis(C, T):
'''
Input:
- C: linearly independent set of Vecs
- T: set of Vecs such that every Vec in C is in Span(T)
Output:
Linearly independent set S consisting of all Vecs in C and some in T
such that the span of S is the span of T (i.e. S is a basis for the span
of T).
Example:
>>> from vec import Vec
>>> from independence import is_independent
>>> a0 = Vec({'a','b','c','d'}, {'a':1})
>>> a1 = Vec({'a','b','c','d'}, {'b':1})
>>> a2 = Vec({'a','b','c','d'}, {'c':1})
>>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3})
>>> sb = superset_basis({a0, a3}, {a0, a1, a2})
>>> a0 in sb and a3 in sb
True
>>> is_independent(sb)
True
>>> all(x in [a0,a1,a2,a3] for x in sb)
True
'''
S = set(C)
S.union(set(T))
for v in S:
if is_superfluous(S, v):
S.remove(v)
return S
## 5: (Problem 7) My Is Independent Procedure
def my_is_independent(L):
'''
Input:
- L: a list of Vecs
Output:
- boolean: true if the list is linearly independent
Examples:
>>> D = {0, 1, 2}
>>> L = [Vec(D,{0: 1}), Vec(D,{1: 1}), Vec(D,{2: 1}), Vec(D,{0: 1, 1: 1, 2: 1}), Vec(D,{0: 1, 1: 1}), Vec(D,{1: 1, 2: 1})]
>>> my_is_independent(L)
False
>>> my_is_independent(L[:2])
True
>>> my_is_independent(L[:3])
True
>>> my_is_independent(L[1:4])
True
>>> my_is_independent(L[0:4])
False
>>> my_is_independent(L[2:])
False
>>> my_is_independent(L[2:5])
False
>>> L == [Vec(D,{0: 1}), Vec(D,{1: 1}), Vec(D,{2: 1}), Vec(D,{0: 1, 1: 1, 2: 1}), Vec(D,{0: 1, 1: 1}), Vec(D,{1: 1, 2: 1})]
True
'''
return rank(L)==len(L)
## 6: (Problem 8) My Rank
def my_rank(L):
'''
Input:
- L: a list of Vecs
Output:
- the rank of the list of Vecs
Example:
>>> L = [list2vec(v) for v in [[1,2,3],[4,5,6],[1.1,1.1,1.1]]]
>>> my_rank(L)
2
>>> L == [list2vec(v) for v in [[1,2,3],[4,5,6],[1.1,1.1,1.1]]]
True
>>> my_rank([list2vec(v) for v in [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[123,432,123]]])
2
'''
return len(subset_basis(L))
## 8: (Problem 9) Direct Sum Unique Representation
def direct_sum_decompose(U_basis, V_basis, w):
'''
Input:
- U_basis: a list of Vecs forming a basis for a vector space U
- V_basis: a list of Vecs forming a basis for a vector space V
- w: a Vec in the direct sum of U and V
Output:
- a pair (u, v) such that u + v = w, u is in U, v is in V
Example:
>>> D = {0,1,2,3,4,5}
>>> U_basis = [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})]
>>> V_basis = [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})]
>>> w = Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0})
>>> (u, v) = direct_sum_decompose(U_basis, V_basis, w)
>>> (u + v - w).is_almost_zero()
True
>>> U_matrix = coldict2mat(U_basis)
>>> V_matrix = coldict2mat(V_basis)
>>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero()
True
>>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero()
True
>>> ww = Vec(D,{0: 2, 1: 5, 2: 51, 4: 1, 5: 7})
>>> (u, v) = direct_sum_decompose(U_basis, V_basis, ww)
>>> (u + v - ww).is_almost_zero()
True
>>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero()
True
>>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero()
True
>>> U_basis == [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})]
True
>>> V_basis == [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})]
True
>>> w == Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0})
True
'''
U_V_basis = list(U_basis)
U_V_basis.extend(V_basis)
w_coordinates = vec2rep(U_V_basis, w)
w_u_coord = list()
w_v_coord = list()
for n in range(len(U_V_basis)):
if n < len(U_basis):
w_u_coord.append(w_coordinates[n])
else:
w_v_coord.append(w_coordinates[n])
u = rep2vec(list2vec(w_u_coord), U_basis)
v = rep2vec(list2vec(w_v_coord), V_basis)
return (u, v)
## 9: (Problem 10) Is Invertible Function
def is_invertible(M):
'''
input: A matrix, M
outpit: A boolean indicating if M is invertible.
>>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
>>> is_invertible(M)
True
>>> M1 = Mat(({0,1,2},{0,1,2}),{(0,0):1,(0,2):2,(1,2):3,(2,2):4})
>>> is_invertible(M1)
False
'''
coldict = mat2coldict(M)
return (my_rank(coldict.values())==len(coldict.values()) and len(coldict.values())==len(mat2rowdict(M).values()))
## 10: (Problem 11) Inverse of a Matrix over GF(2)
def find_matrix_inverse(A):
'''
Input:
- A: an invertible Mat over GF(2)
Output:
- A Mat that is the inverse of A
Examples:
>>> M1 = Mat(({0,1,2}, {0,1,2}), {(0, 1): one, (1, 0): one, (2, 2): one})
>>> find_matrix_inverse(M1) == Mat(M1.D, {(0, 1): one, (1, 0): one, (2, 2): one})
True
>>> M2 = Mat(({0,1,2,3},{0,1,2,3}),{(0,1):one,(1,0):one,(2,2):one})
>>> find_matrix_inverse(M2) == Mat(M2.D, {(0, 1): one, (1, 0): one, (2, 2): one})
True
'''
cols = range(len(mat2coldict(A)))
iden = mat2coldict(identity(A.D[0], one))
B = list()
for n in cols:
ones = iden[n]
B.append(solve(A, ones))
return coldict2mat(B)
from triangular import triangular_solve
## 11: (Problem 12) Inverse of a Triangular Matrix
def find_triangular_matrix_inverse(A):
'''
Supporting GF2 is not required.
Input:
- A: an upper triangular Mat with nonzero diagonal elements
Output:
- Mat that is the inverse of A
Example:
>>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
>>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0})
True
'''
rows = range(len(mat2rowdict(A)))
b = mat2coldict(identity(A.D[0], 1))
rowlist = mat2rowdict(A)
ret = list()
for n in rows:
ones = b[n]
ret.append(triangular_solve(list(rowlist.values()), list(A.D[1]), ones))
return coldict2mat(ret)