Tutorials › Linear Algebra › Gram-Schmidt in a Matrix Space

Orthogonal Sets and Gram-Schmidt · Tutorial 645 of 1000

Gram-Schmidt in a Matrix Space

Use the trace inner product to orthonormalize lists of matrices, with each projection, residual, and normalization verified.

Intermediate 10 min read

What You'll Learn

  • Define the trace inner product and matrix norm for real matrices of the same size
  • Compute projection coefficients using matrix inner products
  • Form and normalize Gram–Schmidt residuals in a matrix space
  • Check orthogonality and unit length by direct entrywise calculations
  • Identify the error caused by omitting a norm-squared denominator

Inner Products for Matrices

Gram–Schmidt is not restricted to vectors written as coordinate columns. Matrices of a fixed size can themselves be treated as vectors: their entries supply the coordinates, and an inner product measures their lengths and angles. This makes the method useful whenever the objects being compared are matrices, such as when building orthonormal lists in a matrix space.

Throughout, the scalar field is \(\mathbb{R}\). For fixed positive integers \(m\) and \(n\), let \(\mathbb{R}^{m\times n}\) be the vector space of \(m\times n\) real matrices. If \(\mathbf{A}=(a_{ij})\) and \(\mathbf{B}=(b_{ij})\) belong to this space, their trace inner product is the sum of the products of corresponding entries. Equivalently, it is the trace of \(\mathbf{A}^{\top}\mathbf{B}\), which is an \(n\times n\) square matrix.

Definition: For \(\mathbf{A},\mathbf{B}\in\mathbb{R}^{m\times n}\), define the trace inner product by \(\langle\mathbf{A},\mathbf{B}\rangle=\operatorname{tr}(\mathbf{A}^{\top}\mathbf{B})=\sum_{i=1}^{m}\sum_{j=1}^{n}a_{ij}b_{ij}\). The induced norm is \(\|\mathbf{A}\|=\sqrt{\langle\mathbf{A},\mathbf{A}\rangle}\), so \(\|\mathbf{A}\|^2=\sum_{i,j}a_{ij}^2\).

This is also called the Frobenius inner product. The entrywise formula is often the quickest way to calculate with it: pair entries in the same position, multiply, and add. In particular, no matrix multiplication is needed to find an inner product, even though the trace formula gives an equivalent definition.

Why this is an inner product. The trace formula has the required properties because its entrywise expression is an ordinary dot product of the \(mn\) entries. More explicitly, for real scalars \(c,d\) and matrices \(\mathbf{A},\mathbf{B},\mathbf{C}\) of the same size, summing entries gives \(\langle c\mathbf{A}+d\mathbf{B},\mathbf{C}\rangle=c\langle\mathbf{A},\mathbf{C}\rangle+d\langle\mathbf{B},\mathbf{C}\rangle\); the same calculation in the second slot gives linearity there. Also, \(\langle\mathbf{A},\mathbf{B}\rangle=\langle\mathbf{B},\mathbf{A}\rangle\), since real multiplication is commutative. Finally, \(\langle\mathbf{A},\mathbf{A}\rangle=\sum_{i,j}a_{ij}^2\geq0\), and this sum is zero exactly when every entry is zero, that is, exactly when \(\mathbf{A}=\mathbf{O}\).

Result: The trace inner product is a real inner product on \(\mathbb{R}^{m\times n}\), and its norm is the square root of the sum of the squares of all matrix entries.

Projections and Residuals in a Matrix Space

The first Gram–Schmidt step uses a matrix already known to be nonzero. If \(\mathbf{U}\ne\mathbf{O}\), the coefficient needed to remove from \(\mathbf{V}\) its component along \(\mathbf{U}\) is \(\langle\mathbf{V},\mathbf{U}\rangle/\langle\mathbf{U},\mathbf{U}\rangle\). The denominator matters when \(\mathbf{U}\) is not a unit matrix. If instead \(\mathbf{Q}\) has norm \(1\), the coefficient is simply \(\langle\mathbf{V},\mathbf{Q}\rangle\).

