o
    .wi                     @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 	dded	ee d
edee def
ddZ				dded	ee d
eded dee defddZdS )    )OptionalN)Tensor)Literal)_check_input_reduce_distance_matrix)TorchMetricsUserError   xyexponentzero_diagonalreturnc                 C   s   t | ||\} }}t|ttfr|dkstd| | j}| tj} |tj}| 	d|	d 
 |dd| }|rH|d ||S )ac  Calculate the pairwise minkowski distance matrix.

    Args:
        x: tensor of shape ``[N,d]``
        y: tensor of shape ``[M,d]``
        exponent: int or float larger than 1, exponent to which the difference between preds and target is to be raised
        zero_diagonal: determines if the diagonal of the distance matrix should be set to zero

       z>Argument ``p`` must be a float or int greater than 1, but got r   g      ?)r   
isinstancefloatintr   dtypetotorchfloat64	unsqueezeabspowsumfill_diagonal_)r	   r
   r   r   _orig_dtypedistance r   g/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/pairwise/minkowski.py#_pairwise_minkowski_distance_update   s   .

r    	reduction)meanr   noneNc                 C   s   t | |||}t||S )a  Calculate pairwise minkowski distances.

    .. math::
        d_{minkowski}(x,y,p) = ||x - y||_p = \sqrt[p]{\sum_{d=1}^D (x_d - y_d)^p}

    If both :math:`x` and :math:`y` are passed in, the calculation will be performed pairwise between the rows of
    :math:`x` and :math:`y`. If only :math:`x` is passed in, the calculation will be performed between the rows
    of :math:`x`.

    Args:
        x: Tensor with shape ``[N, d]``
        y: Tensor with shape ``[M, d]``, optional
        exponent: int or float larger than 1, exponent to which the difference between preds and target is to be raised
        reduction: reduction to apply along the last dimension. Choose between `'mean'`, `'sum'`
            (applied along column dimension) or  `'none'`, `None` for no reduction
        zero_diagonal: if the diagonal of the distance matrix should be set to 0. If only `x` is given
            this defaults to `True` else if `y` is also given it defaults to `False`

    Returns:
        A ``[N,N]`` matrix of distances if only ``x`` is given, else a ``[N,M]`` matrix

    Example:
        >>> import torch
        >>> from torchmetrics.functional.pairwise import pairwise_minkowski_distance
        >>> x = torch.tensor([[2, 3], [3, 5], [5, 8]], dtype=torch.float32)
        >>> y = torch.tensor([[1, 0], [2, 1]], dtype=torch.float32)
        >>> pairwise_minkowski_distance(x, y, exponent=4)
        tensor([[3.0092, 2.0000],
                [5.0317, 4.0039],
                [8.1222, 7.0583]])
        >>> pairwise_minkowski_distance(x, exponent=4)
        tensor([[0.0000, 2.0305, 5.1547],
                [2.0305, 0.0000, 3.1383],
                [5.1547, 3.1383, 0.0000]])

    )r    r   )r	   r
   r   r!   r   r   r   r   r   pairwise_minkowski_distance1   s   +
r$   )Nr   N)Nr   NN)typingr   r   r   typing_extensionsr   (torchmetrics.functional.pairwise.helpersr   r   !torchmetrics.utilities.exceptionsr   r   boolr    r$   r   r   r   r   <module>   sF   
