forked from akinguyen/Coding_the_matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW.py
234 lines (200 loc) · 6.4 KB
/
HW.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
from solver import solve
from mat import Mat
from matutil import rowdict2mat, coldict2mat, mat2coldict, mat2rowdict, listlist2mat,identity
from vec import Vec
from vecutil import list2vec, zero_vec
from independence import rank
from GF2 import *
from triangular import triangular_solve
from echelon import transformation
from QR import factor
from triangular import triangular_solve
from read_data import read_vectors
from cancer_data import read_training_data
from math import sqrt
#HW chapter 5
def rep2vec(u,veclist):
return coldict2mat(veclist)*u
def vec2rep(veclist,v):
return solve(coldict2mat(veclist),v)
def is_superfluous(L,i):
if len(L) <= 1:
return False
L_copy = L.copy()
check_vec = L_copy.pop(i)
A = coldict2mat(L_copy)
res = check_vec - A*solve(A,check_vec)
return res*res < 10e-14
def is_independent(L):
for i in range(len(L)):
if(is_superfluous(L,i)):
return False
return True
def subset_basis(T):
B = []
for i in range(len(T)):
B.append(T[i])
if not is_independent(B):
B.remove(T[i])
return B
def exchange(S,A,z):
S_new = S.copy()
S_new.append(z)
for i in range(len(S_new)):
if is_superfluous(S_new, i) and S_new[i] not in A and S_new[i] is not z:
return S_new[i]
#HW chapter 6
def morph(S,B):
pairs = Ax = b
S_copy = S.copy()
B_copy = []
for i in range(len(B)):
w = exchange(S_copy,B_copy,B[i])
pairs.append((B[i],w))
S_copy.append(B[i])
S_copy.remove(w)
B_copy.append(B[i])
return pairs
def my_is_independent(L):
return len(L) == rank(L)
def my_rank(L):
return len(subset_basis(L))
def direct_sum_decompose(U,V,w):
U_copy = U.copy()
U_copy.extend(V)
linear_comb = solve(coldict2mat(U_copy),w)
u_linear_comb = list2vec([linear_comb[i] for i in range(len(U))])
v_linear_comb = list2vec([linear_comb[i] for i in range(len(U),len(V)+len(U))])
return (coldict2mat(U)*u_linear_comb,coldict2mat(V)*v_linear_comb)
def is_invertible(M):
return len(mat2coldict(M)) == len(mat2rowdict(M)) and my_is_independent([ v for (u,v) in mat2coldict(M).items()])
def find_matrix_inverse(A):
return coldict2mat([solve(A,b) for (a,b) in mat2coldict(identity(A.D[0],one)).items()])
#HW chapter 7
def is_echelon(rowlist):
k = -2
for i in range(len(rowlist)):
for j in range(len(rowlist[0].D)):
if rowlist[i][j] is not 0:
if j <= k:
return False
k = j
break
return True
def remove_irr_col(rowlist):
entry_col = []
label_row = range(len(rowlist))
label_col = list(rowlist[0].D)
assert is_echelon(rowlist)
for i in label_row:
for j in label_col:
if rowlist[i][j] is not 0:
entry_col.append(j)
break
remove_col = [i for i in label_col if i not in entry_col]
collist = mat2coldict(rowdict2mat(rowlist))
for i in remove_col:
collist.pop(i)
return collist
def new_tria_solve(rowlist,b):
collist = remove_irr_col(rowlist)
new_rowlist = [ values for (keys,values) in mat2rowdict(coldict2mat(collist)).items()]
return triangular_solve(new_rowlist,list(new_rowlist[0].D),b)
def has_solution(rowlist,b):
zero_vector = zero_vec(rowlist[0].D)
for i in range(len(rowlist)):
if (rowlist[i] == zero_vector and b[i] is not 0):
return False
return True
def echelon_solve(rowlist,label_list,b):
x = zero_vec(label_list)
entry_col = []
label_row = range(len(rowlist))
assert is_echelon(rowlist)
for i in label_row:
for j in label_list:
if rowlist[i][j] is not 0:
entry_col.append(j)
break
for i in reversed(label_row):
for c in reversed(entry_col):
row = rowlist[i]
x[c] = (b[i] - x*row)/row[c]
entry_col.remove(c)
break
return x
def solve2(A,b):
M = transformation(A)
U = M*A
U_rowdict = mat2rowdict(U)
rowlist = [U_rowdict[i] for i in sorted(U_rowdict)]
label_list = (A.D[1])
return echelon_solve(rowlist,label_list,M*b)
""" basis of NS(A.transpose) """
def basis_AT(A):
basis = []
M = transformation(A)
U = M*A
M_rowdict = mat2rowdict(M)
U_rowdict = mat2rowdict(U)
M_rowlist = [M_rowdict[i] for i in sorted(M_rowdict)]
U_rowlist = [U_rowdict[i] for i in sorted(U_rowdict)]
zero_vector = zero_vec(U_rowlist[0].D)
for i in range(len(U_rowlist)):
if (U_rowlist[i] == zero_vector):
basis.append(M_rowlist[i])
return basis
#HW chapter 9
def find_null_basis(A):
Q,R = factor(A)
R_inverse = find_matrix_inverse(R)
R_inverse_list = mat2coldict(R_inverse)
Q_list = mat2coldict(Q)
zero_vector = zero_vec(Q_list[0].D)
return [R_inverse_list[i] for i in range(len(Q_list)) if Q_list[i] is zero_vector ]
def QR_solve(A,b):
Q,R = factor(A)
R_rowlist = mat2rowdict(R)
label_list = sorted(A.D[1],key =repr)
return triangular_solve(R_rowlist,label_list,Q.transpose()*b)
""" Linear Regression """
def linear_regression(data):
datalist = read_vectors(data)
x = list(datalist[0].D)[1]
y = list(datalist[0].D)[0]
x_domain = {1,x}
x_rowlist = []
y_list = []
for v in datalist:
x_rowlist.append(Vec(x_domain,{1:1, x:v[x]}))
y_list.append(v[y])
y_vec = list2vec(y_list)
minimize = QR_solve(rowdict2mat(x_rowlist),y_vec)
return minimize[1],minimize[x] # return b,a
#HW chapter 11
def squared_Frob(A):
rowlist = mat2rowdict(A)
return sum([element*element for (i,element) in rowlist.items()])
def SVD_solve(A,b):
U,S,V = svd.factor(A)
return V*find_matrix_inverse(S)*U.transpose()*b
#HW chapter 12
def power_method(A, k):
v = Vec(A.D[1],{c:1 for c in A.D[1]})
for i in range(k):
w = A*v
v = w/sqrt(w*w)
lambda1 = v*A*v
return v, lambda1
def inverse_power_method(A,k):
v = Vec(A.D[1],{c:1 for c in A.D[1]})
for i in range(k):
w = solve(A,v)
v = w/sqrt(w*w)
lambda1 = v*A*v
return v, lambda1
def find_eigen_value(A,eigenvector):
return A*eigenvector*eigenvector/(eigenvector*eigenvector)
def find_eigenvector(A,eigenvalue):
A_eigenvalue = A - eigenvalue*identity(A.D)
return find_null_basis(A_eigenvalue)