Linear Algebra: solve, svd, eig

The np.linalg module brings serious linear algebra to NumPy — solving systems of equations, inverting matrices, and factorizing them into eigenvalues or singular values, all with battle-tested numerical routines.

Learn Linear Algebra: solve, svd, eig in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll solve a system with solve , compute inv and det , find eigenvalues with eig , factor a matrix with svd , and measure size with norm and matrix_rank .

Write a system of equations as A x = b and let np.linalg.solve(A, b) find x . It is faster and more stable than inverting A yourself. np.linalg.det gives the determinant (zero means no unique solution) and np.linalg.inv the inverse, which times the original yields the identity.

An eigenvector of a matrix is a direction the matrix only stretches, never rotates, and the eigenvalue is the stretch factor: A v = λ v . np.linalg.eig returns both as a tuple (values, vectors) , where each column of vectors pairs with the value at the same index.

The singular value decomposition np.linalg.svd factors any matrix into U @ diag(s) @ Vt ; the singular values s measure how much the matrix stretches space and come back sorted largest-first. np.linalg.norm gives the length of a vector (or size of a matrix), and np.linalg.matrix_rank counts linearly independent rows.

A rank below the matrix's size is the warning sign that solve and inv will fail — the rows do not carry enough independent information for a unique solution.

Replace each ___ so the program solves a simple diagonal system 3x = 9 , 2y = 8 .

Expected output: [3. 4.] then [9. 8.] . (Answers: solve , @ .)

The matrix has a zero determinant (rank-deficient), so it has no unique solution or inverse.

✅ Fix: check np.linalg.det(A) and np.linalg.matrix_rank(A) first; use np.linalg.lstsq for a least-squares solution.

np.linalg.inv(A) @ b is slower and less stable than solving directly.

✅ Fix: use np.linalg.solve(A, b) — it is the right tool for A x = b.

Solve x + y = 6 , 2x − y = 0 , then report the determinant, the length of the solution vector, and the eigenvalues of a diagonal matrix.

Lesson complete — linear algebra unlocked!

You can now solve systems with solve , compute inv and det , decompose matrices with eig and svd , and measure them with norm and matrix_rank .

🚀 Up next: NaN-Aware Functions — compute means and sums that quietly skip missing values.

Practice quiz

Which function solves a system written as A x = b?

  • np.linalg.solve
  • np.linalg.svd
  • np.linalg.norm
  • np.linalg.det

Answer: np.linalg.solve. np.linalg.solve(A, b) returns the solution vector x directly.

What does np.linalg.solve([[2,1],[1,3]], [5,10]) return?

Solving the system gives x = 1, y = 3, printed as [1. 3.].

What does np.linalg.eig return for a matrix?

  • Only the determinant
  • Only eigenvectors
  • A tuple of (eigenvalues, eigenvectors)
  • A scalar norm

Answer: A tuple of (eigenvalues, eigenvectors). eig returns both eigenvalues and eigenvectors as a tuple.

For the symmetric matrix [[2,1],[1,2]], the eigenvalues are:

Its eigenvalues are 3 and 1.

How are the singular values from np.linalg.svd ordered?

  • Largest first
  • Smallest first
  • Random order
  • Alphabetical

Answer: Largest first. svd returns the singular values sorted largest-first.

What does np.linalg.matrix_rank([[1,2],[2,4]]) return?

  • 4
  • 1
  • 2
  • 0

Answer: 1. The second row is twice the first, so only one row is independent: rank 1.

What does np.linalg.matrix_rank(np.eye(2)) return?

  • 1
  • 0
  • 2
  • 4

Answer: 2. The 2x2 identity has two independent rows, so its rank is 2.

What does np.linalg.svd factor a matrix into?

  • A + b
  • Two eigenvalues
  • U @ diag(s) @ Vt
  • A determinant and a rank

Answer: U @ diag(s) @ Vt. SVD factors any matrix as U @ diag(s) @ Vt.

What does np.linalg.norm([3, 4]) compute?

  • 7.0
  • 1.0
  • 25.0
  • 5.0

Answer: 5.0. The Euclidean norm is sqrt(3**2 + 4**2) = 5.0.

A rank below a square matrix's size warns that which operations may fail?

  • solve and inv
  • transpose
  • reshape
  • ravel

Answer: solve and inv. Rank-deficient matrices lack a unique solution, so solve and inv fail.