-
Notifications
You must be signed in to change notification settings - Fork 1
/
gramschmidt.py
30 lines (21 loc) · 912 Bytes
/
gramschmidt.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
def gramschmidt(U):
'''
W = gramschmidt(X)
Returns orthogonalized vectors in each column of W by using the
Gram-Schmidt method on the column vectors of the matrix X.
Input:
-- X: numpy array of floats with each column corresponding to a
vector.
Output:
-- W: numpy array of floats with each column corresponding to an
orthonalized vector.
'''
import numpy as np
V = np.zeros_like(U)
n = U.shape[1]
V = 1*U
for m in range(n):
for r in range(m):
V[:, [m]] = V[:, [m]] - (V[:, [r]].T@V[:, [m]])/(np.linalg.norm(V[:, [r]]))*V[:, [r]]
V[:,[m]] = V[:,[m]] / np.linalg.norm(V[:,m])
return V