# Gram–Schmidt process

The first two steps of the Gram–Schmidt process

In mathematics, particularly linear algebra and numerical analysis, the Gram–Schmidt process is a method for orthonormalising a set of vectors in an inner product space, most commonly the Euclidean space Rn. The Gram–Schmidt process takes a finite, linearly independent set S = {v1, ..., vk} for kn and generates an orthogonal set S′ = {u1, ..., uk} that spans the same k-dimensional subspace of Rn as S.

The method is named after 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 the Iwasawa decomposition.[1]

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

The Gram-Schmidt process being executed on three linearly independent, non-orthogonal vectors of a basis for R3. Click on image for details.

We define the projection operator by

${\displaystyle {\mathrm {proj} }_{\mathbf {u} }\,({\mathbf {v} })={\langle {\mathbf {u} },{\mathbf {v} }\rangle \over \langle {\mathbf {u} },{\mathbf {u} }\rangle }{\mathbf {u} },}$

where ${\displaystyle \langle {\mathbf {u} },{\mathbf {v} }\rangle }$ denotes the inner product of the vectors u and v. This operator projects the vector v orthogonally onto the line spanned by vector u. If u=0, we define ${\displaystyle {\mathrm {proj} }_{0}\,({\mathbf {v} }):=0}$. i.e., the projection map ${\displaystyle {\mathrm {proj} }_{0}}$ is the zero map, sending every vector to the zero vector.

The Gram–Schmidt process then works as follows:

{\displaystyle {\begin{aligned}{\mathbf {u} }_{1}&={\mathbf {v} }_{1},&{\mathbf {e} }_{1}&={{\mathbf {u} }_{1} \over \|{\mathbf {u} }_{1}\|}\\{\mathbf {u} }_{2}&={\mathbf {v} }_{2}-{\mathrm {proj} }_{{\mathbf {u} }_{1}}\,({\mathbf {v} }_{2}),&{\mathbf {e} }_{2}&={{\mathbf {u} }_{2} \over \|{\mathbf {u} }_{2}\|}\\{\mathbf {u} }_{3}&={\mathbf {v} }_{3}-{\mathrm {proj} }_{{\mathbf {u} }_{1}}\,({\mathbf {v} }_{3})-{\mathrm {proj} }_{{\mathbf {u} }_{2}}\,({\mathbf {v} }_{3}),&{\mathbf {e} }_{3}&={{\mathbf {u} }_{3} \over \|{\mathbf {u} }_{3}\|}\\{\mathbf {u} }_{4}&={\mathbf {v} }_{4}-{\mathrm {proj} }_{{\mathbf {u} }_{1}}\,({\mathbf {v} }_{4})-{\mathrm {proj} }_{{\mathbf {u} }_{2}}\,({\mathbf {v} }_{4})-{\mathrm {proj} }_{{\mathbf {u} }_{3}}\,({\mathbf {v} }_{4}),&{\mathbf {e} }_{4}&={{\mathbf {u} }_{4} \over \|{\mathbf {u} }_{4}\|}\\&{}\ \ \vdots &&{}\ \ \vdots \\{\mathbf {u} }_{k}&={\mathbf {v} }_{k}-\sum _{j=1}^{k-1}{\mathrm {proj} }_{{\mathbf {u} }_{j}}\,({\mathbf {v} }_{k}),&{\mathbf {e} }_{k}&={{\mathbf {u} }_{k} \over \|{\mathbf {u} }_{k}\|}.\end{aligned}}}

The sequence u1, ..., uk is the required system of orthogonal vectors, and the normalized vectors e1, ..., ek form an orthonormal set. The calculation of the sequence u1, ..., uk is known as Gram–Schmidt orthogonalization, while the calculation of the sequence e1, ..., ek is known as Gram–Schmidt orthonormalization as the vectors are normalized.

