NumPy: Linear Algebra
Last updated: 2026-08-26
1. What You'll Learn
- ❶ Matrix multiplication:
@,np.dot,np.matmul - ❷ Inverse and determinant:
np.linalg.inv,np.linalg.det - ❸ Eigenvalues and eigenvectors:
np.linalg.eig - ❹ Solving linear systems:
np.linalg.solve - ❺ Matrix decompositions: QR, SVD, Cholesky
2. Story
Bob needs to solve a system of 1000 linear equations. He writes a Gaussian elimination loop — 30 seconds and a buggy implementation. Alice uses np.linalg.solve(A, b) — 0.01 seconds, exact result. "Linear algebra in NumPy isn't just faster — it calls BLAS/LAPACK, the same libraries used by MATLAB and R. Battle-tested for decades."
3. Key Concepts
(1) Matrix Multiplication
import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
# Three equivalent ways:
print(A @ B) # [[19 22]
# [43 50]]
print(np.dot(A, B)) # same
print(np.matmul(A, B)) # same
> **Output:** Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.
(2) Inverse and Determinant
import numpy as np
A = np.array([[1, 2],
[3, 4]])
det = np.linalg.det(A) # -2.000
inv = np.linalg.inv(A) # [[-2. 1. ]
# [ 1.5 -0.5]]
print(A @ inv) # [[1. 0.]
# [0. 1.]] (identity)
> **Output:** Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.
(3) Solving Linear Systems
# Solve Ax = b
A = np.array([[3, 1],
[1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print(x) # [2. 3.]
print(A @ x) # [9. 8.] — verify
> **Output:** Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.
▶ Example: Matrix multiplication (Difficulty ⭐)
import numpy as np
A = np.array([[1, 2, 3],
[4, 5, 6]])
B = np.array([[7, 8],
[9, 10],
[11, 12]])
# Matrix multiply: (2x3) @ (3x2) = (2x2)
C = A @ B
print("A @ B:\n", C)
# Dot product of vectors
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print("dot(x, y):", np.dot(x, y))
Output:
TEXT 📖 Display onlyA @ B: [[ 58 64] [139 154]] dot(x, y): 32
▶ Example: Solving linear systems (Difficulty ⭐⭐)
import numpy as np
# Solve: 3x + y = 9, x + 2y = 8
A = np.array([[3, 1],
[1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print("Solution:", x)
# Verify
print("A @ x =", A @ x)
print("b =", b)
Output:
TEXT 📖 Display onlySolution: [2. 3.] A @ x = [9. 8.] b = [9. 8.]
▶ Example: Eigenvalues and eigenvectors (Difficulty ⭐⭐)
import numpy as np
A = np.array([[4, -2],
[1, 1]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:", eigenvalues)
# Verify: A @ v = λ * v
v = eigenvectors[:, 0]
lam = eigenvalues[0]
lhs = A @ v
rhs = lam * v
print("A @ v:", lhs)
print("λ * v:", rhs)
print("Match:", np.allclose(lhs, rhs))
Output:
TEXT 📖 Display onlyEigenvalues: [3. 2.] A @ v: [0.89442719 0.4472136 ] λ * v: [0.89442719 0.4472136 ] Match: True
@, np.dot, and np.matmul?@ and np.matmul use broadcasting (stacked matrix multiplication), while np.dot uses sum-product over the last axis of each.solve vs inv?solve — it's faster and more numerically stable. Computing inv(A) @ b is slower and less accurate than solve(A, b).np.linalg.solve raises LinAlgError: Singular matrix. Use np.linalg.lstsq for least-squares solutions to under/over-determined systems.❓ FAQ
📖 Summary
@/np.matmul/np.dot: matrix multiplicationnp.linalg.inv: matrix inverse;np.linalg.det: determinantnp.linalg.eig: eigenvalues and eigenvectorsnp.linalg.solve: solving linear systemsnp.linalg.qr,np.linalg.svd,np.linalg.cholesky: decompositions- Use
solveinstead ofinv @ bfor speed and stability
📝 Exercises
-
Beginner (Difficulty ⭐): Create two 3x3 matrices of random integers. Compute their product, determinant, and inverse. Verify that A @ inv(A) ≈ identity.
-
Intermediate (Difficulty ⭐⭐): Solve the system: 3x + y = 9, x + 2y = 8 using
np.linalg.solve. Verify your solution. -
Advanced (Difficulty ⭐⭐⭐): Generate a 5x5 random matrix, compute its eigenvalues and eigenvectors. Verify that A @ v = λ * v for each eigenpair.