Home → NumPy → Linear Algebra
Reading
0%

1 · Matrix Multiplication

Python
import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])

print(A @ B)          # matrix multiply (recommended)
print(np.dot(A, B))   # same as @
print(A * B)          # element-wise (NOT matrix mul!)

v = np.array([1, 2])
print(A @ v)          # [5, 11] matrix-vector
▶ Output
[[19 22]
[43 50]]
[5 11]

2 · Determinant & Inverse

Python
A = np.array([[1,2],[3,4]])
print(np.linalg.det(A))       # -2.0
print(np.linalg.inv(A))       # [[-2, 1],[1.5,-0.5]]
print(A @ np.linalg.inv(A))   # identity (verify)
print(np.linalg.matrix_rank(A))  # 2
print(np.trace(A))            # 5 (sum of diagonal)
▶ Output
-2.0
[[-2. 1.] [1.5 -0.5]]
2 · 5

3 · Eigenvalues & SVD

Python
# Eigendecomposition
vals, vecs = np.linalg.eig(A)
print(f"Eigenvalues: {vals}")   # [-0.372, 5.372]
print(f"Eigenvectors:\n{vecs}")

# SVD (used in PCA, recommendation systems)
M = np.array([[1,2],[3,4],[5,6]])
U, S, Vt = np.linalg.svd(M, full_matrices=False)
print(f"U shape: {U.shape}")    # (3,2)
print(f"S: {S}")                # singular values
print(f"Vt shape: {Vt.shape}")  # (2,2)
▶ Output
Eigenvalues: [-0.372 5.372]

4 · Solving Linear Systems

Python
# Solve Ax = b
A = np.array([[2,1],[5,7]])
b = np.array([11, 13])

x = np.linalg.solve(A, b)
print(f"Solution: {x}")  # [7.111, -3.222]
print(np.allclose(A @ x, b))  # True (verify)
▶ Output
Solution: [ 7.111 -3.222]
True