-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw4.py
299 lines (225 loc) · 7.77 KB
/
hw4.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
# version code 941
# Please fill out this stencil and submit using the provided submission script.
from GF2 import one
from math import sqrt, pi
from matutil import coldict2mat
from solver import solve
from vec import Vec
## Problem 1
# For each part, please provide your solution as a list of the coefficients for
# the generators of V.
#
# For example, [1, 3, 5] would mean 1*[2,0,4,0] + 3*[0,1,0,1] + 5*[0,0,-1,-1]
rep_1 = [1,1,0]
rep_2 = [.5,1,1]
rep_3 = [0,1,-1]
## Problem 2
# For each part, please provide your solution as a list of the coefficients for
# the generators of V.
lin_comb_coefficients_1 = [3,-1,1]
lin_comb_coefficients_2 = [0.5,-1.5,1]
lin_comb_coefficients_3 = [0.5,-5.5,4]
lin_comb_coefficients_4 = [1,-2,1]
## Problem 3
# Use one from the GF2 module, not the integer 1.
# For each part, please provide your solution as a list of the coefficients for
# the generators of V.
gf2_rep_1 = [one,0,one,0]
gf2_rep_2 = [one,0,0,one]
gf2_rep_3 = [one,one,0,one]
## Problem 4
# Use one from the GF2 module, not the integer 1.
# For each part, please provide your solution as a list of the coefficients for
# the generators of V.
gf2_lc_rep_1 = [0,0,0,0,one,one,0,0]
gf2_lc_rep_2 = [0,0,0,0,0,0,one,one]
gf2_lc_rep_3 = [0,0,one,0,0,one,0,0]
gf2_lc_rep_4 = [one,0,one,0,0,0,0,0]
## Problem 5
# For each part, please provide your solution as a list of the coefficients for
# the generators of V.
lin_dep_R_1 = [-2,1,1]
lin_dep_R_2 = [-4,1,-4/7]
lin_dep_R_3 = [1.5/5,0,0,-1,-3]
## Problem 6
# Please record your solution as a list of coefficients
linear_dep_R_1 = [-1,1,-3]
linear_dep_R_2 = [2*pi,1,pi/sqrt(2)]
linear_dep_R_3 = [1,1,1,1,1]
## Problem 7
# Assign the COEFFICIENT of the vector to each variable.
# Assign sum_to to the vector that you are expressing as a linear combination
# of the other two. Write the name of the vector as a STRING. i.e. 'u' or 'w'
u = -1
v = 1
w = 0
sum_to = 'w'
## Problem 8
# Please use the Vec class to represent your vectors
indep_vec_1 = Vec({0,1,2}, {0:1,})
indep_vec_2 = Vec({0,1,2}, {1:1})
indep_vec_3 = Vec({0,1,2}, {2:1})
indep_vec_4 = Vec({0,1,2}, {0:1,1:1,2:1})
## Problem 9
# Please give your solution as a list of coefficients of the linear combination
zero_comb_1 = [one,one,0,one]
zero_comb_2 = [0,one,one,1]
zero_comb_3 = [one,one,0,0,one]
## Problem 10
# Please give your solution as a list of coefficients of the vectors
# in the set in order (list the coefficient for v_i before v_j if i < j).
sum_to_zero_1 = [0,one,0,one,one]
sum_to_zero_2 = [0,one,0,one,one,0,0]
sum_to_zero_3 = [one,0,one,one,one]
sum_to_zero_4 = [one,one,one,one,one,0,0]
## Problem 11
## Please express your answer a list of ints, such as [1,0,0,0,0]
exchange_1 = [0,0,0,0,1]
exchange_2 = [0,0,0,1,0]
exchange_3 = [0,0,1,0,0]
## Problem 12
# Please give the name of the vector you want to replace as a string (e.g. 'v1')
replace_1 = 'v3'
replace_2 = 'v1'
replace_3 = 'v1'
## Problem 13
def rep2vec(u, veclist):
'''
Input:
- u: a vector as an instance of your Vec class with domain set(range(len(veclist)))
- veclist: a list of n vectors (as Vec instances)
Output:
vector v (as Vec instance) whose coordinate representation is u
Example:
>>> a0 = Vec({'a','b','c','d'}, {'a':1})
>>> a1 = Vec({'a','b','c','d'}, {'b':1})
>>> a2 = Vec({'a','b','c','d'}, {'c':1})
>>> rep2vec(Vec({0,1,2}, {0:2, 1:4, 2:6}), [a0,a1,a2]) == Vec({'a', 'c', 'b', 'd'},{'a': 2, 'c': 6, 'b': 4, 'd': 0})
True
'''
return coldict2mat(veclist)*u
## Problem 14
def vec2rep(veclist, v):
'''
Input:
- veclist: a list of vectors (as instances of your Vec class)
- v: a vector (as Vec instance) with domain set(range(len(veclist)))
with v in the span of set(veclist).
Output:
Vec instance u whose coordinate representation w.r.t. veclist is v
Example:
>>> a0 = Vec({'a','b','c','d'}, {'a':1})
>>> a1 = Vec({'a','b','c','d'}, {'b':1})
>>> a2 = Vec({'a','b','c','d'}, {'c':1})
>>> vec2rep([a0,a1,a2], Vec({'a','b','c','d'}, {'a':3, 'c':-2})) == Vec({0, 1, 2},{0: 3.0, 1: 0.0, 2: -2.0})
True
'''
return solve(coldict2mat(veclist),v)
## Problem 15
def is_superfluous(L, i):
'''
Input:
- L: list of vectors as instances of Vec class
- i: integer in range(len(L))
Output:
True if the span of the vectors of L is the same
as the span of the vectors of L, excluding L[i].
False otherwise.
Examples:
>>> 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})
>>> is_superfluous(L, 3)
True
>>> is_superfluous([a0,a1,a2,a3], 3)
True
>>> is_superfluous([a0,a1,a2,a3], 0)
True
>>> is_superfluous([a0,a1,a2,a3], 1)
False
'''
if len(L) == 1:
return False
Li = L.pop(i)
mL = coldict2mat(L)
sol = solve(mL,Li)
res = Li - mL * sol
if abs(res*res) < 10**-14:
return True
else:
return False
## Problem 16
def is_independent(L):
'''
input: a list L of vectors (using vec class)
output: True if the vectors form a linearly independent list.
>>> vlist = [Vec({0, 1, 2},{0: 1, 1: 0, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 0})]
>>> is_independent(vlist)
False
>>> is_independent(vlist[:3])
True
>>> is_independent(vlist[:2])
True
>>> is_independent(vlist[1:4])
True
>>> is_independent(vlist[2:5])
True
>>> is_independent(vlist[2:6])
False
>>> is_independent(vlist[1:3])
True
>>> is_independent(vlist[5:])
True
'''
isIndependent = True
for i in range(len(L)):
isIndependent = not is_superfluous(L,i)
if not isIndependent:
return False
return True
## Problem 17
def superset_basis(S, L):
'''
Input:
- S: linearly independent list of Vec instances
- L: list of Vec instances such that every vector in S is in Span(L)
Output:
Linearly independent list T containing all vectors (as instances of Vec)
such that the span of T is the span of L (i.e. T is a basis for the span
of L).
Example:
>>> 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})
>>> superset_basis([a0, a3], [a0, a1, a2]) == [Vec({'a', 'c', 'b', 'd'},{'a': 1}), Vec({'a', 'c', 'b', 'd'},{'b':1}),Vec({'a', 'c', 'b', 'd'},{'c': 1})]
True
'''
for iVec in L:
iS = S.copy()
iS.append(iVec)
if is_independent(iS):
S.append(iVec)
return S
## Problem 18
def exchange(S, A, z):
'''
Input:
- S: a list of vectors, as instances of your Vec class
- A: a list of vectors, each of which are in S, with len(A) < len(S)
- z: an instance of Vec such that A+[z] is linearly independent
Output: a vector w in S but not in A such that Span S = Span ({z} U S - {w})
Example:
>>> S = [list2vec(v) for v in [[0,0,5,3],[2,0,1,3],[0,0,1,0],[1,2,3,4]]]
>>> A = [list2vec(v) for v in [[0,0,5,3],[2,0,1,3]]]
>>> z = list2vec([0,2,1,1])
>>> exchange(S, A, z) == Vec({0, 1, 2, 3},{0: 0, 1: 0, 2: 1, 3: 0})
True
'''
zRep = solve(coldict2mat(S),z)
testVecs = [S[index] for index,sol in zRep.f.items() if sol > 10**-3]
for i in testVecs:
if not i in A:
return i
print("DAMMIT!")