Result: If \(\mathbf{U}\ne\mathbf{O}\) and \(\mathbf{R}=\mathbf{V}-\dfrac{\langle\mathbf{V},\mathbf{U}\rangle}{\langle\mathbf{U},\mathbf{U}\rangle}\mathbf{U}\), then \(\langle\mathbf{R},\mathbf{U}\rangle=0\). Equivalently, if \(\mathbf{Q}=\mathbf{U}/\|\mathbf{U}\|\), then \(\mathbf{V}-\langle\mathbf{V},\mathbf{Q}\rangle\mathbf{Q}\) is orthogonal to \(\mathbf{Q}\).

Proof. Since \(\mathbf{U}\ne\mathbf{O}\), positive definiteness gives \(\langle\mathbf{U},\mathbf{U}\rangle\ne0\). By linearity in the first slot and symmetry of the real inner product,

$$ \langle\mathbf{R},\mathbf{U}\rangle =\langle\mathbf{V},\mathbf{U}\rangle -\frac{\langle\mathbf{V},\mathbf{U}\rangle}{\langle\mathbf{U},\mathbf{U}\rangle} \langle\mathbf{U},\mathbf{U}\rangle =0. $$

For the unit-matrix form, \(\langle\mathbf{Q},\mathbf{Q}\rangle=1\), so the coefficient in the first formula becomes \(\langle\mathbf{V},\mathbf{Q}\rangle/1\). This proves both statements. \(\square\)

For a list of matrices, the same idea is applied successively: after making the first matrix a unit matrix, subtract the components of the next matrix along the unit matrices already constructed, then normalize the new residual. The examples below calculate those components using the trace inner product, rather than relying on visual impressions of the matrices.

Two Matrices: One Projection and One Normalization

Worked Example: Orthonormalizing Two Matrices

Apply Gram–Schmidt to the following matrices in \(\mathbb{R}^{2\times2}\):

$$ \mathbf{A}_1=\begin{bmatrix}1&1\\0&1\end{bmatrix}, \qquad \mathbf{A}_2=\begin{bmatrix}1&0\\1&1\end{bmatrix}. $$

First calculate the squared norm of \(\mathbf{A}_1\) and its inner product with \(\mathbf{A}_2\):

$$ \langle\mathbf{A}_1,\mathbf{A}_1\rangle=1^2+1^2+0^2+1^2=3, \qquad \langle\mathbf{A}_2,\mathbf{A}_1\rangle=1(1)+0(1)+1(0)+1(1)=2. $$

Thus \(\mathbf{Q}_1=\mathbf{A}_1/\sqrt{3}\). The coefficient of the projection of \(\mathbf{A}_2\) onto \(\mathbf{Q}_1\) is \(\langle\mathbf{A}_2,\mathbf{Q}_1\rangle=2/\sqrt{3}\). Subtracting this multiple gives the residual:

$$ \mathbf{U}_2=\mathbf{A}_2-\frac{2}{\sqrt{3}}\mathbf{Q}_1 =\mathbf{A}_2-\frac{2}{3}\mathbf{A}_1 =\begin{bmatrix}\frac13&-\frac23\\1&\frac13\end{bmatrix}. $$

Its squared norm and norm are

$$ \|\mathbf{U}_2\|^2=\frac19+\frac49+1+\frac19=\frac{15}{9}=\frac53, \qquad \|\mathbf{U}_2\|=\sqrt{\frac53}. $$

Dividing by this norm produces the second unit matrix:

$$ \mathbf{Q}_2=\frac{\mathbf{U}_2}{\sqrt{5/3}} =\frac{1}{\sqrt{15}}\begin{bmatrix}1&-2\\3&1\end{bmatrix}. $$

Check. The squared norm of \(\mathbf{Q}_1\) is \(3/3=1\). The squared norm of \(\mathbf{Q}_2\) is \((1+4+9+1)/15=1\). Their inner product is \((1-2+0+1)/\sqrt{45}=0\). The input matrices are also recovered by \(\mathbf{A}_1=\sqrt{3}\mathbf{Q}_1\) and \(\mathbf{A}_2=(2/\sqrt{3})\mathbf{Q}_1+\sqrt{5/3}\mathbf{Q}_2\); substituting the definitions gives \((2/3)\mathbf{A}_1+\mathbf{U}_2=\mathbf{A}_2\), as required.

A Three-Matrix Calculation

Worked Example: Three Matrices in \(\mathbb{R}^{2\times2}\)

Orthonormalize the ordered list

$$ \mathbf{B}_1=\begin{bmatrix}1&0\\0&0\end{bmatrix}, \qquad \mathbf{B}_2=\begin{bmatrix}1&1\\0&0\end{bmatrix}, \qquad \mathbf{B}_3=\begin{bmatrix}1&0\\1&1\end{bmatrix}. $$

The first matrix already has norm \(1\), so \(\mathbf{Q}_1=\mathbf{B}_1\). For the second matrix, \(\langle\mathbf{B}_2,\mathbf{Q}_1\rangle=1\), hence

$$ \mathbf{U}_2=\mathbf{B}_2-\mathbf{Q}_1 =\begin{bmatrix}0&1\\0&0\end{bmatrix}, \qquad \|\mathbf{U}_2\|=1, \qquad \mathbf{Q}_2=\mathbf{U}_2. $$

For the third matrix, its coefficients along the two unit matrices are \(\langle\mathbf{B}_3,\mathbf{Q}_1\rangle=1\) and \(\langle\mathbf{B}_3,\mathbf{Q}_2\rangle=0\). Subtract both components:

$$ \mathbf{U}_3=\mathbf{B}_3-\mathbf{Q}_1-0\mathbf{Q}_2 =\begin{bmatrix}0&0\\1&1\end{bmatrix}. $$

Here \(\|\mathbf{U}_3\|^2=0^2+0^2+1^2+1^2=2\), so \(\mathbf{Q}_3=\mathbf{U}_3/\sqrt{2}\). Direct checks give \(\|\mathbf{Q}_1\|=\|\mathbf{Q}_2\|=\|\mathbf{Q}_3\|=1\), \(\langle\mathbf{Q}_1,\mathbf{Q}_2\rangle=0\), \(\langle\mathbf{Q}_1,\mathbf{Q}_3\rangle=0\), and \(\langle\mathbf{Q}_2,\mathbf{Q}_3\rangle=0\), since the nonzero entries of these matrices occupy different positions.

Check by reconstruction. The first two inputs satisfy \(\mathbf{B}_1=\mathbf{Q}_1\) and \(\mathbf{B}_2=\mathbf{Q}_1+\mathbf{Q}_2\). For the third, \(\mathbf{B}_3=\mathbf{Q}_1+\sqrt{2}\mathbf{Q}_3\). The coefficients match the projections and the norm of the residual, and each expression gives the displayed input matrix.

This example also shows why the order of the input list matters: each new residual is formed by removing components along the unit matrices already obtained. The third matrix’s projection onto \(\mathbf{Q}_2\) happens to be zero, but that coefficient is still calculated rather than assumed.

When More Than One Component Must Be Removed

Worked Example: Two Projections in a Nontrivial Residual

Let

$$ \mathbf{C}_1=\begin{bmatrix}1&1\\0&0\end{bmatrix}, \quad \mathbf{C}_2=\begin{bmatrix}1&0\\1&0\end{bmatrix}, \quad \mathbf{C}_3=\begin{bmatrix}1&1\\1&1\end{bmatrix}. $$

For the first matrix, \(\|\mathbf{C}_1\|^2=2\), so \(\mathbf{Q}_1=\mathbf{C}_1/\sqrt{2}\). The inner product \(\langle\mathbf{C}_2,\mathbf{Q}_1\rangle=1/\sqrt{2}\). Removing this component gives

$$ \mathbf{U}_2=\mathbf{C}_2-\frac{1}{\sqrt{2}}\mathbf{Q}_1 =\mathbf{C}_2-\frac12\mathbf{C}_1 =\begin{bmatrix}\frac12&-\frac12\\1&0\end{bmatrix}. $$

The squared norm is \(1/4+1/4+1=3/2\), so \(\|\mathbf{U}_2\|=\sqrt{3/2}\) and

$$ \mathbf{Q}_2=\frac{1}{\sqrt{6}}\begin{bmatrix}1&-1\\2&0\end{bmatrix}. $$

For \(\mathbf{C}_3\), calculate both projection coefficients separately:

$$ \langle\mathbf{C}_3,\mathbf{Q}_1\rangle=\frac{2}{\sqrt{2}}=\sqrt{2}, \qquad \langle\mathbf{C}_3,\mathbf{Q}_2\rangle=\frac{1-1+2+0}{\sqrt{6}}=\frac{2}{\sqrt{6}}. $$

