def cost_function(a, b, l1=False):
    """Calculates cost function.

    This function calculates L1-norm (element-wise sum of absolute differences)
    or L2-norm (element-wise sum of square of differences) between the original
    matrix and reconstructed matrix.

    Args:
        a: A NumPy Matrix object representing the original matrix.
        b: A NumPy Matrix object representing the reconstructed matrix.
        l1: A boolean indicating whether or not L1 norm should be used instead
        of L2 norm.

    Returns:
        A double representing the value of cost function.
    """
    cost = 0
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
            cost += abs(a[i, j] - b[i, j]) if l1 else pow(a[i, j] - b[i, j], 2)
    return cost