Tutorials › Linear Algebra › Computing An SVD From The Gram Matrix

Singular Value Decomposition · Tutorial 787 of 1000

Computing An SVD From The Gram Matrix

Use the Gram matrix to build and check the singular values and orthonormal vector factors of a real matrix.

Advanced 10 min read

What You'll Learn

  • Form the Gram matrix and use its orthonormal eigenvectors to determine right singular vectors.
  • Convert the nonnegative Gram eigenvalues into singular values by taking square roots.
  • Construct left singular vectors from the matrix images of right singular vectors.
  • Handle zero singular values without dividing by zero.
  • Assemble the full SVD and verify it by multiplying the factors.

From the Gram Matrix to an SVD

For a real matrix \(\mathbf{A}\), the matrix \(\mathbf{A}^{\top}\mathbf{A}\) packages information about how \(\mathbf{A}\) acts on vectors in its domain. Its orthonormal eigenvectors provide the right singular vectors of \(\mathbf{A}\), and the square roots of its eigenvalues give the singular values. The remaining task is to turn the images of those right singular vectors into left singular vectors and assemble the factors in the correct order.

Throughout, let \(\mathbf{A}\in\mathbb{R}^{m\times n}\). An SVD, or singular value decomposition, is a factorization \(\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^{\top}\), where \(\mathbf{U}\) and \(\mathbf{V}\) are orthogonal matrices and \(\boldsymbol{\Sigma}\) is an \(m\times n\) diagonal-shaped matrix. Orthogonal means that the columns form an orthonormal basis, equivalently \(\mathbf{U}^{\top}\mathbf{U}=\mathbf{I}_m\) and \(\mathbf{V}^{\top}\mathbf{V}=\mathbf{I}_n\). The singular values are the nonnegative diagonal entries of \(\boldsymbol{\Sigma}\), written in decreasing order.

The Gram matrix \(\mathbf{A}^{\top}\mathbf{A}\) is \(n\times n\), symmetric, and positive semidefinite: for every \(\mathbf{x}\in\mathbb{R}^n\), \(\mathbf{x}^{\top}\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=(\mathbf{A}\mathbf{x})^{\top}(\mathbf{A}\mathbf{x})\ge 0\). By the real Spectral Theorem, it has an orthonormal eigenbasis. Also, if \(\mathbf{A}^{\top}\mathbf{A}\mathbf{v}=\lambda\mathbf{v}\) with \(\mathbf{v}\ne\mathbf{0}\), then \(\lambda\|\mathbf{v}\|^2=\|\mathbf{A}\mathbf{v}\|^2\ge0\), so every eigenvalue is nonnegative.

Key relation: If \(\mathbf{v}\) is a unit eigenvector of \(\mathbf{A}^{\top}\mathbf{A}\) with eigenvalue \(\lambda\), then \(\|\mathbf{A}\mathbf{v}\|^2=\lambda\). Thus the associated singular value is \(\sigma=\sqrt{\lambda}\). When \(\sigma>0\), the corresponding left singular vector is \(\mathbf{u}=\mathbf{A}\mathbf{v}/\sigma\).

Why the Construction Produces Orthonormal Vectors

Before carrying out the procedure, it is useful to check why dividing the images by their singular values works. Take an orthonormal list of eigenvectors \(\mathbf{v}_1,\dots,\mathbf{v}_r\) of \(\mathbf{A}^{\top}\mathbf{A}\) with positive eigenvalues \(\lambda_i=\sigma_i^2\). Define \(\mathbf{u}_i=\mathbf{A}\mathbf{v}_i/\sigma_i\).

Proposition: The vectors \(\mathbf{u}_1,\dots,\mathbf{u}_r\) are orthonormal, and \(\mathbf{A}\mathbf{v}_i=\sigma_i\mathbf{u}_i\) for each \(i\).

