-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhw6.py
160 lines (118 loc) · 3.66 KB
/
hw6.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
# version code 988
# Please fill out this stencil and submit using the provided submission script.
from matutil import *
from GF2 import one
from vecutil import zero_vec
## Problem 1
# Write each matrix as a list of row lists
echelon_form_1 = [[1, 2, 0, 2, 0],
[0, 1, 0, 3, 4],
[0, 0, 2, 3, 4],
[0, 0, 0, 2, 0],
[0, 0, 0, 0, 4]]
echelon_form_2 = [[0, 4, 3, 4, 4],
[0, 0, 4, 2, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0]]
echelon_form_3 = [[1, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 0]]
echelon_form_4 = [[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
## Problem 2
def is_echelon(A):
'''
Input:
- A: a list of row lists
Output:
- True if A is in echelon form
- False otherwise
Examples:
>>> is_echelon([[1,1,1],[0,1,1],[0,0,1]])
True
>>> is_echelon([[0,1,1],[0,1,0],[0,0,1]])
False
'''
i = -1 # initializing lowest index of first non-zero entry
ncols = len(A[0])
for row in A:
j = 0 #initializing current row index
while j < ncols and row[j] == 0:
j += 1
if j <= i and j < ncols-1:
return False
i = j
return True
## Problem 3
# Give each answer as a list
echelon_form_vec_a = [1, 0, 3, 0]
echelon_form_vec_b = [-3, 0, -2, 3]
echelon_form_vec_c = [-5, 0, 2, 0, 2]
## Problem 4
# If a solution exists, give it as a list vector.
# If no solution exists, provide "None".
solving_with_echelon_form_a = None
solving_with_echelon_form_b = [21, 0, 2, 0, 0]
## Problem 5
def echelon_solve(rowlist, label_list, b):
'''
Input:
- rowlist: a list of Vecs
- label_list: a list of labels establishing an order on the domain of
Vecs in rowlist
- b: a vector (represented as a list)
Output:
- Vec x such that rowlist * x is b
>>> D = {'A','B','C','D','E'}
>>> U_rows = [Vec(D, {'A':one, 'E':one}), Vec(D, {'B':one, 'E':one}), Vec(D,{'C':one})]
>>> b_list = [one,0,one]>>> cols = ['A', 'B', 'C', 'D', 'E']
>>> echelon_solve(U_rows, cols, b_list)
Vec({'B', 'C', 'A', 'D', 'E'},{'B': 0, 'C': one, 'A': one})
'''
D = rowlist[0].D
ncols = len(D)
x = zero_vec(D)
# remove all zero rows at the bottom
i = len(rowlist)
while rowlist[i-1] == zero_vec(D):
i -= 1
for j in reversed(range(i)):
row = rowlist[j]
pivot_index = 0 # lowest possible pivot index (should be set to j)
c = label_list[pivot_index]
while row[c] == 0:
pivot_index += 1
c = label_list[pivot_index]
x[c] = (b[j] - x*row)/row[c]
return x
## Problem 6
D = {'A', 'B', 'C', 'D'}
rowlist = [Vec(D, {'A':one, 'B':one, 'D':one}),
Vec(D, {'B':one}),
Vec(D, {'C':one}),
Vec(D, {'D':one})] # Provide as a list of Vec instances
label_list = ['A', 'B', 'C', 'D'] # Provide as a list
b = [one, one, 0, 0] # Provide as a list
## Problem 7
null_space_rows_a = {3, 4} # Put the row numbers of M from the PDF
## Problem 8
null_space_rows_b = {4}
## Problem 9
# Write each vector as a list
closest_vector_1 = [8/5, 16/5]
closest_vector_2 = [0, 1, 0]
closest_vector_3 = [3, 2, 1, -4]
## Problem 10
# Write each vector as a list
project_onto_1 = [2, 0]
projection_orthogonal_1 = [0, 1]
project_onto_2 = [-1/6, -1/3, 1/6]
projection_orthogonal_2 = [7/6, 4/3, 23/6]
project_onto_3 = [1, 1, 4]
projection_orthogonal_3 = [0, 0, 0]
## Problem 11
norm1 = 3
norm2 = 4
norm3 = 1