
    vhG                        d Z ddlmZ ddlZddlZddlmZmZ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 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+  e*d      Z,e,	 	 	 	 	 	 dPde%de%de-dz  ddde.dz  de/de/de&dz  de$fd        Z0 eed!"      	 	 dPde-dz  fd#       Z1d$ Z2e, eed%"      	 	 dQd&e%de%de-dz  de.ejf                  z  dz  de&dz  de$fd'              Z4d( Z5e, eed)"      	 dRdd+d&e%de%d,e%de-dz  d-e/de.dz  de$fd.              Z6d/ede/fd0Z7d/ede/fd1Z8d2 Z9d3 Z:	 dSd&e$d/ede.dz  de$dz  fd4Z;	 	 dTd5Z<d6 Z= G d7 d8e      Z>d9 Z?d: Z@d; ZAd< ZB	 dRd=ee-   d/ee   d>e/de>fd?ZCd@ ZDdA ZEdB ZFdC ZGdD ZHdE ZIdF ZJdG ZKdH ZLe,d*dId&e%dJe%dKe%d-e/de$f
dL       ZMe,	 dSd*dIde%dMe%dNe%de.dz  d-e/de$fdO       ZNy)UzIndexing code for jax.numpy.    )partialN)Any
NamedTupleSequence)lax)array)config)core)dispatch)dtypes)errors)jit)einsum)error)	lax_numpy)ufuncs)util)	auto_axes)tree_flatten)Array	ArrayLikeStaticScalar)canonicalize_axissafe_zip
set_moduletuple_updatez	jax.numpyaindicesaxisoutmodeunique_indicesindices_are_sorted
fill_valuereturnc           
      V    t        | ||dnt        j                  |      |||||      S )aY  Take elements from an array.

  JAX implementation of :func:`numpy.take`, implemented in terms of
  :func:`jax.lax.gather`. JAX's behavior differs from NumPy in the case
  of out-of-bound indices; see the ``mode`` parameter below.

  Args:
    a: array from which to take values.
    indices: N-dimensional array of integer indices of values to take from the array.
    axis: the axis along which to take values. If not specified, the array will
      be flattened before indexing is applied.
    mode: Out-of-bounds indexing mode, either ``"fill"`` or ``"clip"``. The default
      ``mode="fill"`` returns invalid values (e.g. NaN) for out-of bounds indices;
      the ``fill_value`` argument gives control over this value. For more discussion
      of ``mode`` options, see :attr:`jax.numpy.ndarray.at`.
    fill_value: The fill value to return for out-of-bounds slices when mode is 'fill'.
      Ignored otherwise. Defaults to NaN for inexact types, the largest negative value for
      signed types, the largest positive value for unsigned types, and True for booleans.
    unique_indices: If True, the implementation will assume that the indices are unique,
      which can result in more efficient execution on some backends. If set to True and
      indices are not unique, the output is undefined.
    indices_are_sorted : If True, the implementation will assume that the indices are
      sorted in ascending order, which can lead to more efficient execution on some
      backends. If set to True and indices are not sorted, the output is undefined.

  Returns:
    Array of values extracted from ``a``.

  See also:
    - :attr:`jax.numpy.ndarray.at`: take values via indexing syntax.
    - :func:`jax.numpy.take_along_axis`: take values along an axis

  Examples:
    >>> x = jnp.array([[1., 2., 3.],
    ...                [4., 5., 6.]])
    >>> indices = jnp.array([2, 0])

    Passing no axis results in indexing into the flattened array:

    >>> jnp.take(x, indices)
    Array([3., 1.], dtype=float32)
    >>> x.ravel()[indices]  # equivalent indexing syntax
    Array([3., 1.], dtype=float32)

    Passing an axis results ind applying the index to every subarray along the axis:

    >>> jnp.take(x, indices, axis=1)
    Array([[3., 1.],
           [6., 4.]], dtype=float32)
    >>> x[:, indices]  # equivalent indexing syntax
    Array([[3., 1.],
           [6., 4.]], dtype=float32)

    Out-of-bound indices fill with invalid values. For float inputs, this is `NaN`:

    >>> jnp.take(x, indices, axis=0)
    Array([[nan, nan, nan],
           [ 1.,  2.,  3.]], dtype=float32)
    >>> x.at[indices].get(mode='fill', fill_value=jnp.nan)  # equivalent indexing syntax
    Array([[nan, nan, nan],
           [ 1.,  2.,  3.]], dtype=float32)

    This default out-of-bound behavior can be adjusted using the ``mode`` parameter, for
    example, we can instead clip to the last valid value:

    >>> jnp.take(x, indices, axis=0, mode='clip')
    Array([[4., 5., 6.],
           [1., 2., 3.]], dtype=float32)
    >>> x.at[indices].get(mode='clip')  # equivalent indexing syntax
    Array([[4., 5., 6.],
           [1., 2., 3.]], dtype=float32)
  N)r"   r#   r$   )_takeoperatorindex)r   r   r   r    r!   r"   r#   r$   s           R/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/numpy/indexing.pytaker+   /   s4    f 
q'4<4X^^D5I3NGY$
& &    )r   r!   r"   r#   r$   )static_argnamesc                    |t        d      t        j                  d| |      \  } }|| j                         } d}nt	        |t        j                  |             }||dk(  rEt        j                  j                  }	t        j                  |dk  || j                  |   z   |      }n|dk(  rt        d      |dk(  rRt        j                  |t        j                  || j                  |               }t        j                  j                   }	n/|dk(  rt        j                  j"                  }	nt%        d	| d
      t'        t        j                  |            }
t)        t        j                  |             }||   dk(  r|j*                  dk7  rt-        d      | S |j*                  dk(  r<|d | t)        |j                        z   ||dz   d  z   }t        j.                  | d|      S d||<   t        j0                  t3        t)        t5        |            t)        t5        ||
z   t'        | j                        |
z   dz
              z         |f|f      }t        j6                  | |d   |t3        |      |	|||      S )Nz0The 'out' argument to jnp.take is not supported.r+   r   fillraisez.The 'raise' mode to jnp.take is not supported.wrapclipzInvalid mode 'z' for np.takez4Cannot do a non-empty jnp.take() from an empty axis.   shapeoffset_dimscollapsed_slice_dimsstart_index_map).N)dimension_numbersslice_sizesr!   r"   r#   r$   )NotImplementedErrorr   ensure_arraylikeravelr   npndimr   GatherScatterModeFILL_OR_DROP_wherer5   r   modlax_internal_constPROMISE_IN_BOUNDSCLIP
ValueErrorlenlistsize
IndexError	full_likeGatherDimensionNumberstuplerangegather)r   r   r   r    r!   r"   r#   r$   axis_idxgather_mode
index_dimsr;   	out_shapednumss                 r*   r'   r'      sG    	_
P
QQ$$VQ8*!W	\		AH rwwqz2H	\TV^''44Kkk'A+w1B'BGLGw
N
OOv~jj,"5"5gqwwx?P"QRG''99Kv~'',,K
~dV=9
::288G$%*RXXa[!+a||qMNNH\\QYh'$w}}*==X\]+,I==AY//+h

$
$
5?
5J&AGGz(AA(EFGHI #K!% 
Awy)U %k 2$^'9j
R Rr,   c                 "   t        j                  t        j                  | d      t        j                        r| S t        j                  |      rt        j                  | |      }n>t        j                  t        j                  |      t        j                  | d            }t        | t        t        j                  f      r| dk  rt        j                  | |      S | S t        j                   | dk  t        j                  | |      |       S )zCNormalizes an index value in the range [-N, N) to the range [0, N).Tcanonicalizer   )r   
issubdtypedtyper?   unsignedintegerr
   is_constant_dimrE   rF   r   convert_element_typedimension_as_value
isinstanceintintegeraddselect)r)   	axis_sizeaxis_size_vals      r*   _normalize_indexrh      s    v||E=r?Q?QRL	)$ ''y9M,,T-D-DY-O-3\\%d-SUMRZZ(),1AI3775-(@5@::eai!>FFr,   )r   r!   r$   arrc           	      
  # t        j                  d| |      \  }}t        j                  |      }t	        j
                  |      }t        j                  |t        j                        st        d|       |Ft	        j                  |      dk7  rd}t        |j                  |            |j                         }d}|j                  }	|	t	        j                  |      k7  r:d}t        |j                  t	        j                  |      |j                              t        ||	      ##fd}
t        d |j
                  D              }t	        j                  |rd	nd
      }t        j                   ||      }|j
                  #   } |
|j
                  d      }t        j"                  ||      }|dk(  r!t        j$                  |d|j                        S |dk(  rddlm} t+        ||      }|j-                  ||t        j.                        }|j                  dk(  r#t1        j0                  d|||j                        S #t3        t4        j6                        dz
  kD  rt        d      dj9                  t;        #      D cg c]  }t4        j6                  |    c}      }|dz   |z   dz   |z   dz   }t1        j0                  |||t        j<                  j>                  |j                        S tA        |      D cg c]#  \  }}|#k(  stC        jD                  |d      r"|% }}}tG        t	        jH                  |      |         dz   }g }g }g }g }g }g }g }g }d}d} t;        |	      D ]X  }|#k(  rt|dk7  rt+        ||      }|jK                  t        jL                  ||             |jK                  d       |jK                  |       |jK                  |       |dz  }| dz  } }tC        jD                  ||   d      r+|jK                  |       |jK                  ||          |dz  }tC        jD                  ||   d      r|jK                  |       | dz  } tC        jD                  ||   d      r|jK                  d       n|jK                  d       |jK                  |       |jK                  |        |dz  }| dz  } [ t        jN                  ||      }t        jP                  ||       }!t        jR                  tG        |      tG        |      tG        |      tG        |      tG        |            }"t        jT                  ||!|"tG        |      |d|      S ||      S c c}w c c}}w )a
  Take elements from an array.

  JAX implementation of :func:`numpy.take_along_axis`, implemented in
  terms of :func:`jax.lax.gather`. JAX's behavior differs from NumPy
  in the case of out-of-bound indices; see the ``mode`` parameter below.

  Args:
    a: array from which to take values.
    indices: array of integer indices. If ``axis`` is ``None``, must be one-dimensional.
      If ``axis`` is not None, must have ``a.ndim == indices.ndim``, and ``a`` must be
      broadcast-compatible with ``indices`` along dimensions other than ``axis``.
    axis: the axis along which to take values. If not specified, the array will
      be flattened before indexing is applied.
    mode: Out-of-bounds indexing mode, either ``"fill"`` or ``"clip"``. The default
      ``mode="fill"`` returns invalid values (e.g. NaN) for out-of bounds indices.
      For more discussion of ``mode`` options, see :attr:`jax.numpy.ndarray.at`.

  Returns:
    Array of values extracted from ``a``.

  See also:
    - :attr:`jax.numpy.ndarray.at`: take values via indexing syntax.
    - :func:`jax.numpy.take`: take the same indices along every axis slice.

  Examples:
    >>> x = jnp.array([[1., 2., 3.],
    ...                [4., 5., 6.]])
    >>> indices = jnp.array([[0, 2],
    ...                      [1, 0]])
    >>> jnp.take_along_axis(x, indices, axis=1)
    Array([[1., 3.],
           [5., 4.]], dtype=float32)
    >>> x[jnp.arange(2)[:, None], indices]  # equivalent via indexing syntax
    Array([[1., 3.],
           [5., 4.]], dtype=float32)

    Out-of-bound indices fill with invalid values. For float inputs, this is `NaN`:

    >>> indices = jnp.array([[1, 0, 2]])
    >>> jnp.take_along_axis(x, indices, axis=0)
    Array([[ 4.,  2., nan]], dtype=float32)
    >>> x.at[indices, jnp.arange(3)].get(
    ...     mode='fill', fill_value=jnp.nan)  # equivalent via indexing syntax
    Array([[ 4.,  2., nan]], dtype=float32)

    ``take_along_axis`` is helpful for extracting values from multi-dimensional
    argsorts and arg reductions. For, here we compute :func:`~jax.numpy.argsort`
    indices along an axis, and use ``take_along_axis`` to construct the sorted
    array:

    >>> x = jnp.array([[5, 3, 4],
    ...                [2, 7, 6]])
    >>> indices = jnp.argsort(x, axis=1)
    >>> indices
    Array([[1, 2, 0],
           [0, 2, 1]], dtype=int32)
    >>> jnp.take_along_axis(x, indices, axis=1)
    Array([[3, 4, 5],
           [2, 6, 7]], dtype=int32)

    Similarly, we can use :func:`~jax.numpy.argmin` with ``keepdims=True`` and
    use ``take_along_axis`` to extract the minimum value:

    >>> idx = jnp.argmin(x, axis=1, keepdims=True)
    >>> idx
    Array([[1],
           [0]], dtype=int32)
    >>> jnp.take_along_axis(x, idx, axis=1)
    Array([[3],
           [2]], dtype=int32)
  take_along_axisz5take_along_axis indices must be of integer type, got r3   z=take_along_axis indices must be 1D if axis=None, got shape {}r   zBindices and arr must have the same number of dimensions; {} vs. {}c                 :    t        |       }||<   t        |      S N)rK   rP   )tupvallstaxis_ints      r*   replacez take_along_axis.<locals>.replace-  s    
