Jump to content

Gram–Schmidt process: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Algorithm: no need to divide U by norm U since U is divided by norm U in line 11
Bytbox (talk | contribs)
No edit summary
 
(48 intermediate revisions by 37 users not shown)
Line 1: Line 1:
{{Short description|Orthonormalization of a set of vectors}}
{{Short description|Orthonormalization of a set of vectors}}
[[File:Gram–Schmidt process.svg|right|frame|The first two steps of the Gram–Schmidt process]]
[[File:Gram–Schmidt process.svg|right|frame|The first two steps of the Gram–Schmidt process]]
In [[mathematics]], particularly [[linear algebra]] and [[numerical analysis]], the '''Gram–Schmidt process''' or Gram-Schmidt algorithm is a way of finding a set of two or more vectors that are perpendicular to each other.
In [[mathematics]], particularly [[linear algebra]] and [[numerical analysis]], the '''Gram–Schmidt process''' is a method for [[Orthonormal basis|orthonormalizing]] a set of [[vector (geometry)|vectors]] in an [[inner product space]], most commonly the [[Euclidean space]] {{math|'''R'''<sup>''n''</sup>}} equipped with the [[standard inner product]]. The Gram–Schmidt process takes a [[finite set|finite]], [[linearly independent]] set of vectors {{math|1=''S'' = {'''v'''<sub>1</sub>, ..., '''v'''<sub>''k''</sub>}<nowiki/>}} for {{math|''k'' ≤ ''n''}} and generates an [[orthogonal set]] {{math|1=''S′'' = {'''u'''<sub>1</sub>, ..., '''u'''<sub>''k''</sub>}<nowiki/>}} that spans the same ''k''-dimensional subspace of '''R'''<sup>''n''</sup> as ''S''.


By technical definition, it is a method of constructing an [[orthonormal basis]] from a set of [[vector (geometry)|vectors]] in an [[inner product space]], most commonly the [[Euclidean space]] <math>\mathbb{R}^n</math> equipped with the [[standard inner product]]. The Gram–Schmidt process takes a [[finite set|finite]], [[linearly independent]] set of vectors <math>S = \{ \mathbf{v}_1, \ldots , \mathbf{v}_k \}</math> for {{math|''k'' ≤ ''n''}} and generates an [[orthogonal set]] <math>S' = \{ \mathbf{u}_1 , \ldots , \mathbf{u}_k \}</math> that spans the same <math>k</math>-dimensional subspace of <math>\mathbb{R}^n</math> as <math>S</math>.
The method is named after [[Jørgen Pedersen Gram]] and [[Erhard Schmidt]], but [[Pierre-Simon Laplace]] had been familiar with it before Gram and Schmidt.<ref>{{cite book |last1=Cheney |first1=Ward |last2=Kincaid |first2=David |title=Linear Algebra: Theory and Applications |location=Sudbury, Ma |publisher=Jones and Bartlett |year=2009 |url={{Google books |plainurl=yes |id=Gg3Uj1GkHK8C |page=544 }} |isbn=978-0-7637-5020-6 |pages=544, 558 }}</ref> In the theory of [[Lie group decompositions]] it is generalized by the [[Iwasawa decomposition]].

The method is named after [[Jørgen Pedersen Gram]] and [[Erhard Schmidt]], but [[Pierre-Simon Laplace]] had been familiar with it before Gram and Schmidt.<ref>{{cite book |last1=Cheney |first1=Ward |last2=Kincaid |first2=David |title=Linear Algebra: Theory and Applications |location=Sudbury, Ma |publisher=Jones and Bartlett |year=2009 |url={{Google books |plainurl=yes |id=Gg3Uj1GkHK8C |page=544 }} |isbn=978-0-7637-5020-6 |pages=544, 558 }}</ref> In the theory of [[Lie group decompositions]], it is generalized by the [[Iwasawa decomposition]].


The application of the Gram–Schmidt process to the column vectors of a full column [[rank (linear algebra)|rank]] [[matrix (mathematics)|matrix]] yields the [[QR decomposition]] (it is decomposed into an [[orthogonal matrix|orthogonal]] and a [[triangular matrix]]).
The application of the Gram–Schmidt process to the column vectors of a full column [[rank (linear algebra)|rank]] [[matrix (mathematics)|matrix]] yields the [[QR decomposition]] (it is decomposed into an [[orthogonal matrix|orthogonal]] and a [[triangular matrix]]).


== The Gram–Schmidt process ==
== The Gram–Schmidt process ==
[[File:Gram-Schmidt orthonormalization process.gif|frame|The modified Gram-Schmidt process being executed on three linearly independent, non-orthogonal vectors of a basis for '''R'''<sup>3</sup>. Click on image for details. Modification is explained in the Numerical Stability section of this article.]]
[[File:Gram-Schmidt orthonormalization process.gif|thumb|400px|The modified Gram-Schmidt process being executed on three linearly independent, non-orthogonal vectors of a basis for <math>\mathbb{R}^3</math>. Click on image for details. Modification is explained in the Numerical Stability section of this article.]]

We define the [[projection (linear algebra)|projection]] [[operator (mathematics)|operator]] by
<math display="block">\operatorname{proj}_{\mathbf{u}} (\mathbf{v}) = \frac{\langle \mathbf{u}, \mathbf{v}\rangle}{\langle \mathbf{u}, \mathbf{u}\rangle}{\mathbf{u}} , </math>


