Skip to content
Tutorials Center
My learning

Topics

Lesson 59 of 62 ยท Practice and Review

Practice: Eigenvalues and Decompositions

Ten graded exercises on eigenvalues, diagonalization, the spectral theorem, positive definiteness and the SVD, each with a runnable NumPy solution.

Eigenvalues and decompositions are the point where linear algebra stops being bookkeeping and starts explaining behavior: what a matrix does to space, how it behaves when repeated, which directions carry the information. This page gives you ten exercises that walk through that chapter, from checking a single eigenvector to measuring the error of a low-rank approximation.

Every exercise states a task and then shows a runnable solution. The solutions finish with assert statements, so if you edit the code and break the idea, the run fails loudly instead of quietly printing nonsense.

How to use this page

Do not run the solution first. Read the task, predict the answer on paper for the small matrices, and only then compare. If you get stuck for ten minutes, read the solution, close it, and redo the exercise from memory the next day.

Four habits for eigen problems

Predict first Guess the answer by hand for small matrices.
Check Av = ฮปv Never trust eigenvectors you have not multiplied back.
Mind the shapes Eigenvectors are columns, not rows.
Use tolerances Compare floats with np.allclose.

These four habits catch most mistakes in this chapter.

1 / 2

Part 1: Eigenvalues and eigenvectors

Related lessons: Eigenvalues and Eigenvectors.

Exercise 1. For A = [[4, 1], [2, 3]], find the eigenvalues and eigenvectors with NumPy and verify that A v = lambda v for every pair. Remember that the eigenvectors are the columns of the second result.

Python Exercise 1: verify A v = lambda v

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 2. Show that the trace equals the sum of the eigenvalues and the determinant equals their product, for A = [[2, 1, 0], [1, 3, 1], [0, 1, 4]].

Python Exercise 2: trace and determinant

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 3. Build the characteristic polynomial of A = [[4, 1], [2, 3]] with np.poly, then find its roots. They must match the eigenvalues from Exercise 1.

Python Exercise 3: characteristic polynomial

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Part 2: Diagonalization and matrix powers

Related lessons: Diagonalization and Matrix Powers.

Exercise 4. Use A = P D P^-1 to compute A^10 for A = [[4, 1], [2, 3]] without multiplying ten times. Compare with np.linalg.matrix_power.

Python Exercise 4: A to the tenth via eigenvalues

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 5. The Fibonacci numbers satisfy [F(n+1), F(n)] = Q [F(n), F(n-1)] with Q = [[1, 1], [1, 0]]. Use the eigen-decomposition of Q to compute F(30).

Python Exercise 5: Fibonacci from eigenvalues

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Part 3: Symmetric matrices and the spectral theorem

Related lessons: Symmetric Matrices and the Spectral Theorem.

Exercise 6. For the symmetric matrix S = [[2, -1, 0], [-1, 2, -1], [0, -1, 2]], use np.linalg.eigh. Show that the eigenvector matrix Q is orthogonal (Q^T Q = I) and that S = Q diag(lambda) Q^T.

Python Exercise 6: the spectral theorem

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 7. A symmetric matrix is positive definite when all eigenvalues are positive. Test S from Exercise 6 and M = [[1, 2], [2, 1]] this way, and confirm with a Cholesky factorization, which only succeeds for positive definite matrices. (On a failure, desktop NumPy raises LinAlgError, while the browser's LAPACK returns nan; the solution handles both.)

Python Exercise 7: positive definiteness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Part 4: The SVD

Related lessons: Singular Value Decomposition and Low-Rank Approximation.

Exercise 8. For A = [[3, 0], [4, 5]], show that the singular values are the square roots of the eigenvalues of A^T A.

Python Exercise 8: singular values and A transpose A

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 9. The matrix A = [[1, 2], [2, 4], [3, 6]] has rank 1. Find its rank from the singular values, then compute the pseudoinverse with the SVD and check that A A+ A = A.

Python Exercise 9: rank and pseudoinverse

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 10. For a random 6 by 5 matrix, build the best rank-2 approximation by truncating the SVD. The spectral-norm error should equal the third singular value, and the Frobenius error should be the root of the sum of the remaining squared singular values.

Python Exercise 10: Eckart-Young check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. np.linalg.eig returns (vals, vecs). Where is the eigenvector for vals[1]?

    The eigenvectors are the columns of vecs. Using a row is a classic mistake that gives wrong results without any error.
  2. 2. A 3 by 3 matrix has eigenvalues 2, 3 and 5. What is its determinant?

    The determinant is the product of the eigenvalues, so 2 * 3 * 5 = 30. The trace would be their sum, 10.
  3. 3. Which function is the right choice for the eigenvalues of a symmetric matrix?

    eigh exploits symmetry: eigenvalues are real and sorted ascending, and the eigenvectors are guaranteed orthonormal.
  4. 4. A matrix has singular values 9, 4 and 0. What is its rank?

    Rank is the number of nonzero singular values, here two. In floating point you compare against a small tolerance instead of exactly zero.

Summary

  • Eigenvectors are the columns of the matrix returned by np.linalg.eig; always verify with A @ v against lambda * v.
  • Trace is the sum of eigenvalues, determinant is their product, and np.poly gives the characteristic polynomial.
  • With A = P D P^-1, powers cost almost nothing: A**n = P D**n P^-1.
  • Use eigh for symmetric matrices: real eigenvalues, orthonormal eigenvectors, and S = Q diag Q^T.
  • Positive definite means all eigenvalues are positive; Cholesky fails on anything else.
  • Singular values are the square roots of the eigenvalues of A^T A; their count above a tolerance is the rank.
  • The truncated SVD is the best low-rank approximation, and its error is the next singular value.

Next, learn to spot and fix the usual traps in Common Mistakes and Debugging.