s)CCM:r,   c              3   V   K   | ]!  }t        j                  |       xs |d k\   # ywl        Nr
   r^   .0ds     r*   	<genexpr>z"take_along_axis.<locals>.<genexpr>2  s*     W!D0033EqW~EW   ')int64int32one_hot)nnr\   z...b,b->...)preferred_element_type   zCOne Hot indexing is only supported for up to 50 leading dimensions. zy...z,zz...->zy...)	precisionr   )r3   promise_in_bounds)	dimension)r7   r8   r9   operand_batching_dimsstart_indices_batching_dimsr/   )r!   r$   )+r   r=   r   r\   r?   r5   r[   rc   	TypeErrorr@   rI   formatr>   r   anyr   r_   broadcast_shapesfulljaxr~   rh   r}   bool_r   rJ   stringascii_lettersjoinrQ   	PrecisionHIGHEST	enumerater
   definitely_equalrP   r   appendreshapesqueezeconcatenaterO   rR   )$ri   r   r   r!   r$   r   index_dtype	idx_shapemsgrankrr   use_64bit_indexrf   	arr_shaperV   r~   hotilabelseqidxrU   gather_index_shapegather_indicesr;   r7   r9   r8   r   r   dims_to_squeezenew_ijgather_indices_arrrW   rq   s$                                      @r*   rk   rk      s   ` $$%6WE*!WW%+hhw)			;

	3
K"m% & &	\	www1Kcszz),--		AD	
$	RWWW
NC
SZZ 0!&&9
::tT*(
 WqwwWW/OA+$$Wk:'ggh)aggq!)""9i8)!^88Iq!''**	Yw	2G
**Wirxx*
8Cvv{]]=#qQQ#f**+a//
O  WWuXG!f**1-GHF	(	V	#h	.	7&	@B==
	--'' ww  !*) 4kfaXTMbMbcfhiMjk*kRXXi0<=D.++/ "
 /
%!; aH}	$	$"7I6CKK1CDEU#!!%(qje1fa			y|Q	/ 1&qje			y|Q	/ Q1fa 
		y|Q	/11""5)!((+qje1fa?D 
kk!_%!~C

$
$k"34/* 56 %&A BD% 
A)5%2D#'<j
R R59j
R RK H ls   U.#U3&U3c                 l    |dk  r|t        |       z  }t        t        j                  | d      ||      S )Nr   T)sparse)rJ   r   r   r   )r5   r   r   s      r*   _make_along_axis_idxr     s3    	AXCJD	i''d;T7	KKr,   )r   inplacer!   Tr!   valuesr   c                V   |rt        d      t        j                  d| ||      \  } }}|}| j                  }|| j	                         } d}| j
                  |j
                  k(  s&t        d| j
                  d|j
                  d      	 t        j                  ||j                        }t        | j                  ||      }| j                  |   j                  ||	      }	||	j                  |      }	|	S # t         $ r' t        d|j                  d|j                  d      w xY w)
a  Put values into the destination array by matching 1d index and data slices.

  JAX implementation of :func:`numpy.put_along_axis`.

  The semantics of :func:`numpy.put_along_axis` are to modify arrays in-place, which
  is not possible for JAX's immutable arrays. The JAX version returns a modified
  copy of the input, and adds the ``inplace`` parameter which must be set to
  `False`` by the user as a reminder of this API difference.

  Args:
    arr: array into which values will be put.
    indices: array of indices at which to put values.
    values: array of values to put into the array.
    axis: the axis along which to put values. If not specified, the array will
      be flattened before indexing is applied.
    inplace: must be set to False to indicate that the input is not modified
      in-place, but rather a modified copy is returned.
    mode: Out-of-bounds indexing mode. For more discussion of ``mode`` options,
      see :attr:`jax.numpy.ndarray.at`.

  Returns:
    A copy of ``a`` with specified entries updated.

  See Also:
    - :func:`jax.numpy.put`: put elements into an array at given indices.
    - :func:`jax.numpy.place`: place elements into an array via boolean mask.
    - :func:`jax.numpy.ndarray.at`: array updates using NumPy-style indexing.
    - :func:`jax.numpy.take`: extract values from an array at given indices.
    - :func:`jax.numpy.take_along_axis`: extract values from an array along an axis.

  Examples:
    >>> from jax import numpy as jnp
    >>> a = jnp.array([[10, 30, 20], [60, 40, 50]])
    >>> i = jnp.argmax(a, axis=1, keepdims=True)
    >>> print(i)
    [[1]
     [0]]
    >>> b = jnp.put_along_axis(a, i, 99, axis=1, inplace=False)
    >>> print(b)
    [[10 99 20]
     [99 40 50]]
  zjax.numpy.put_along_axis cannot modify arrays in-place, because JAX arraysare immutable. Pass inplace=False to instead return an updated array.put_along_axisr   zOput_along_axis arguments 'arr' and 'indices' must have same ndim. Got arr.ndim=z and indices.ndim=.zVput_along_axis argument 'values' must be broadcastable to 'indices'. Got values.shape=z and indices.shape=r   )rI   r   r=   r5   r>   r@   _broadcast_tor   atsetr   )
ri   r   r   r   r   r!   original_axisoriginal_arr_shaper   results
             r*   r   r     s@   j 
NO O ../?gvV#w-yy	\
))+CD	W\\	!
	&q* 
6F 	SYY6#66#;??6?-&^^./F	- 
 
	+W]],A/ s    C8 80D(r   c                     t        | t        t        j                  f      xr! t        | t        t        j
                  f       S rm   )ra   rb   r?   rc   boolr   r   s    r*   _is_integer_indexr     s0    	C#rzz*	+	UJsT288DT4U0UUr,   c                     t        | t              xrO | j                  | j                  cxu xr d u nc xr+ t        | j                  t
              xr | j                  dk(  S )N)ra   slicestartstopsteprb   r   s    r*   _is_simple_reverse_slicer     sN    
S%
  7
))sxx
'4
'7
SXXs
#7(+B8r,   c                 
   |dk(  ryt        |       r| | cxk  xr |k  S c S 	 t        j                  |       t        j                  | d      }}|dk(  r(t        j
                  |t        j                        r|dv S y#  Y yxY w)Nr   FTrY    )Npromise_inbounds)r   r?   r5   r   r\   r[   rc   )r   rL   r!   r5   r\   s        r*   !_is_valid_integer_index_for_slicer     s    	QYs5C$88C=&,,s"F5E b[R]]5"**5---	s   ,A> >Bc                 .   t        | t              xr | j                  d u xs t        | j                        xr] | j                  d u xs t        | j                        xr6 | j
                  d u xs& t        | j
                        xr | j
                  dk(  S Nr3   )ra   r   r   r   r   r   r   s    r*   _is_contiguous_slicer     s    
S%
  P99< 1#)) <P88t:0:P 88tN 1#(( ; MAQr,   c           
      D	    t        t              rnft        d  j                  D              sy t	        d D              ry t	        d D              ry t	        d D              rt         j                        t               j                  kD  ry t              D ch c]  \  }}t        |      s| c}}t        t         j                              D ch c]  \  }\  }}t        |||      r| }}}}t              D ch c]  \  }}t        |      s| }}}t         t        j                        xr  t        j                    j"                         }	t	         fd|D              }
