-
Notifications
You must be signed in to change notification settings - Fork 1
/
Orthogonalization_problems.py
211 lines (166 loc) · 6.37 KB
/
Orthogonalization_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
# version code 0672c0a36066
coursera = 1
# Please fill out this stencil and submit using the provided submission script.
from mat import Mat
from vec import Vec
from vecutil import list2vec
from matutil import listlist2mat,mat2rowdict
from orthogonalization import orthogonalize,aug_orthogonalize
from math import sqrt
## 1: (Problem 1) Generators for orthogonal complement
U_vecs_1 = [list2vec([0,0,3,2])]
W_vecs_1 = [list2vec(v) for v in [[1,2,-3,-1],[1,2,0,1],[3,1,0,-1],[-1,-2,3,1]]]
# Give a list of Vecs
ortho_compl_generators_1 = [v for v in orthogonalize(U_vecs_1 + W_vecs_1)[len(U_vecs_1):] if v*v > 1e-10]
U_vecs_2 = [list2vec([3,0,1])]
W_vecs_2 = [list2vec(v) for v in [[1,0,0],[1,0,1]]]
# Give a list of Vecs
ortho_compl_generators_2 = [v for v in orthogonalize(U_vecs_2 + W_vecs_2)[len(U_vecs_2):] if v*v > 1e-10]
U_vecs_3 = [list2vec(v) for v in [[-4,3,1,-2],[-2,2,3,-1]]]
W_vecs_3 = [list2vec(v) for v in [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]]
# Give a list of Vecs
ortho_compl_generators_3 = [v for v in orthogonalize(U_vecs_3 + W_vecs_3)[len(U_vecs_1):] if v*v > 1e-10]
## 2: (Problem 2) Basis for null space
A_vecs = [list2vec(v) for v in [[-4,-1,-3,-2],[0,4,0,-1]]]
e_vecs = [list2vec(v) for v in [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]]
null_space_basis = [v for v in orthogonalize(A_vecs + e_vecs)[len(A_vecs):] if v*v > 1e-10]
## 3: (Problem 3) Orthonormalize(L)
def orthonormalize(L):
'''
Input: a list L of linearly independent Vecs
Output: A list Lstar of len(L) orthonormal Vecs such that, for all i in range(len(L)),
Span L[:i+1] == Span Lstar[:i+1]
>>> from vec import Vec
>>> D = {'a','b','c','d'}
>>> L = [Vec(D, {'a':4,'b':3,'c':1,'d':2}), Vec(D, {'a':8,'b':9,'c':-5,'d':-5}), Vec(D, {'a':10,'b':1,'c':-1,'d':5})]
>>> for v in orthonormalize(L): print(v)
...
<BLANKLINE>
a b c d
-----------------------
0.73 0.548 0.183 0.365
<BLANKLINE>
a b c d
--------------------------
0.187 0.403 -0.566 -0.695
<BLANKLINE>
a b c d
--------------------------
0.528 -0.653 -0.512 0.181
'''
vecs = orthogonalize(L)
norms = [sqrt(v*v) for v in vecs]
return [vecs[i]/norms[i] for i in range(len(vecs))]
## 4: (Problem 4) aug_orthonormalize(L)
def aug_orthonormalize(L):
'''
Input:
- L: a list of Vecs
Output:
- A pair Qlist, Rlist such that:
* coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
* Qlist = orthonormalize(L)
>>> from vec import Vec
>>> D={'a','b','c','d'}
>>> L = [Vec(D, {'a':4,'b':3,'c':1,'d':2}), Vec(D, {'a':8,'b':9,'c':-5,'d':-5}), Vec(D, {'a':10,'b':1,'c':-1,'d':5})]
>>> Qlist, Rlist = aug_orthonormalize(L)
>>> from matutil import coldict2mat
>>> print(coldict2mat(Qlist))
<BLANKLINE>
0 1 2
---------------------
a | 0.73 0.187 0.528
b | 0.548 0.403 -0.653
c | 0.183 -0.566 -0.512
d | 0.365 -0.695 0.181
<BLANKLINE>
>>> print(coldict2mat(Rlist))
<BLANKLINE>
0 1 2
------------------
0 | 5.48 8.03 9.49
1 | 0 11.4 -0.636
2 | 0 0 6.04
<BLANKLINE>
>>> print(coldict2mat(Qlist)*coldict2mat(Rlist))
<BLANKLINE>
0 1 2
---------
a | 4 8 10
b | 3 9 1
c | 1 -5 -1
d | 2 -5 5
<BLANKLINE>
'''
(a,b) = aug_orthogonalize(L)
norms = [sqrt(v*v) for v in a]
Qlist = [a[i]/norms[i] for i in range(len(a))]
scaler = coldict2mat([Vec(b[i].D,{i:norms[i]}) for i in range(len(b))])
Rlist = [scaler*v for v in b]
return (Qlist, Rlist)
## 5: (Problem 5) QR factorization of small matrices
#Compute the QR factorization
#Please represent your solution as a list of rows, such as [[1,0,0],[0,1,0],[0,0,1]]
part_1_Q = [[.857,.256],[.286,-.958],[.429,.128]]
part_1_R = [[7,6.43],[0,1.92]]
part_2_Q = [[.667,.707],[.667,-.707],[.333,0]]
part_2_R = [[3,3],[0,1.41]]
## 6: (Problem 6) QR Solve
from matutil import mat2coldict, coldict2mat
from python_lab import dict2list, list2dict
from triangular import triangular_solve
def QR_factor(A):
col_labels = sorted(A.D[1], key=repr)
Acols = dict2list(mat2coldict(A),col_labels)
Qlist, Rlist = aug_orthonormalize(Acols)
#Now make Mats
Q = coldict2mat(Qlist)
R = coldict2mat(list2dict(Rlist, col_labels))
return Q,R
def QR_solve(A, b):
'''
Input:
- A: a Mat with linearly independent columns
- b: a Vec whose domain equals the set of row-labels of A
Output:
- vector x that minimizes norm(b - A*x)
Note: This procedure uses the procedure QR_factor, which in turn uses dict2list and list2dict.
You wrote these procedures long back in python_lab. Make sure the completed python_lab.py
is in your matrix directory.
Example:
>>> domain = ({'a','b','c'},{'A','B'})
>>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
>>> Q, R = QR_factor(A)
>>> b = Vec(domain[0], {'a': 1, 'b': -1})
>>> x = QR_solve(A, b)
>>> result = A.transpose()*(b-A*x)
>>> result.is_almost_zero()
True
'''
q,r = QR_factor(A)
c = q.transpose()*b
rr = mat2rowdict(r)
labels = sorted(A.D[1], key=repr)
x = triangular_solve(rr,labels,c)
return x
## 7: (Problem 7) Least Squares Problem
# Please give each solution as a Vec
least_squares_A1 = listlist2mat([[8, 1], [6, 2], [0, 6]])
least_squares_Q1 = listlist2mat([[.8,-0.099],[.6, 0.132],[0,0.986]])
least_squares_R1 = listlist2mat([[10,2],[0,6.08]])
least_squares_b1 = list2vec([10, 8, 6])
x_hat_1 = Vec({0, 1},{0: 1.0832236842105263, 1: 0.9838815789473685})
least_squares_A2 = listlist2mat([[3, 1], [4, 1], [5, 1]])
least_squares_Q2 = listlist2mat([[.424, .808],[.566, .115],[.707, -.577]])
least_squares_R2 = listlist2mat([[7.07, 1.7],[0,.346]])
least_squares_b2 = list2vec([10,13,15])
x_hat_2 = Vec({0, 1},{0: 2.501098838207519, 1: 2.658959537572254})
## 8: (Problem 8) Small examples of least squares
#Find the vector minimizing (Ax-b)^2
#Please represent your solution as a list
your_answer_1 = [1.08, 0.984]
your_answer_2 = [3,1]
## 9: (Problem 9) Linear regression example
#Find a and b for the y=ax+b line of best fit
a = 0.6349650349650294
b = 64.92832167832181