where <math>\langle \mathbf{u}, \mathbf{v}\rangle</math> 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 <math>\operatorname{proj}_\mathbf{0} (\mathbf{v}) := \mathbf{0}</math>, i.e., the projection map <math>\operatorname{proj}_\mathbf{0}</math> is the zero map, sending every vector to the zero vector.
The [[vector projection]] of a vector <math>\mathbf v</math> on a nonzero vector <math>\mathbf u</math> is defined as<ref group="note">In the complex case, this assumes that the inner product is linear in the first argument and conjugate-linear in the second. In physics a more common convention is linearity in the second argument, in which case we define <math display="block">\operatorname{proj}_{\mathbf u} (\mathbf{v}) = \frac{\langle \mathbf{u}, \mathbf{v}\rangle}{\langle \mathbf{u}, \mathbf{u}\rangle} \,\mathbf{u}. </math></ref>
<math display="block">\operatorname{proj}_{\mathbf u} (\mathbf{v}) = \frac{\langle \mathbf{v}, \mathbf{u}\rangle}{\langle \mathbf{u}, \mathbf{u}\rangle} \,\mathbf{u} , </math>
where <math>\langle \mathbf{v}, \mathbf{u}\rangle</math> denotes the [[inner product]] of the vectors <math>\mathbf u</math> and <math>\mathbf v</math>. This means that <math>\operatorname{proj}_{\mathbf u} (\mathbf{v})</math> is the [[orthogonal projection]] of <math>\mathbf v</math> onto the line spanned by <math>\mathbf u</math>. If <math>\mathbf u</math> is the zero vector, then <math>\operatorname{proj}_{\mathbf u} (\mathbf{v})</math> is defined as the zero vector.


The Gram–Schmidt process then works as follows:
Given <math>k</math> vectors <math>\mathbf{v}_1, \ldots, \mathbf{v}_k</math> the Gram–Schmidt process defines the vectors <math>\mathbf{u}_1, \ldots, \mathbf{u}_k</math> as follows:
<math display="block">\begin{align}
<math display="block">\begin{align}
\mathbf{u}_1 & = \mathbf{v}_1, & \mathbf{e}_1 & = \frac{\mathbf{u}_1}{\|\mathbf{u}_1\|} \\
\mathbf{u}_1 & = \mathbf{v}_1, & \!\mathbf{e}_1 & = \frac{\mathbf{u}_1}{\|\mathbf{u}_1\|} \\
\mathbf{u}_2 & = \mathbf{v}_2-\operatorname{proj}_{\mathbf{u}_1} (\mathbf{v}_2),
\mathbf{u}_2 & = \mathbf{v}_2-\operatorname{proj}_{\mathbf{u}_1} (\mathbf{v}_2),
& \mathbf{e}_2 & = \frac{\mathbf{u}_2}{\|\mathbf{u}_2\|} \\
& \!\mathbf{e}_2 & = \frac{\mathbf{u}_2}{\|\mathbf{u}_2\|} \\
\mathbf{u}_3 & = \mathbf{v}_3-\operatorname{proj}_{\mathbf{u}_1} (\mathbf{v}_3) - \operatorname{proj}_{\mathbf{u}_2} (\mathbf{v}_3),
\mathbf{u}_3 & = \mathbf{v}_3-\operatorname{proj}_{\mathbf{u}_1} (\mathbf{v}_3) - \operatorname{proj}_{\mathbf{u}_2} (\mathbf{v}_3),
& \mathbf{e}_3 & = \frac{\mathbf{u}_3 }{\|\mathbf{u}_3\|} \\
& \!\mathbf{e}_3 & = \frac{\mathbf{u}_3 }{\|\mathbf{u}_3\|} \\
\mathbf{u}_4 & = \mathbf{v}_4-\operatorname{proj}_{\mathbf{u}_1} (\mathbf{v}_4)-\operatorname{proj}_{\mathbf{u}_2} (\mathbf{v}_4)-\operatorname{proj}_{\mathbf{u}_3} (\mathbf{v}_4),
\mathbf{u}_4 & = \mathbf{v}_4-\operatorname{proj}_{\mathbf{u}_1} (\mathbf{v}_4)-\operatorname{proj}_{\mathbf{u}_2} (\mathbf{v}_4)-\operatorname{proj}_{\mathbf{u}_3} (\mathbf{v}_4),
& \mathbf{e}_4 & = {\mathbf{u}_4 \over \|\mathbf{u}_4\|} \\
& \!\mathbf{e}_4 & = {\mathbf{u}_4 \over \|\mathbf{u}_4\|} \\
& {}\ \ \vdots & & {}\ \ \vdots \\
& {}\ \ \vdots & & {}\ \ \vdots \\
\mathbf{u}_k & = \mathbf{v}_k - \sum_{j=1}^{k-1}\operatorname{proj}_{\mathbf{u}_j} (\mathbf{v}_k),
\mathbf{u}_k & = \mathbf{v}_k - \sum_{j=1}^{k-1}\operatorname{proj}_{\mathbf{u}_j} (\mathbf{v}_k),
& \mathbf{e}_k & = \frac{\mathbf{u}_k}{\|\mathbf{u}_k\|}.
& \!\mathbf{e}_k & = \frac{\mathbf{u}_k}{\|\mathbf{u}_k\|}.
\end{align}</math>
\end{align}</math>


