labs / lab-c-linear-algebra--rank-nullity-theorem
Next task →
Practical Labs/Lab: Rank–nullity theorem
Level 1 · linear-algebra

Lab: Rank–nullity theorem

Build a minimal, verifiable implementation that demonstrates Rank–nullity theorem. Connect the code to the governing equation, inspect its output numerically, and explain one failure case.

Mathematical basis

For Rank–nullity theorem, use A≈UkΣkVk⊤A\approx U_k\Sigma_kV_k^\top and the Eckart–Young bound ∥A−Ak∥F2=∑i>kσi2\|A-A_k\|_F^2=\sum_{i>k}\sigma_i^2.

Instructions

  1. 01Read section 7 of the Rank–nullity theorem learning page and write its governing equation on paper.
  2. 02Implement the core operation in NumPy on a synthetic rank-2 matrix and MNIST patches. Do not call a library function that solves the whole task.
  3. 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
  4. 04Run the experiment with the target rank k and record relative reconstruction error.
  5. 05Inspect the output, identify one discrepancy with the theory, and write a one-paragraph explanation.

Starter code

import numpy as np

def low_rank(x, k):
    x = np.asarray(x, float)
    u, s, vt = np.linalg.svd(x, full_matrices=False)
    return (u[:, :k] * s[:k]) @ vt[:k]

x = np.array([[3.,1.],[1.,3.]])
y = low_rank(x, 1)
assert y.shape == x.shape and np.linalg.norm(x-y) > 0
print(np.round(y, 3))