Skip to content

Instantly share code, notes, and snippets.

@codeboy101
Created September 6, 2017 15:41
Show Gist options
  • Save codeboy101/f345e1392c57fdf05d3794a576f54aba to your computer and use it in GitHub Desktop.
Save codeboy101/f345e1392c57fdf05d3794a576f54aba to your computer and use it in GitHub Desktop.
import numpy as np
def gram_schmidt(vectors):
q1 = vectors[0]/np.linalg.norm(vectors[0])
result = [q1]
for b in vectors[1:]:
comp = b + rm_dupl_projctns(b, result)
result.append(comp/np.linalg.norm(comp))
return np.concatenate(result, axis=1)
def rm_dupl_projctns(b, result):
total = 0
for x in result:
total -= np.matmul(x.T, b) * x
return total
def get_projection(a, b):
return np.matmul(a.T, b) * a
a = np.array([1,2,2]).reshape(3,1)
b = np.array([1,3,1]).reshape(3,1)
#c = np.array([0,1,1]).reshape(3,1)
vectors = [a, b]
x = gram_schmidt(vectors)
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment