
    `ihS                        d dl Z dZdZ	 d dlmZ dZn$# e$ r 	 d dlmZ dZn# e$ r dZdZY nw xY wY nw xY wd Zd Z G d d          Z	 e	d	d	h
           e	dh d
           e	dh d
           e	dddh
           e	dddh
           e	dh dddg           e	dh d
           e	dddh
           e	dh d
           e	ddhdg           e	dh d 
           e	d!d!h
           e	d"h d#
          gZ
d$ e
D             Z ed% e
D                       Z ee                                          Zd& Zd' Zd( Zd) Zd* Zd+ Zd, Zd- Zd. Zd/ Zd0 Zd1 Zd2 Zd3 Zd9d4Zd:dd5d6Z d;d8Z!dS )<    NF)pairwise_distanceTc                 .    t          j        | |          S )Ndtype)cupyascontiguousarray)Xout_types     p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/spatial/distance.py_convert_to_typer      s    !!84444    c                     |j         }| j        |v r ||                    | j                           n|d         }t          | |          } |j        }|r || ||fi |}| ||fS )Nr   )r
   )typesr   indexr   	validator)r	   mnmetric_infokwargsr   typ_validate_kwargss           r   _validate_pdist_inputr      s    E)*E)9)9%AG$$
%
%uQxCS)))A #, 5!!!Q44V44c6>r   c                       e Zd Z	 	 ddZdS )
MetricInfoNc                 >    || _         || _        || _        || _        d S N)canonical_name_aka_
validator_types_)selfcanonical_nameakar   r   s        r   __init__zMetricInfo.__init__'   s#    -	#r   )NNNN)__name__
__module____qualname__r$    r   r   r   r   %   s.        04'+     r   r   canberra)r"   r#   	chebyshev>   chchebcheby	chebychevr*   	cityblock>   ccbcblockr/   correlationcocosinecoshamming>   hhahammr7   matchingdoublebool)r"   r#   r   	euclidean>   eeueuclidr>   jensenshannonjs	minkowski>   r   mipnormrD   
russellraosqeuclidean>   sqesqeuclidrH   	hellingerkl_divergence>   kldkl_divrL   c                     i | ]
}|j         |S r(   )r   ).0infos     r   
<dictcomp>rR   j   s    AAA4D $AAAr   c              #   2   K   | ]}|j         D ]}||fV  	d S r   )r   )rP   rQ   aliass      r   	<genexpr>rU   k   sQ       - -"&)- - T] - - - - - - -r   c                  D    t           st          st          d          d S d S )NzJcuVS >= 24.12 or pylibraft < 24.12 should be installed to use this feature)cuvs_availablepylibraft_availableRuntimeErrorr(   r   r   check_soft_dependenciesrZ   r   sG     P" 	P  O P P PP P	P 	Pr   c                 "   t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d|           |d         S )	a  Compute the Minkowski distance between two 1-D arrays.

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)
        p (float): The order of the norm of the difference
            :math:`{\|u-v\|}_p`. Note that for :math:`0 < p < 1`,
            the triangle inequality only holds with an additional
            multiplicative factor, i.e. it is only a quasi-metric.

    Returns:
        minkowski (double): The Minkowski distance between vectors `u` and `v`.
    FC+u and v must have the same layout (u.order=
, v.order=   ra   r   orderrD   r   r   rZ   r   	isfortran
ValueErroremptyr   r   )uvpu_orderv_order
output_arrs         r   rD   rD   y   s     ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJQ777dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	az  Compute the Canberra distance between two 1-D arrays.

    The Canberra distance is defined as

    .. math::
        d(u, v) = \sum_{i} \frac{| u_i - v_i |}{|u_i| + |v_i|}

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        canberra (double): The Canberra distance between vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   r)   rd   re   ri   rj   rl   rm   rn   s        r   r)   r)      s     ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ
333dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	af  Compute the Chebyshev distance between two 1-D arrays.

    The Chebyshev distance is defined as

    .. math::
        d(u, v) = \max_{i} |u_i - v_i|

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        chebyshev (double): The Chebyshev distance between vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   r*   rd   re   rp   s        r   r*   r*           ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ444dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a}  Compute the City Block (Manhattan) distance between two 1-D arrays.

    The City Block distance is defined as

    .. math::
        d(u, v) = \sum_{i} |u_i - v_i|

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        cityblock (double): The City Block distance between
        vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   r/   rd   re   rp   s        r   r/   r/      s      ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ444dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a2  Compute the correlation distance between two 1-D arrays.

    The correlation distance is defined as

    .. math::
        d(u, v) = 1 - \frac{(u - \bar{u}) \cdot (v - \bar{v})}{
        \|(u - \bar{u})\|_2 \|(v - \bar{v})\|_2}

    where :math:`\bar{u}` is the mean of the elements of :math:`u` and
    :math:`x \cdot y` is the dot product.

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        correlation (double): The correlation distance between
        vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   r3   rd   re   rp   s        r   r3   r3      s    ( ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ666dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a  Compute the Cosine distance between two 1-D arrays.

    The Cosine distance is defined as

    .. math::
        d(u, v) = 1 - \frac{u \cdot v}{\|u\|_2 \|v\|_2}

    where :math:`x \cdot y` is the dot product.

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        cosine (double): The Cosine distance between vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   r5   rd   re   rp   s        r   r5   r5     s    " ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ111dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a/  Compute the Hamming distance between two 1-D arrays.

    The Hamming distance is defined as the proportion of elements
    in both `u` and `v` that are not in the exact same position:

    .. math::
        d(u, v) = \frac{1}{n} \sum_{k=0}^n u_i \neq v_i

    where :math:`x \neq y` is one if :math:`x` is different from :math:`y`
    and zero otherwise.

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        hamming (double): The Hamming distance between vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   r7   rd   re   rp   s        r   r7   r7   ;  s    & ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ	222dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a  Compute the Euclidean distance between two 1-D arrays.

    The Euclidean distance is defined as

    .. math::
        d(u, v) = \left(\sum_{i} (u_i - v_i)^2\right)^{\sfrac{1}{2}}

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        euclidean (double): The Euclidean distance between vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   r>   rd   re   rp   s        r   r>   r>   ^  rr   r   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a  Compute the Jensen-Shannon distance between two 1-D arrays.

    The Jensen-Shannon distance is defined as

    .. math::
        d(u, v) = \sqrt{\frac{KL(u \| m) + KL(v \| m)}{2}}

    where :math:`KL` is the Kullback-Leibler divergence and :math:`m` is the
    pointwise mean of `u` and `v`.

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        jensenshannon (double): The Jensen-Shannon distance between
        vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   rB   rd   re   rp   s        r   rB   rB   }  s    & ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ888dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a-  Compute the Russell-Rao distance between two 1-D arrays.

    The Russell-Rao distance is defined as the proportion of elements
    in both `u` and `v` that are in the exact same position:

    .. math::
        d(u, v) = \frac{1}{n} \sum_{k=0}^n u_i = v_i

    where :math:`x = y` is one if :math:`x` is different from :math:`y`
    and zero otherwise.

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        hamming (double): The Hamming distance between vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   rG   rd   re   rp   s        r   rG   rG     s    & ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ555dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a  Compute the squared Euclidean distance between two 1-D arrays.

    The squared Euclidean distance is defined as

    .. math::
        d(u, v) = \sum_{i} (u_i - v_i)^2

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        sqeuclidean (double): The squared Euclidean distance between
        vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   rH   rd   re   rp   s        r   rH   rH     s      ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ666dr   c                     t                       t          j        |           rdnd}t          j        |          rdnd}||k    rt          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a  Compute the Hellinger distance between two 1-D arrays.

    The Hellinger distance is defined as

    .. math::
        d(u, v) = \frac{1}{\sqrt{2}} \sqrt{
            \sum_{i} (\sqrt{u_i} - \sqrt{v_i})^2}

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        hellinger (double): The Hellinger distance between
        vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   rK   rd   re   rp   s        r   rK   rK     s    " ^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ444dr   c                 p   t                       t          j        |           } t          j        |          }t          j        |           rdnd}t          j        |          rdnd}||k    rt	          d|d|          t          j        d| j        |          }t          | ||d           |d         S )	a  Compute the Kullback-Leibler divergence between two 1-D arrays.

    The Kullback-Leibler divergence is defined as

    .. math::
        KL(U \| V) = \sum_{i} U_i \log{\left(\frac{U_i}{V_i}\right)}

    Args:
        u (array_like): Input array of size (N,)
        v (array_like): Input array of size (N,)

    Returns:
        kl_divergence (double): The Kullback-Leibler divergence between
        vectors `u` and `v`.
    r\   r]   r^   r_   r`   rb   rL   rd   )rZ   r   asarrayrf   rg   rh   r   r   rp   s        r   rL   rL     s      QAQA^A&&/ccCG^A&&/ccCG'j6=ggwwH I I 	I F!'AAAJaJ888dr   c                    t                       t          st          r| j        dvrt	          j        | d          } t          st          r|j        dvrt	          j        |d          }t	          j        |           rdnd}t	          j        |          rdnd}||k    rt          d|d|          | j        }|j        }t          |          dk    rt          d	          t          |          dk    rt          d
          |d         |d         k    rt          d          |d         }	|d         }
