import torch


def compute_iou_tensor(box, boxes, box_area, boxes_area):
    """Calculates IoU of the given box with the array of the given boxes.
    box: 1D vector [y1, x1, y2, x2]
    boxes: [boxes_count, (y1, x1, y2, x2)]
    box_area: float. the area of 'box'
    boxes_area: array of length boxes_count.
    Note: the areas are passed in rather than calculated here for
    efficiency. Calculate once in the caller to avoid duplicate work.
    """
    # Calculate intersection areas
    y1 = torch.maximum(box[0], boxes[:, 0])
    y2 = torch.minimum(box[2], boxes[:, 2])
    x1 = torch.maximum(box[1], boxes[:, 1])
    x2 = torch.minimum(box[3], boxes[:, 3])
    intersection = torch.maximum(x2 - x1, torch.tensor(0)) * torch.maximum(y2 - y1, torch.tensor(0))
    union = box_area + boxes_area[:] - intersection[:]
    iou = intersection / union
    return iou