-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgram-schmidt.py
More file actions
30 lines (20 loc) · 831 Bytes
/
gram-schmidt.py
File metadata and controls
30 lines (20 loc) · 831 Bytes
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
import numpy as np
#input as comma seperated values (CSV)
given = [ float(x) for x in input("1st basis vector: ").split(",") ]
vector_x1 = np.array(given)
given = [ float(x) for x in input("2nd basis vector: ").split(",") ]
vector_x2 = np.array(given)
given = [ float(x) for x in input("3nd basis vector: ").split(",") ]
vector_x3 = np.array(given)
print("Given:")
print(f"y = {vector_x1}, β1 = {vector_x2}, β2 = {vector_x3}")
# find orthogonal basis of vectors
v1 = vector_x1
v2 = vector_x2 - ((np.dot(vector_x2, v1) / np.dot(v1, v1))*v1)
v3 = vector_x3 - ((np.dot(vector_x3, v1) / np.dot(v1, v1))*v1) - ((np.dot(vector_x3, v2) / np.dot(v2, v2))*v2)
print(f"v1 = {v1}")
print(f"v2 = {v2}")
print(f"v3 = {v3}")
print(f"v1 = {np.linalg.norm(v1)}")
print(f"v2 = {np.linalg.norm(v2)}")
print(f"v3 = {np.linalg.norm(v3)}")