Subtract the corresponding multiples. The second projection is \((2/\sqrt{6})\mathbf{Q}_2=(1/3)\begin{bmatrix}1&-1\\2&0\end{bmatrix}\), and the first is \(\sqrt{2}\mathbf{Q}_1=\mathbf{C}_1\). Therefore

$$ \mathbf{U}_3 =\mathbf{C}_3-\mathbf{C}_1-\frac13\begin{bmatrix}1&-1\\2&0\end{bmatrix} =\begin{bmatrix}-\frac13&\frac13\\\frac13&1\end{bmatrix}. $$

The squared norm is \(1/9+1/9+1/9+1=4/3\), so \(\|\mathbf{U}_3\|=2/\sqrt{3}\). Thus

$$ \mathbf{Q}_3=\frac{\mathbf{U}_3}{2/\sqrt{3}} =\frac{1}{2\sqrt{3}}\begin{bmatrix}-1&1\\1&3\end{bmatrix}. $$

Check. The numerators of \(\mathbf{Q}_1,\mathbf{Q}_2,\mathbf{Q}_3\), when written over their respective scalar denominators, have squared entry sums \(2\), \(6\), and \(12\); the denominators’ squares are \(2\), \(6\), and \(12\). Each norm is therefore \(1\). For orthogonality, the entrywise products of the numerators give \(1-1=0\) for \(\mathbf{Q}_1,\mathbf{Q}_2\), \(-1+1=0\) for \(\mathbf{Q}_1,\mathbf{Q}_3\), and \(-1-1+2=0\) for \(\mathbf{Q}_2,\mathbf{Q}_3\). Reconstruction gives \(\mathbf{C}_3=\sqrt{2}\mathbf{Q}_1+(2/\sqrt{6})\mathbf{Q}_2+(2/\sqrt{3})\mathbf{Q}_3\), which reproduces the all-ones matrix.

A Common Coefficient Error

A frequent mistake is to subtract the inner product with a non-unit matrix as though that matrix had norm \(1\). In the first worked example, \(\langle\mathbf{A}_2,\mathbf{A}_1\rangle=2\), but \(\|\mathbf{A}_1\|^2=3\). The incorrect calculation \(\mathbf{A}_2-2\mathbf{A}_1\) gives \(\begin{bmatrix}-1&-2\\1&-1\end{bmatrix}\). Its inner product with \(\mathbf{A}_1\) is \(-1-2+0-1=-4\), so this is not orthogonal to \(\mathbf{A}_1\). The correction is to divide by the squared norm: subtract \((2/3)\mathbf{A}_1\), yielding the residual \(\begin{bmatrix}1/3&-2/3\\1&1/3\end{bmatrix}\), whose inner product with \(\mathbf{A}_1\) is \(1/3-2/3+0+1/3=0\).

The distinction is simple to retain: with a non-unit matrix \(\mathbf{U}\), divide by \(\langle\mathbf{U},\mathbf{U}\rangle\); with a unit matrix \(\mathbf{Q}\), the projection coefficient is just \(\langle\mathbf{V},\mathbf{Q}\rangle\). After every subtraction, check orthogonality by an entrywise inner product, and after every normalization, check that the squared entries sum to \(1\).

Check Your Understanding

For each calculation, use corresponding-entry products to check the inner products and norms.

  1. Compute \(\langle\begin{bmatrix}1&2\\0&-1\end{bmatrix},\begin{bmatrix}2&0\\3&1\end{bmatrix}\rangle\) and the norm of the first matrix.
  2. For \(\mathbf{U}=\begin{bmatrix}1&1\\0&0\end{bmatrix}\) and \(\mathbf{V}=\begin{bmatrix}1&0\\1&0\end{bmatrix}\), find the residual after removing the component of \(\mathbf{V}\) along \(\mathbf{U}\).
  3. Normalize \(\begin{bmatrix}0&1\\1&0\end{bmatrix}\) in the trace inner product.
  4. Explain why the projection coefficient onto a non-unit matrix includes a norm-squared denominator.
  5. For the three matrices \(\mathbf{B}_1,\mathbf{B}_2,\mathbf{B}_3\) above, verify directly that \(\mathbf{Q}_2\) is orthogonal to \(\mathbf{Q}_3\).

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon