
    dh
                        S r SSKrSSKJrJrJrJr  SSKr\R                  " \
5      r\\\\      \\R                     \R                  4   rS\S\S\R                  4S jr  SS\S\S\\   S	\\   S\\\\\4      \\   4   4
S
 jjrg)zMath utils.    N)ListOptionalTupleUnionXYreturnc           	      *   [        U 5      S:X  d  [        U5      S:X  a  [        R                  " / 5      $ [        R                  " U 5      n [        R                  " U5      nU R                  S   UR                  S   :w  a&  [	        SU R                   SUR                   S35      e SSKn[        R                  " U [        R                  S9n [        R                  " U[        R                  S9nS[        R                  " UR                  XSS	95      -
  nU$ ! [         a    [        R                  S
5        [        R                  R                  U SS9n[        R                  R                  USS9n[        R                  " SSS9   [        R                  " XR                  5      [        R                   " XE5      -  nSSS5        O! , (       d  f       O= fSW[        R"                  " U5      [        R$                  " U5      -  '   Us $ f = f)z<Row-wise cosine similarity between two equal-width matrices.r      z;Number of columns in X and Y must be the same. X has shape z and Y has shape .N)dtypecosine)metriczUnable to import simsimd, defaulting to NumPy implementation. If you want to use simsimd please install with `pip install simsimd`.axisignore)divideinvalidg        )lennparrayshape
ValueErrorsimsimdfloat32cdistImportErrorloggerdebuglinalgnormerrstatedotTouterisnanisinf)r   r   simdZX_normY_norm
similaritys          V/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utils/math.pycosine_similarityr.      s   
1v{c!fkxx|
A
AwwqzQWWQZI!'' S wwiq*
 	
HHQbjj)HHQbjj)A:;; H	
 **[[(;33"((6*BBJ <;;BE
288J'"((:*>>?s,   (A2D A0H8G	H
G	9HHtop_kscore_thresholdc                     [        U 5      S:X  d  [        U5      S:X  a  / / 4$ [        X5      nU=(       d    SnSXDU:  '   [        U=(       d    [        U5      [        R                  " U5      5      n[        R
                  " XB* SS9U* S nU[        R                  " UR                  5       U   5         SSS2   n[        R                  " XTR                  5      nUR                  5       U   R                  5       n[        [        U6 5      U4$ )a}  Row-wise cosine similarity with optional top-k and score threshold filtering.

Args:
    X: Matrix.
    Y: Matrix, same width as X.
    top_k: Max number of results to return.
    score_threshold: Minimum cosine similarity of results.

Returns:
    Tuple of two lists. First contains two-tuples of indices (X_idx, Y_idx),
        second contains corresponding cosine similarities.
r   g      Nr   )r   r.   minr   count_nonzeroargpartitionargsortravelunravel_indexr   tolistlistzip)r   r   r/   r0   score_array
top_k_idxsret_idxsscoress           r-   cosine_similarity_top_kr@   .   s    $ 1v{c!fk2v#A)K%-O12Ko-.)[)2+;+;K+HIEf4@%IJBJJ{'8'8'::'FGH2NJ
,=,=>H ,335FX''    )   N)__doc__loggingtypingr   r   r   r   numpyr   	getLogger__name__r   floatndarrayMatrixr.   intr@    rA   r-   <module>rN      s      / / 			8	$	tDK $rzz"2BJJ>	? F rzz H '+	((( C=( e_	(
 4c3h $u+-.(rA   