Linear Algebra

Layer 3 · Code

Linear Algebra

Implementing dot products, matrix multiplies, and a linear layer from scratch with NumPy, then verifying against PyTorch.

12 min read110 XP

By hand (loops)

  • Triple nested for-loop
  • Correct but painfully slow
  • Good for building intuition

Vectorized (NumPy/PyTorch)

  • Single @ operator call
  • Runs on optimized BLAS/GPU kernels
  • What real code always uses
python
import numpy as np

def dot(x, y):
    total = 0.0
    for xi, yi in zip(x, y):
        total += xi * yi
    return total

def matmul_naive(A, B):
    m, n = A.shape
    n2, p = B.shape
    assert n == n2, "inner dimensions must match"
    C = np.zeros((m, p))
    for i in range(m):
        for j in range(p):
            C[i, j] = dot(A[i, :], B[:, j])
    return C

A = np.random.randn(4, 3)
B = np.random.randn(3, 2)
print(np.allclose(matmul_naive(A, B), A @ B))  # True
A dot product and a naive matrix multiply, written with explicit loops.