From 1982e0b0abe8b94cbc6c3701ba3e6e193616d117 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Wed, 6 Oct 2021 15:18:32 -0400 Subject: interim changes to matlab hw --- li/matlab_hw.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/li/matlab_hw.py b/li/matlab_hw.py index 71b89b6..3473934 100644 --- a/li/matlab_hw.py +++ b/li/matlab_hw.py @@ -133,24 +133,47 @@ start = perf_counter() np.linalg.solve(A, I) print("A\I:", str(perf_counter() - start) + "s") -#print("Section 1.6: #71") +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]] -# TODO +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]] +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(sympy.Matrix.rref(A)) # ew is there no automated way to do this ? print("Section 2.2: #36") @@ -172,12 +195,44 @@ 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") -- cgit