Skip to content

Instantly share code, notes, and snippets.

@rlee287
Created December 9, 2018 00:54
Show Gist options
  • Save rlee287/280ed5b9c96be7639ed9905d13b4099f to your computer and use it in GitHub Desktop.
Save rlee287/280ed5b9c96be7639ed9905d13b4099f to your computer and use it in GitHub Desktop.
Gram Schmidt process on np.ndarray vectors
#DISCLAIMER: Gram-Schmidt is numerically unstable. Use np.linalg.qr if you actually need to calculate an orthonormal basis.
def proj(vec,u):
return np.dot(u,vec)/np.dot(u,u)*u
def gram_schmidt(*vectors):
retlist=list()
for vec in vectors:
vec_calc=np.copy(vec)
for exist_vec in retlist:
vec_calc=vec_calc-proj(vec,exist_vec)
retlist.append(vec_calc)
return retlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment