aboutsummaryrefslogtreecommitdiff
path: root/li/matlab_hw.py
blob: 34739349ee8474357fc26dd9357d6e634b0e9269 (plain)
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
import numpy as np
import scipy.linalg

print("Section 1.3: #30")

coeff = (
    [[ 1, 1, 1 ],
     [ 1, 2, 2 ],
     [ 2, 3, -4]]
    )
print(
np.linalg.solve(
    coeff,
    [6, 11, 3])
)

print(
np.linalg.solve(
    coeff,
    [7, 10, 3])
)

print("Section 1.3: #32")

dimension = 3
tot = [0]*3
ct = 10000
for x in range(ct):
    P, L, U = scipy.linalg.lu(np.random.rand(dimension, dimension))
    for i in range(dimension):
        tot[i] += U[i][i]
print([x/ct for x in tot]);

print("Section 1.4: #21")

A = [[.5, .5], [.5, .5]]
B = [[1, 0], [0, -1]]
C = np.matmul(A, B)
print("A^2")
print(np.linalg.matrix_power(A, 2))
print("A^3")
print(np.linalg.matrix_power(A, 3))
print("A^k = A");
print("B^2")
print(np.linalg.matrix_power(B, 2))
print("B^3")
print(np.linalg.matrix_power(B, 3))
print("B^{2k} = B^2. B^{2k+1} = B.")
print("C^2")
print(np.linalg.matrix_power(C, 2))
print("C^3")
print(np.linalg.matrix_power(C, 3))
print("C^k = 0")

print("Section 1.4: #59")

print("A * v = [3, 4, 5]")
print("v' * v = 50")
print(f"v * A = {np.matmul([3,4,5], np.identity(3))}")

print("Section 1.4: #60")

print("Av = [4. 4. 4. 4.]")
print("Bw = [10. 10. 10. 10.]")

print("Section 1.6: #12")

print("Section 1.6: #32")

print("the inverse of the first matrix is")
print(np.linalg.inv(5*np.identity(4) - np.ones((4,4))))
print("(meaning a = .4, b = .2)")
print("ones(4)*ones(4) =")
print(np.matmul(np.ones((4,4)),np.ones((4,4))))
print("so we can solve this like the following equation: (a*eye +\n\
b*ones)(c*eye + d*ones) = ac*eye + (ad+bc+dimension*bd)*ones = eye")
print("c = 1/a. d = -b/a/(a+dimension*b), giving for the next matrix")
print("a = 1/6, d = 6/(1/6+5*-1)") # WRONG

print("Section 1.6: #47")

print("I'm not sure. This is Python. It says (in either case):")
print("numpy.linalg.LinAlgError: Singular matrix")
'''
print(np.linalg.solve(np.ones((4, 4)), np.random.rand(4, 1)))
print(np.linalg.solve(np.ones((4, 4)), np.ones((4, 1))))
'''

''' #68
dimension = 500
northwest = np.random.rand(dimension, dimension)
for x in range(0, dimension):
    for y in range(x+1, dimension):
        northwest[y][x] = 0
print(northwest)
'''
print("Section 1.6: #69")


from time import perf_counter
matrix = np.random.rand(500, 500)
start = perf_counter()
np.linalg.inv(matrix)
first_time = perf_counter() - start
print("500x500 inverse:", str(first_time) + "s")
matrix = np.random.rand(1000, 1000)
start = perf_counter()
np.linalg.inv(matrix)
second_time = perf_counter() - start
print("1000x1000 inverse:", str(second_time) + "s")
print("factor:", second_time / first_time)

print("Section 1.6: #70")

dimension = 1000
I = np.identity(dimension)
A = np.random.rand(dimension, dimension)
U = np.random.rand(dimension, dimension)
for x in range(0, dimension):
    for y in range(x+1, dimension):
        U[y][x] = 0

start = perf_counter()
np.linalg.inv(U)
print("inverse of U:", str(perf_counter() - start) + "s")
start = perf_counter()
np.linalg.solve(U, I)
print("U\I:", str(perf_counter() - start) + "s")
start = perf_counter()
np.linalg.inv(A)
print("inverse of A:", str(perf_counter() - start) + "s")
start = perf_counter()
np.linalg.solve(A, I)
print("A\I:", str(perf_counter() - start) + "s")

print("Section 1.6: #71")

def proof(dim):
    L = np.identity(dim) - np.diag([x/(1+x) for x in range(1,dim)], -1)
    print(L)
    Linv = np.linalg.inv(L)
    print("L^{-1}:")
    print(Linv)
    right = True
    for j in range(1,dim+1):
        for i in range(1,j+1):
            if not np.isclose(Linv[j-1][i-1], i/j):
                print("the assertion in the book is wrong at",i,j)
                right = False
    if right:
        print("The book is right")

proof(4)
proof(5)

print("Section 2.2: #33")

print("For the first matrix:")
A = [[1, 3, 3], [2, 6, 9], [-1, -3, 3]]
b = [1, 5, 5]
print("null space:")
print(scipy.linalg.null_space(A))
print("particular solution:")
print(np.linalg.lstsq(A, b, rcond=None)[0])
print("For the second matrix:")
A = [[1, 3, 1, 2], [2, 6, 4, 8], [0, 0, 2, 4]]
b = [1, 3, 1]
print("null space:")
print(scipy.linalg.null_space(A))
print("particular solution:")
print(np.linalg.lstsq(A, b, rcond=None)[0])

print("Section 2.2: #35")
print("TODO")
print("For the first system:")
A = [[1, 2], [2, 4], [2, 5], [3, 9]]

print("Section 2.2: #36")

print("(a)")
A = np.array([[1, 2, 1], [2, 6, 3], [0, 2, 5]])
print("Basis of the column space:")
print(scipy.linalg.orth(A))
print("Multiplying the rows by this gives zero (for this matrix, \
equivalent to [0 0 0]^T)")
print(scipy.linalg.null_space(A.transpose()))

print("(b)")
A = np.array([[1, 1, 1], [1, 2, 4], [2, 4, 8]])
print("Basis of the column space:")
print(scipy.linalg.orth(A))
print("Multiplying the rows by this gives zero (for this matrix, \
equivalent to [0 2 -1]^T)")
print(scipy.linalg.null_space(A.transpose()))

print("Section 2.3: #2")

print("Largest possible number of independent vectors is the dimension \
of the space spanned by these vectors, or the number of vectors in the \
basis, which in this case is:")
v = [[ 1,  1,  1,  0,  0,  0],
     [-1,  0,  0,  1,  1,  0],
     [ 0, -1,  0, -1,  0,  1],
     [ 0,  0, -1,  0, -1, -1]]
print(len(scipy.linalg.orth(v)))

print("Section 2.3: #5")

print("(a)")

v = [[1, 2, 3],
     [3, 1, 2],
     [2, 3, 1]]
if len(scipy.linalg.orth(v)) == 3: print("they are independent")
else: print("they are not independent")

print("(b)")

v = [[ 1,  2, -3],
     [-3,  1,  2],
     [ 2, -3,  1]]
if len(scipy.linalg.orth(v)) == 3: print("they are independent")
else: print("they are not independent")

print("Section 2.3: #13")
# probably out of spirit of the matlab hw
print("This one can be done easily without a calculator because the \
        echelon matrix has already been computed.")
print("The dimensions of all of these is 2, and the row spaces are the \
        same.")

print("Section 2.3: #16")
print("TODO")

print("Section 2.3: #18")
print("TODO")

print("Section 2.3: #24")