Loading the problem
Implement matrix multiplication without using @, np.dot or np.matmul.
For A of shape (n, k) and B of shape (k, m), the product C has shape
(n, m) and
C[i, j] = sum over p of A[i, p] * B[p, j]
If the inner dimensions do not agree, return None.
Loops are fine here. The point is to see the contraction over the shared axis before letting BLAS hide it from you.
def matmul(A, B):
Input
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
matmul(A, B)
Output
array([[19., 22.],
[43., 50.]])
Reasoning
C[0, 0] = 1 * 5 + 2 * 7 = 19 and C[0, 1] = 1 * 6 + 2 * 8 = 22. The second row is computed the same way from [3, 4].
None when A.shape[1] != B.shape[0].@, np.dot or np.matmul.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell