-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearAlgebraSolverNumpy.py
More file actions
61 lines (45 loc) · 1.28 KB
/
linearAlgebraSolverNumpy.py
File metadata and controls
61 lines (45 loc) · 1.28 KB
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
#Michael Wieck-Sosa Problem 4
import numpy as np
import matplotlib.pyplot as plt
#1
Q = np.random.random((4,4))
print("Printing random 4x4 matrix below...")
print(Q)
u = Q[:,0]
v = Q[:,1]
w = Q[:,2]
x = Q[:,3]
#Gram-Schmidt process
u /= np.linalg.norm(u)
v -= v.dot(u)*u
v /= np.linalg.norm(v)
w -= w.dot(u)*u + w.dot(v)*v
w /= np.linalg.norm(w)
x -= x.dot(u)*u + x.dot(v)*v + x.dot(w)*w
x /= np.linalg.norm(x)
#orthogonal matrix O
O = np.stack((u,v,w,x))
print("Printing orthogonal matrix below...")
print(O)
print("Printing result of matrix multiplication below...")
print(O.dot(O.T))
print("The resulting I.D. matrix implies that the orthogonality property holds")
#2
#problem 4 called "Using Numpy in Linear Algebra" so assuming numpy ok
A = np.zeros((100,100))
for i in range(100):
for j in range(100):
if i == j:
A[i][j] = 2
elif abs(i - j) == 1:
A[i][j] = -1
eigVals, eigVecs = np.linalg.eig(A)
minEigVal = eigVals[0]
for i in range(eigVals.shape[0]): #quick way to implement because numpy eig is ordered
if eigVals[i] < minEigVal:
minEigVal = eigVals[i]
minEigVec = eigVecs[i]
print("Smallest eigenvalue is %f:"%(minEigVal))
print("Plotting eigenvector corresponding to minimum eigenvalue...")
plt.plot(minEigVec)
plt.show()