Proof. The equation \(\mathbf{A}\mathbf{v}_i=\sigma_i\mathbf{u}_i\) follows from the definition of \(\mathbf{u}_i\), since \(\sigma_i>0\). For any \(i,j\), use the eigenvector equation and the orthonormality of the \(\mathbf{v}_i\):

$$ \mathbf{u}_i^{\top}\mathbf{u}_j =\frac{\mathbf{v}_i^{\top}\mathbf{A}^{\top}\mathbf{A}\mathbf{v}_j}{\sigma_i\sigma_j} =\frac{\lambda_j\mathbf{v}_i^{\top}\mathbf{v}_j}{\sigma_i\sigma_j}. $$

If \(i\ne j\), then \(\mathbf{v}_i^{\top}\mathbf{v}_j=0\), so \(\mathbf{u}_i^{\top}\mathbf{u}_j=0\). If \(i=j\), then \(\mathbf{v}_i^{\top}\mathbf{v}_i=1\) and \(\lambda_i=\sigma_i^2\), so \(\mathbf{u}_i^{\top}\mathbf{u}_i=1\). These are exactly the required orthogonality and unit-length conditions. \(\square\)

The division in this definition requires a positive singular value. If \(\lambda=0\), then \(\|\mathbf{A}\mathbf{v}\|^2=0\), which gives \(\mathbf{A}\mathbf{v}=\mathbf{0}\); dividing that image by \(\sqrt{\lambda}=0\) is not allowed. Instead, the left singular vectors not yet constructed are chosen to complete the existing orthonormal list to an orthonormal basis of \(\mathbb{R}^m\). Orthonormal completion in a finite-dimensional real inner-product space can be performed by extending a list to a basis and applying Gram–Schmidt.

A Step-by-Step Procedure

1
Form the Gram matrix.
Compute \(\mathbf{G}=\mathbf{A}^{\top}\mathbf{A}\), which is an \(n\times n\) real symmetric matrix.
2
Find its orthonormal eigenbasis.
Choose orthonormal eigenvectors \(\mathbf{v}_1,\dots,\mathbf{v}_n\) and list their eigenvalues in decreasing order, \(\lambda_1\ge\cdots\ge\lambda_n\ge0\).
3
Take square roots.
Set \(\sigma_i=\sqrt{\lambda_i}\). These are nonnegative and remain in decreasing order.
4
Construct the left vectors for positive values.
For each \(\sigma_i>0\), compute \(\mathbf{u}_i=\mathbf{A}\mathbf{v}_i/\sigma_i\). The proposition proves these vectors are orthonormal.
5
Complete the left basis if needed.
Do not divide by a zero singular value. Complete the already constructed left vectors to an orthonormal basis of \(\mathbb{R}^m\).
6
Assemble and check.
Put the left vectors into \(\mathbf{U}\), the right vectors into \(\mathbf{V}\), and the singular values on the diagonal of \(\boldsymbol{\Sigma}\). Check the orthogonality of the factors and multiply \(\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^{\top}\).

Here is the precise reason this procedure gives the original matrix. Let \(r\) be the number of positive Gram eigenvalues, so \(\sigma_1,\dots,\sigma_r>0\) and the remaining eigenvalues are zero. For \(j>r\), the norm identity gives \(\|\mathbf{A}\mathbf{v}_j\|^2=0\), hence \(\mathbf{A}\mathbf{v}_j=\mathbf{0}\). The constructed \(\mathbf{u}_1,\dots,\mathbf{u}_r\) are orthonormal, so \(r\le m\) and they can be completed to \(m\) orthonormal vectors.

Theorem (Gram-matrix construction of the SVD): Let \(\mathbf{A}\in\mathbb{R}^{m\times n}\). Choose an orthonormal eigenbasis \(\mathbf{v}_1,\dots,\mathbf{v}_n\) of \(\mathbf{A}^{\top}\mathbf{A}\), ordered so that the eigenvalues satisfy \(\lambda_1\ge\cdots\ge\lambda_n\ge0\). Set \(\sigma_i=\sqrt{\lambda_i}\). For each \(\sigma_i>0\), set \(\mathbf{u}_i=\mathbf{A}\mathbf{v}_i/\sigma_i\), and complete these vectors to an orthonormal basis of \(\mathbb{R}^m\). With \(\mathbf{U}\) and \(\mathbf{V}\) having these left and right vectors as columns, and \(\boldsymbol{\Sigma}\) having \(\sigma_1,\dots,\sigma_{\min(m,n)}\) on its diagonal and zeros elsewhere, \(\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^{\top}\).

Proof. The positive-eigenvalue vectors constructed as \(\mathbf{u}_i\) are orthonormal by the proposition. For every remaining eigenvector, \(\lambda_j=0\), and \(\|\mathbf{A}\mathbf{v}_j\|^2=\lambda_j\|\mathbf{v}_j\|^2=0\), so \(\mathbf{A}\mathbf{v}_j=\mathbf{0}\). The columns of \(\mathbf{V}\) form an orthonormal basis, and therefore \(\mathbf{I}_n=\sum_{j=1}^n\mathbf{v}_j\mathbf{v}_j^{\top}\). It follows that \(\mathbf{A}=\mathbf{A}\mathbf{I}_n=\sum_{j=1}^n(\mathbf{A}\mathbf{v}_j)\mathbf{v}_j^{\top}=\sum_{i=1}^r\sigma_i\mathbf{u}_i\mathbf{v}_i^{\top}\). In \(\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^{\top}\), each positive diagonal entry contributes exactly \(\sigma_i\mathbf{u}_i\mathbf{v}_i^{\top}\); every other diagonal entry is zero, so the completed left vectors contribute nothing further. Thus this product equals the displayed sum and hence equals \(\mathbf{A}\). \(\square\)

Worked Calculations

Worked Example: Distinct Positive Singular Values

Compute an SVD of

$$ \mathbf{A}= \begin{bmatrix} \frac{3}{\sqrt{2}}&\frac{3}{\sqrt{2}}&0\\ -\sqrt{2}&\sqrt{2}&0\\ 0&0&1 \end{bmatrix}. $$

The columns have squared lengths \(13/2,13/2,1\), with the first two having inner product \(5/2\); each of the first two has inner product \(0\) with the third. Thus

$$ \mathbf{A}^{\top}\mathbf{A} = \begin{bmatrix} \frac{13}{2}&\frac{5}{2}&0\\ \frac{5}{2}&\frac{13}{2}&0\\ 0&0&1 \end{bmatrix}. $$

The first two coordinates give eigenvector \(\mathbf{v}_1=(1,1,0)^{\top}/\sqrt{2}\) with eigenvalue \(9\), and \(\mathbf{v}_2=(1,-1,0)^{\top}/\sqrt{2}\) with eigenvalue \(4\). The third coordinate gives \(\mathbf{v}_3=(0,0,1)^{\top}\) with eigenvalue \(1\). For example, the first two eigenvalue checks are \((13/2+5/2)/\sqrt{2}=9/\sqrt{2}\) and \((13/2-5/2)/\sqrt{2}=4/\sqrt{2}\) in the first coordinate, with the corresponding second coordinates \(9/\sqrt{2}\) and \(-4/\sqrt{2}\). Therefore the singular values are \(3,2,1\).

Apply \(\mathbf{A}\) to the right vectors:

$$ \mathbf{A}\mathbf{v}_1= \begin{bmatrix}3\\0\\0\end{bmatrix}, \qquad \mathbf{A}\mathbf{v}_2= \begin{bmatrix}0\\-2\\0\end{bmatrix}, \qquad \mathbf{A}\mathbf{v}_3= \begin{bmatrix}0\\0\\1\end{bmatrix}. $$

Dividing by \(3,2,1\), respectively, gives \(\mathbf{u}_1=(1,0,0)^{\top}\), \(\mathbf{u}_2=(0,-1,0)^{\top}\), and \(\mathbf{u}_3=(0,0,1)^{\top}\). Hence