|	r|s|
ry t              t        |      z   t        |      z   t              k7  ry rAt%        j&                   t                     t        fdt              D              |z  }|s|
s S  j                  t              z
  t)        d       fz  z  g }g }g }t+         j                        D ]2  \  }}t        |t(              rg|j-                  |      \  }}}|dk(  sJ |j/                  |       |j/                  t1        d	||z
               |j/                  |d	k  xs |d	k         ~t3        j4                  t7        j8                  |      t2        j:                        sJ t3        j                  |      d
k(  sJ |j/                  |       |j/                  d       |j/                  t        |t<        t2        j:                  f       xs t?        |d	k               5 t        d |D              rR|D cg c]  }t=        |       }}t        ||      D cg c]
  \  }}||z    }}}t%        j(                   ||       n~t        |      dkD  rtA        jB                  | }tE        jF                   j                  |||       tI        t$        jJ                  ||      }| tM        ||       |       n	 | |       |rt%        jN                   t        |              S c c}}w c c}}}w c c}}w c c}w c c}}w )Nc              3   <   K   | ]  }t        |t                y wrm   )ra   rb   rw   r   s     r*   ry   z4_attempt_rewriting_take_via_slice.<locals>.<genexpr>  s     3AZ33   c              3   $   K   | ]  }|d u  
 y wrm   r   r   s     r*   ry   z4_attempt_rewriting_take_via_slice.<locals>.<genexpr>  s      qd s   c              3      K   | ]Q  }t        |t              r?|j                  |j                  |j                  fD ]  }t        j                  |        S y wrm   )ra   r   r   r   r   r
   is_symbolic_dimrw   r   elts      r*   ry   z4_attempt_rewriting_take_via_slice.<locals>.<genexpr>  sR      	/Au-WWaffaff-	/ 
		c	" 	/	" 	/s   AAc              3   ,   K   | ]  }|t         u   y wrm   Ellipsisr   s     r*   ry   z4_attempt_rewriting_take_via_slice.<locals>.<genexpr>  s     $1h$   r   c              3      K   | ]8  }|   j                  j                  |         d j                  |   dfk7   : yw)r   r3   N)r   r5   )rw   r   ri   r   s     r*   ry   z4_attempt_rewriting_take_via_slice.<locals>.<genexpr>/  sB      7  q6>>#))A,7Asyy|Q;OO 7s   >Ac              3   F   K   | ]  \  }}|v rt        d       n|  y wrm   )r   )rw   r   indsimple_revss      r*   ry   z4_attempt_rewriting_take_via_slice.<locals>.<genexpr>9  s-      .As  !K/dS8 .s   !r3   r   r   c              3   j   K   | ]+  }t        |t        t        j                  f      xr |d k\   - ywr   N)ra   rb   r?   rc   r   s     r*   ry   z4_attempt_rewriting_take_via_slice.<locals>.<genexpr>U  s,     LARZZ(	)	4a1f	4Ls   13)start_indiceslimit_indices)r;   allow_negative_indicesout_sharding)(ra   rP   allr5   r   _canonicalize_tuple_indexr@   rJ   r   r   zipr   r   r   	ArrayImplr   is_single_device_shardingshardingr   revr   r   r   r   maxr?   r[   r   r\   rc   rb   r   r   promote_dtypes	jnp_error%_check_precondition_oob_dynamic_slicer   dynamic_slicer   r   )ri   r   r!   r   r   r   rL   int_indicescontiguous_slices
is_shardedhas_partial_slicesr   r;   r   r   r   r   int_start_indicessint_limit_indicesinternal_dsr   s   ``                   @r*   !_attempt_rewriting_take_via_slicer     s6    #u%C6#	33	3 C   	/	/ / $$$ $CHH#
6CX!*3Qvq#3KC3PQ+)23sCII3F)G H H~q+35c4F  H+ H'0~SVQ9Mc9RqSS 30 E66s||DD  7$57 7[$6K((3/@+AASXM
''#u[)
*C
 .'n. .C$
+J#((SX
%+	//#')-+')C+ Cic4#u++d+eT4QYY5!Qu-.##EAI$9:]]6<<,bjj999XXc]b   3##sBJJ/0
0
ADqMCC  	LmLL)67AQ77+./@++NO41aQOO
)),<MOC
 =A))=9m33		=+/E #++1GIK=Ik=c=Qc]+c
++c5-
.C	*M RHS^ 8Os*   ,R R+R
R-RR8Rc           	         t        | |||      }||S t        j                  j                  r| j                  dkD  rt        j                  |      }t        |t
        j                        r|j                  dk(  rt        j                  |j                  t        j                        rct        j                  |j                  t        j                        s5t        | j                  d   t               rt#        j$                  | |d      S t'        || j                        \  }	}
}t)        t*        |	|
||||      }| t-        ||      | |      S  || |      S #  Y SxY w)Nr   r   F)keepdims)treedef
static_idxr#   r"   r!   r$   r   )r   r	   dynamic_shapesvaluer@   r
   get_avalra   DShapedArrayr5   r   r[   r\   r?   rc   r   rb   r   dynamic_index_in_dimsplit_index_for_jitr   _gatherr   )ri   r   r#   r"   r!   r$   r   r   avalr   r   dynamic_idxinternal_gathers                r*   rewriting_taker  m  s+    -S#t\J&M   SXX\c" T4,,
-$**2B


DJJ


3