d|v r|d         nd}|t          r|j        dk    st          rP|j        dvrGt	          j        |          rdnd}||k    rt          d|z            |
                    dd          }|j        |	|
fk    rt	          j        ||	|
f           t          |t                    rz|                                }t                              |d          }|7||nt	          j        |	|
f| j        |          }t%          | ||||           |S t          d|z            t'          d          )a  Compute distance between each pair of the two collections of inputs.

    Args:
        XA (array_like): An :math:`m_A` by :math:`n` array of :math:`m_A`
            original observations in an :math:`n`-dimensional space.
            Inputs are converted to float type.
        XB (array_like): An :math:`m_B` by :math:`n` array of :math:`m_B`
            original observations in an :math:`n`-dimensional space.
            Inputs are converted to float type.
        metric (str, optional): The distance metric to use.
            The distance function can be 'canberra', 'chebyshev',
            'cityblock', 'correlation', 'cosine', 'euclidean', 'hamming',
            'hellinger', 'jensenshannon', 'kl_divergence', 'matching',
            'minkowski', 'russellrao', 'sqeuclidean'.
        out (cupy.ndarray, optional): The output array. If not None, the
            distance matrix Y is stored in this array.
        **kwargs (dict, optional): Extra arguments to `metric`: refer to each
            metric documentation for a list of all possible arguments.
            Some possible arguments:
            p (float): The p-norm to apply for Minkowski, weighted and
            unweighted. Default: 2.0

    Returns:
        Y (cupy.ndarray): A :math:`m_A` by :math:`m_B` distance matrix is
            returned. For each :math:`i` and :math:`j`, the metric
            ``dist(u=XA[i], v=XB[j])`` is computed and stored in the
            :math:`ij` th entry.
    )float32float64r   r   r\   r]   z.XA and XB must have the same layout (XA.order=z, XB.order=   z!XA must be a 2-dimensional array.z!XB must be a 2-dimensional array.ra   zHXA and XB must have the same number of columns (i.e. feature dimension.)r   rk          @Nz1out must have same layout as input (out.order=%s)F)copyrb   zUnknown Distance Metric: %sz/2nd argument metric must be a string identifier)rZ   rX   rW   r   r   r}   rf   rg   shapelenastyperesize
