Linear Algebra: solve, svd, eig

Reviewed & published by Brayan K

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.

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.

What You'll Learn in This Lesson

1 Solving Systems, inv, and det

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.

import numpy as np

# 2x + y = 5 ; x + 3y = 10
A = np.array([[2, 1], [1, 3]])
b = np.array([5, 10])

x = np.linalg.solve(A, b)
print(x)        # [1. 3.]
print(A @ x)    # [ 5. 10.]  (back to b)

# Determinant and inverse
M = np.array([[4, 7], [2, 6]])
print(round(np.linalg.det(M), 1))            # 10.0
print(np.allclose(M @ np.linalg.inv(M), np.eye(2)))  # True

# ✅ Expected output:
# [1. 3.]
# [ 5. 10.]
# 10.0
# True

2 Eigenvalues and Eigenvectors

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.

import numpy as np

# Diagonal matrix: eigenvalues are the diagonal entries
E = np.array([[2, 0], [0, 3]])
vals, vecs = np.linalg.eig(E)
print(vals)   # [2. 3.]
print(vecs)
# [[1. 0.]
#  [0. 1.]]

# A symmetric matrix with clean eigenvalues 3 and 1
E2 = np.array([[2, 1], [1, 2]])
vals2, _ = np.linalg.eig(E2)
print(vals2)  # [3. 1.]

# ✅ Expected output:
# [2. 3.]
# [[1. 0.]
#  [0. 1.]]
# [3. 1.]

3 SVD, norm, and matrix_rank

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.

import numpy as np

# Diagonal matrix: singular values are |diagonal|, sorted
S = np.array([[3, 0], [0, 4]])
U, s, Vt = np.linalg.svd(S)
print(s)    # [4. 3.]

# Vector length: 3-4-5 right triangle
print(np.linalg.norm(np.array([3, 4])))   # 5.0

# Rank: second row is 2x the first -> not independent
print(np.linalg.matrix_rank(np.array([[1, 2], [2, 4]])))  # 1
print(np.linalg.matrix_rank(np.eye(2)))                   # 2

# ✅ Expected output:
# [4. 3.]
# 5.0
# 1
# 2

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.

🎯 YOUR TURN: Fill in the Blanks

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

import numpy as np

A = np.array([[3, 0], [0, 2]])
b = np.array([9, 8])

# Solve A x = b
x = np.linalg.___(A, b)    # fill in: solve

print(x)        # [3. 4.]
print(A ___ x)  # fill in: @  -> [9. 8.]

# Expected output:
# [3. 4.]
# [9. 8.]

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

⚠️ Common Errors & Quick Reference

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.

❌ Inverting just to solve a system

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.

TaskCode
Solve A x = bnp.linalg.solve(A, b)
Determinantnp.linalg.det(A)
Eigenvaluesnp.linalg.eig(A)
Singular valuesnp.linalg.svd(A)
Ranknp.linalg.matrix_rank(A)

🎯 Mini Challenge: Solve and Analyze

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

import numpy as np

# x + y = 6 ; 2x - y = 0  -> x = 2, y = 4
A = np.array([[1, 1], [2, -1]])
b = np.array([6, 0])

sol = np.linalg.solve(A, b)
print(sol)                           # [2. 4.]
print(round(np.linalg.det(A), 1))    # -3.0
print(round(np.linalg.norm(sol), 4)) # 4.4721

vals, _ = np.linalg.eig(np.array([[2, 0], [0, 5]]))
print(vals)                          # [2. 5.]

# ✅ Expected output:
# [2. 4.]
# -3.0
# 4.4721
# [2. 5.]

❓ Frequently Asked Questions

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.

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?

  • [5. 10.]
  • [1. 3.]
  • [3. 1.]
  • [2. 1.]

Answer: [1. 3.]. 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:

  • [2. 2.]
  • [0. 4.]
  • [1. 1.]
  • [3. 1.]

Answer: [3. 1.]. 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.

Continue this course

Frequently asked questions

How do I solve a system of linear equations in NumPy?

Write the system in matrix form A x = b, then call np.linalg.solve(A, b) to get the solution vector x. solve is faster and more numerically stable than computing the inverse and multiplying, so prefer np.linalg.solve(A, b) over np.linalg.inv(A) @ b. It requires A to be square and non-singular (non-zero determinant).

What is the difference between eigenvalues and singular values?

Eigenvalues come from np.linalg.eig and describe how a square matrix scales its eigenvectors: A v = lambda v. Singular values come from np.linalg.svd and exist for any matrix, square or not; they are the non-negative numbers on the diagonal of the SVD that measure how much the matrix stretches space along its principal directions. For a symmetric positive matrix the two coincide, but in general they are different.

What does np.linalg.matrix_rank tell me?

matrix_rank returns the number of linearly independent rows or columns in a matrix — effectively how many dimensions the matrix actually spans. A full-rank square matrix is invertible and has a non-zero determinant; a rank-deficient matrix has at least one row or column that is a combination of the others, so np.linalg.solve will fail or be unstable. Rank is computed robustly from the singular values.