Skip to content

Instantly share code, notes, and snippets.

@anmolkabra
anmolkabra / gram_schmidt.py
Last active September 30, 2023 01:54
Gram Schmidt Process to orthogonalize a matrix's columns using NumPy
#!/usr/bin/env python
import numpy as np
import numpy.linalg as la
def orthogonalize(U, eps=1e-15):
"""
Orthogonalizes the matrix U (d x n) using Gram-Schmidt Orthogonalization.
If the columns of U are linearly dependent with rank(U) = r, the last n-r columns
will be 0.