
    7|hc
                        d Z ddlZddlmZmZmZmZ ddlZ ej                  e
      Zeeee      eej                     ej                  f   Zdededej                  fdZ	 	 ddededee   d	ee   deeeeef      ee   f   f
d
Zy)zMath utils.    N)ListOptionalTupleUnionXYreturnc                 ,   t        |       dk(  st        |      dk(  rt        j                  g       S t        j                  |       } t        j                  |      }| j                  d   |j                  d   k7  r&t	        d| j                   d|j                   d      	 ddl}t        j                  | t        j                        } t        j                  |t        j                        }dt        j                  |j                  | |d	            z
  }|S # t        $ r t        j                  d
       t        j                  j                  | d      }t        j                  j                  |d      }t        j                  dd      5  t        j                  | |j                        t        j                   ||      z  }ddd       n# 1 sw Y   nxY wdt        j"                  |      t        j$                  |      z  <   |cY S w xY w)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          ]/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/utils/math.pycosine_similarityr.      s   
1v{c!fkxx|
A
AwwqzQWWQZI!'' S wwiq*
 	
HHQbjj)HHQbjj)Aq:;; H	
 **[[(; 	C133"((66*BBJ	C 	C 	CBE
288J'"((:*>>?s,   "A9D A7H8G	HG	7HHtop_kscore_thresholdc                    t        |       dk(  st        |      dk(  rg g fS t        | |      }|xs d}d|||k  <   t        |xs t        |      t        j                  |            }t        j
                  || d      | d }|t        j                  |j                         |            ddd   }t        j                  ||j                        }|j                         |   j                         }t        t        |       |fS )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#Aq)K%-O12Ko-.)[)2+;+;K+HIEuf4@%IJBJJ{'8'8'::'FGH2NJ
K,=,=>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   