The sequence {{math|'''u'''<sub>1</sub>, ..., '''u'''<sub>''k''</sub>}} is the required system of orthogonal vectors, and the normalized vectors {{math|'''e'''<sub>1</sub>, ..., '''e'''<sub>''k''</sub>}} form an [[orthonormal|ortho''normal'']] set. The calculation of the sequence {{math|'''u'''<sub>1</sub>, ..., '''u'''<sub>''k''</sub>}} is known as ''Gram–Schmidt [[orthogonalization]]'', while the calculation of the sequence {{math|'''e'''<sub>1</sub>, ..., '''e'''<sub>''k''</sub>}} is known as ''Gram–Schmidt [[orthonormalization]]'' as the vectors are normalized.
The sequence <math>\mathbf{u}_1, \ldots, \mathbf{u}_k</math> is the required system of orthogonal vectors, and the normalized vectors <math>\mathbf{e}_1, \ldots, \mathbf{e}_k</math> form an [[orthonormal set]]. The calculation of the sequence <math>\mathbf{u}_1, \ldots, \mathbf{u}_k</math> is known as ''Gram–Schmidt [[orthogonalization]]'', and the calculation of the sequence <math>\mathbf{e}_1, \ldots, \mathbf{e}_k</math> is known as ''Gram–Schmidt [[orthonormalization]]''.


To check that these formulas yield an orthogonal sequence, first compute <math>\langle \mathbf{u}_1, \mathbf{u}_2 \rangle</math> by substituting the above formula for '''u'''<sub>2</sub>: we get zero. Then use this to compute <math>\langle \mathbf{u}_1, \mathbf{u}_3 \rangle</math> again by substituting the formula for '''u'''<sub>3</sub>: we get zero. The general proof proceeds by [[mathematical induction]].
To check that these formulas yield an orthogonal sequence, first compute <math>\langle \mathbf{u}_1, \mathbf{u}_2 \rangle</math> by substituting the above formula for <math>\mathbf{u}_2</math>: we get zero. Then use this to compute <math>\langle \mathbf{u}_1, \mathbf{u}_3 \rangle</math> again by substituting the formula for <math>\mathbf{u}_3</math>: we get zero. For arbitrary <math>k</math> the proof is accomplished by [[mathematical induction]].


Geometrically, this method proceeds as follows: to compute '''u'''<sub>''i''</sub>, it projects '''v'''<sub>''i''</sub> orthogonally onto the subspace ''U'' generated by {{math|'''u'''<sub>1</sub>, ..., '''u'''<sub>''i''−1</sub>}}, which is the same as the subspace generated by {{math|'''v'''<sub>1</sub>, ..., '''v'''<sub>''i''−1</sub>}}. The vector '''u'''<sub>''i''</sub> is then defined to be the difference between '''v'''<sub>''i''</sub> and this projection, guaranteed to be orthogonal to all of the vectors in the subspace ''U''.
Geometrically, this method proceeds as follows: to compute <math>\mathbf{u}_i</math>, it projects <math>\mathbf{v}_i</math> orthogonally onto the subspace <math>U</math> generated by <math>\mathbf{u}_1, \ldots, \mathbf{u}_{i-1}</math>, which is the same as the subspace generated by <math>\mathbf{v}_1, \ldots, \mathbf{v}_{i-1}</math>. The vector <math>\mathbf{u}_i</math> is then defined to be the difference between <math>\mathbf{v}_i</math> and this projection, guaranteed to be orthogonal to all of the vectors in the subspace <math>U</math>.


The Gram–Schmidt process also applies to a linearly independent [[countably infinite]] sequence {{math|{'''v'''<sub>''i''</sub>}<sub>''i''</sub>}}. The result is an orthogonal (or orthonormal) sequence {{math|{'''u'''<sub>''i''</sub>}<sub>''i''</sub>}} such that for natural number {{mvar|n}}:
The Gram–Schmidt process also applies to a linearly independent [[countably infinite]] sequence {{math|{'''v'''<sub>''i''</sub>}<sub>''i''</sub>}}. The result is an orthogonal (or orthonormal) sequence {{math|{'''u'''<sub>''i''</sub>}<sub>''i''</sub>}} such that for natural number {{mvar|n}}: the algebraic span of <math>\mathbf{v}_1, \ldots, \mathbf{v}_{n}</math> is the same as that of <math>\mathbf{u}_1, \ldots, \mathbf{u}_{n}</math>.
the algebraic span of {{math|'''v'''<sub>1</sub>, ..., '''v'''<sub>''n''</sub>}} is the same as that of {{math|'''u'''<sub>1</sub>, ..., '''u'''<sub>''n''</sub>}}.