isinstancestrlower_METRIC_ALIASgetrh   r   	TypeError)XAXBmetricoutr   XA_orderXB_orderssBmAmBrk   	out_ordermstrr   rn   s                   r   cdistr   &  s   :  // "0F F F\"I... // "0F F F\"I...nR((1sscHnR((1sscH8j8@((L M M 	M 	A	B
1vv{{<===
2ww!||<===tr!u}} 5 6 6 	6 
1B	ABf}}s#A
 	4CI$:$: %;$'I5K$K$K#~c22;IH$$  "24="> ? ? ?**YU*33C9R  Kb"X&&&&# K||~~#''d33" #TZRFHhFN6P 6P 6PJ b"j&!<<<:TABBBIJJJr   )r   c                ^    t          | | f||d|}t          j        |d          }||         S )a[  Compute distance between observations in n-dimensional space.

    Args:
        X (array_like): An :math:`m` by :math:`n` array of :math:`m`
            original observations in an :math:`n`-dimensional space.
            Inputs are converted to float type.
        metric (str, optional): The distance metric to use.
            The distance function can be 'canberra', 'chebyshev',
            'cityblock', 'correlation', 'cosine', 'euclidean', 'hamming',
            'hellinger', 'jensenshannon', 'kl_divergence', 'matching',
            'minkowski', 'russellrao', 'sqeuclidean'.
        out (cupy.ndarray, optional): The output array. If not None, the
            distance matrix Y is stored in this array.
        **kwargs (dict, optional): Extra arguments to `metric`: refer to each
            metric documentation for a list of all possible arguments.
            Some possible arguments:
            p (float): The p-norm to apply for Minkowski, weighted and
            unweighted. Default: 2.0

    Returns:
        Y (cupy.ndarray):
            Returns a condensed distance matrix Y. For each :math:`i` and
            :math:`j` and (where :math:`i < j < m`), where m is the number of
            original observations. The metric ``dist(u=X[i], v=X[j])`` is
            computed and stored in entry
            ``m * i + j - ((i + 2) * (i + 1)) // 2``.
    )r   r   ra   )r   r   triu_indices_from)r	   r   r   r   all_distup_idxs         r   pdistr   ~  s?    8 Q<&c<<V<<H#Ha00FFr   r   c                     t          j        |           } | j        \  }}t          j        |          }|j        \  }}||k    rt          d||fz            t	          | |d|          S )a  Compute the distance matrix.

    Returns the matrix of all pair-wise distances.

    Args:
        x (array_like): Matrix of M vectors in K dimensions.
        y (array_like): Matrix of N vectors in K dimensions.
        p (float): Which Minkowski p-norm to use (1 <= p <= infinity).
            Default=2.0
    Returns:
        result (cupy.ndarray): Matrix containing the distance from every
            vector in `x` to every vector in `y`, (size M, N).
    zGx contains %d-dimensional vectors but y contains %d-dimensional vectorsrD   )r   rk   )r   r}   r   rg   r   )xyrk   r   kr   kks          r   distance_matrixr     s}     	QA7DAqQAGEArBww ;>?WE F F 	F AkQ////r   )r>   N)r>   )r   )"r   rW   rX   cuvs.distancer   ImportErrorpylibraft.distancer   r   r   _METRIC_INFOS_METRICSdictr   listkeys_METRICS_NAMESrZ   rD   r)   r*   r/   r3   r5   r7   r>   rB   rG   rH   rK   rL   r   r   r   r(   r   r   <module>r      s=    
$//////NN $ $ $$888888" $ $ $#$$5 5 5          J!L   J"===   J"...   J$D!   Ju   J 666   
 J"...   J&d#   J"---   J#Nh  
 J$...   J"M   J&...  g9v BA=AAA - -!.- - - - - hmmoo&&P P P  >  >  >  @! ! !H  B     F  >     F     F  @  B  DUK UK UK UKp     B0 0 0 0 0 0s$    4"4	.4.44