import torch


def compute_intersection(set_1, set_2):
    """
    Compute the intersection between set_1 and set_2.

    Args:
        set_1 (tensor): n1 boxes with 4 positions illustrate the area
        set_2 (tensor): n2 boxes with 4 positions illustrate the area

    Returns:
        tensor: intersections of all sets comparision
    """
    # PyTorch auto-broadcasts singleton dimensions
    # find out the center little area positions
    # unsqueeze is used to extend one dimension
    lower_bounds = torch.max(set_1[:, :2].unsqueeze(
        1), set_2[:, :2].unsqueeze(0))  # (n1, n2, 2)
    upper_bounds = torch.min(set_1[:, 2:].unsqueeze(
        1), set_2[:, 2:].unsqueeze(0))  # (n1, n2, 2)
    # get widths and heights
    intersection_dims = torch.clamp(
        upper_bounds - lower_bounds, min=0)  # (n1, n2, 2)
    # multiply to get spaces
    return intersection_dims[:, :, 0] * intersection_dims[:, :, 1]