
    i                         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Z	 	 	 	 ddedee   d	eded   d
ee   defdZy)    )OptionalN)Tensor)Literal)_check_input_reduce_distance_matrix)TorchMetricsUserErrorxyexponentzero_diagonalreturnc                    t        | ||      \  } }}t        |t        t        f      r|dk\  st	        d|       | j
                  }| j                  t        j                        } |j                  t        j                        }| j                  d      |j                  d      z
  j                         j                  |      j                  d      j                  d|z        }|r|j                  d       |j                  |      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distances         /Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/pairwise/minkowski.py#_pairwise_minkowski_distance_updater       s     'q!];Aq-x%.8q=#&demdn$opp''K	U]]A	U]]AAQ/446::8DHHLPPQTW_Q_`H";;{##    	reduction)meanr   noneNc                 6    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   s         r   pairwise_minkowski_distancer&   1   s"    V 31a=QH"8Y77r!   )N   N)Nr'   NN)typingr   r   r   typing_extensionsr   (torchmetrics.functional.pairwise.helpersr   r   !torchmetrics.utilities.exceptionsr   r   boolr    r&    r!   r   <module>r.      s       % Z C ae$$6"$5:$OWX\~$$6 6:$(,8,8,8 ,8 23	,8
 D>,8 ,8r!   