2D Convolution (Single Channel)
2D Convolution
A 2D convolution slides a kernel (filter) over an input matrix, computing the dot product at each position.
For a valid (no-padding) convolution:
In NumPy you can compute each output element as:
out[i, j] = np.sum(X[i:i+Hf, j:j+Wf] * K)
This is a double loop over output positions — acceptable for small inputs. For large inputs, use scipy.signal.correlate2d or deep learning frameworks.
Your task:
Implement conv2d_single(X, kernel) with stride 1, no padding. Return the output 2D array.
Example Tests
4x4 input, 2x2 all-ones kernel: each output is a 2x2 patch sum
Input: {"X":[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],"kernel":[[1,1],[1,1]]}
Expected: [[14,18,22],[30,34,38],[46,50,54]]
3x3 input, 2x2 identity-like kernel
Input: {"X":[[1,0,0],[0,1,0],[0,0,1]],"kernel":[[1,0],[0,1]]}
Expected: [[2,0],[0,2]]
Output shape check: (H-Hf+1) x (W-Wf+1)
Input: {"X":[[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]],"kernel":[[1,1,1],[1,1,1]]}
Expected: [2,3]