To check that these formulas yield an orthogonal sequence, first compute ‹ u1,u2 › by substituting the above formula for u2: we get zero. Then use this to compute ‹ u1,u3 › again by substituting the formula for u3: we get zero. The general proof proceeds by mathematical induction.

Geometrically, this method proceeds as follows: to compute ui, it projects vi orthogonally onto the subspace U generated by u1, ..., ui−1, which is the same as the subspace generated by v1, ..., vi−1. The vector ui is then defined to be the difference between vi 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 countably infinite sequence {vi}i. The result is an orthogonal (or orthonormal) sequence {ui}i such that for natural number n: the algebraic span of v1, ..., vn is the same as that of u1, ..., un.

If the Gram–Schmidt process is applied to a linearly dependent sequence, it outputs the 0 vector on the ith step, assuming that vi is a linear combination of v1, ..., vi−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.

A variant of the Gram–Schmidt process using transfinite recursion applied to a (possibly uncountably) infinite sequence of vectors ${\displaystyle (v_{\alpha })_{\alpha <\lambda }}$ yields a set of orthonormal vectors ${\displaystyle (u_{\alpha })_{\alpha <\kappa }}$ with ${\displaystyle \kappa \leq \lambda }$ such that for any ${\displaystyle \alpha \leq \lambda }$, the completion of the span of ${\displaystyle \lbrace u_{\beta }:\beta <\min(\alpha ,\kappa )\rbrace }$ is the same as that of ${\displaystyle \lbrace v_{\beta }:\beta <\alpha \rbrace }$. In particular, when applied to a (algebraic) basis of a Hilbert space (or, more generally, a basis of any dense subspace), it yields a (functional-analytic) orthonormal basis. Note that in the general case often the strict inequality ${\displaystyle \kappa <\lambda }$ holds, even if the starting set was linearly independent, and the span of ${\displaystyle (u_{\alpha })_{\alpha <\kappa }}$ need not be a subspace of the span of ${\displaystyle (v_{\alpha })_{\alpha <\lambda }}$ (rather, it's a subspace of its completion).

## Example

Consider the following set of vectors in R2 (with the conventional inner product)

${\displaystyle S=\left\lbrace {\mathbf {v} }_{1}={\begin{pmatrix}3\\1\end{pmatrix}},{\mathbf {v} }_{2}={\begin{pmatrix}2\\2\end{pmatrix}}\right\rbrace .}$

Now, perform Gram–Schmidt, to obtain an orthogonal set of vectors:

${\displaystyle \mathbf {u} _{1}=\mathbf {v} _{1}={\begin{pmatrix}3\\1\end{pmatrix}}}$
${\displaystyle \mathbf {u} _{2}=\mathbf {v} _{2}-\mathrm {proj} _{\mathbf {u} _{1}}\,(\mathbf {v} _{2})={\begin{pmatrix}2\\2\end{pmatrix}}-\mathrm {proj} _{({3 \atop 1})}\,({{\begin{pmatrix}2\\2\end{pmatrix}})}={\begin{pmatrix}-2/5\\6/5\end{pmatrix}}.}$

We check that the vectors u1 and u2 are indeed orthogonal:

${\displaystyle \langle {\mathbf {u} }_{1},{\mathbf {u} }_{2}\rangle =\left\langle {\begin{pmatrix}3\\1\end{pmatrix}},{\begin{pmatrix}-2/5\\6/5\end{pmatrix}}\right\rangle =-{\frac {6}{5}}+{\frac {6}{5}}=0,}$

noting that if the dot product of two vectors is 0 then they are orthogonal.

We can then normalize the vectors by dividing out their sizes as shown above:

${\displaystyle {\mathbf {e} }_{1}={1 \over {\sqrt {10}}}{\begin{pmatrix}3\\1\end{pmatrix}}}$
${\displaystyle {\mathbf {e} }_{2}={1 \over {\sqrt {40 \over 25}}}{\begin{pmatrix}-2/5\\6/5\end{pmatrix}}={1 \over {\sqrt {10}}}{\begin{pmatrix}-1\\3\end{pmatrix}}.}$

## Numerical stability

When this process is implemented on a computer, the vectors ${\displaystyle {\mathbf {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; this version is sometimes referred to as modified Gram-Schmidt or MGS. This approach gives the same result as the original formula in exact arithmetic and introduces smaller errors in finite-precision arithmetic. Instead of computing the vector uk as

${\displaystyle {\mathbf {u} }_{k}={\mathbf {v} }_{k}-{\mathrm {proj} }_{{\mathbf {u} }_{1}}\,({\mathbf {v} }_{k})-{\mathrm {proj} }_{{\mathbf {u} }_{2}}\,({\mathbf {v} }_{k})-\cdots -{\mathrm {proj} }_{{\mathbf {u} }_{k-1}}\,({\mathbf {v} }_{k}),}$

it is computed as

{\displaystyle {\begin{aligned}\mathbf {u} _{k}^{(1)}&=\mathbf {v} _{k}-\mathrm {proj} _{\mathbf {u} _{1}}\,(\mathbf {v} _{k}),\\\mathbf {u} _{k}^{(2)}&=\mathbf {u} _{k}^{(1)}-\mathrm {proj} _{\mathbf {u} _{2}}\,(\mathbf {u} _{k}^{(1)}),\\&\,\,\,\vdots \\\mathbf {u} _{k}^{(k-2)}&=\mathbf {u} _{k}^{(k-3)}-\mathrm {proj} _{\mathbf {u} _{k-2}}\,(\mathbf {u} _{k}^{(k-3)}),\\\mathbf {u} _{k}^{(k-1)}&=\mathbf {u} _{k}^{(k-2)}-\mathrm {proj} _{\mathbf {u} _{k-1}}\,(\mathbf {u} _{k}^{(k-2)}).\end{aligned}}}

Each step finds a vector ${\displaystyle {\mathbf {u} }_{k}^{(i)}}$ orthogonal to ${\displaystyle {\mathbf {u} }_{k}^{(i-1)}}$. Thus ${\displaystyle {\mathbf {u} }_{k}^{(i)}}$ is also orthogonalized against any errors introduced in computation of ${\displaystyle {\mathbf {u} }_{k}^{(i-1)}}$.

This method is used in the previous animation, when the intermediate v'3 vector is used when orthogonalizing the blue vector v3.

## Algorithm

The following algorithm implements the stabilized Gram–Schmidt orthonormalization. The vectors v1, ..., vk are replaced by orthonormal vectors which span the same subspace.

for i from 1 to k do
${\displaystyle {\mathbf {v} }_{i}\leftarrow {\frac {{\mathbf {v} }_{i}}{\|{\mathbf {v} }_{i}\|}}}$ (normalize)
for j from i+1 to k do
${\displaystyle {\mathbf {v} }_{j}\leftarrow {\mathbf {v} }_{j}-{\mathrm {proj} }_{{\mathbf {v} }_{i}}\,({\mathbf {v} }_{j})}$ (remove component in direction vi)
next j
next i

The cost of this algorithm is asymptotically 2nk2 floating point operations, where n is the dimensionality of the vectors Template:Harv.

## Determinant formula

The result of the Gram–Schmidt process may be expressed in a non-recursive formula using determinants.

${\displaystyle {\mathbf {e} }_{j}={\frac {1}{\sqrt {D_{j-1}D_{j}}}}{\begin{vmatrix}\langle {\mathbf {v} }_{1},{\mathbf {v} }_{1}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{1}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{1}\rangle \\\langle {\mathbf {v} }_{1},{\mathbf {v} }_{2}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{2}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{2}\rangle \\\vdots &\vdots &\ddots &\vdots \\\langle {\mathbf {v} }_{1},{\mathbf {v} }_{j-1}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{j-1}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{j-1}\rangle \\{\mathbf {v} }_{1}&{\mathbf {v} }_{2}&\dots &{\mathbf {v} }_{j}\end{vmatrix}}}$
${\displaystyle {\mathbf {u} }_{j}={\frac {1}{D_{j-1}}}{\begin{vmatrix}\langle {\mathbf {v} }_{1},{\mathbf {v} }_{1}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{1}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{1}\rangle \\\langle {\mathbf {v} }_{1},{\mathbf {v} }_{2}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{2}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{2}\rangle \\\vdots &\vdots &\ddots &\vdots \\\langle {\mathbf {v} }_{1},{\mathbf {v} }_{j-1}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{j-1}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{j-1}\rangle \\{\mathbf {v} }_{1}&{\mathbf {v} }_{2}&\dots &{\mathbf {v} }_{j}\end{vmatrix}}}$

where D 0=1 and, for j ≥ 1, D j is the Gram determinant

${\displaystyle D_{j}={\begin{vmatrix}\langle {\mathbf {v} }_{1},{\mathbf {v} }_{1}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{1}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{1}\rangle \\\langle {\mathbf {v} }_{1},{\mathbf {v} }_{2}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{2}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{2}\rangle \\\vdots &\vdots &\ddots &\vdots \\\langle {\mathbf {v} }_{1},{\mathbf {v} }_{j}\rangle &\langle {\mathbf {v} }_{2},{\mathbf {v} }_{j}\rangle &\dots &\langle {\mathbf {v} }_{j},{\mathbf {v} }_{j}\rangle \end{vmatrix}}.}$

Note that the expression for uk is a "formal" determinant, i.e. the matrix contains both scalars and vectors; the meaning of this expression is defined to be the result of a cofactor expansion along the row of vectors.

The determinant formula for the Gram-Schmidt is computationally slower (exponentially slower) than the recursive algorithms described above; it is mainly of theoretical interest.

## Alternatives

Other orthogonalization algorithms use Householder transformations or Givens rotations. The algorithms using Householder transformations are more stable than the stabilized Gram–Schmidt process. On the other hand, the Gram–Schmidt process produces the ${\displaystyle j}$th orthogonalized vector after the ${\displaystyle j}$th iteration, while orthogonalization using Householder reflections produces all the vectors only at the end. This makes only the Gram–Schmidt process applicable for iterative methods like the Arnoldi iteration.

Yet another alternative is motivated by the use of Cholesky decomposition for inverting the matrix of the normal equations in linear least squares. Let ${\displaystyle {\mathbf {V} }}$ be a full column rank matrix, which columns need to be orthogonalized. The matrix ${\displaystyle {\mathbf {V} }^{*}{\mathbf {V} }}$ is Hermitian and positive definite, so it can be written as ${\displaystyle {\mathbf {V} }^{*}{\mathbf {V} }={\mathbf {L} }{\mathbf {L} }^{*},}$ using the Cholesky decomposition. The lower triangular matrix ${\displaystyle {\mathbf {L} }}$ with strictly positive diagonal entries is invertible. Then columns of the matrix ${\displaystyle {\mathbf {U} }={\mathbf {V} }({\mathbf {L} }^{-1})^{*}}$ are orthonormal and span the same subspace as the columns of the original matrix ${\displaystyle {\mathbf {V} }}$. The explicit use of the product ${\displaystyle {\mathbf {V} }^{*}{\mathbf {V} }}$ makes the algorithm unstable, especially if the product's condition number is large. Nevertheless, this algorithm is used in practice and implemented in some software packages because of its high efficiency and simplicity.

In quantum mechanics there are several orthogonalization schemes with characteristics better suited for applications than the Gram–Schmidt one. The most important among them are the symmetric and the canonical orthonormalization (see Solivérez & Gagliano).Template:Clarify

## References

1. Cheney, Ward; Kincaid, David: Linear Algebra: Theory and Applications. Sudbury, Ma: 2009.Pg. 544, 558.
• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.