Given a set of linearly independent vectors, the Gram-Schmidt process produces a new set of vectors spanning the same subset, but the new set of vectors are now mutually orthogonal. The algorithm is usually stated as working over a vector space, but will in this post be altered to work over a *module* which is a vector space where the scalars are elements in a ring instead of a field. This could for instance be the case if we are given a set of integer vectors we wish to orthogonalise.

The original version can be described as follows: Given a set of vectors v_1, \ldots, v_n we define u_1 = v_1 and

u_k = v_k - \text{proj}_{u_1}(v_k) - \text{proj}_{u_2}(v_k) - \cdots - \text{proj}_{u_{k-1}}(v_k)

for k = 2,3,\ldots,n. The projection operator is defined as

\text{proj}_u(v) = \frac{\langle u, v \rangle}{\langle u, u \rangle} u.

If we say that all v_i have integer entries, we see that u_i must have rational entries, and simply scaling each vector with a multiple of all the denominators of the entries will give a vector parallel to the original vector but with integer entries. But what if we are interested in an algorithm that can only represent integers?

The algorithm is presented below. Note that the algorithm works over any module with inner product (aka a *Hilbert Module*).

```
# Given: Set of vectors V.
# Returns: A set of mutually orthogonal vectors spanning
# the same subspace as the vectors in V.
U := Ø
k := 1
while V ≠ Ø do
poll vector v from V
w := kv
for (u,m) in U do
w -= m〈v,u〉u
# Optional: Divide all entries in w by gcd of entries
if V ≠ Ø do
n :=〈w,w〉
for (u,m) in U do
m *= n
Put (w,k) in U
k *= n
else do
# No more iterations after this one
Put (w,k) in U
return first coordinates of elements in U
```

When used on integers, the entries in the vectors grow very fast, but this may be avoided by dividing w by the greatest common divisor of the entries.