How to implement Gram Schmidt Orthogonalization on a matrix in RStan?

Vector and matrix operations are built into the language. For the Gram-Schmidt process you only need to compute the projections using product operations and subtract them from the original set of vectors (assuming they are linearly independent, as they must be).

For a general function that will work for a vector space of any size maybe there are a few details, like moving from two to three dimensions because you’d meed a matrix-vector product instead of vector dot-product, or maybe storing each of the projections becomes a bit fiddly, but you should be able to compute everything necessary with the available operations.

2 Likes