import torch


def _calculate_offsets(anchors: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
    """Calculate offsets for bounding box regression. Offsets are calculated following the
    equations below:
    ..math:

            x'' = \frac{x'}{\sigma_{x}^{2}} = \frac{x_{gt} - x_{anchor}}{w_{anchor}} / \sigma_{X'}

            y'' = \frac{y'}{\sigma_{y}^{2}} = \frac{y_{gt} - y_{anchor}}{h_{anchor}} / \sigma_{Y'}

            w'' = \frac{w'}{\sigma_{w}^{2}} = \ln \left[ \frac{w_{gt}}{w_{anchor}} \right] / \sigma_{W'}

            h'' = \frac{h'}{\sigma_{h}^{2}} = \ln \left[ \frac{h_{gt}}{h_{anchor}} \right] / \sigma_{H'}
    Args:
        anchors (torch.Tensor): anchor boxes (priors). Shape `(A, 4)`. the boxes must be in
        :math:`(x_{center}, y_{center}, width, height)` format.
        targets (torch.Tensor): ground truth boxes. Shape `(A, 4)`. the boxes must be in
        :math:`(x_{center}, y_{center}, width, height)` format.

    Returns:
        (torch.Tensor): the calculated offsets (deltas) for :math:`(x_{center}, y_{center}, width, height)`.
        Shape `(A, 4)`

    """
    targets[:, :2] = (targets[:, :2] - anchors[:, :2]) / anchors[:, 2:]
    targets[:, 2:] = torch.log(targets[:, 2:] / anchors[:, 2:])
    return targets