
    Vpf                    \    d dl mZ d dl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dS )    )annotationsN)vmap)check_arraylikepromote_dtypes_inexact)Array	ArrayLikeTobsr   	code_bookcheck_finiteboolreturntuple[Array, Array]c                   ~t          d| |           t          | |          \  }|j        j        k    rt          d          |j        dk    r|d         d         c}|j        dk    rt          d           t	          fd          |          }t          j        |d	          } t	          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                X    t           j                            | d          z
  d          S )Naxis)jnplinalgnorm)obcb_arrs    Y/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/scipy/cluster/vq.py<lambda>zvq.<locals>.<lambda>G   s"    DF):DD     r   r   )	r   r   ndim
ValueErrorr   r   argminoperatorgetitem)r	   r
   r   obs_arrdistcodedist_minr   s          @r   vqr'      s    L 'i888*3	::/'6\V[  LMMM\Q	*F9,=ogv\QEFFF	EDDDD	E	Eg	N	N$	Dr	"	"	"$#T("##D$//(	xr   )T)r	   r   r
   r   r   r   r   r   )
__future__r   r!   jaxr   	jax.numpynumpyr   jax._src.numpy.utilr   r   jax._src.typingr   r   r'    r   r   <module>r/      s    # " " " " "              G G G G G G G G , , , , , , , ,2 2 2 2 2 2 2r   