NumPy: Linear Algebra

Last updated: 2026-08-26

1. What You'll Learn



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

PYTHON
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
TEXT 📖 Display only
> **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

PYTHON
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)
TEXT 📖 Display only
> **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

PYTHON
# 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
TEXT 📖 Display only
> **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 ⭐)

PYTHON
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 only
A @ B:
 [[ 58  64]
 [139 154]]
dot(x, y): 32

▶ Example: Solving linear systems (Difficulty ⭐⭐)

PYTHON
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 only
Solution: [2. 3.]
A @ x = [9. 8.]
b     = [9. 8.]

▶ Example: Eigenvalues and eigenvectors (Difficulty ⭐⭐)

PYTHON
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 only
Eigenvalues: [3. 2.]
A @ v: [0.89442719 0.4472136 ]
λ * v: [0.89442719 0.4472136 ]
Match: True

Q What's the difference between @, np.dot, and np.matmul?
A For 2D arrays, they're identical. For higher dimensions, @ and np.matmul use broadcasting (stacked matrix multiplication), while np.dot uses sum-product over the last axis of each.
Q When should I use solve vs inv?
A Always use solve — it's faster and more numerically stable. Computing inv(A) @ b is slower and less accurate than solve(A, b).
Q What if A is singular?
A np.linalg.solve raises LinAlgError: Singular matrix. Use np.linalg.lstsq for least-squares solutions to under/over-determined systems.

❓ FAQ

Q What is the most important thing to remember?
A NumPy operations are vectorized — avoid Python loops for better performance.
Q Where can I learn more?
A Check the official NumPy documentation at numpy.org for detailed references and advanced topics.
Q Does this work with NumPy 2.x?
A Yes — all examples are compatible with NumPy 2.x. Some older APIs (like np.random.seed) are still supported but the modern alternatives are recommended.

📖 Summary



📝 Exercises

  1. Beginner (Difficulty ⭐): Create two 3x3 matrices of random integers. Compute their product, determinant, and inverse. Verify that A @ inv(A) ≈ identity.

  2. Intermediate (Difficulty ⭐⭐): Solve the system: 3x + y = 9, x + 2y = 8 using np.linalg.solve. Verify your solution.

  3. Advanced (Difficulty ⭐⭐⭐): Generate a 5x5 random matrix, compute its eigenvalues and eigenvectors. Verify that A @ v = λ * v for each eigenpair.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