the Gram–Schmidt process is a method for orthonormalising a set of vectors in an inner product space, most commonly theEuclidean space R^{n}. The Gram–Schmidt process takes a finite, linearly independent set S = {v_{1}, …, v_{k}} for k ≤ n and generates an orthogonal set S′ = {u_{1}, …, u_{k}} that spans the same kdimensional subspace of R^{n} as S.
The method is named for Jørgen Pedersen Gram and Erhard Schmidt but it appeared earlier in the work of Laplace and Cauchy. In the theory of Lie group decompositions it is generalized by theIwasawa decomposition.
The application of the Gram–Schmidt process to the column vectors of a full column rank matrix yields the QR decomposition (it is decomposed into an orthogonal and a triangular matrix).
The Gram–Schmidt process
We define the projection operator by
where 〈u, v〉 denotes the inner product of the vectors u and v. This operator projects the vector v orthogonally onto the vector u.
The Gram–Schmidt process then works as follows:
The sequence u_{1}, …, u_{k} is the required system of orthogonal vectors, and the normalized vectors e_{1}, …, e_{k} form an orthonormal set. The calculation of the sequence u_{1}, …, u_{k} is known as Gram–Schmidt orthogonalization, while the calculation of the sequence e_{1}, …,e_{k} is known as Gram–Schmidt orthonormalization as the vectors are normalized.
To check that these formulas yield an orthogonal sequence, first compute 〈u_{1}, u_{2}〉 by substituting the above formula for u_{2}: we get zero. Then use this to compute 〈u_{1}, u_{3}〉 again by substituting the formula for u_{3}: we get zero. The general proof proceeds bymathematical induction.
Geometrically, this method proceeds as follows: to compute u_{i}, it projects v_{i} orthogonally onto the subspace U generated by u_{1}, …,u_{i−1}, which is the same as the subspace generated by v_{1}, …, v_{i−1}. The vector u_{i} is then defined to be the difference between v_{i} and this projection, guaranteed to be orthogonal to all of the vectors in the subspace U.
The Gram–Schmidt process also applies to a linearly independent infinite sequence {v_{i}}_{i}. The result is an orthogonal (or orthonormal) sequence {u_{i}}_{i} such that for natural number n: the algebraic span of v_{1}, …, v_{n} is the same as that of u_{1}, …, u_{n}.
If the Gram–Schmidt process is applied to a linearly dependent sequence, it outputs the 0 vector on the ith step, assuming that v_{i} is a linear combination of v_{1}, …, v_{i−1}. If an orthonormal basis is to be produced, then the algorithm should test for zero vectors in the output and discard them because no multiple of a zero vector can have a length of 1. The number of vectors output by the algorithm will then be the dimension of the space spanned by the original inputs.
Numerical stability
When this process is implemented on a computer, the vectors u_{k} are often not quite orthogonal, due to rounding errors. For the Gram–Schmidt process as described above (sometimes referred to as “classical Gram–Schmidt”) this loss of orthogonality is particularly bad; therefore, it is said that the (classical) Gram–Schmidt process is numerically unstable.
The Gram–Schmidt process can be stabilized by a small modification. Instead of computing the vector u_{k} as
it is computed as
Each step finds a vector orthogonal to . Thus is also orthogonalized against any errors introduced in computation of . This approach (sometimes referred to as “modified Gram–Schmidt”) gives the same result as the original formula in exact arithmetic and introduces smaller errors in finiteprecision arithmetic.
Algorithm
The following algorithm implements the stabilized Gram–Schmidt orthonormalization. The vectors v_{1}, …, v_{k} are replaced by orthonormal vectors which span the same subspace.

for j from 1 to k do

for i from 1 to j − 1 do
 (remove component in direction v_{i})
 next i
 (normalize)

for i from 1 to j − 1 do
 next j
The cost of this algorithm is asymptotically 2nk^{2} floating point operations, where n is the dimensionality of the vectors (Golub & Van Loan 1996, §5.2.8)