If the Gram–Schmidt process is applied to a linearly dependent sequence, it outputs the {{math|'''0'''}} vector on the ''i''th step, assuming that {{math|'''v'''<sub>''i''</sub>}} is a linear combination of {{math|'''v'''<sub>1</sub>, ..., '''v'''<sub>''i''−1</sub>}}. 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.
If the Gram–Schmidt process is applied to a linearly dependent sequence, it outputs the {{math|'''0'''}} vector on the <math>i</math>th step, assuming that <math>\mathbf{v}_i</math> is a linear combination of <math>\mathbf{v}_1, \ldots, \mathbf{v}_{i-1}</math>. 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 <math>(v_\alpha)_{\alpha<\lambda}</math> yields a set of orthonormal vectors <math>(u_\alpha)_{\alpha<\kappa}</math> with <math>\kappa\leq\lambda</math> such that for any <math>\alpha\leq\lambda</math>, the [[Complete space#Completion|completion]] of the span of <math>\{ u_\beta : \beta<\min(\alpha,\kappa) \}</math> is the same as that of {{nowrap|<math>\{ v_\beta : \beta < \alpha\}</math>.}} 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 <math>\kappa < \lambda</math> holds, even if the starting set was linearly independent, and the span of <math>(u_\alpha)_{\alpha<\kappa}</math> need not be a subspace of the span of <math>(v_\alpha)_{\alpha<\lambda}</math> (rather, it's a subspace of its completion).
A variant of the Gram–Schmidt process using [[transfinite recursion]] applied to a (possibly uncountably) infinite sequence of vectors <math>(v_\alpha)_{\alpha<\lambda}</math> yields a set of orthonormal vectors <math>(u_\alpha)_{\alpha<\kappa}</math> with <math>\kappa\leq\lambda</math> such that for any <math>\alpha\leq\lambda</math>, the [[Complete space#Completion|completion]] of the span of <math>\{ u_\beta : \beta<\min(\alpha,\kappa) \}</math> is the same as that of {{nowrap|<math>\{ v_\beta : \beta < \alpha\}</math>.}} 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 <math>\kappa < \lambda</math> holds, even if the starting set was linearly independent, and the span of <math>(u_\alpha)_{\alpha<\kappa}</math> need not be a subspace of the span of <math>(v_\alpha)_{\alpha<\lambda}</math> (rather, it's a subspace of its completion).
Line 45: Line 45:


===Euclidean space===
===Euclidean space===
Consider the following set of vectors in {{math|'''R'''<sup>2</sup>}} (with the conventional [[Inner product space#Euclidean vector space|inner product]])
Consider the following set of vectors in <math>\mathbb{R}^2</math> (with the conventional [[Inner product space#Euclidean vector space|inner product]])
<math display="block">S = \left\{\mathbf{v}_1=\begin{bmatrix} 3 \\ 1\end{bmatrix}, \mathbf{v}_2=\begin{bmatrix}2 \\2\end{bmatrix}\right\}.</math>
<math display="block">S = \left\{\mathbf{v}_1=\begin{bmatrix} 3 \\ 1\end{bmatrix}, \mathbf{v}_2=\begin{bmatrix}2 \\2\end{bmatrix}\right\}.</math>


Line 55: Line 55:
= \begin{bmatrix} -2/5 \\6/5 \end{bmatrix}. </math>
= \begin{bmatrix} -2/5 \\6/5 \end{bmatrix}. </math>


We check that the vectors {{math|'''u'''<sub>1</sub>}} and {{math|'''u'''<sub>2</sub>}} are indeed orthogonal:
We check that the vectors <math>\mathbf{u}_1</math> and <math>\mathbf{u}_2</math> are indeed orthogonal:
<math display="block">\langle\mathbf{u}_1,\mathbf{u}_2\rangle = \left\langle \begin{bmatrix}3\\1\end{bmatrix}, \begin{bmatrix} -2/5 \\ 6/5 \end{bmatrix} \right\rangle = -\frac{6}{5} + \frac{6}{5} = 0,</math>
<math display="block">\langle\mathbf{u}_1,\mathbf{u}_2\rangle = \left\langle \begin{bmatrix}3\\1\end{bmatrix}, \begin{bmatrix} -2/5 \\ 6/5 \end{bmatrix} \right\rangle = -\frac{6}{5} + \frac{6}{5} = 0,</math>
noting that if the dot product of two vectors is 0 then they are orthogonal.
noting that if the [[dot product]] of two vectors is 0 then they are orthogonal.


For non-zero vectors, we can then normalize the vectors by dividing out their sizes as shown above:
For non-zero vectors, we can then normalize the vectors by dividing out their sizes as shown above:
Line 77: Line 77:
<math display="block"> \operatorname{GS}(g(\mathbf{v}_1),\dots,g(\mathbf{v}_k)) = \left( g(\operatorname{GS}(\mathbf{v}_1,\dots,\mathbf{v}_k)_1),\dots,g(\operatorname{GS}(\mathbf{v}_1,\dots,\mathbf{v}_k)_k) \right) </math>
<math display="block"> \operatorname{GS}(g(\mathbf{v}_1),\dots,g(\mathbf{v}_k)) = \left( g(\operatorname{GS}(\mathbf{v}_1,\dots,\mathbf{v}_k)_1),\dots,g(\operatorname{GS}(\mathbf{v}_1,\dots,\mathbf{v}_k)_k) \right) </math>


Further a parametrized version of the Gram–Schmidt process yields a (strong) [[Retraction (topology)#Deformation retract and strong deformation retract|deformation retraction]] of the general linear group <math> \mathrm{GL}(\R^n)</math> onto the orthogonal group <math> O(\R^n)</math>.
Further, a parametrized version of the Gram–Schmidt process yields a (strong) [[Retraction (topology)#Deformation retract and strong deformation retract|deformation retraction]] of the general linear group <math> \mathrm{GL}(\R^n)</math> onto the orthogonal group <math> O(\R^n)</math>.


== Numerical stability ==
== Numerical stability ==
Line 83: Line 83:


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.
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 {{math|'''u'''<sub>''k''</sub>}} as
Instead of computing the vector {{math|'''u'''<sub>''k''</sub>}} as
<math display="block"> \mathbf{u}_k = \mathbf{v}_k - \operatorname{proj}_{\mathbf{u}_1} (\mathbf{v}_k) - \operatorname{proj}_{\mathbf{u}_2} (\mathbf{v}_k) - \cdots - \operatorname{proj}_{\mathbf{u}_{k-1}} (\mathbf{v}_k), </math>
<math display="block"> \mathbf{u}_k = \mathbf{v}_k - \operatorname{proj}_{\mathbf{u}_1} (\mathbf{v}_k) - \operatorname{proj}_{\mathbf{u}_2} (\mathbf{v}_k) - \cdots - \operatorname{proj}_{\mathbf{u}_{k-1}} (\mathbf{v}_k), </math>
Line 95: Line 96:
\end{align} </math>
\end{align} </math>


This method is used in the previous animation, when the intermediate {{math|'''v'''<nowiki/>'<sub>3</sub>}} vector is used when orthogonalizing the blue vector {{math|'''v'''<sub>3</sub>}}.
This method is used in the previous animation, when the intermediate <math>\mathbf{v}'_3</math> vector is used when orthogonalizing the blue vector <math>\mathbf{v}_3</math>.


Here is another description of the modified algorithm. Given the vectors <math>v_1, v_2, \dots, v_n</math>, in our first step we produce vectors <math>v_1, v_2^{(1)}, \dots, v_n^{(1)}</math>by removing components along the direction of <math>v_1</math>. In formulas, <math>v_k^{(1)} := v_k - \frac{\langle v_k, v_1 \rangle}{\langle v_1, v_1 \rangle}v_1</math>. After this step we already have two of our desired orthogonal vectors <math>u_1, \dots, u_n</math>, namely <math>u_1 = v_1, u_2 = v_2^{(1)}</math>, but we also made <math>v_3^{(1)}, \dots, v_n^{(1)}</math> already orthogonal to <math>u_1</math>. Next, we orthogonalize those remaining vectors against <math>u_2 = v_2^{(1)}</math>. This means we compute <math>v_3^{(2)}, v_4^{(2)}, \dots, v_n^{(2)}</math> by subtraction <math>v_k^{(2)} := v_k^{(1)} - \frac{\langle v_k^{(1)}, u_2 \rangle}{\langle u_2, u_2 \rangle} u_2</math>. Now we have stored the vectors <math>v_1, v_2^{(1)}, v_3^{(2)}, v_4^{(2)}, \dots, v_n^{(2)}</math> where the first three vectors are already <math>u_1, u_2, u_3</math> and the remaining vectors are already orthogonal to <math>u_1, u_2</math>. As should be clear now, the next step orthogonalizes <math>v_4^{(2)}, \dots, v_n^{(2)}</math> against <math>u_3 = v_3^{(2)}</math>. Proceeding in this manner we find the full set of orthogonal vectors <math>u_1, \dots, u_n</math>. If orthonormal vectors are desired, then we normalize as we go, so that the denominators in the subtraction formulas turn into ones.
Here is another description of the modified algorithm. Given the vectors <math>\mathbf{v}_1, \mathbf{v}_2, \dots, \mathbf{v}_n</math>, in our first step we produce vectors <math>\mathbf{v}_1, \mathbf{v}_2^{(1)}, \dots, \mathbf{v}_n^{(1)}</math>by removing components along the direction of <math>\mathbf{v}_1</math>. In formulas, <math>\mathbf{v}_k^{(1)} := \mathbf{v}_k - \frac{\langle \mathbf{v}_k, \mathbf{v}_1 \rangle}{\langle \mathbf{v}_1, \mathbf{v}_1 \rangle} \mathbf{v}_1</math>. After this step we already have two of our desired orthogonal vectors <math>\mathbf{u}_1, \dots, \mathbf{u}_n</math>, namely <math>\mathbf{u}_1 = \mathbf{v}_1, \mathbf{u}_2 = \mathbf{v}_2^{(1)}</math>, but we also made <math>\mathbf{v}_3^{(1)}, \dots, \mathbf{v}_n^{(1)}</math> already orthogonal to <math>\mathbf{u}_1</math>. Next, we orthogonalize those remaining vectors against <math>\mathbf{u}_2 = \mathbf{v}_2^{(1)}</math>. This means we compute <math>\mathbf{v}_3^{(2)}, \mathbf{v}_4^{(2)}, \dots, \mathbf{v}_n^{(2)}</math> by subtraction <math>\mathbf{v}_k^{(2)} := \mathbf{v}_k^{(1)} - \frac{\langle \mathbf{v}_k^{(1)}, \mathbf{u}_2 \rangle}{\langle \mathbf{u}_2, \mathbf{u}_2 \rangle} \mathbf{u}_2</math>. Now we have stored the vectors <math>\mathbf{v}_1, \mathbf{v}_2^{(1)}, \mathbf{v}_3^{(2)}, \mathbf{v}_4^{(2)}, \dots, \mathbf{v}_n^{(2)}</math> where the first three vectors are already <math>\mathbf{u}_1, \mathbf{u}_2, \mathbf{u}_3</math> and the remaining vectors are already orthogonal to <math>\mathbf{u}_1, \mathbf{u}_2</math>. As should be clear now, the next step orthogonalizes <math>\mathbf{v}_4^{(2)}, \dots, \mathbf{v}_n^{(2)}</math> against <math>\mathbf{u}_3 = \mathbf{v}_3^{(2)}</math>. Proceeding in this manner we find the full set of orthogonal vectors <math>\mathbf{u}_1, \dots, \mathbf{u}_n</math>. If orthonormal vectors are desired, then we normalize as we go, so that the denominators in the subtraction formulas turn into ones.


== Algorithm ==
== Algorithm ==
The following MATLAB algorithm implements the Gram–Schmidt orthonormalization for Euclidean Vectors. The vectors {{math|'''v'''<sub>1</sub>, ..., '''v'''<sub>''k''</sub>}} (columns of matrix <code>V</code>, so that <code>V(:,j)</code> is the ''j''th vector) are replaced by orthonormal vectors (columns of <code>U</code>) which span the same subspace.
The following [[MATLAB]] algorithm implements classical Gram–Schmidt orthonormalization. The vectors {{math|'''v'''<sub>1</sub>, ..., '''v'''<sub>''k''</sub>}} (columns of matrix <code>V</code>, so that <code>V(:,j)</code> is the <math>j</math>th vector) are replaced by orthonormal vectors (columns of <code>U</code>) which span the same subspace.


<syntaxhighlight lang="matlab" line="1">
<syntaxhighlight lang="matlab" line="1">
function [U]=gramschmidt(V)
function U = gramschmidt(V)
[n,k] = size(V);
[n, k] = size(V);
U = zeros(n,k);
U = zeros(n,k);
U(:,1) = V(:,1)/norm(V(:,1));
U(:,1) = V(:,1) / norm(V(:,1));
for i = 2:k
for i = 2:k
U(:,i)=V(:,i);
U(:,i) = V(:,i);
for j=1:i-1
for j = 1:i-1
U(:,i)=U(:,i)-(U(:,j)'*U(:,i)) * U(:,j);
U(:,i) = U(:,i) - (U(:,j)'*U(:,i)) * U(:,j);
end
U(:,i) = U(:,i) / norm(U(:,i));
end
end
U(:,i) = U(:,i)/norm(U(:,i));
end
end
end
</syntaxhighlight>
</syntaxhighlight>


The cost of this algorithm is asymptotically {{math|O(''nk''<sup>2</sup>)}} floating point operations, where {{mvar|n}} is the dimensionality of the vectors {{harv|Golub|Van Loan|1996|loc=§5.2.8}}.
The cost of this algorithm is asymptotically {{math|O(''nk''<sup>2</sup>)}} floating point operations, where {{mvar|n}} is the dimensionality of the vectors.{{sfn|Golub|Van Loan|1996|loc=§5.2.8}}


== Via Gaussian elimination ==
== Via Gaussian elimination ==
Line 151: Line 152:
\end{vmatrix} </math>
\end{vmatrix} </math>


where ''D''<sub>0</sub>=1 and, for ''j'' 1, ''D<sub>j</sub>'' is the [[Gram determinant]]
where <math>D_0 = 1</math> and, for <math>j \ge 1</math>, <math>D_j</math> is the [[Gram determinant]]


<math display="block"> D_j = \begin{vmatrix}
<math display="block"> D_j = \begin{vmatrix}
Line 160: Line 161:
\end{vmatrix}. </math>
\end{vmatrix}. </math>


Note that the expression for '''u'''<sub>''k''</sub> is a "formal" determinant, i.e. the matrix contains both scalars
Note that the expression for <math>\mathbf{u}_k</math> 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 [[Laplace expansion|cofactor expansion]] along the row of vectors.
and vectors; the meaning of this expression is defined to be the result of a [[Laplace expansion|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.
The determinant formula for the Gram-Schmidt is computationally (exponentially) slower than the recursive algorithms described above; it is mainly of theoretical interest.

== Expressed using geometric algebra ==
Expressed using notation used in [[geometric algebra]], the unnormalized results of the Gram–Schmidt process can be expressed as
<math display="block">\mathbf{u}_k = \mathbf{v}_k - \sum_{j=1}^{k-1} (\mathbf{v}_k \cdot \mathbf{u}_j)\mathbf{u}_j^{-1}\ ,</math>
which is equivalent to the expression using the <math>\operatorname{proj}</math> operator defined above. The results can equivalently be expressed as<ref>{{cite book |last1=Doran |first1=Chris |last2=Lasenby |first2=Anthony |title=Geometric Algebra for Physicists |publisher=Cambridge University Press |year=2007 |isbn=978-0-521-71595-9 |page=124 }}</ref>
<math display="block">\mathbf{u}_k = \mathbf{v}_{k}\wedge\mathbf{v}_{k-1}\wedge\cdot\cdot\cdot\wedge\mathbf{v}_{1}(\mathbf{v}_{k-1}\wedge\cdot\cdot\cdot\wedge\mathbf{v}_{1})^{-1},</math>
which is closely related to the expression using determinants above.


== Alternatives ==
== Alternatives ==
Line 170: Line 177:
Yet another alternative is motivated by the use of [[Cholesky decomposition]] for [[Ordinary least squares|inverting the matrix of the normal equations in linear least squares]]. Let <math>V</math> be a [[full rank|full column rank]] matrix, whose columns need to be orthogonalized. The matrix <math>V^* V </math> is [[Hermitian matrix|Hermitian]] and [[Positive definite matrix|positive definite]], so it can be written as <math> V^* V = L L^*, </math> using the [[Cholesky decomposition]]. The lower triangular matrix <math>L </math> with strictly positive diagonal entries is [[invertible]]. Then columns of the matrix <math>U = V\left(L^{-1}\right)^*</math> are [[orthonormal]] and [[linear span|span]] the same subspace as the columns of the original matrix <math>V</math>. The explicit use of the product <math>V^* V </math> 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.
Yet another alternative is motivated by the use of [[Cholesky decomposition]] for [[Ordinary least squares|inverting the matrix of the normal equations in linear least squares]]. Let <math>V</math> be a [[full rank|full column rank]] matrix, whose columns need to be orthogonalized. The matrix <math>V^* V </math> is [[Hermitian matrix|Hermitian]] and [[Positive definite matrix|positive definite]], so it can be written as <math> V^* V = L L^*, </math> using the [[Cholesky decomposition]]. The lower triangular matrix <math>L </math> with strictly positive diagonal entries is [[invertible]]. Then columns of the matrix <math>U = V\left(L^{-1}\right)^*</math> are [[orthonormal]] and [[linear span|span]] the same subspace as the columns of the original matrix <math>V</math>. The explicit use of the product <math>V^* V </math> 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 certain applications than original Gram–Schmidt. Nevertheless, it remains a popular and effective algorithm for even the largest electronic structure calculations.<ref>{{cite journal|last1=Pursell|first1=Yukihiro| display-authors=etal|title=First-principles calculations of electron states of a silicon nanowire with 100,000 atoms on the K computer|journal=SC '11 Proceedings of 2011 International Conference for High Performance Computing, Networking, Storage and Analysis|date=2011|pages=1:1–1:11| doi=10.1145/2063384.2063386 | isbn=9781450307710|s2cid=14316074}}</ref>
In [[quantum mechanics]] there are several orthogonalization schemes with characteristics better suited for certain applications than original Gram–Schmidt. Nevertheless, it remains a popular and effective algorithm for even the largest electronic structure calculations.<ref>{{cite book|last1=Pursell|first1=Yukihiro|title=Proceedings of 2011 International Conference for High Performance Computing, Networking, Storage and Analysis |chapter=First-principles calculations of electron states of a silicon nanowire with 100,000 atoms on the K computer | display-authors=etal|date=2011|pages=1:1–1:11| doi=10.1145/2063384.2063386 | isbn=9781450307710|s2cid=14316074}}</ref>

== Run-time complexity ==
Gram-Schmidt orthogonalization can be done in [[strongly-polynomial time]]. The run-time analysis is similar to that of [[Gaussian elimination]].<ref name=":0">{{Cite Geometric Algorithms and Combinatorial Optimization}}</ref>{{Rp|page=40}}

==See also==
* [[Linear algebra]]
* [[Recursion]]
* [[Orthogonality (mathematics)]]


== References ==
== References ==
<references />
<references />
== Notes ==
<references group="note"/>


== Sources ==
== Sources ==
Line 179: Line 196:
* {{Citation | last1=Golub | first1=Gene H. | author1-link=Gene H. Golub | last2=Van Loan | first2=Charles F. | author2-link=Charles F. Van Loan | title=Matrix Computations | publisher=Johns Hopkins | edition=3rd | isbn=978-0-8018-5414-9 | year=1996}}.
* {{Citation | last1=Golub | first1=Gene H. | author1-link=Gene H. Golub | last2=Van Loan | first2=Charles F. | author2-link=Charles F. Van Loan | title=Matrix Computations | publisher=Johns Hopkins | edition=3rd | isbn=978-0-8018-5414-9 | year=1996}}.
* {{Citation | last1=Greub | first1=Werner | title=Linear Algebra | publisher = Springer | edition=4th |year = 1975}}.
* {{Citation | last1=Greub | first1=Werner | title=Linear Algebra | publisher = Springer | edition=4th |year = 1975}}.
* {{Citation | last1=Soliverez | first1=C. E. | last2=Gagliano | first2=E. | url=http://rmf.smf.mx/pdf/rmf/31/4/31_4_743.pdf| title=Orthonormalization on the plane: a geometric approach | journal=Mex. J. Phys. | volume=31 | number=4 | pages=743–758 | year=1985}}.
* {{Citation | last1=Soliverez | first1=C. E. | last2=Gagliano | first2=E. | url=http://rmf.smf.mx/pdf/rmf/31/4/31_4_743.pdf | title=Orthonormalization on the plane: a geometric approach | journal=Mex. J. Phys. | volume=31 | number=4 | pages=743–758 | year=1985 | access-date=2013-06-22 | archive-date=2014-03-07 | archive-url=https://web.archive.org/web/20140307095009/http://rmf.smf.mx/pdf/rmf/31/4/31_4_743.pdf | url-status=dead }}.


== External links ==
== External links ==

Latest revision as of 03:57, 5 September 2024

The first two steps of the Gram–Schmidt process

In mathematics, particularly linear algebra and numerical analysis, the Gram–Schmidt process or Gram-Schmidt algorithm is a way of finding a set of two or more vectors that are perpendicular to each other.

By technical definition, it is a method of constructing an orthonormal basis from a set of vectors in an inner product space, most commonly the Euclidean space equipped with the standard inner product. The Gram–Schmidt process takes a finite, linearly independent set of vectors for kn and generates an orthogonal set that spans the same -dimensional subspace of as .

The method is named after Jørgen Pedersen Gram and Erhard Schmidt, but Pierre-Simon Laplace had been familiar with it before Gram and Schmidt.[1] In the theory of Lie group decompositions, it is generalized by the Iwasawa 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

[edit]
The modified Gram-Schmidt process being executed on three linearly independent, non-orthogonal vectors of a basis for . Click on image for details. Modification is explained in the Numerical Stability section of this article.

The vector projection of a vector on a nonzero vector is defined as[note 1] where denotes the inner product of the vectors and . This means that is the orthogonal projection of onto the line spanned by . If is the zero vector, then is defined as the zero vector.

Given vectors the Gram–Schmidt process defines the vectors as follows:

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

To check that these formulas yield an orthogonal sequence, first compute by substituting the above formula for : we get zero. Then use this to compute again by substituting the formula for : we get zero. For arbitrary the proof is accomplished by mathematical induction.

Geometrically, this method proceeds as follows: to compute , it projects orthogonally onto the subspace generated by , which is the same as the subspace generated by . The vector is then defined to be the difference between and this projection, guaranteed to be orthogonal to all of the vectors in the subspace .

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 is the same as that of .

If the Gram–Schmidt process is applied to a linearly dependent sequence, it outputs the 0 vector on the th step, assuming that is a linear combination of . 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 yields a set of orthonormal vectors with such that for any , the completion of the span of is the same as that of . 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 holds, even if the starting set was linearly independent, and the span of need not be a subspace of the span of (rather, it's a subspace of its completion).

Example

[edit]

Euclidean space

[edit]

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

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

We check that the vectors and are indeed orthogonal: noting that if the dot product of two vectors is 0 then they are orthogonal.

For non-zero vectors, we can then normalize the vectors by dividing out their sizes as shown above:

Properties

[edit]

Denote by the result of applying the Gram–Schmidt process to a collection of vectors . This yields a map .

It has the following properties:

  • It is continuous
  • It is orientation preserving in the sense that .
  • It commutes with orthogonal maps:

Let be orthogonal (with respect to the given inner product). Then we have

Further, a parametrized version of the Gram–Schmidt process yields a (strong) deformation retraction of the general linear group onto the orthogonal group .

Numerical stability

[edit]

When this process is implemented on a computer, the vectors 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 it is computed as

This method is used in the previous animation, when the intermediate vector is used when orthogonalizing the blue vector .

Here is another description of the modified algorithm. Given the vectors , in our first step we produce vectors by removing components along the direction of . In formulas, . After this step we already have two of our desired orthogonal vectors , namely , but we also made already orthogonal to . Next, we orthogonalize those remaining vectors against . This means we compute by subtraction . Now we have stored the vectors where the first three vectors are already and the remaining vectors are already orthogonal to . As should be clear now, the next step orthogonalizes against . Proceeding in this manner we find the full set of orthogonal vectors . If orthonormal vectors are desired, then we normalize as we go, so that the denominators in the subtraction formulas turn into ones.

Algorithm

[edit]

The following MATLAB algorithm implements classical Gram–Schmidt orthonormalization. The vectors v1, ..., vk (columns of matrix V, so that V(:,j) is the th vector) are replaced by orthonormal vectors (columns of U) which span the same subspace.

function U = gramschmidt(V)
    [n, k] = size(V);
    U = zeros(n,k);
    U(:,1) = V(:,1) / norm(V(:,1));
    for i = 2:k
        U(:,i) = V(:,i);
        for j = 1:i-1
            U(:,i) = U(:,i) - (U(:,j)'*U(:,i)) * U(:,j);
        end
        U(:,i) = U(:,i) / norm(U(:,i));
    end
end

The cost of this algorithm is asymptotically O(nk2) floating point operations, where n is the dimensionality of the vectors.[2]

Via Gaussian elimination

[edit]

If the rows {v1, ..., vk} are written as a matrix , then applying Gaussian elimination to the augmented matrix will produce the orthogonalized vectors in place of . However the matrix must be brought to row echelon form, using only the row operation of adding a scalar multiple of one row to another.[3] For example, taking as above, we have

And reducing this to row echelon form produces

The normalized vectors are then as in the example above.

Determinant formula

[edit]

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

where and, for , is the Gram determinant

Note that the expression for 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 (exponentially) slower than the recursive algorithms described above; it is mainly of theoretical interest.

Expressed using geometric algebra

[edit]

Expressed using notation used in geometric algebra, the unnormalized results of the Gram–Schmidt process can be expressed as which is equivalent to the expression using the operator defined above. The results can equivalently be expressed as[4] which is closely related to the expression using determinants above.

Alternatives

[edit]

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 th orthogonalized vector after the 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 be a full column rank matrix, whose columns need to be orthogonalized. The matrix is Hermitian and positive definite, so it can be written as using the Cholesky decomposition. The lower triangular matrix with strictly positive diagonal entries is invertible. Then columns of the matrix are orthonormal and span the same subspace as the columns of the original matrix . The explicit use of the product 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 certain applications than original Gram–Schmidt. Nevertheless, it remains a popular and effective algorithm for even the largest electronic structure calculations.[5]

Run-time complexity

[edit]

Gram-Schmidt orthogonalization can be done in strongly-polynomial time. The run-time analysis is similar to that of Gaussian elimination.[6]: 40 

See also

[edit]

References

[edit]
  1. ^ Cheney, Ward; Kincaid, David (2009). Linear Algebra: Theory and Applications. Sudbury, Ma: Jones and Bartlett. pp. 544, 558. ISBN 978-0-7637-5020-6.
  2. ^ Golub & Van Loan 1996, §5.2.8.
  3. ^ Pursell, Lyle; Trimble, S. Y. (1 January 1991). "Gram-Schmidt Orthogonalization by Gauss Elimination". The American Mathematical Monthly. 98 (6): 544–549. doi:10.2307/2324877. JSTOR 2324877.
  4. ^ Doran, Chris; Lasenby, Anthony (2007). Geometric Algebra for Physicists. Cambridge University Press. p. 124. ISBN 978-0-521-71595-9.
  5. ^ Pursell, Yukihiro; et al. (2011). "First-principles calculations of electron states of a silicon nanowire with 100,000 atoms on the K computer". Proceedings of 2011 International Conference for High Performance Computing, Networking, Storage and Analysis. pp. 1:1–1:11. doi:10.1145/2063384.2063386. ISBN 9781450307710. S2CID 14316074.
  6. ^ Grötschel, Martin; Lovász, László; Schrijver, Alexander (1993), Geometric algorithms and combinatorial optimization, Algorithms and Combinatorics, vol. 2 (2nd ed.), Springer-Verlag, Berlin, doi:10.1007/978-3-642-78240-4, ISBN 978-3-642-78242-8, MR 1261419

Notes

[edit]
  1. ^ In the complex case, this assumes that the inner product is linear in the first argument and conjugate-linear in the second. In physics a more common convention is linearity in the second argument, in which case we define

Sources

[edit]
[edit]