$$ \mathbf{U}= \begin{bmatrix}1&0&0\\0&-1&0\\0&0&1\end{bmatrix}, \quad \boldsymbol{\Sigma}= \begin{bmatrix}3&0&0\\0&2&0\\0&0&1\end{bmatrix}, \quad \mathbf{V}= \begin{bmatrix} \frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}&0\\ \frac{1}{\sqrt{2}}&-\frac{1}{\sqrt{2}}&0\\ 0&0&1 \end{bmatrix}. $$

Check: Multiplying \(\mathbf{U}\boldsymbol{\Sigma}\) gives \(\begin{bmatrix}3&0&0\\0&-2&0\\0&0&1\end{bmatrix}\). Multiplying this by \(\mathbf{V}^{\top}\) gives rows \((3/\sqrt{2},3/\sqrt{2},0)\), \((-\sqrt{2},\sqrt{2},0)\), and \((0,0,1)\), exactly \(\mathbf{A}\). The columns of both \(\mathbf{U}\) and \(\mathbf{V}\) are orthonormal.

Worked Example: A Zero Singular Value and Left-Basis Completion

For

$$ \mathbf{A}= \begin{bmatrix}1&1&0\\-1&1&0\\2&0&0\end{bmatrix}, $$

the columns are \((1,-1,2)^{\top}\), \((1,1,0)^{\top}\), and \(\mathbf{0}\). Their pairwise inner products give \(\mathbf{A}^{\top}\mathbf{A}=\begin{bmatrix}6&0&0\\0&2&0\\0&0&0\end{bmatrix}\). Its standard basis eigenvectors give singular values \(\sqrt{6},\sqrt{2},0\), in that order. The first two left vectors are

$$ \mathbf{u}_1=\frac{\mathbf{A}\mathbf{e}_1}{\sqrt{6}} =\frac{(1,-1,2)^{\top}}{\sqrt{6}}, \qquad \mathbf{u}_2=\frac{\mathbf{A}\mathbf{e}_2}{\sqrt{2}} =\frac{(1,1,0)^{\top}}{\sqrt{2}}. $$

For the third left vector, choose \(\mathbf{u}_3=(1,-1,-1)^{\top}/\sqrt{3}\). Its squared length is \((1+1+1)/3=1\). Its inner products with the first two are \((1+1-2)/\sqrt{18}=0\) and \((1-1)/\sqrt{6}=0\). Thus the columns of \(\mathbf{U}=[\mathbf{u}_1\ \mathbf{u}_2\ \mathbf{u}_3]\) are orthonormal. With \(\mathbf{V}=\mathbf{I}_3\), use

$$ \boldsymbol{\Sigma}= \begin{bmatrix}\sqrt{6}&0&0\\0&\sqrt{2}&0\\0&0&0\end{bmatrix}. $$

Check: The columns of \(\mathbf{U}\boldsymbol{\Sigma}\) are \(\sqrt{6}\mathbf{u}_1=(1,-1,2)^{\top}\), \(\sqrt{2}\mathbf{u}_2=(1,1,0)^{\top}\), and \(\mathbf{0}\). Since \(\mathbf{V}^{\top}=\mathbf{I}_3\), their assembled matrix is exactly \(\mathbf{A}\). No left vector was obtained by dividing by the zero singular value.

Worked Example: A Non-Diagonal Gram Matrix

Let \(\mathbf{A}\) have columns \(\mathbf{c}_1=(1,0,1)^{\top}\), \(\mathbf{c}_2=(0,1,1)^{\top}\), and \(\mathbf{c}_3=(1,1,2)^{\top}\). Computing the six distinct column inner products gives

$$ \mathbf{A}^{\top}\mathbf{A} = \begin{bmatrix}2&1&3\\1&2&3\\3&3&6\end{bmatrix}. $$

