Matrix Math with Pure Python Lists
In pure Python without NumPy, a matrix is a 2D list of lists (`matrix[row][col]`). Matrix multiplication computes row-by-column dot products in $O(n^3)$ using nested comprehensions 📐.
A = [[1, 2],
[3, 4]]
B = [[5, 6],
[7, 8]]
# Compute C = A x B:
C = [[sum(A[i][k] * B[k][j] for k in range(2)) for j in range(2)] for i in range(2)]
print(f"Matrix product: {C} 📐") # [[19, 22], [43, 50]]
# 46. Matrix Math with Pure Python Lists In pure Python without NumPy, a matrix is a 2D list of lists (`matrix[row][col]`). Matrix multiplication computes row-by-column dot products in $O(n^3)$ using nested comprehensions 📐. ```python A = [[1, 2], [3, 4]] B = [[5, 6], [7, 8]] # Compute C = A x B: C = [[sum(A[i][k] * B[k][j] for k in range(2)) for j in range(2)] for i in range(2)] print(f"Matrix product: {C} 📐") # [[19, 22], [43, 50]] ``` Understanding raw matrix operations from first principles ensures you grasp what tensor libraries (PyTorch, TensorFlow) are doing under the hood! 📐 --- ### Question In the pure Python matrix expression `grid[2][3]`, what does index `2` and index `3` represent? 🧠 - [ ] a. Row 2 and Column 3 📐 - [ ] b. Column 2 and Row 3 - [ ] c. Layer 2 and Channel 3 - [ ] d. CPU Core 2 and Thread 3 --- ### Challenge Create matrices `A = [[1, 0], [0, 1]]` (identity) and `B = [[7, 8], [9, 10]]`. Multiply them using list comprehension. Print result. 🎯 <details> <summary>Solution</summary> ```python A = [[1, 0], [0, 1]] B = [[7, 8], [9, 10]] C = [[sum(A[i][k] * B[k][j] for k in range(2)) for j in range(2)] for i in range(2)] print(C) ``` </details>