FLL9
SYYq\3
'''S5AA%8cii%H"':{w:+NJ(/ 9_< K) )	k	**! Ds   E Ec          
         t        |||      }t        t        j                  |       |      }	t	        j
                  | j                  |	j                         | }
|dt        j                  d |d       t        j                  |      dk7  rt        d      t        |t        j                        r|j                         }|	j                  r t        j                   |
|	j                        }
t        j"                  |	j$                        r"t        j&                  |
d|	j$                        S t        j"                  |	j                  j                        sWt        j(                  |
|	j                  |	j*                  |	j,                  |xs |	j.                  |xs |	j0                  ||      }
|	j2                  r t        j4                  |
|	j2                        }
t        j                   |
|	j6                        S )Nz$fill_value argument to indexed get()r   z5fill_value argument to indexed get() must be a scalarr4   )r"   r#   r!   r$   ) merge_static_and_dynamic_indicesindex_to_gatherr?   r5   r   _check_precondition_oob_gatherr   r
   concrete_or_errorr@   rI   ra   ndarrayitemscalar_bool_dimsr   expand_dimsis_empty_shapeslice_shaperN   rR   rW   gather_slice_shaper"   r#   reversed_y_dimsr   newaxis_dims)ri   r  r   r   r#   r"   r!   r$   r   indexerys              r*   r  r    s   (*kJ#BHHSM3/'**399g6L6LM	!4AC	wwzaNOO*bjj)??$j7334A 
,,-==AW%8%899 
		W3399	:

	7!!7=='2L2L%?)?)?-K1K1Kj		*A 7**+A	G00	11r,   c                       e Zd ZU ee   ed<   ee   ed<   eed<   ej                  ed<   e	ed<   e	ed<   ee   ed<   ee   ed<   ee   ed	<   y
)_Indexerr  r  r   rW   r"   r#   r  r  r  N)
__name__
__module____qualname__r   rb   __annotations__r   r   rO   r   r   r,   r*   r  r    sZ    }sm#		#	##
  C=   SM!r,   r  c                    t        |       } t        d | D              rt        d|       t        | |      } t	        |       \  }}dgt        |      z  }dgt        |      z  }t        |      D ]O  \  }}|t        u r|||<   t        |t              r'|j                  |j                  |j                  f||<   K|||<   Q |t        |      |fS )zmSplits indices into necessarily-static and dynamic parts.

  Used to pass indices into `jit`-ted function.
  c              3   <   K   | ]  }t        |t                y wrm   )ra   strr   s     r*   ry   z&split_index_for_jit.<locals>.<genexpr>  s     )As	)r   z.JAX does not support string indexing; got idx=N)"eliminate_deprecated_list_indexingr   r   _expand_bool_indicesr   rJ   r   r   ra   r   r   r   r   rP   )r   r5   leavesr   dynamicstaticr   xs           r*   r  r    s     	+3/#)S))
EG
HH 	S%(# %/&'FS[ '6CK& daH}fQi	Au	77AFFAFF+fQigaj 
%-	((r,   c           	         g }t        ||      D ]`  \  }}||j                  |       t        |t              r&|j                  t	        |d   |d   |d                P|j                  |       b | j                  |      S )z:Recombines indices that were split by split_index_for_jit.r   r3   r   )r   r   ra   rP   r   	unflatten)r   r   r  r   r   rx   s         r*   r	  r	    sz    
#*k* da}	jjm	Au		jjqtQqT1Q4()	jjm 
		3	r,   c                 |    | j                    xr. t        j                  | j                  t        j
                        S rm   )r5   r   r[   r\   r?   rc   )r  s    r*   _intr*    s(    ZZ	EF--djj"**EEr,   c                 <    	 t        j                  |       S #  Y y xY wrm   )r
   r   r&  s    r*   _aval_or_noner-     s     ==s    x_shapenormalize_indicesc                 T   01 t        d |D              }|D cg c]  }t        |       }}t        t        ||            D cg c]3  \  }\  }}|)t	        j
                  |t        j                        r|||f5 }}}}|r;|d   \  }}}d}t        |j                  |j                  j                  ||            t        j                  |D 	cg c]M  }	t        |	t        t        j                  t         t        j"                  f      xs t%        j&                  |	      O c}	      \  }
t        j(                  t        j*                  |
      dk(        }t-        t/               |      }t        |      D cg c]  \  }}t        |t0              s| }}}|rJt        d |D              }t3                t5        |      D ]  } j7                  |d        t                d }g }d }t9        |      rMt        |      D cg c]  \  }}|	||f }}}d t        |      D        }|r fd|D        }t        | \  }}}d}d}d}g }g }g }t;        d  D              xr t<        j>                  j@                  }|rt        j                  d	      nt        j                  d
      0g }g }g }g }g }g } t        |      D ]  \  }!}||r|!|d   k(  s|s|!dk(  rtC        jD                  | }"|"d   jF                  }#t/        |#      }$t/        |      1|jI                  01fd|"D               ||#z  }|J |jI                  |       |jI                  |       |jI                  |#       ||$z  }||$z  }|!|v r|dz  }| jK                  d       t        |      }%t        |%tL        jN                        rtQ        |%      rtM        jR                   |   d      rtU        d| d      |rtW        | |         n|}tY        jZ                  |0      }&|jK                  |&t/        |      f       |jK                  |       | jK                  d       |jK                  |       |dz  }|)|jK                  d       |jK                  |       |dz  }t        |t\              rt)        d |j^                  |j`                  |jb                  fD              s5d|j^                   d|j`                   d|jb                   d}tU        |      tM        jd                  | |         \  }'}(})|jK                  |)       tM        jR                  |(d      r}tM        jR                  |) |         sA|jK                  tY        jZ                  |'0      t/        |      f       |jK                  |       | jK                  |)       |jK                  |       nt%        jf                  |'0      t%        jf                  |(0      tY        jh                  0|)      z  z   }*|(dk  r(|jK                  |       tY        jj                  |*d      }*| jK                  d       |jK                  |*t/        |      f       |jK                  |       |jK                  |)       |jK                  |       |dz  }|dz  }|dz  }|%t	        j
                  |%j                  t        j                        s`t	        j
                  |%j                  t        jl                        s2d}t        |j                  |%j                  j                  |!|            tU        d|! d|       t/        |      dk(  rt        jn                  d0      }+nt/        |      dk(  r*|d   \  },}-tY        jp                  |,|,jr                  f      }+nzt/        |      }.|jK                  d       tY        jt                  |D ,cg c]<  \  },}tY        jv                  |,|t        ty        |||,jr                  z                     > c}},|.      }+tY        jz                  t        |      t        t5        |            t        |            }/t}        |t        |      | ||/|+|d u |d u |	      S c c}w c c}}}w c c}	w c c}}w c c}}w c c}},w )Nc              3   ~   K   | ]5  }t        |t              rt        j                  ||rd nt              n| 7 y w)Nr   )ra   r   r   asarrayrb   r   s     r*   ry   z"index_to_gather.<locals>.<genexpr>	  s:      >45Ax( <./0 >s   ;=r   zdIndexer must have integer or boolean type, got indexer with type {} at position {}, indexer value {}r3   c              3   |   K   | ]4  }t        |t              rt        j                  t	        |            n| 6 y wrm   )ra   r   r?   arangerb   r   s     r*   ry   z"index_to_gather.<locals>.<genexpr>%  s+     MAZ4%8		#a&!a?Ms   :<c              3      K   | ]\  \  }\  }}t        j                  |      s%t        |t        t        t
        j                  f      rt        j                  |      ||f ^ y wrm   )r   isscalarra   r   r   r?   r  r2  )rw   r   r   es       r*   ry   z"index_to_gather.<locals>.<genexpr>9  sV      P'0q&1a			A	*Q5"**0M"N Q"Ps   A"A$c              3   J   K   | ]  \  }}}t        ||         ||f  y wrm   )rh   )rw   r7  r   r   r.  s       r*   ry   z"index_to_gather.<locals>.<genexpr>=  s1      7#Aq! *!WQZ8!Q? 7s    #c              3   V   K   | ]!  }t        j                  |       xs |d k\   # ywrt   ru   rv   s     r*   ry   z"index_to_gather.<locals>.<genexpr>K  s*     G!D  ##5qW~5Grz   r{   r|   c              3   N   K   | ]  }t        j                  |      f  y wrm   )r   r_   )rw   r   r   	start_dims     r*   ry   z"index_to_gather.<locals>.<genexpr>p  s+       ##A{3Y
?s   "%z index is out of bounds for axis z with size 0c              3   2   K   | ]  }t        |        y wrm   )._is_slice_element_none_or_constant_or_symbolic)rw   r   s     r*   ry   z"index_to_gather.<locals>.<genexpr>  s      7 @D 7   zhArray slice indices must have static start/stop/step to be used with NumPy indexing syntax. Found slice(z, z). To index a statically sized array at a dynamic position, try lax.dynamic_slice/dynamic_update_slice (JAX does not support dynamically sized arrays within JIT compiled functions).r   )r   )
dimensionszEIndexing mode not yet supported. Got unsupported indexer at position z: r6   )	r  r  r  r  rW   r   r"   r#   r  )?rP   r-  r   r   r   r[   r?   inexactr   r   r\   namenonzerora   rb   rc   r   r  r   r6  r   diffr   rJ   r   rK   sortedinsert_is_advanced_int_indexerr   r	   
enable_x64r   r   _broadcast_arraysr5   extendr   r
   ShapedArrayr*  r   rM   rh   r   r_   r   r   r   r   canonicalize_slicer   iotar   r   zerosr  r@   r   broadcast_in_dimrQ   rO   r  )2r.  r   r/  r   abstract_idxro   r  float_indicesr   r7  is_advancedadvanced_axes_are_contiguousnr  advanced_indexesidx_advanced_axesx_advanced_axesrx   idx_no_nonesadvanced_pairsx_axisy_axiscollapsed_y_axisr7   r8   r9   r   r   gather_indices_shaper  r  r  r  idx_posadvanced_index_arrsr5   r@   
abstract_ii_convertedr   r   
slice_sizer   gather_indices_arrayg_last_dimrW   r   r;  s2   `                                               @@r*   r
  r
    sr    	 >9<> 	>#,/0q-"0,08A#c<BX8Y P Pna#t&6+<+<T2::+N sD> P- P  #LAsD;C
CJJtzz37
88 ?BD:; (CUBJJ+OP 6 ) 2 21 56 D E,+!#(<(A!B 	"#g,4# 4=S>$Y41aZPQSWEXQ$Y$Y
MM
MC7mG$% nnQGnG
 ;? &( +//c"'0~Gtq!QFGLGP4=l4KPN 7'57n;>;O8'&& +$&!/ GwGG 
  &5!"((7:K+ -/.$&
 + ,  "/"$cN b7jgq 	$	%'5Fq5I*I)gl 224DE!!$**eZd*+i &  e#(((_-!!/2nf$ ##kf" q!J*d../D4D			wv	2;F8<PQQ2C
1gfo
.a,,Q<k[#.B*CDE!!&)"V$kf	
&!kf	Au	 7WWaffaff57 7ggYb166( ;88 o $ 7 776? KeT:$			tQ	'$$ZA


!9!9%!M #$8 9!; <

 
 
(!!*-+,??5<??4{;chh{T^>__`!8

 
 !1
2GGG5'!!!$w,@(ABCv&##J/##F+!kfkf

   !1!12::>&BSBST^TdTdfhfnfnBo?

:#3#3#8#8'1EFF %%,IRu6 7 7Cb7H 	A&(hht;&G
>a!DAq??1qvvi8'(H"?? ,"
!Q 
12E%1qvv::N4OP," 
 
$
$$ (<!=>O,%
 
|$)#
'#t+'4/%	
' 	'_ 1PD %Z* Hp,"s1   b8bAb<bb
bb Ab$
c                     t        | t        j                  t        f      xr t        j                  |       dk7  xs& t        | t
        t        f      xs | t        u xs | du S )z.Helper for eliminate_deprecated_list_indexing.r   N)ra   r?   r  r   r@   r   r   r   r,  s    r*   _should_unpack_list_indexrg    sW    
QU+
,
@q (He,-((](4i)r,   c                     t        | t              sdt        | t              rQt        | t        t        j
                  t        f      s,t        d | D              rd}t        |      d}t        |      | f} | S )Nc              3   2   K   | ]  }t        |        y wrm   )rg  r   s     r*   ry   z5eliminate_deprecated_list_indexing.<locals>.<genexpr>  s     7a&q)7r>  zUsing a non-tuple sequence for multidimensional indexing is not allowed; use `arr[tuple(seq)]` instead of `arr[seq]`. See https://github.com/jax-ml/jax/issues/4564 for more information.zUsing a non-tuple sequence for multidimensional indexing is not allowed; use `arr[array(seq)]` instead of `arr[seq]`. See https://github.com/jax-ml/jax/issues/4564 for more information.)	ra   rP   r   r   r?   r  r   r   r   )r   r   s     r*   r!  r!    sp     
C	#x C%S9Q)R
 
737	7U cNU cNFc	*r,   c                 8   	 t        j                  |       }t        |t         j                        xr. t        j                  |j                  t        j                        xs( t        | t              xr | xr t        d | D              S # t        $ r d }Y w xY w)Nc              3      K   | ]H  }t        |      xr7 t        j                  t        j                  |      t        j
                         J y wrm   )
_is_scalarr   r[   r\   r?   r   rw   r7  s     r*   ry   z$_is_boolean_index.<locals>.<genexpr>  sD      /G?@ 0:!} 0;Q:0; /Gs   AA)r
   r   r   ra   rJ  r   r[   r\   r?   r   rK   r   )r   r_  s     r*   _is_boolean_indexrn    s    q!J Z!1!1
2
dv7H7HIYIY[][c[c7d G4  GQ G3 /GDE/G ,GH 
 Js   B BBc                 h   g }t        |      }t        d | D              }|dkD  rt        d      |dk(  rt        d | D              }d}d}t        |       D ]  \  }}	 t	        j
                  |      }	t        |      rbt        |t              r*t        j                  |      }t	        j
                  |      }	t	        j                  |      st        j                  |	      t        j                   |      dk(  r|j#                  t%        |             nt        j&                  |      }
t        |      |z   |z
  }|||t        j                   |      z    }t        |
      t        |      k7  rt        d| d|
 d	t        |       d
      t)        d t+        |
|      D              st        d| d|
 d|       |j-                  t        j.                  |             n|j#                  |       |t0        u rt        |      |z
  dz
  }||dz  } t3        |      S # t        $ r d}	Y w xY w)z=Converts concrete bool indexes into advanced integer indexes.c              3   ,   K   | ]  }|t         u   y wrm   r   rm  s     r*   ry   z'_expand_bool_indices.<locals>.<genexpr>  s     0qQ(]0r   r3   z0an index can only have a single ellipsis ('...')c              3   t   K   | ]0  }|,|t         ur$t        |      rt        j                  |      nd 2 y wr   )r   rn  r?   r@   rm  s     r*   ry   z'_expand_bool_indices.<locals>.<genexpr>  s7      =1!8*; $5Q#7RWWQZQ> =s   68r   Nz"too many boolean indices at index z: got mask of shape z, but only z dimensions remain.c              3   .   K   | ]  \  }}|d |fv   ywr   r   )rw   s1s2s      r*   ry   z'_expand_bool_indices.<locals>.<genexpr>-  s     LVR2!R=Ls   z<boolean index did not match shape of indexed array in index z: got z, expected )rJ   sumrM   r   r
   r   r   rn  ra   rK   r   r   is_concreter   NonConcreteBooleanIndexErrorr?   r@   r   r   r5   r   r   rI  wherer   rP   )r   r5   r    
total_dimsnum_ellipsisellipsis_offsetnewaxis_offset
dim_numberr   r_  i_shaper   expected_shapes                r*   r"  r"    s   
#5z*0C00,A
G
HHq = = =J/. ~ mj!==#j 	At	OOA]]1%
a 11*==771:?

47((1+C?*^;uebggaj&89w<3~..?
|K_%Yk#n2E1FFY[ \ \Ls7N/KLLY(\yNCSU V V

288A;	jjmH}E
Z/!3oyn?@ 
s;  js   H""H10H1c                 ~    | yt        j                  |       ry	 t        j                  |       S # t        $ r Y yw xY w)z)Return True if elt is a constant or None.TF)r
   r   rv  r   )r   s    r*   r=  r=  :  sA    [	#tC  	 s   0 	<<c                 p    t        | t              sJ t        d | D              ryt        d | D              S )zGReturns True if idx should trigger int array indexing, False otherwise.c              3      K   | ]j  }|d u xs` |t         u xsV t        |t              xsD t        |      xr7 t	        j
                  t	        j                  |      t        j                         l y wrm   )	r   ra   r   rl  r   r[   r\   r?   rc   rm  s     r*   ry   z+_is_advanced_int_indexer.<locals>.<genexpr>H  sj      	[QR 
d 
Ma8m 
Mz!U'; 
MqMLf//QL
M 	[s   A0A2Fc              3   v   K   | ]1  }|d u xs' |t         u xs t        |t              xs t        |       3 y wrm   )r   ra   r   _is_int_arraylikerm  s     r*   ry   z+_is_advanced_int_indexer.<locals>.<genexpr>K  sG      3)* $Y %!x- %:a+? %!!$% 3s   79)ra   rP   r   r   s    r*   rF  rF  D  sI     
C		 	[VY	[ [	 3.13 
3 3r,   c                    t        | t              xr t        | t               xs[ t        j                  t        | dd      t        j                        xs* t        | t        t        f      xr t        d | D              S )zDReturns True if x is array-like with integer dtype, False otherwise.r\   Nc              3   2   K   | ]  }t        |        y wrm   )r  rm  s     r*   ry   z$_is_int_arraylike.<locals>.<genexpr>R  s     1R12CA2F1Rr>  )ra   rb   r   r   r[   getattrr?   rc   rK   rP   r   r,  s    r*   r  r  N  si    
Q

8Z4%8!8 Swq'48"**ESD%=)Rc1RPQ1R.RTr,   c                     t        j                  |       xs: t        | t         j                  t        f      xr t        j
                  |       dk(  S )z#Checks if a Python or NumPy scalar.r   )r?   r6  ra   r  r   r@   r,  s    r*   rl  rl  T  s=    
++a. 2ZBJJ+>? 1!#q2r,   c           	         t        d |D              }|| kD  r|dk(  rdnd}t        d|  d| d| d      d	 t        |      D        }t        |d
      }|Wt        |d
      &t        dt	        t        t        |             d      t        d
      f| |z
  z  }|d
| |z   ||dz   d
 z   }|S || k  r t        d
      f| |z
  z  }t        |      |z   }|S )zGHelper to remove Ellipsis and add in the implicit trailing slice(None).c              3   ^   K   | ]%  }|d u xs |t         u xs t        |t                ' y wrm   )r   ra   r   rm  s     r*   ry   z,_canonicalize_tuple_index.<locals>.<genexpr>[  s.     e\]Q$Y%V!x-%V:aQUCV Wes   +-r3   r)   r   zToo many indices: z -dimensional array indexed with z	 regular r   c              3   8   K   | ]  \  }}|t         u s|  y wrm   r   r   s      r*   ry   z,_canonicalize_tuple_index.<locals>.<genexpr>a  s     @FAsxa@s   Nz'Multiple ellipses (...) not supported: )	ru  rM   r   nextrK   maptyper   rP   )arr_ndimr   num_dimensions_consumedindex_or_indicesellipsesellipsis_indexcolonss          r*   r   r   Y  s&   eadeex'"9Q">wI

XJ ''(	2B1C1	FG G Ain@($'.Hd'3DT34H3I
KM MDk^x*AABF
o~

'#nq.@.A*B
BC 
* )Dk^x*AABF
*v
C	*r,   )r   maskvalsc                L   t        j                  d| ||      \  }}}|j                         }|rt        d      |j                  |j                  k7  rt        d      |j                  st        d      |j                  s|S t        j                  |j                         |j                  |j                        d   }t        j                  |t        |            }|j                         j                  |   j                  |d      j                  |j                        S )	a  Update array elements based on a mask.

  JAX implementation of :func:`numpy.place`.

  The semantics of :func:`numpy.place` are to modify arrays in-place, which
  is not possible for JAX's immutable arrays. The JAX version returns a modified
  copy of the input, and adds the ``inplace`` parameter which must be set to
  `False`` by the user as a reminder of this API difference.

  Args:
    arr: array into which values will be placed.
    mask: boolean mask with the same size as ``arr``.
    vals: values to be inserted into ``arr`` at the locations indicated
      by mask. If too many values are supplied, they will be truncated.
      If not enough values are supplied, they will be repeated.
    inplace: must be set to False to indicate that the input is not modified
      in-place, but rather a modified copy is returned.

  Returns:
    A copy of ``arr`` with masked values set to entries from `vals`.

  See Also:
    - :func:`jax.numpy.put`: put elements into an array at numerical indices.
    - :func:`jax.numpy.ndarray.at`: array updates using NumPy-style indexing

  Examples:
    >>> x = jnp.zeros((3, 5), dtype=int)
    >>> mask = (jnp.arange(x.size) % 3 == 0).reshape(x.shape)
    >>> mask
    Array([[ True, False, False,  True, False],
           [False,  True, False, False,  True],
           [False, False,  True, False, False]], dtype=bool)

    Placing a scalar value:

    >>> jnp.place(x, mask, 1, inplace=False)
    Array([[1, 0, 0, 1, 0],
           [0, 1, 0, 0, 1],
           [0, 0, 1, 0, 0]], dtype=int32)

    In this case, ``jnp.place`` is similar to the masked array update syntax:

    >>> x.at[mask].set(1)
    Array([[1, 0, 0, 1, 0],
           [0, 1, 0, 0, 1],
           [0, 0, 1, 0, 0]], dtype=int32)

    ``place`` differs when placing values from an array. The array is repeated
    to fill the masked entries:

    >>> vals = jnp.array([1, 3, 5])
    >>> jnp.place(x, mask, vals, inplace=False)
    Array([[1, 0, 0, 3, 0],
           [0, 5, 0, 0, 1],
           [0, 0, 3, 0, 0]], dtype=int32)
  placezjax.numpy.place cannot modify arrays in-place, because JAX arrays are immutable. Pass inplace=False to instead return an updated array.z)place: arr and mask must be the same sizez'Cannot place values from an empty array)rL   r$   r   dropr   )r   r=   r>   rI   rL   r   rx  _tile_to_sizerJ   r   r   r   r5   )ri   r  r  r   datamask_arrvals_arrr   s           r*   r  r  o  s    v "227CtL$(^^(
?@ @ 
YY(--
@
AA	
>
??	KOOHNN,8==X]][\]^'$$Xs7|<(		!	%	%hV	%	<	D	DTZZ	PPr,   r   vc                p   |rt        d      t        j                  d| ||      \  }}}|j                         }t	        j                  |      }|j
                  r|j
                  r|j
                  s|S t	        j                  |t        |            }|d}	na|dk(  r't	        j                  |d|j
                  dz
        }d}	n5|dk(  r||j
                  z  }d}	n|d	k(  rt        d
      t        d|      |j                  t	        j                  ||j                           j                  ||	      S )aH	  Put elements into an array at given indices.

  JAX implementation of :func:`numpy.put`.

  The semantics of :func:`numpy.put` are to modify arrays in-place, which
  is not possible for JAX's immutable arrays. The JAX version returns a modified
  copy of the input, and adds the ``inplace`` parameter which must be set to
  `False`` by the user as a reminder of this API difference.

  Args:
    a: array into which values will be placed.
    ind: array of indices over the flattened array at which to put values.
    v: array of values to put into the array.
    mode: string specifying how to handle out-of-bound indices. Supported values:

      - ``"clip"`` (default): clip out-of-bound indices to the final index.
      - ``"wrap"``: wrap out-of-bound indices to the beginning of the array.

    inplace: must be set to False to indicate that the input is not modified
      in-place, but rather a modified copy is returned.

  Returns:
    A copy of ``a`` with specified entries updated.

  See Also:
    - :func:`jax.numpy.place`: place elements into an array via boolean mask.
    - :func:`jax.numpy.ndarray.at`: array updates using NumPy-style indexing.
    - :func:`jax.numpy.take`: extract values from an array at given indices.

  Examples:
    >>> x = jnp.zeros(5, dtype=int)
    >>> indices = jnp.array([0, 2, 4])
    >>> values = jnp.array([10, 20, 30])
    >>> jnp.put(x, indices, values, inplace=False)
    Array([10,  0, 20,  0, 30], dtype=int32)

    This is equivalent to the following :attr:`jax.numpy.ndarray.at` indexing syntax:

    >>> x.at[indices].set(values)
    Array([10,  0, 20,  0, 30], dtype=int32)

    There are two modes for handling out-of-bound indices. By default they are
    clipped:

    >>> indices = jnp.array([0, 2, 6])
    >>> jnp.put(x, indices, values, inplace=False, mode='clip')
    Array([10,  0, 20,  0, 30], dtype=int32)

    Alternatively, they can be wrapped to the beginning of the array:

    >>> jnp.put(x, indices, values, inplace=False, mode='wrap')
    Array([10,  30, 20,  0, 0], dtype=int32)

    For N-dimensional inputs, the indices refer to the flattened array:

    >>> x = jnp.zeros((3, 5), dtype=int)
    >>> indices = jnp.array([0, 7, 14])
    >>> jnp.put(x, indices, values, inplace=False)
    Array([[10,  0,  0,  0,  0],
           [ 0,  0, 20,  0,  0],
           [ 0,  0,  0,  0, 30]], dtype=int32)
  zjax.numpy.put cannot modify arrays in-place, because JAX arrays are immutable. Pass inplace=False to instead return an updated array.putr  r2   r   r3   r   r1   r0   z-The 'raise' mode to jnp.put is not supported.z1mode should be one of 'wrap' or 'clip'; got mode=r   )rI   r   r=   r>   r   rL   r  rJ   r2   r<   r   unravel_indexr5   r   )
r   r   r  r!   r   ri   ind_arrrd  v_arrscatter_modes
             r*   r  r    s!   B 
?@ @ ))%C;/#wMMO'
//!
%	UZZJ

!
!%W
6%	\Lv~nnWaA6G&Lv~ G&Lw
M
NN
ID7K
LL		'';	<	@	@\	@	ZZr,   )NNNFFN)NN)Trm   )FFNNN)O__doc__	functoolsr   r(   r   typingr   r   r   numpyr?   r   r   jax._srcr   r	   r
   r   r   r   jax._src.apir   jax._src.laxrE   jax._src.numpyr   r   r   r   r   r   jax._src.pjitr   jax._src.tree_utilr   jax._src.typingr   r   r   jax._src.utilr   r   r   r   exportrb   r   r   r+   r'   rh   rA   rk   r   r   r   r   r   r   r   r  r  r  r  r	  r*  r-  r
  rg  r!  rn  r"  r=  rF  r  rl  r   r  r  r   r,   r*   <module>r     s     #    , ,          , ! - $ !  # + : : O O	K	    $&*T&T&T& *T& 
	T&
 *T& T& T& t#T& T& T&n 	de>BEI1RC$J 1R f1RhG <=
 04&*@R	@R@R *@R %%
%
,	@R
 t#@R @R > @RFL 9: U UUU U 	d
	U
 U 	d
U U ; UtV3 V4 V8# 8$ 8
Q 48\5 \s \#* \<ADL\~ GL<@ +L#2L"z ":)6
 F /3]'Xc] ]'# ]''+]'7?]'~).H,^3T2
, HQy HQ	 HQ HQHQ#(HQ HQV V[48V[9 V[9 V[ V[DjV[-1V[=BV[ V[r,   