Direct multiplication shows that \(\mathbf{v}_1=(1,1,2)^{\top}/\sqrt{6}\), \(\mathbf{v}_2=(1,-1,0)^{\top}/\sqrt{2}\), and \(\mathbf{v}_3=(-1,-1,1)^{\top}/\sqrt{3}\) are eigenvectors with eigenvalues \(9,1,0\). For instance, the matrix sends \((1,1,2)^{\top}\) to \((9,9,18)^{\top}\), sends \((1,-1,0)^{\top}\) to \((1,-1,0)^{\top}\), and sends \((-1,-1,1)^{\top}\) to \(\mathbf{0}\). The three vectors are unit and pairwise orthogonal, so \(\mathbf{V}=[\mathbf{v}_1\ \mathbf{v}_2\ \mathbf{v}_3]\) is orthogonal. The singular values are \(3,1,0\).

Applying \(\mathbf{A}\) gives \(\mathbf{A}\mathbf{v}_1=3\mathbf{v}_1\), \(\mathbf{A}\mathbf{v}_2=\mathbf{v}_2\), and \(\mathbf{A}\mathbf{v}_3=\mathbf{0}\). Therefore take \(\mathbf{U}=\mathbf{V}\) and \(\boldsymbol{\Sigma}=\operatorname{diag}(3,1,0)\). To check the product, its nonzero terms are \(3\mathbf{v}_1\mathbf{v}_1^{\top}+\mathbf{v}_2\mathbf{v}_2^{\top}\):

$$ \begin{bmatrix} \frac12&\frac12&1\\ \frac12&\frac12&1\\ 1&1&2 \end{bmatrix} + \begin{bmatrix} \frac12&-\frac12&0\\ -\frac12&\frac12&0\\ 0&0&0 \end{bmatrix} = \begin{bmatrix}1&0&1\\0&1&1\\1&1&2\end{bmatrix} =\mathbf{A}. $$

This verifies \(\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^{\top}=\mathbf{A}\), including the zero contribution from the third singular value.

Common Mistakes to Avoid

Using an eigenvalue as the singular value. In the last example, the largest Gram eigenvalue is \(9\), but the corresponding singular value is \(\sqrt{9}=3\), not \(9\). If one incorrectly places \(9\) on the diagonal and uses the same left and right vectors, the first rank-one term becomes \(9\mathbf{v}_1\mathbf{v}_1^{\top}\) instead of \(3\mathbf{v}_1\mathbf{v}_1^{\top}\); it does not reproduce \(\mathbf{A}\).

Dividing by zero. If \(\lambda_i=0\), then \(\mathbf{A}\mathbf{v}_i=\mathbf{0}\), but \(\mathbf{A}\mathbf{v}_i/\sqrt{\lambda_i}\) is undefined. Complete the left orthonormal basis using vectors orthogonal to those already found. Then check the resulting columns directly, as in the second example.

Checking only the factor sizes is not enough. A proposed SVD also requires orthonormal columns in \(\mathbf{U}\) and \(\mathbf{V}\), correct square roots on the diagonal, and a product equal to the original matrix. Multiplying the factors back is a direct way to catch sign, ordering, or scaling errors.

Check Your Understanding

For each question, use the Gram-matrix procedure and check any proposed factors by multiplication.

  1. For \(\mathbf{A}=\begin{bmatrix}1&0\\0&2\end{bmatrix}\), find the eigenvalues of \(\mathbf{A}^{\top}\mathbf{A}\) and the singular values.
  2. Explain why a unit eigenvector \(\mathbf{v}\) with eigenvalue \(0\) satisfies \(\mathbf{A}\mathbf{v}=\mathbf{0}\).
  3. If \(\mathbf{A}^{\top}\mathbf{A}\mathbf{v}=16\mathbf{v}\) and \(\|\mathbf{v}\|=1\), what is the singular value, and how is its left singular vector computed?
  4. For the matrix in the second worked example, verify directly that the three columns of \(\mathbf{U}\) are pairwise orthogonal.
  5. Why is completing the left singular vectors necessary when a positive singular value has already supplied fewer than \(m\) orthonormal vectors?

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