
    uki                    f    d dl mZ d dlZd dlmZ d dlmZ d dlmZ	 d dl
mZmZ d dlmZmZ d	d
dZy)    )annotationsN)api)numpy)linalg)check_arraylikepromote_dtypes_inexact)Array	ArrayLikec                   ~t        d| |       t        | |      \  }|j                  j                  k7  rt        d      |j                  dk(  r|d   d   c}|j                  dk7  rt        d       t	        j
                  fd      |      }t        j                  |d	      } t	        j
                  t        j                        ||      }||fS )
a  Assign codes from a code book to a set of observations.

  JAX implementation of :func:`scipy.cluster.vq.vq`.

  Assigns each observation vector in ``obs`` to a code from ``code_book``
  based on the nearest Euclidean distance.

  Args:
    obs: array of observation vectors of shape ``(M, N)``. Each row represents
      a single observation. If ``obs`` is one-dimensional, then each entry is
      treated as a length-1 observation.
    code_book: array of codes with shape ``(K, N)``. Each row represents a single
      code vector. If ``code_book`` is one-dimensional, then each entry is treated
      as a length-1 code.
    check_finite: unused in JAX

  Returns:
    A tuple of arrays ``(code, dist)``

    - ``code`` is an integer array of shape ``(M,)`` containing indices ``0 <= i < K``
      of the closest entry in ``code_book`` for the given entry in ``obs``.
    - ``dist`` is a float array of shape ``(M,)`` containing the euclidean
      distance between each observation and the nearest code.

  Examples:
    >>> obs = jnp.array([[1.1, 2.1, 3.1],
    ...                  [5.9, 4.8, 6.2]])
    >>> code_book = jnp.array([[1., 2., 3.],
    ...                        [2., 3., 4.],
    ...                        [3., 4., 5.],
    ...                        [4., 5., 6.]])
    >>> codes, distances = jax.scipy.cluster.vq.vq(obs, code_book)
    >>> print(codes)
    [0 3]
    >>> print(distances)
    [0.17320499 1.9209373 ]
  zscipy.cluster.vq.vqz3Observation and code_book should have the same rank   ).N   z,ndim different than 1 or 2 are not supportedc                >    t        j                  | d    z
  d      S )Naxis)
jnp_linalgnorm)obcb_arrs    T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/scipy/cluster/vq.py<lambda>zvq.<locals>.<lambda>H   s    Z__RX->RH     r   r   )
r   r   ndim
ValueErrorr   vmapjnpargminoperatorgetitem)obs	code_bookcheck_finiteobs_arrdistcodedist_minr   s          @r   vqr'      s    L 'i8*3	:/'6\\V[[ LMM\\Q	*F9,=ogv\\QEFF	IH	I'	R$	Dr	"$'SXXh&&'d3(	xr   )T)r    r
   r!   r
   r"   boolreturnztuple[Array, Array])
__future__r   r   jax._srcr   r   r   jax._src.numpyr   r   jax._src.numpy.utilr   r   jax._src.typingr	   r
   r'    r   r   <module>r0      s!    #   ! / G ,2r   