import torch


def corr2d(X, K):
    """
    The cross-correlation layer, or named convolution layer. It multiply every 
    corresponding elements and add them together to be the output element.

    Parameters
    ----------
    X : [tensor]
        the original matrix to be calculate
    K : [tensor]
        the kernel matrix of the convolution layer

    Returns
    -------
    [tensor]
        return the output matrix
    """
    h, w = K.shape
    # pre-init
    Y = torch.zeros((X.shape[0]-h+1, X.shape[1]-w+1))
    for i in range(Y.shape[0]):
        for j in range(Y.shape[1]):
            # for every possible positions
            # get the sub matrix and multiply to kernel then add up
            Y[i, j] = (X[i:i+h, j:j+w]*K).sum()
    return Y