import torch


def _generate_anchors(size: int, scales: torch.Tensor, ratios: torch.Tensor) -> torch.Tensor:
    """
    calculates N = S x A anchors given S scales and A aspect ratios (AR).
    Args:
        size [int]: base size of the anchors on which scales and AR will be applied.
        In retinanet implementation, the authors consider the sizes (32**2, 64**2, 128**2,
        256**2, 512**2)
        scales [torch.Tensor]: scales of the anchors
        ratios [torch.Tensor]: aspect ratios for the anchors

    Returns:
        [torch.Tensor]: tensor of size (N, 4). the anchors are in the format (x_min, y_min, x_max, y_max)
    """
    # scales = _get_default_scales() if scales is None else scales
    # ratios = _get_default_ratios() if ratios is None else ratios

    num_anchors = len(scales) * len(ratios)
    anchors = torch.zeros(num_anchors, 4)

    wh = scales.repeat(2 * len(ratios)).view(2, -1).T * size
    areas = torch.prod(wh, dim=1)

    ratios = ratios.repeat(len(scales), 1).T.flatten()
    anchors[:, 3] = torch.sqrt(areas / ratios)
    anchors[:, 2] = anchors[:, 3] * ratios

    anchors[:, 0::2] -= (anchors[:, 2] * 0.5).repeat(2).view(2, -1).T
    anchors[:, 1::2] -= (anchors[:, 3] * 0.5).repeat(2).view(2, -1).T
    return anchors