§
    ’©ºj,  ã            	       ó¢   — d dl mZ d dlZd dlmZ d dlmZ dededeeef         fd„Zddeded
ee         defd„Z	ddeded
ee         defd„Z
dS )é    )ÚOptionalN)ÚTensor)Ú_check_same_shapeÚpredsÚtargetÚreturnc                 ó¾   — t          | |¦  «         | j        dk    rt          d| j        › �¦  «        ‚|                      ¦   «         } |                     ¦   «         }| |fS )zÀUpdate and returns variables required to compute Cosine Similarity. Checks for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor

    é   z¦Expected input to cosine similarity to be 2D tensors of shape `[N,D]` where `N` is the number of samples and `D` is the number of dimensions, but got tensor of shape )r   ÚndimÚ
ValueErrorÚshapeÚfloat)r   r   s     ú„/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/regression/cosine_similarity.pyÚ_cosine_similarity_updater      sn   € õ �e˜VÑ$Ô$Ð$Ø„z�Q‚€Ýð[ØMRÌ[ð[ð [ñ
ô 
ð 	
ð �KŠK‰MŒM€EØ�\Š\‰^Œ^€Fà�&ˆ=Ðó    ÚsumÚ	reductionc                 óø   — | |z                        d¬¦  «        }|                      d¬¦  «        }|                     d¬¦  «        }|||z  z  }t          j         t          j        d„ d„ dœ} ||         |¦  «        S )a&  Compute Cosine Similarity.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        reduction:
            The method of reducing along the batch dimension using sum, mean or taking the individual scores

    Example:
        >>> target = torch.tensor([[1, 2, 3, 4], [1, 2, 3, 4]])
        >>> preds = torch.tensor([[1, 2, 3, 4], [-1, -2, -3, -4]])
        >>> preds, target = _cosine_similarity_update(preds, target)
        >>> _cosine_similarity_compute(preds, target, 'none')
        tensor([ 1.0000, -1.0000])

    éÿÿÿÿ)Údimc                 ó   — | S ©N© ©Úxs    r   ú<lambda>z,_cosine_similarity_compute.<locals>.<lambda>E   s   € ˜!€ r   c                 ó   — | S r   r   r   s    r   r   z,_cosine_similarity_compute.<locals>.<lambda>F   s   € ˜€ r   )r   ÚmeanÚnoneN)r   ÚnormÚtorchr   )r   r   r   Údot_productÚ
preds_normÚtarget_normÚ
similarityÚreduction_mappings           r   Ú_cosine_similarity_computer'   -   sŒ   € ð" ˜6‘>×&Ò&¨2Ð&Ñ.Ô.€KØ—’ �Ñ#Ô#€JØ—+’+ "�+Ñ%Ô%€KØ 
¨[Ñ 8Ñ9€JåŒyÝ”
Ø�Øˆkð	ð Ðð (Ð˜YÔ'¨
Ñ3Ô3Ð3r   c                 óJ   — t          | |¦  «        \  } }t          | ||¦  «        S )až  Compute the `Cosine Similarity`_.

    .. math::
        cos_{sim}(x,y) = \frac{x \cdot y}{||x|| \cdot ||y||} =
        \frac{\sum_{i=1}^n x_i y_i}{\sqrt{\sum_{i=1}^n x_i^2}\sqrt{\sum_{i=1}^n y_i^2}}

    where :math:`y` is a tensor of target values, and :math:`x` is a tensor of predictions.

    Args:
        preds: Predicted tensor with shape ``(N,d)``
        target: Ground truth tensor with shape ``(N,d)``
        reduction:
            The method of reducing along the batch dimension using sum, mean or taking the individual scores

    Example:
        >>> from torchmetrics.functional.regression import cosine_similarity
        >>> target = torch.tensor([[1, 2, 3, 4],
        ...                        [1, 2, 3, 4]])
        >>> preds = torch.tensor([[1, 2, 3, 4],
        ...                       [-1, -2, -3, -4]])
        >>> cosine_similarity(preds, target, 'none')
        tensor([ 1.0000, -1.0000])

    )r   r'   )r   r   r   s      r   Úcosine_similarityr)   K   s)   € õ2 .¨e°VÑ<Ô<�M€Eˆ6Ý% e¨V°YÑ?Ô?Ð?r   )r   )Útypingr   r!   r   Útorchmetrics.utilities.checksr   Útupler   Ústrr'   r)   r   r   r   ú<module>r.      s  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð à ;Ð ;Ð ;Ð ;Ð ;Ð ;ðØðàðð ˆ6�6ˆ>Ôðð ð ð ð.4ð 4 fð 4°fð 4ÈÐRUÌð 4Ðcið 4ð 4ð 4ð 4ð<@ð @˜Vð @¨Vð @ÀÈÄð @ÐZ`ð @ð @ð @ð @ð @ð @r   