import torch


def get_intersection(interval_1, interval_2):
    """Determine the intersection of two intervals

    Args
        interval_1 [*shape, 2]
        interval_2 [*shape, 2]

    Returns
        intersection [*shape, 2]: this will be nan if there is no intersection
    """
    # Extract
    left_1, right_1 = interval_1[..., 0], interval_1[..., 1]
    left_2, right_2 = interval_2[..., 0], interval_2[..., 1]
    shape = interval_1.shape[:-1]

    # Compute the intersection for cases in which there is an intersection
    intersection = torch.stack([torch.max(left_1, left_2), torch.min(right_1, right_2)], dim=-1)

    # Fill with nan if there isn't an interesection
    is_there_an_intersection = (right_1 < left_2) | (left_1 < right_2)
    intersection[~is_there_an_intersection[..., None].expand(*[*shape, 2])] = float("nan")

    return intersection