o
    .wi                     @   sz   d dl Z d dl mZ d dlmZ d dlmZ dedededefd	d
ZdededefddZdedededefddZ	dS )    N)Tensor)_check_same_shape)TorchMetricsUserErrorpredstargetspreturnc                 C   sN   t | | t|ttfr|dkstd| t| | }tt||S )a  Update and return variables required to compute Minkowski distance.

    Checks for same shape of input tensors.

    Args:
        preds: Predicted tensor
        targets: Ground truth tensor
        p: Non-negative number acting as the p to the errors

       z>Argument ``p`` must be a float or int greater than 1, but got )	r   
isinstancefloatintr   torchabssumpow)r   r   r   
difference r   i/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/regression/minkowski.py_minkowski_distance_update   s
   
r   distancec                 C   s   t | d| S )a  Compute Minkowski Distance.

    Args:
        distance: Sum of the p-th powers of errors over all observations
        p: The non-negative numeric power the errors are to be raised to

    Example:
        >>> preds = torch.tensor([0., 1, 2, 3])
        >>> target = torch.tensor([0., 2, 3, 1])
        >>> distance_p_sum = _minkowski_distance_update(preds, target, 5)
        >>> _minkowski_distance_compute(distance_p_sum, 5)
        tensor(2.0244)

    g      ?)r   r   )r   r   r   r   r   _minkowski_distance_compute)   s   r   c                 C   s   t | ||}t||S )a'  Compute the `Minkowski distance`_.

    .. math:: d_{\text{Minkowski}} = \\sum_{i}^N (| y_i - \\hat{y_i} |^p)^\frac{1}{p}

    This metric can be seen as generalized version of the standard euclidean distance which corresponds to minkowski
    distance with p=2.

    Args:
        preds: estimated labels of type Tensor
        targets: ground truth labels of type Tensor
        p: int or float larger than 1, exponent to which the difference between preds and target is to be raised

    Return:
        Tensor with the Minkowski distance

    Example:
        >>> from torchmetrics.functional.regression import minkowski_distance
        >>> x = torch.tensor([1.0, 2.8, 3.5, 4.5])
        >>> y = torch.tensor([6.1, 2.11, 3.1, 5.6])
        >>> minkowski_distance(x, y, p=3)
        tensor(5.1220)

    )r   r   )r   r   r   minkowski_dist_sumr   r   r   minkowski_distance;   s   
r   )
r   r   torchmetrics.utilities.checksr   !torchmetrics.utilities.exceptionsr   r   r   r   r   r   r   r   r   <module>   s   