
    ukiP                   
   d dl mZ d dlZd dlmZmZ d dlZd dlZd dlm	Z	m
Z
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mZmZmZ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'm(Z(m)Z) d dl*m+Z+m,Z,m-Z-m.Z.  e.d      Z/ej`                  Z1ee2ee2   df   Z3dwdZ4dxdZ5dydZ6dzdZ7d{dZ8ee
e
ge
f   Z9ddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d|dZ:d Z;d}dZ<d~dZ=ddZ>ddZ?dd Z@dd!ZA ej                  d"d#      	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd$       ZCe/	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd%       ZD ej                  d"d#      	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd&       ZEe/	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd'       ZF ej                  d(d#      	 	 	 d	 	 	 	 	 	 	 	 	 dd)       ZGe/	 	 	 d	 	 	 	 	 	 	 dd*       ZH ej                  d+d#      	 	 	 d	 	 	 	 	 	 	 	 	 dd,       ZIe/	 	 	 d	 	 	 	 	 	 	 dd-       ZJ ej                  d(d#      	 	 ddd.	 	 	 	 	 dd/       ZKe/	 	 ddd.	 	 	 	 	 dd0       Z0 ej                  d(d#      	 	 ddd.	 	 	 	 	 dd1       ZLe/	 	 ddd.	 	 	 	 	 dd2       ZM ej                  d+d#      	 	 	 d	 	 	 	 	 	 	 	 	 dd3       ZN ej                  d+d#      	 	 	 d	 	 	 	 	 	 	 	 	 dd4       ZO ej                  d+d#      	 	 	 d	 	 	 	 	 	 	 	 	 dd5       ZP ej                  d+d#      	 	 	 d	 	 	 	 	 	 	 	 	 dd6       ZQ ej                  d+d#      	 	 	 d	 	 	 	 	 	 	 	 	 dd7       ZR ej                  d+d#      	 	 	 d	 	 	 	 	 	 	 	 	 dd8       ZS	 	 	 d	 	 	 	 	 	 	 	 	 dd9ZT	 	 	 d	 	 	 	 	 	 	 	 	 dd:ZUe/	 	 	 d	 	 	 	 	 	 	 dd;       ZVe/	 	 	 d	 	 	 	 	 	 	 dd<       ZWdd=ZXe/	 	 ddd.	 	 	 	 	 	 	 dd>       ZY	 	 	 	 	 	 	 	 	 	 dd?ZZ ej                  d@d#      	 	 ddddA	 	 	 	 	 	 	 	 	 ddB       Z[e		 	 d	 	 	 	 	 ddC       Z\e	dddD	 	 	 	 	 ddE       Z\e		 	 d	 	 	 	 	 ddF       Z\e/	 	 d	 	 	 	 	 ddG       Z\ ej                  dHd#      	 	 d	 	 	 	 	 ddI       Z]e/	 	 dddddJ	 	 	 	 	 	 	 	 	 	 	 	 	 ddK       Z^ ej                  dLM      dddd ddddN	 	 	 	 	 	 	 	 	 	 	 ddO       Z_ddPZ`e/	 	 dddddJ	 	 	 	 	 	 	 	 	 	 	 	 	 ddQ       Za ej                  dLM      dddd ddddR	 	 	 	 	 	 	 	 	 	 	 ddS       Zbe/	 	 d	 	 	 ddT       Zc ej                  d(M      	 	 d	 	 	 ddU       Zde/ ej                  d(M      	 	 d	 	 	 ddV              Ze	 d	 	 	 	 	 	 	 	 	 	 	 ddWZfe/ ej                  d(M      	 	 	 d	 	 	 	 	 	 	 ddX              Zge/ ej                  d(M      	 	 	 d	 	 	 	 	 	 	 ddY              Zhe/ ej                  dLM      	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZ              Zie/ ej                  dLM      	 	 	 d	 	 	 	 	 	 	 dd[              Zje/ ej                  dLM      	 	 d	 	 	 	 	 dd\              Zke/ ej                  dLM      	 	 	 d	 	 	 	 	 	 	 	 	 dd]              Zldddd dddd^	 	 	 	 	 	 	 	 	 dd_Zme/ ej                  dLM      	 	 	 d	 	 	 	 	 	 	 	 	 dd`              Zn G da dbe      Zo	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddcZpe/ ej                  ddM      	 	 d	 	 	 	 	 dde              Zqe/ ej                  ddM      	 	 d	 	 	 	 	 ddf              Zre/ ej                  ddM      	 	 d	 	 	 	 	 ddg              Zse/ ej                  ddM      	 	 d	 	 	 	 	 ddh              Zt ej                  ddM      	 	 d	 	 	 	 	 ddi       Zue/ddddj	 	 	 	 	 	 	 	 	 ddk       Zve/ddddj	 	 	 	 	 	 	 	 	 ddl       Zwe/ ej                  dmM      	 	 	 d e)       dn	 	 	 	 	 	 	 	 	 	 	 ddo              Zxe/ ej                  dmM      	 	 	 d e)       dn	 	 	 	 	 	 	 	 	 	 	 ddp              Zy	 	 	 	 	 	 	 	 ddqZze/ ej                  dmM      	 	 	 d e)       dn	 	 	 	 	 	 	 	 	 	 	 	 	 ddr              Z{e/ ej                  dmM      	 	 	 d e)       dn	 	 	 	 	 	 	 	 	 	 	 	 	 dds              Z|e/ ej                  dtM      	 	 	 d	 	 	 	 	 	 	 ddu              Z}e/ ej                  dtM      	 	 	 d	 	 	 	 	 	 	 ddv              Z~y)    )annotationsN)CallableSequence)overloadAnyLiteralProtocolUnion)api)config)core)dtypes)_broadcast_toensure_arraylikepromote_dtypes_inexactpromote_dtypes_numeric_where)control_flow)lax)other)parallel)slicing)Array	ArrayLikeDType	DTypeLikeDeprecatedArg)canonicalize_axiscanonicalize_axis_tuplemaybe_named_axis
set_modulez	jax.numpyc                    t        | d      r| j                         } t        j                  |       xs t	        j
                  |       S )N__jax_array__)hasattrr#   r   is_python_scalarnpisscalar)elements    T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/numpy/reductions.py	_isscalarr*   0   s9    Wo&##%G		 	 	)	AR[[-AA    c                R   t        d|       } t        |t        j                  |             }t        |t        j                  |             }t	        t        j                  |             D cg c]
  }||k7  s	| }}|j                  ||       t        j                  | |      S c c}w )Nmoveaxis)r   r   r&   ndimrangeinsertr   	transpose)asourcedestinationiperms        r)   	_moveaxisr7   5   s    z1%!VRWWQZ0&!+rwwqz:+2771:&	6!v+!	6$	6++k6"	q$	 
7s   +
B$6B$c                    t        j                  |       t         j                  t        j                  fv rt        j                  d      S t        j                  |       S )Nfloat32)r&   dtypefloat16r   bfloat16r:   s    r)   _upcast_f16r>   >   s<    XXe_V__5588I	%r+   c                ^   t        j                  | t        j                        rt        j                         S t        j                  | t        j
                        rWt        j                         }t        j                  |       j                  t        j                  |      j                  k  r|S | S t        j                  | t        j                        rUt        j                         }t        j                  |       j                  t        j                  |      j                  k  r|S | S N)
r   
issubdtyper&   bool_default_int_dtypeunsignedintegerdefault_uint_dtypeiinfobitsinteger)r:   rE   rC   s      r)   _promote_integer_dtyperI   C   s     ubhh'##%% 2 23224	xxbhh'9:???
 
,	 

+002	xxbhh'89>>>	,r+   c                    ||S t        | |      }|j                  t        k7  rt        d|  d|j                  d      |S )Nzjnp.z9: where must be None or a boolean array; got where.dtype=.)r   r:   bool
ValueError)namewheres     r)   check_whererP   R   sO    
]L
4
'%
[[D
TFL~QO  
,r+   TF)has_identitypreprocbool_opupcast_f16_for_computationaxisr:   outkeepdimsinitialwhere_parallel_reducepromote_integersc               h   |xs |}|
t        d| d      t        ||       } t        ||      }t        j                  d |d| d      }||s|t        d| d      |r ||       n| } t        | |      \  }}|:|s8t        j                  |       t        fd|D              st        d| d	      |	| j                  }|r"t        |      }nt        j                  |	|      }|r0t        j                  |t        j                        rt!        |      }n|}t#        j$                  | |      } |t        j&                  k7  r|n|}t)        | |      }|t+        || |      } ||ur|t        d
| d       || |      }nt#        j,                  | |||      }|ct#        j$                  |t#        j.                  |       j                        }|j                  dk7  rt        d|j                          |||      }|rt#        j0                  ||      }t#        j$                  ||	xs |      S )NThe 'out' argument to jnp.z is not supported.zaxis argument to jnp.z().zreduction operation zO does not have an identity, so to use a where mask one has to specify 'initial'c              3  .   K   | ]  }|   d k\    yw   N ).0dshapes     r)   	<genexpr>z_reduction.<locals>.<genexpr>{   s     0!aA0s   z'zero-size array to reduction operation z which has no identityz)Named reductions not implemented for jnp.z()ra   z3initial value must be a scalar. Got array of shape )NotImplementedErrorr   rP   r   concrete_or_errorrM   _reduction_dimsr&   rd   _allr:   rI   r   !check_and_canonicalize_user_dtyperA   inexactr>   r   convert_element_typerB   _reduction_init_valr   reduceasarrayexpand_dims)r2   rN   opinit_valrQ   rR   rS   rT   rU   r:   rV   rW   rX   rY   rZ   r[   pos_dimsdimsresult_dtypecomputation_dtyperesultinitial_arrrd   s                         @r)   
_reductionry   ^   sW    Mr' 	_
 :4&@RS
TTtQ!tV$&			d.CD6,M	N$_\f.@
+D6 2? @ A A gaj!"1d+.(D_\HHQKE0x00@F\]^^ ]77L+L9l;;E4HLF$5$5lBJJ$O#L1$	q"34!"((*r" !H-(vq(#AT"KD6QS TUUQ%FZZ8R.F**7CKKN4H4HIKB --8->->,?A B BV$F__VX.F		!	!&%*?<	@@r+   c                &    t        | fdd       S )Nc                    t        |       S r@   r   )r5   ranks    r)   <lambda>z0_canonicalize_axis_allow_named.<locals>.<lambda>   s    '8D'A r+   c                    | S r@   ra   )rN   s    r)   r~   z0_canonicalize_axis_allow_named.<locals>.<lambda>   s    PT r+   )r    )xr}   s    `r)   _canonicalize_axis_allow_namedr      s    	!ACT	UUr+   c                    |+t        t        t        j                                     fdz  S t	        |t        j
                  t         t        f      s|f}t         fd|D              }t        |      t        t        |            k7  rt        d|       t        d |D              }t        |      t        |      k7  r||fS ||fS )N   c              3  \   K   | ]#  }t        |t        j                               % y wr@   )r   r&   r.   )rb   r   r2   s     r)   re   z"_reduction_dims.<locals>.<genexpr>   s(      $ 4ArwwqzB $s   ),zduplicate value in 'axis': c              3  B   K   | ]  }t        |t              s|  y wr@   )
isinstanceint)rb   r   s     r)   re   z"_reduction_dims.<locals>.<genexpr>   s     Eq*Q2DEs   )
tupler/   r&   r.   r   ndarraylistlensetrM   )r2   rU   
canon_axiscanon_pos_axiss   `   r)   rh   rh      s    	\%
#$&**dRZZ567D $"$ $*_C
O,,
24&9
::EJEE.C
O+:%%z!!r+   c                   | j                   }|dk(  rt        j                  |dkD  |      S t        j                  |      rt	        j
                  |t        j                        r}t	        j                  |      sht        j                  t        j                  |      rt	        j                  |      j                  nt	        j                  |      j                  |      }	 t        j                  ||      S # t        $ r t	        j
                  |t        j                        sJ t        j                  |      t	        j                  |      }}t        j                  |dk  r|j                  n|j                  |      cY S w xY w)NrL   r   r=   )r:   r&   arrayisinfr   rA   floatingsupports_infisneginffinfominmaxOverflowErrorrH   signrF   )r2   rr   a_dtyper   infos        r)   rm   rm      s    GG'88HqL00hhxV..wD!!'*xxR[[5JW-11#\\'266gGHG88HG,,	 GWbjj111"FLL$9$D88qDHHdhhgFFGs   #C: :BFFc                    t        j                  | j                  t        j                        r| j
                  } t        j                  | t        j                        S r@   )	r   rA   r:   r&   complexfloatingrealr   rl   rB   operands    r)   _cast_to_boolr      s<    w}}b&8&89llG		!	!'288	44r+   c                    t        |       d   S )Nr   )r   r   s    r)   _cast_to_numericr      s    		(	++r+   c                v    t        j                  | j                  d      st        d| j                         | S )N)rL   integralz%integer argument required; got dtype=)r   isdtyper:   rM   )arrs    r)   _require_integerr      s1    			#7	8
<SYYKH
II	*r+   c                6    d }t        j                  || d      S )Nc                v    | y 	 t        j                  |       S # t        $ r t        d | D              cY S w xY w)Nc              3  j   K   | ]+  }t        |t              r|nt        j                  |       - y wr@   )r   stroperatorindex)rb   r5   s     r)   re   z7_ensure_optional_axes.<locals>.force.<locals>.<genexpr>   s'     Ma
1c*1q0AAMs   13)r   r   	TypeErrorr   )r   s    r)   forcez$_ensure_optional_axes.<locals>.force   sA    yN^^A NM1MMMNs    88z+The axis argument must be known statically.)r   rg   )r   r   s     r)   _ensure_optional_axesr      s%    N 
			1;
= =r+   )rU   r:   rW   r[   )static_argnamesinlinec                    t        | dt        j                  dt        t        j                  |d u ||||||t
        j                  |      S )Nsumr   )rR   rS   rT   rU   r:   rV   rW   rX   rY   rZ   r[   )ry   r   addr   
bitwise_orlax_parallelpsumr2   rU   r:   rV   rW   rX   rO   r[   s           r)   _reduce_sumr      sG    
 
Aucggq2BNNQUUh#E<CTCT%5	
7 7r+   c           
     :    t        | t        |      ||||||      S )a	  Sum of the elements of the array over a given axis.

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

  Args:
    a: Input array.
    axis: int or array, default=None. Axis along which the sum to be computed.
      If None, the sum is computed along all the axes.
    dtype: The type of the output array. Default=None.
    out: Unused by JAX
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    initial: int or array, Default=None. Initial value for the sum.
    where: int or array, default=None. The elements to be used in the sum. Array
      should be broadcast compatible to the input.
    promote_integers : bool, default=True. If True, then integer inputs will be
      promoted to the widest available integer dtype, following numpy's behavior.
      If False, the result will have the same dtype as the input.
      ``promote_integers`` is ignored if ``dtype`` is specified.

  Returns:
    An array of the sum along the given axis.

  See also:
    - :func:`jax.numpy.prod`: Compute the product of array elements over a given
      axis.
    - :func:`jax.numpy.max`: Compute the maximum of array elements over given axis.
    - :func:`jax.numpy.min`: Compute the minimum of array elements over given axis.

  Examples:

    By default, the sum is computed along all the axes.

    >>> x = jnp.array([[1, 3, 4, 2],
    ...                [5, 2, 6, 3],
    ...                [8, 1, 3, 9]])
    >>> jnp.sum(x)
    Array(47, dtype=int32)

    If ``axis=1``, the sum is computed along axis 1.

    >>> jnp.sum(x, axis=1)
    Array([10, 16, 21], dtype=int32)

    If ``keepdims=True``, ``ndim`` of the output is equal to that of the input.

    >>> jnp.sum(x, axis=1, keepdims=True)
    Array([[10],
           [16],
           [21]], dtype=int32)

    To include only specific elements in the sum, you can use ``where``.

    >>> where=jnp.array([[0, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.sum(x, axis=1, keepdims=True, where=where)
    Array([[ 4],
           [ 9],
           [12]], dtype=int32)
    >>> where=jnp.array([[False],
    ...                  [False],
    ...                  [False]])
    >>> jnp.sum(x, axis=0, keepdims=True, where=where)
    Array([[0, 0, 0, 0]], dtype=int32)
  rU   r:   rV   rW   rX   rO   r[   )r   r   r   s           r)   r   r      s*    L 
Q2483&u&6
8 8r+   c                x    t        | dt        j                  dt        t        j                  |d u |||||||      S )Nprodr`   )
rR   rS   rT   rU   r:   rV   rW   rX   rY   r[   )ry   r   mulr   bitwise_andr   s           r)   _reduce_prodr   5  sA    
 
Avsww3COORVUh#EDT
V Vr+   c           
     :    t        | t        |      ||||||      S )aD	  Return product of the array elements over a given axis.

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

  Args:
    a: Input array.
    axis: int or array, default=None. Axis along which the product to be computed.
      If None, the product is computed along all the axes.
    dtype: The type of the output array. Default=None.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    initial: int or array, Default=None. Initial value for the product.
    where: int or array, default=None. The elements to be used in the product.
      Array should be broadcast compatible to the input.
    promote_integers : bool, default=True. If True, then integer inputs will be
      promoted to the widest available integer dtype, following numpy's behavior.
      If False, the result will have the same dtype as the input.
      ``promote_integers`` is ignored if ``dtype`` is specified.
    out: Unused by JAX.

  Returns:
    An array of the product along the given axis.

  See also:
    - :func:`jax.numpy.sum`: Compute the sum of array elements over a given axis.
    - :func:`jax.numpy.max`: Compute the maximum of array elements over given axis.
    - :func:`jax.numpy.min`: Compute the minimum of array elements over given axis.

  Examples:
    By default, ``jnp.prod`` computes along all the axes.

    >>> x = jnp.array([[1, 3, 4, 2],
    ...                [5, 2, 1, 3],
    ...                [2, 1, 3, 1]])
    >>> jnp.prod(x)
    Array(4320, dtype=int32)

    If ``axis=1``, product is computed along axis 1.

    >>> jnp.prod(x, axis=1)
    Array([24, 30,  6], dtype=int32)

    If ``keepdims=True``, ``ndim`` of the output is equal to that of the input.

    >>> jnp.prod(x, axis=1, keepdims=True)
    Array([[24],
           [30],
           [ 6]], dtype=int32)

    To include only specific elements in the sum, you can use a``where``.

    >>> where=jnp.array([[1, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.prod(x, axis=1, keepdims=True, where=where)
    Array([[4],
           [3],
           [6]], dtype=int32)
    >>> where = jnp.array([[False],
    ...                    [False],
    ...                    [False]])
    >>> jnp.prod(x, axis=1, keepdims=True, where=where)
    Array([[1],
           [1],
           [1]], dtype=int32)
  r   )r   r   r   s           r)   r   r   @  s*    N 
a3D9''7
9 9r+   rU   rW   c                    t        | dt        j                  t        j                   d||||||t
        j                        S )Nr   FrQ   rU   r:   rV   rW   rX   rY   rZ   )ry   r   r   r&   infr   pmaxr2   rU   r:   rV   rW   rX   rO   s          r)   _reduce_maxr     s=     
AucggwUUh#E<CTCT
V Vr+   c                6    t        | t        |      ||||      S )a  Return the maximum of the array elements along a given axis.

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

  Args:
    a: Input array.
    axis: int or array, default=None. Axis along which the maximum to be computed.
      If None, the maximum is computed along all the axes.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    initial: int or array, default=None. Initial value for the maximum.
    where: int or array of boolean dtype, default=None. The elements to be used
      in the maximum. Array should be broadcast compatible to the input.
      ``initial`` must be specified when ``where`` is used.
    out: Unused by JAX.

  Returns:
    An array of maximum values along the given axis.

  See also:
    - :func:`jax.numpy.min`: Compute the minimum of array elements along a given
      axis.
    - :func:`jax.numpy.sum`: Compute the sum of array elements along a given axis.
    - :func:`jax.numpy.prod`: Compute the product of array elements along a given
      axis.

  Examples:

    By default, ``jnp.max`` computes the maximum of elements along all the axes.

    >>> x = jnp.array([[9, 3, 4, 5],
    ...                [5, 2, 7, 4],
    ...                [8, 1, 3, 6]])
    >>> jnp.max(x)
    Array(9, dtype=int32)

    If ``axis=1``, the maximum will be computed along axis 1.

    >>> jnp.max(x, axis=1)
    Array([9, 7, 8], dtype=int32)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.max(x, axis=1, keepdims=True)
    Array([[9],
           [7],
           [8]], dtype=int32)

    To include only specific elements in computing the maximum, you can use
    ``where``. It can either have same dimension as input

    >>> where=jnp.array([[0, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.max(x, axis=1, keepdims=True, initial=0, where=where)
    Array([[4],
           [7],
           [8]], dtype=int32)

    or must be broadcast compatible with input.

    >>> where = jnp.array([[False],
    ...                    [False],
    ...                    [False]])
    >>> jnp.max(x, axis=0, keepdims=True, initial=0, where=where)
    Array([[0, 0, 0, 0]], dtype=int32)
  rU   rV   rW   rX   rO   )r   r   r2   rU   rV   rW   rX   rO   s         r)   r   r     s'    N 
Q248c&u
F Fr+   )rU   rW   r:   c                    t        | dt        j                  t        j                  d||||||t
        j                        S )Nr   Fr   )ry   r   r   r&   r   r   pminr   s          r)   _reduce_minr     s;     
AucggrvvEUh#E<CTCT
V Vr+   c                6    t        | t        |      ||||      S )a  Return the minimum of array elements along a given axis.

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

  Args:
    a: Input array.
    axis: int or array, default=None. Axis along which the minimum to be computed.
      If None, the minimum is computed along all the axes.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    initial: int or array, Default=None. Initial value for the minimum.
    where: int or array, default=None. The elements to be used in the minimum.
      Array should be broadcast compatible to the input. ``initial`` must be
      specified when ``where`` is used.
    out: Unused by JAX.

  Returns:
    An array of minimum values along the given axis.

  See also:
    - :func:`jax.numpy.max`: Compute the maximum of array elements along a given
      axis.
    - :func:`jax.numpy.sum`: Compute the sum of array elements along a given axis.
    - :func:`jax.numpy.prod`: Compute the product of array elements along a given
      axis.

  Examples:
    By default, the minimum is computed along all the axes.

    >>> x = jnp.array([[2, 5, 1, 6],
    ...                [3, -7, -2, 4],
    ...                [8, -4, 1, -3]])
    >>> jnp.min(x)
    Array(-7, dtype=int32)

    If ``axis=1``, the minimum is computed along axis 1.

    >>> jnp.min(x, axis=1)
    Array([ 1, -7, -4], dtype=int32)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.min(x, axis=1, keepdims=True)
    Array([[ 1],
           [-7],
           [-4]], dtype=int32)

    To include only specific elements in computing the minimum, you can use
    ``where``. ``where`` can either have same dimension as input.

    >>> where=jnp.array([[1, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.min(x, axis=1, keepdims=True, initial=0, where=where)
    Array([[ 0],
           [-2],
           [-4]], dtype=int32)

    or must be broadcast compatible with input.

    >>> where = jnp.array([[False],
    ...                    [False],
    ...                    [False]])
    >>> jnp.min(x, axis=0, keepdims=True, initial=0, where=where)
    Array([[0, 0, 0, 0]], dtype=int32)
  r   )r   r   r   s         r)   r   r     s'    L 
Q248c&u
F Fr+   rO   c               N    t        | dt        j                  dt        ||||	      S )NallTrR   rU   rV   rW   rY   )ry   r   r   r   r2   rU   rV   rW   rO   s        r)   _reduce_allr   1  s*     
Aucoot]3%
I Ir+   c               4    t        | t        |      |||      S )a  Test whether all array elements along a given axis evaluate to True.

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

  Args:
    a: Input array.
    axis: int or array, default=None. Axis along which to be tested. If None,
      tests along all the axes.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    where: int or array of boolean dtype, default=None. The elements to be used
      in the test. Array should be broadcast compatible to the input.
    out: Unused by JAX.

  Returns:
    An array of boolean values.

  Examples:
    By default, ``jnp.all`` tests for True values along all the axes.

    >>> x = jnp.array([[True, True, True, False],
    ...                [True, False, True, False],
    ...                [True, True, False, False]])
    >>> jnp.all(x)
    Array(False, dtype=bool)

    If ``axis=0``, tests for True values along axis 0.

    >>> jnp.all(x, axis=0)
    Array([ True, False, False, False], dtype=bool)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.all(x, axis=0, keepdims=True)
    Array([[ True, False, False, False]], dtype=bool)

    To include specific elements in testing for True values, you can use a``where``.

    >>> where=jnp.array([[1, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.all(x, axis=0, keepdims=True, where=where)
    Array([[ True,  True, False, False]], dtype=bool)
  rU   rV   rW   rO   )r   r   r   s        r)   r   r   8  #    ^ 
Q248c&e
5 5r+   c               N    t        | dt        j                  dt        ||||	      S )NanyFr   )ry   r   r   r   r   s        r)   _reduce_anyr   j  s*     
Aucnne]3%
I Ir+   c               4    t        | t        |      |||      S )a  Test whether any of the array elements along a given axis evaluate to True.

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

  Args:
    a: Input array.
    axis: int or array, default=None. Axis along which to be tested. If None,
      tests along all the axes.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    where: int or array of boolean dtype, default=None. The elements to be used
      in the test. Array should be broadcast compatible to the input.
    out: Unused by JAX.

  Returns:
    An array of boolean values.

  Examples:
    By default, ``jnp.any`` tests along all the axes.

    >>> x = jnp.array([[True, True, True, False],
    ...                [True, False, True, False],
    ...                [True, True, False, False]])
    >>> jnp.any(x)
    Array(True, dtype=bool)

    If ``axis=0``, tests along axis 0.

    >>> jnp.any(x, axis=0)
    Array([ True,  True,  True, False], dtype=bool)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.any(x, axis=0, keepdims=True)
    Array([[ True,  True,  True, False]], dtype=bool)

    To include specific elements in testing for True values, you can use a``where``.

    >>> where=jnp.array([[1, 0, 1, 0],
    ...                  [0, 1, 0, 1],
    ...                  [1, 0, 1, 0]], dtype=bool)
    >>> jnp.any(x, axis=0, keepdims=True, where=where)
    Array([[ True, False,  True, False]], dtype=bool)
  r   )r   r   r   s        r)   r   r   q  r   r+   c                    t        j                  |       }t        j                  d      j	                  |xs |j
                        }t        |dt         j                  |t        t        |      |||||      S )Nreduce_bitwise_and
rN   rq   rr   rR   rU   r:   rV   rW   rX   rY   )
r   ro   r&   r   astyper:   ry   r   r   r   )	r2   rU   r:   rV   rW   rX   rO   r   rr   s	            r)   _reduce_bitwise_andr     sd     	A#XXb\  !3#))4(	C2sQYcs.t4EsU]#E
3 3r+   c                d    t        | dt        j                  dt        t	        |      |||||      S )Nreduce_bitwise_orr   r   )ry   r   r   r   r   r   s          r)   _reduce_bitwise_orr     s5     
A/CNNQXh.t4EsU]#E
3 3r+   c                d    t        | dt        j                  dt        t	        |      |||||      S )Nreduce_bitwise_xorr   r   )ry   r   bitwise_xorr   r   r   s          r)   _reduce_bitwise_xorr     s5     
A0S__qZj.t4EsU]#E
3 3r+   c                d    t        | dt        j                  dt        t	        |      |||||      S )Nreduce_logical_andTr   )ry   r   r   r   r   r   s          r)   _reduce_logical_andr     s5     
A0S__t]j.t4EsU]#E
3 3r+   c                d    t        | dt        j                  dt        t	        |      |||||      S )Nreduce_logical_orFr   )ry   r   r   r   r   r   s          r)   _reduce_logical_orr     s5     
A/CNNU\i.t4EsU]#E
3 3r+   c                d    t        | dt        j                  dt        t	        |      |||||      S )Nreduce_logical_xorFr   )ry   r   r   r   r   r   s          r)   _reduce_logical_xorr     s5     
A0S__u^k.t4EsU]#E
3 3r+   c           
     B   |t        d      |t        j                  |d      }~t        d|       } t	        d|      }t        |       \  }t        ||      \  }}	t        |j                  |	||t        j                         }
t        j                  t        j                  t        j                  |
      |
t        j                  |
d                  }
|r|
nt        j                   |
|      }t        j"                  t        j$                  ||j'                  |j(                                    }|j+                  |	||      }t        j,                  t        j.                  |      |
j'                  |j(                              }||S t1        j2                  ||      S )z7Compute log(sum(exp(a))) while avoiding precision loss.z<The 'out' argument to jnp.logaddexp.reduce is not supported.zjnp.logaddexp.reduce	logsumexp)rU   rW   rO   rX   r   rU   rW   rO   )rf   r   rj   r   rP   r   rh   r   r   r&   r   r   stop_gradientselect	is_finite	full_likerp   expsubr   r:   r   r   log	lax_other	logaddexp)r2   rU   r:   rV   rW   rX   rO   a_arrrs   rt   amaxamax_with_dimsexp_asumexprw   s                  r)   
_logsumexpr    sD    	_
\
]]
44U<RSE{A&!
k5
)%!!$&%"5$/.(D	UZZdXURVVG	T$			3::cmmD&94tUVAWX	Y$#4x)H.
''#''%!6!6u{{!CD
E%99$9?&773776?DKK$=>&?L	(;(;GV(LLr+   c                    |t        d      |t        j                  |d      }t        d|       } t	        d|      }t        t        j                  d            }|||z  }t        | |z  |||||      |z  S )z(Compute log2(sum(2 ** a)) via logsumexp.z=The 'out' argument to jnp.logaddexp2.reduce is not supported.zjnp.logaddexp2.reduce
logsumexp2r   )rU   r:   rW   rO   rX   )	rf   r   rj   r   rP   floatr&   r   r  )r2   rU   r:   rV   rW   rX   rO   ln2s           r)   _logsumexp2r    s     	_
]
^^
44&(E|Q'!
lE
*%bffQi#sNG	AG$eh
247
8 8r+   c                $    t        | |||||      S )zAlias of :func:`jax.numpy.min`.r   )r   r   s         r)   aminr        
 
QTsXE
+ +r+   c                $    t        | |||||      S )zAlias of :func:`jax.numpy.max`.r   )r   r   s         r)   r  r    r  r+   c                    t        |t        t        f      s|f}n|}d}t        j                  |       |D ]#  } |t        | fdt        j                        z  }% |S )Nr`   c                    |    S r@   ra   )r5   a_shapes    r)   r~   z_axis_size.<locals>.<lambda>  s    '!* r+   )r   r   r   r&   rd   r    r   	axis_size)r2   rU   axis_seqsizer  s       @r)   
_axis_sizer    s`    	D5$-	(#gHH	
$HHQK' NaQ 4l6L6LMMDN	+r+   c          
     <    t        | t        |      |||||du       S )a  Return the mean of array elements along a given axis.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      mean to be computed. If None, mean is computed along all the axes.
    dtype: The type of the output array. If None (default) then the output dtype
      will be match the input dtype for floating point inputs, or be set to float32
      or float64 for non-floating-point inputs.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    where: optional, boolean array, default=None. The elements to be used in the
      mean. Array should be broadcast compatible to the input.
    out: Unused by JAX.

  Returns:
    An array of the mean along the given axis.

  Notes:
    For inputs of type `float16` or `bfloat16`, the reductions will be performed at
    float32 precision.

  See also:
    - :func:`jax.numpy.average`: Compute the weighted average of array elements
    - :func:`jax.numpy.sum`: Compute the sum of array elements.

  Examples:
    By default, the mean is computed along all the axes.

    >>> x = jnp.array([[1, 3, 4, 2],
    ...                [5, 2, 6, 3],
    ...                [8, 1, 2, 9]])
    >>> jnp.mean(x)
    Array(3.8333335, dtype=float32)

    If ``axis=1``, the mean is computed along axis 1.

    >>> jnp.mean(x, axis=1)
    Array([2.5, 4. , 5. ], dtype=float32)

    If ``keepdims=True``, ``ndim`` of the output is equal to that of the input.

    >>> jnp.mean(x, axis=1, keepdims=True)
    Array([[2.5],
           [4. ],
           [5. ]], dtype=float32)

    To use only specific elements of ``x`` to compute the mean, you can use
    ``where``.

    >>> where = jnp.array([[1, 0, 1, 0],
    ...                    [0, 1, 0, 1],
    ...                    [1, 1, 0, 1]], dtype=bool)
    >>> jnp.mean(x, axis=1, keepdims=True, where=where)
    Array([[2.5],
           [2.5],
           [6. ]], dtype=float32)
  N)rO   rT   )_meanr   )r2   rU   r:   rV   rW   rO   s         r)   meanr     s-    @ 
q'-uc8
H Hr+   c                &   |b|)t        j                  t        j                  |             }nt        j                  t	        | |            }t        j                  ||      }|S t        t        |t        j                  |             |||      }|S )N)r:   rW   )
r   dimension_as_valuer&   r  r  r   rl   r   r   rd   )r2   rU   rW   rO   r:   counts         r)   _countr  c  s|     ]|%%bggaj1e%%jD&9:e$$UE2E 
, eRXXa[14uxXE	,r+   )rU   r:   rW   rT   )rT   rO   c          	        t        d|       } t        d|      }|t        d      | t        j                  | j
                        }nt        j                  |d      }|r0t        j                  |t        j                        rt        |      }n|}t        | ||||      }	t        j                  t        | ||||      |	      j                  |      S )Nr  z0The 'out' argument to jnp.mean is not supported.rU   rW   rO   r:   r:   rW   rO   )r   rP   rf   r   to_inexact_dtyper:   rj   rA   r&   rk   r>   r  r   divr   r   )
r2   rU   r:   rV   rW   rT   rO   ru   rv   
normalizers
             r)   r  r  t  s     vq!!
fe
$%_
P
QQ
]**1773L;;E6JLF$5$5lBJJ$O#L1$* 
	!T*XUK
 F<r+   c                     y r@   ra   r2   rU   weightsreturnedrW   s        r)   averager(    s    PSr+   )rW   c                    y r@   ra   r%  s        r)   r(  r(    s    GJr+   c                     y r@   ra   r%  s        r)   r(  r(    s    \_r+   c                2    t        | t        |      |||      S )a5  Compute the weighed average.

  JAX Implementation of :func:`numpy.average`.

  Args:
    a: array to be averaged
    axis: an optional integer or sequence of integers specifying the axis along which
      the mean to be computed. If not specified, mean is computed along all the axes.
    weights: an optional array of weights for a weighted average. This must either exactly
      match the shape of `a`, or if `axis` is specified, it must have shape ``a.shape[axis]``
      for a single axis, or shape ``tuple(a.shape[ax] for ax in axis)`` for multiple axes.
    returned: If False (default) then return only the average. If True then return both
      the average and the normalization factor (i.e. the sum of weights).
    keepdims: If True, reduced axes are left in the result with size 1. If False (default)
      then reduced axes are squeezed out.

  Returns:
    An array ``average`` or tuple of arrays ``(average, normalization)`` if
    ``returned`` is True.

  See also:
    - :func:`jax.numpy.mean`: unweighted mean.

  Examples:
    Simple average:

    >>> x = jnp.array([1, 2, 3, 2, 4])
    >>> jnp.average(x)
    Array(2.4, dtype=float32)

    Weighted average:

    >>> weights = jnp.array([2, 1, 3, 2, 2])
    >>> jnp.average(x, weights=weights)
    Array(2.5, dtype=float32)

    Use ``returned=True`` to optionally return the normalization, i.e. the
    sum of weights:

    >>> jnp.average(x, returned=True)
    (Array(2.4, dtype=float32), Array(5., dtype=float32))
    >>> jnp.average(x, weights=weights, returned=True)
    (Array(2.5, dtype=float32), Array(10., dtype=float32))

    Weighted average along a specified axis:

    >>> x = jnp.array([[8, 2, 7],
    ...                [3, 6, 4]])
    >>> weights = jnp.array([1, 2, 3])
    >>> jnp.average(x, weights=weights, axis=1)
    Array([5.5, 4.5], dtype=float32)
  )_averager   r%  s        r)   r(  r(    s    n 
!*40'8X	NNr+   )rU   r'  rW   c           	         d nt        t        j                               |t        d        t	               \   t         |      }At        j                  dt        j                   j                        |j                        }n>t        t              r-t        j                  dt        j                   fdD              |j                        }nt        d |      \   }t	         |      \   } j                   |j                   k7  rt#        d      |j                   t         fdD              k7  rt#        d      t        fd	t%         j                         D              }t        j&                  ||t        t        j(                              
      }t+        ||      }t+         |z  |      |z  }|r3|j                   j                   k7  rt-        ||j                         }||fS |S )Nr(  r   ra   r=   c              3  b   K   | ]&  }t        j                  j                  |          ( y wr@   )r   r  rd   )rb   rc   r2   s     r)   re   z_average.<locals>.<genexpr>  s%     *]ST4+B+B1771:+N*]s   ,/z;Axis must be specified when shapes of a and weights differ.c              3  <   K   | ]  }j                   |     y wr@   )rd   )rb   axr2   s     r)   re   z_average.<locals>.<genexpr>  s     ;;s   zIShape of weights must be consistent with shape of a along specified axis.c              3  4   K   | ]  \  }}|v r|nd   ywr_   ra   )rb   r5   dimrU   s      r)   re   z_average.<locals>.<genexpr>  s      QFAsqDya/Qs   )
dimensions)r   r&   r.   r   r   r  r   fullr   r  r  r:   r   r   mathr   rd   rM   	enumeratereshapeargsortr   r   )r2   rU   r&  r'  rW   avgweights_sum	new_shapes   ``      r)   r,  r,    s    #:4#L$_A&A		"BA
qth
/C|HHR!8!8!@		Rk	D%	 HHR*]X\*]!]ehenenok!)Q8JAw'73JAwww'--	 + , 	,	%;d;;	; 6 7 	7Qi>PQQiGY5DAQ;RSggD8<K
a'kx
8;
FC
yyK%%%!+syy9k	*r+   )rO   r  
correctionc          
         ||}n t        |t              r|dk7  rt        d      t        d|       } t	        | t        |      ||||||      S )a  Compute the variance along a given axis.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      variance is computed. If None, variance is computed along all the axes.
    dtype: The type of the output array. Default=None.
    ddof: int, default=0. Degrees of freedom. The divisor in the variance computation
      is ``N-ddof``, ``N`` is number of elements along given axis.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    where: optional, boolean array, default=None. The elements to be used in the
      variance. Array should be broadcast compatible to the input.
    mean: optional, mean of the input array, computed along the given axis.
      If provided, it will be used to compute the variance instead of
      computing it from the input array. If specified, mean must be broadcast-compatible
      with the input array. In the general case, this can be achieved by computing the mean with
      ``keepdims=True`` and ``axis`` matching this function's ``axis`` argument.
    correction: int or float, default=None. Alternative name for ``ddof``.
      Both ddof and correction can't be provided simultaneously.
    out: Unused by JAX.

  Returns:
    An array of the variance along the given axis.

  See also:
    - :func:`jax.numpy.mean`: Compute the mean of array elements over a given axis.
    - :func:`jax.numpy.std`: Compute the standard deviation of array elements over
      given axis.
    - :func:`jax.numpy.nanvar`: Compute the variance along a given axis, ignoring
      NaNs values.
    - :func:`jax.numpy.nanstd`: Computed the standard deviation of a given axis,
      ignoring NaN values.

  Examples:
    By default, ``jnp.var`` computes the variance along all axes.

    >>> x = jnp.array([[1, 3, 4, 2],
    ...                [5, 2, 6, 3],
    ...                [8, 4, 2, 9]])
    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   jnp.var(x)
    Array(5.74, dtype=float32)

    If ``axis=1``, variance is computed along axis 1.

    >>> jnp.var(x, axis=1)
    Array([1.25  , 2.5   , 8.1875], dtype=float32)

    To preserve the dimensions of input, you can set ``keepdims=True``.

    >>> jnp.var(x, axis=1, keepdims=True)
    Array([[1.25  ],
           [2.5   ],
           [8.1875]], dtype=float32)

    If ``ddof=1``:

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.var(x, axis=1, keepdims=True, ddof=1))
    [[ 1.67]
     [ 3.33]
     [10.92]]

    To include specific elements of the array to compute variance, you can use
    ``where``.

    >>> where = jnp.array([[1, 0, 1, 0],
    ...                    [0, 1, 1, 0],
    ...                    [1, 1, 1, 0]], dtype=bool)
    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.var(x, axis=1, keepdims=True, where=where))
    [[2.25]
     [4.  ]
     [6.22]]
  r   5ddof and correction can't be provided simultaneously.varrU   r:   rV   r<  rW   rO   a_mean)r   r   rM   r   _varr   	r2   rU   r:   rV   ddofrW   rO   r  r<  s	            r)   r?  r?    s^    f JdC DAI
L
MMua !	a+D1CT^iq$
( (r+   rU   r:   rW   )r   r@  c                  t        d|      }|t        j                  |d      }|t        d      t	        | j
                  |      \  }}t        j                  |       j                  |      } |t        | ||d|      }nt        d|      j                  |      }t        j                  | |      }	t        j                  |t        j                        rIt        j                  t        j                   |	t        j"                  |	                  }	|	j
                  }nt        j$                  |	      }	t'        | ||||      }
t        j                  |
t        j(                  ||            }
t+        |	||||      }t        j,                  ||
      j                  |      }t/        j0                  d      5  t3        |
dkD  |t        j4                        }d d d        |S # 1 sw Y   |S xY w)Nr?  z/The 'out' argument to jnp.var is not supported.Tr   r  Fr   )rP   r   rj   rf   _var_promote_typesr:   r   ro   r   r  r   r   rA   r&   r   r   r   conjsquarer  rl   r   r"  r   
debug_nansr   nan)r2   rU   r:   rV   r<  rW   rO   rA  rv   centeredr#  rw   s               r)   rB  rB  X  s    eU
#%
44UEBE_
O
PP/?U	kk!n-.!^!T!2TOFeV,334EFFWWQ(("*<*<=xx#((8*<=>H zz(#H* wwz3#;#;JHY#Z[*x%6QVW&776:&--e4& 4JNFBFF3F4	-4	-s   9G""G,c                   |rXt        j                  |t        j                        s1t        j                  | t        j                        rd}t	        |      |}nht        j                  | t        j
                        st        j                  |       }|}n,t        j                  d|       j                  j                  }| }t        |      t        j                  |      fS )Na  jax.numpy.var does not yet support real dtype parameters when computing the variance of an array of complex values. The semantics of numpy.var seem unclear in this case. Please comment on https://github.com/jax-ml/jax/issues/2283 if this behavior is important to you.r   )r   rA   r&   r   rM   rk   r!  r   r   r:   r>   )r   r:   msgrv   s       r)   rG  rG    s    
eR%7%78'2#5#56!c
 sOWbjj1%%g.ehhq'"''--e!	&	'%	88r+   c          
         ||}n t        |t              r|dk7  rt        d      t        d|       } t	        | t        |      ||||||      S )a  Compute the standard deviation along a given axis.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      standard deviation is computed. If None, standard deviaiton is computed
      along all the axes.
    dtype: The type of the output array. Default=None.
    ddof: int, default=0. Degrees of freedom. The divisor in the standard deviation
      computation is ``N-ddof``, ``N`` is number of elements along given axis.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    where: optional, boolean array, default=None. The elements to be used in the
      standard deviation. Array should be broadcast compatible to the input.
    mean: optional, mean of the input array, computed along the given axis.
      If provided, it will be used to compute the standard deviation instead of
      computing it from the input array. If specified, mean must be broadcast-compatible
      with the input array. In the general case, this can be achieved by computing the mean with
      ``keepdims=True`` and ``axis`` matching this function's ``axis`` argument.
    correction: int or float, default=None. Alternative name for ``ddof``.
      Both ddof and correction can't be provided simultaneously.
    out: Unused by JAX.

  Returns:
    An array of the standard deviation along the given axis.

  See also:
    - :func:`jax.numpy.var`: Compute the variance of array elements over given
      axis.
    - :func:`jax.numpy.mean`: Compute the mean of array elements over a given axis.
    - :func:`jax.numpy.nanvar`: Compute the variance along a given axis, ignoring
      NaNs values.
    - :func:`jax.numpy.nanstd`: Computed the standard deviation of a given axis,
      ignoring NaN values.

  Examples:
    By default, ``jnp.std`` computes the standard deviation along all axes.

    >>> x = jnp.array([[1, 3, 4, 2],
    ...                [4, 2, 5, 3],
    ...                [5, 4, 2, 3]])
    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   jnp.std(x)
    Array(1.21, dtype=float32)

    If ``axis=0``, computes along axis 0.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.std(x, axis=0))
    [1.7  0.82 1.25 0.47]

    To preserve the dimensions of input, you can set ``keepdims=True``.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.std(x, axis=0, keepdims=True))
    [[1.7  0.82 1.25 0.47]]

    If ``ddof=1``:

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.std(x, axis=0, keepdims=True, ddof=1))
    [[2.08 1.   1.53 0.58]]

    To include specific elements of the array to compute standard deviation, you
    can use ``where``.

    >>> where = jnp.array([[1, 0, 1, 0],
    ...                    [0, 1, 0, 1],
    ...                    [1, 1, 1, 0]], dtype=bool)
    >>> jnp.std(x, axis=0, keepdims=True, where=where)
    Array([[2., 1., 1., 0.]], dtype=float32)
  r   r>  stdrU   r:   rV   r<  rW   rO   r  )r   r   rM   r   _stdr   rC  s	            r)   rP  rP    s^    ^ JdC DAI
L
MMua !	a+D1CT^iq
& &r+   rQ  c                  t        d|      }|Ht        j                  |d      }t        j                  |t        j
                        st        d|       |t        d      t        j                  t        | ||||||            S )NrP  z/dtype argument to jnp.std must be inexact; got z/The 'out' argument to jnp.std is not supported.)rU   r:   r<  rW   rO   r  )rP   r   rj   rA   r&   rk   rM   rf   r   sqrtr?  )r2   rU   r:   rV   r<  rW   rO   r  s           r)   rR  rR    s     eU
#%
44UEBEUBJJ/HPQQ_
O
PP	#ad%J'u4A 
B Br+   c                H    t        d|       } t        | t        |      ||      S )a  Return the peak-to-peak range along a given axis.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      range is computed. If None, the range is computed on the flattened array.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    out: Unused by JAX.

  Returns:
    An array with the range of elements along specified axis of input.

  Examples:
    By default, ``jnp.ptp`` computes the range along all axes.

    >>> x = jnp.array([[1, 3, 5, 2],
    ...                [4, 6, 8, 1],
    ...                [7, 9, 3, 4]])
    >>> jnp.ptp(x)
    Array(8, dtype=int32)

    If ``axis=1``, computes the range along axis 1.

    >>> jnp.ptp(x, axis=1)
    Array([4, 7, 6], dtype=int32)

    To preserve the dimensions of input, you can set ``keepdims=True``.

    >>> jnp.ptp(x, axis=1, keepdims=True)
    Array([[4],
           [7],
           [6]], dtype=int32)
  ptp)r   _ptpr   )r2   rU   rV   rW   s       r)   rV  rV    s(    N ua !	a&t,c8	<<r+   c                    |t        d      t        | ||      }t        | ||      }t        j                  ||      S )Nz/The 'out' argument to jnp.ptp is not supported.r   )rf   r  r  r   r   )r2   rU   rV   rW   r   ys         r)   rW  rW  %  sA     	_
O
PP
14(+!
14(+!	Ar+   c           	         t        d|       } t        t        j                  | t        j                  | d            |t        j                         |      S )a#  Return the number of nonzero elements along a given axis.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      number of nonzeros are counted. If None, counts within the flattened array.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.

  Returns:
    An array with number of nonzeros elements along specified axis of the input.

  Examples:
    By default, ``jnp.count_nonzero`` counts the nonzero values along all axes.

    >>> x = jnp.array([[1, 0, 0, 0],
    ...                [0, 0, 1, 0],
    ...                [1, 1, 1, 0]])
    >>> jnp.count_nonzero(x)
    Array(5, dtype=int32)

    If ``axis=1``, counts along axis 1.

    >>> jnp.count_nonzero(x, axis=1)
    Array([1, 1, 3], dtype=int32)

    To preserve the dimensions of input, you can set ``keepdims=True``.

    >>> jnp.count_nonzero(x, axis=1, keepdims=True)
    Array([[1],
           [1],
           [3]], dtype=int32)
  count_nonzeror   rE  )r   r   r   ne_constr   rC   )r2   rU   rW   s      r)   r[  r[  /  sH    N *!	SVVAszz!Q'(t++-
B Br+   c           	        t        ||       } t        ||      }t        j                  | j                  t
        j                        s || f|||d|S  |t        t        j                  |       t        | |      |       f|||d|}	|rNt        t        t        j                  |       ||      t        j                  | t
        j                        |	      S |	S )Nr   r   )r   rP   r   rA   r:   r&   rk   r   r   _isnanrm   r   r]  rK  )
r2   rN   jnp_reductionrr   nan_if_all_nanrU   rW   rO   kwargsrV   s
             r)   _nan_reductionrc  [  s     tQ!
dE
"%			177BJJ	/PPPPfSZZ],?8,LaP 	K(%	KCI	K##cjjm$B**Q'. . Jr+   c                T    t        | dt        t        j                  |du |||||
      S )aJ
  Return the minimum of the array elements along a given axis, ignoring NaNs.

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

  Args:
    a: Input array.
    axis: int or sequence of ints, default=None. Axis along which the minimum is
      computed. If None, the minimum is computed along the flattened array.
    keepdims: bool, default=False. If True, reduced axes are left in the result
      with size 1.
    initial: int or array, default=None. Initial value for the minimum.
    where: array of boolean dtype, default=None. The elements to be used in the
      minimum. Array should be broadcast compatible to the input. ``initial``
      must be specified when ``where`` is used.
    out: Unused by JAX.

  Returns:
    An array of minimum values along the given axis, ignoring NaNs. If all values
    are NaNs along the given axis, returns ``nan``.

  See also:
    - :func:`jax.numpy.nanmax`: Compute the maximum of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nansum`: Compute the sum of array elements along a given
      axis, ignoring NaNs.
    - :func:`jax.numpy.nanprod`: Compute the product of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nanmean`: Compute the mean of array elements along a given
      axis, ignoring NaNs.

  Examples:

    By default, ``jnp.nanmin`` computes the minimum of elements along the flattened
    array.

    >>> nan = jnp.nan
    >>> x = jnp.array([[1, nan, 4, 5],
    ...                [nan, -2, nan, -4],
    ...                [2, 1, 3, nan]])
    >>> jnp.nanmin(x)
    Array(-4., dtype=float32)

    If ``axis=1``, the maximum will be computed along axis 1.

    >>> jnp.nanmin(x, axis=1)
    Array([ 1., -4.,  1.], dtype=float32)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.nanmin(x, axis=1, keepdims=True)
    Array([[ 1.],
           [-4.],
           [ 1.]], dtype=float32)

    To include only specific elements in computing the maximum, you can use
    ``where``. It can either have same dimension as input

    >>> where=jnp.array([[0, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.nanmin(x, axis=1, keepdims=True, initial=0, where=where)
    Array([[ 0.],
           [-4.],
           [ 0.]], dtype=float32)

    or must be broadcast compatible with input.

    >>> where = jnp.array([[False],
    ...                    [True],
    ...                    [False]])
    >>> jnp.nanmin(x, axis=0, keepdims=True, initial=0, where=where)
    Array([[ 0., -2.,  0., -4.]], dtype=float32)
  nanminNra  rU   rV   rW   rX   rO   )rc  r   r&   r   r   s         r)   re  re  m  s0    \ 
8S"&&D!sX 'u
6 6r+   c                V    t        | dt        t        j                   |du |||||
      S )aC
  Return the maximum of the array elements along a given axis, ignoring NaNs.

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

  Args:
    a: Input array.
    axis: int or sequence of ints, default=None. Axis along which the maximum is
      computed. If None, the maximum is computed along the flattened array.
    keepdims: bool, default=False. If True, reduced axes are left in the result
      with size 1.
    initial: int or array, default=None. Initial value for the maximum.
    where: array of boolean dtype, default=None. The elements to be used in the
      maximum. Array should be broadcast compatible to the input. ``initial``
      must be specified when ``where`` is used.
    out: Unused by JAX.

  Returns:
    An array of maximum values along the given axis, ignoring NaNs. If all values
    are NaNs along the given axis, returns ``nan``.

  See also:
    - :func:`jax.numpy.nanmin`: Compute the minimum of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nansum`: Compute the sum of array elements along a given
      axis, ignoring NaNs.
    - :func:`jax.numpy.nanprod`: Compute the product of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nanmean`: Compute the mean of array elements along a given
      axis, ignoring NaNs.

  Examples:

    By default, ``jnp.nanmax`` computes the maximum of elements along the flattened
    array.

    >>> nan = jnp.nan
    >>> x = jnp.array([[8, nan, 4, 6],
    ...                [nan, -2, nan, -4],
    ...                [-2, 1, 7, nan]])
    >>> jnp.nanmax(x)
    Array(8., dtype=float32)

    If ``axis=1``, the maximum will be computed along axis 1.

    >>> jnp.nanmax(x, axis=1)
    Array([ 8., -2.,  7.], dtype=float32)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.nanmax(x, axis=1, keepdims=True)
    Array([[ 8.],
           [-2.],
           [ 7.]], dtype=float32)

    To include only specific elements in computing the maximum, you can use
    ``where``. It can either have same dimension as input

    >>> where=jnp.array([[0, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.nanmax(x, axis=1, keepdims=True, initial=0, where=where)
    Array([[4.],
           [0.],
           [7.]], dtype=float32)

    or must be broadcast compatible with input.

    >>> where = jnp.array([[True],
    ...                    [False],
    ...                    [False]])
    >>> jnp.nanmax(x, axis=0, keepdims=True, initial=0, where=where)
    Array([[8., 0., 4., 6.]], dtype=float32)
  nanmaxNrf  )rc  r   r&   r   r   s         r)   rh  rh    s2    \ 
8S266''T/!sX 'u
6 6r+   c                f    |t        j                  |d      }t        | dt        dd||||||      S )a
  Return the sum of the array elements along a given axis, ignoring NaNs.

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

  Args:
    a: Input array.
    axis: int or sequence of ints, default=None. Axis along which the sum is
      computed. If None, the sum is computed along the flattened array.
    dtype: The type of the output array. Default=None.
    keepdims: bool, default=False. If True, reduced axes are left in the result
      with size 1.
    initial: int or array, default=None. Initial value for the sum.
    where: array of boolean dtype, default=None. The elements to be used in the
      sum. Array should be broadcast compatible to the input.
    out: Unused by JAX.

  Returns:
    An array containing the sum of array elements along the given axis, ignoring
    NaNs. If all elements along the given axis are NaNs, returns 0.

  See also:
    - :func:`jax.numpy.nanmin`: Compute the minimum of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nanmax`: Compute the maximum of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nanprod`: Compute the product of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nanmean`: Compute the mean of array elements along a given
      axis, ignoring NaNs.

  Examples:

    By default, ``jnp.nansum`` computes the sum of elements along the flattened
    array.

    >>> nan = jnp.nan
    >>> x = jnp.array([[3, nan, 4, 5],
    ...                [nan, -2, nan, 7],
    ...                [2, 1, 6, nan]])
    >>> jnp.nansum(x)
    Array(26., dtype=float32)

    If ``axis=1``, the sum will be computed along axis 1.

    >>> jnp.nansum(x, axis=1)
    Array([12.,  5.,  9.], dtype=float32)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.nansum(x, axis=1, keepdims=True)
    Array([[12.],
           [ 5.],
           [ 9.]], dtype=float32)

    To include only specific elements in computing the sum, you can use ``where``.

    >>> where=jnp.array([[1, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.nansum(x, axis=1, keepdims=True, where=where)
    Array([[7.],
           [7.],
           [9.]], dtype=float32)

    If ``where`` is ``False`` at all elements, ``jnp.nansum`` returns 0 along
    the given axis.

    >>> where = jnp.array([[False],
    ...                    [False],
    ...                    [False]])
    >>> jnp.nansum(x, axis=0, keepdims=True, where=where)
    Array([[0., 0., 0., 0.]], dtype=float32)
  nanprodnansumr   Fra  rU   r:   rV   rW   rX   rO   )r   rj   rc  r   r   s          r)   rk  rk    sB    ^ 44UIFE	8S!E!C( 'u
6 6r+   c                f    |t        j                  |d      }t        | dt        dd||||||      S )aE
  Return the product of the array elements along a given axis, ignoring NaNs.

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

  Args:
    a: Input array.
    axis: int or sequence of ints, default=None. Axis along which the product is
      computed. If None, the product is computed along the flattened array.
    dtype: The type of the output array. Default=None.
    keepdims: bool, default=False. If True, reduced axes are left in the result
      with size 1.
    initial: int or array, default=None. Initial value for the product.
    where: array of boolean dtype, default=None. The elements to be used in the
      product. Array should be broadcast compatible to the input.
    out: Unused by JAX.

  Returns:
    An array containing the product of array elements along the given axis,
    ignoring NaNs. If all elements along the given axis are NaNs, returns 1.

  See also:
    - :func:`jax.numpy.nanmin`: Compute the minimum of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nanmax`: Compute the maximum of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nansum`: Compute the sum of array elements along a given
      axis, ignoring NaNs.
    - :func:`jax.numpy.nanmean`: Compute the mean of array elements along a given
      axis, ignoring NaNs.

  Examples:

    By default, ``jnp.nanprod`` computes the product of elements along the flattened
    array.

    >>> nan = jnp.nan
    >>> x = jnp.array([[nan, 3, 4, nan],
    ...                [5, nan, 1, 3],
    ...                [2, 1, nan, 1]])
    >>> jnp.nanprod(x)
    Array(360., dtype=float32)

    If ``axis=1``, the product will be computed along axis 1.

    >>> jnp.nanprod(x, axis=1)
    Array([12., 15.,  2.], dtype=float32)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.nanprod(x, axis=1, keepdims=True)
    Array([[12.],
           [15.],
           [ 2.]], dtype=float32)

    To include only specific elements in computing the maximum, you can use ``where``.

    >>> where=jnp.array([[1, 0, 1, 0],
    ...                  [0, 0, 1, 1],
    ...                  [1, 1, 1, 0]], dtype=bool)
    >>> jnp.nanprod(x, axis=1, keepdims=True, where=where)
    Array([[4.],
           [3.],
           [2.]], dtype=float32)

    If ``where`` is ``False`` at all elements, ``jnp.nanprod`` returns 1 along
    the given axis.

    >>> where = jnp.array([[False],
    ...                    [False],
    ...                    [False]])
    >>> jnp.nanprod(x, axis=0, keepdims=True, where=where)
    Array([[1., 1., 1., 1.]], dtype=float32)
  rj  r`   Frl  )r   rj   rc  r   r   s          r)   rj  rj  i  sB    \ 44UIFE	9dAe!C( 'u
6 6r+   c           	     R   t        d|       } t        d|      }|t        d      t        j                  | j
                  t        j                        s.t        j                  | j
                  t        j                        rt        | |||||      S | t        j                  | j
                        }nt        j                  |d      }t        j                  t        j                  |             }t        |||||      }t        j                   t#        | ||||      |      }|S )a
  Return the mean of the array elements along a given axis, ignoring NaNs.

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

  Args:
    a: Input array.
    axis: int or sequence of ints, default=None. Axis along which the mean is
      computed. If None, the mean is computed along the flattened array.
    dtype: The type of the output array. Default=None.
    keepdims: bool, default=False. If True, reduced axes are left in the result
      with size 1.
    where: array of boolean dtype, default=None. The elements to be used in
      computing mean. Array should be broadcast compatible to the input.
    out: Unused by JAX.

  Returns:
    An array containing the mean of array elements along the given axis, ignoring
    NaNs. If all elements along the given axis are NaNs, returns ``nan``.

  See also:
    - :func:`jax.numpy.nanmin`: Compute the minimum of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nanmax`: Compute the maximum of array elements along a
      given axis, ignoring NaNs.
    - :func:`jax.numpy.nansum`: Compute the sum of array elements along a given
      axis, ignoring NaNs.
    - :func:`jax.numpy.nanprod`: Compute the product of array elements along a
      given axis, ignoring NaNs.

  Examples:

    By default, ``jnp.nanmean`` computes the mean of elements along the flattened
    array.

    >>> nan = jnp.nan
    >>> x = jnp.array([[2, nan, 4, 3],
    ...                [nan, -2, nan, 9],
    ...                [4, -7, 6, nan]])
    >>> jnp.nanmean(x)
    Array(2.375, dtype=float32)

    If ``axis=1``, mean will be computed along axis 1.

    >>> jnp.nanmean(x, axis=1)
    Array([3. , 3.5, 1. ], dtype=float32)

    If ``keepdims=True``, ``ndim`` of the output will be same of that of the input.

    >>> jnp.nanmean(x, axis=1, keepdims=True)
    Array([[3. ],
           [3.5],
           [1. ]], dtype=float32)

    ``where`` can be used to include only specific elements in computing the mean.

    >>> where = jnp.array([[1, 0, 1, 0],
    ...                    [0, 0, 1, 1],
    ...                    [1, 1, 0, 1]], dtype=bool)
    >>> jnp.nanmean(x, axis=1, keepdims=True, where=where)
    Array([[ 3. ],
           [ 9. ],
           [-1.5]], dtype=float32)

    If ``where`` is ``False`` at all elements, ``jnp.nanmean`` returns ``nan``
    along the given axis.

    >>> where = jnp.array([[False],
    ...                    [False],
    ...                    [False]])
    >>> jnp.nanmean(x, axis=0, keepdims=True, where=where)
    Array([[nan, nan, nan, nan]], dtype=float32)
  nanmeanz3The 'out' argument to jnp.nanmean is not supported.r   r  )rU   r:   rW   rO   r   )r   rP   rf   r   rA   r:   r&   rB   rH   r  r!  rj   r   bitwise_notr_  r   r"  rk  )	r2   rU   r:   rV   rW   rO   nan_maskr#  tds	            r)   ro  ro    s    X y!$!
i
'%_
S
TTqww)V->->qww

-S4XU;;
]##AGG,E44UFCE__SZZ]+(8$eheT*
wwvaUXUKZX"	)r+   c           
         t        d|       } t        d|      }|t        j                  |d      }|t	        d      t        | |||||||      S )a_  Compute the variance of array elements along a given axis, ignoring NaNs.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      variance is computed. If None, variance is computed along flattened array.
    dtype: The type of the output array. Default=None.
    ddof: int, default=0. Degrees of freedom. The divisor in the variance computation
      is ``N-ddof``, ``N`` is number of elements along given axis.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    where: optional, boolean array, default=None. The elements to be used in the
      variance. Array should be broadcast compatible to the input.
    mean: optional, mean of the input array, computed along the given axis.
      If provided, it will be used to compute the variance instead of
      computing it from the input array. If specified, mean must be broadcast-compatible
      with the input array. In the general case, this can be achieved by computing the mean with
      ``keepdims=True`` and ``axis`` matching this function's ``axis`` argument.
    out: Unused by JAX.

  Returns:
    An array containing the variance of array elements along specified axis. If
    all elements along the given axis are NaNs, returns ``nan``.

  See also:
    - :func:`jax.numpy.nanmean`: Compute the mean of array elements over a given
      axis, ignoring NaNs.
    - :func:`jax.numpy.nanstd`: Computed the standard deviation of a given axis,
      ignoring NaNs.
    - :func:`jax.numpy.var`: Compute the variance of array elements along a given
      axis.

  Examples:
    By default, ``jnp.nanvar`` computes the variance along all axes.

    >>> nan = jnp.nan
    >>> x = jnp.array([[1, nan, 4, 3],
    ...                [nan, 2, nan, 9],
    ...                [4, 8, 6, nan]])
    >>> jnp.nanvar(x)
    Array(6.984375, dtype=float32)

    If ``axis=1``, variance is computed along axis 1.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.nanvar(x, axis=1))
    [ 1.56 12.25  2.67]

    To preserve the dimensions of input, you can set ``keepdims=True``.

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.nanvar(x, axis=1, keepdims=True))
    [[ 1.56]
     [12.25]
     [ 2.67]]

    If ``ddof=1``:

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.nanvar(x, axis=1, keepdims=True, ddof=1))
    [[ 2.33]
     [24.5 ]
     [ 4.  ]]

    To include specific elements of the array to compute variance, you can use
    ``where``.

    >>> where = jnp.array([[1, 0, 1, 0],
    ...                    [0, 1, 1, 0],
    ...                    [1, 1, 0, 1]], dtype=bool)
    >>> jnp.nanvar(x, axis=1, keepdims=True, where=where)
    Array([[2.25],
           [0.  ],
           [4.  ]], dtype=float32)
  nanvarz2The 'out' argument to jnp.nanvar is not supported.rU   r:   rV   rD  rW   rO   rA  )r   rP   r   rj   rf   _nanvarr2   rU   r:   rV   rD  rW   rO   r  s           r)   rt  rt    sa    d x#!
h
&%
44UHEE_
R
SS	U$Y^gk	llr+   ru  c                  t        | j                  |      \  }}t        j                  |       j	                  |      } |t        | ||d|      }nt        d|      j	                  |      }t        t        j                  |       dt        j                  | |            }	t        j                  |	j                  t        j                        r=t        j                  t        j                  |	t        j                   |	                  }	nt        j"                  |	      }	t%        t        j&                  t        j                  |             |||      }
|
|z
  }
t        j(                  |
t        j*                  |
            }t%        |	|||      }t        |t        j,                  |      }t        |d|
      }t        j.                  |t        j0                  ||j                              }t        j0                  ||      S )NTr   rt  r   r   )rW   rO   r`   )rG  r:   r   ro   r   ro  r   r   r_  r   r   rA   r&   r   r   r   rH  rI  r   rp  le_zerorK  r"  rl   )r2   rU   r:   rV   rD  rW   rO   rA  rv   rL  r#  normalizer_maskrw   divisors                 r)   rv  rv  t  sy    0?U	kk!n-.!^Q$5ERFh/667HIFCJJqM1cgga&89(x~~r'9'9:xx#((8*<=>Hzz(#H3??3::a=1xu>*D *FF:syy'<=/x>&/26662&?Az2'776333GV\\JK&		!	!&%	00r+   c                    t        d|       } t        d|      }|t        j                  |d      }|t	        d      t        j                  t        | ||||||            S )a   Compute the standard deviation along a given axis, ignoring NaNs.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      standard deviation is computed. If None, standard deviaiton is computed
      along flattened array.
    dtype: The type of the output array. Default=None.
    ddof: int, default=0. Degrees of freedom. The divisor in the standard deviation
      computation is ``N-ddof``, ``N`` is number of elements along given axis.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    where: optional, boolean array, default=None. The elements to be used in the
      standard deviation. Array should be broadcast compatible to the input.
    mean: optional, mean of the input array, computed along the given axis.
      If provided, it will be used to compute the standard deviation instead of
      computing it from the input array. If specified, mean must be broadcast-compatible
      with the input array. In the general case, this can be achieved by computing the mean with
      ``keepdims=True`` and ``axis`` matching this function's ``axis`` argument.
    out: Unused by JAX.

  Returns:
    An array containing the standard deviation of array elements along the given
    axis. If all elements along the given axis are NaNs, returns ``nan``.

  See also:
    - :func:`jax.numpy.nanmean`: Compute the mean of array elements over a given
      axis, ignoring NaNs.
    - :func:`jax.numpy.nanvar`: Compute the variance along the given axis, ignoring
      NaNs values.
    - :func:`jax.numpy.std`: Computed the standard deviation along the given axis.

  Examples:
    By default, ``jnp.nanstd`` computes the standard deviation along flattened array.

    >>> nan = jnp.nan
    >>> x = jnp.array([[3, nan, 4, 5],
    ...                [nan, 2, nan, 7],
    ...                [2, 1, 6, nan]])
    >>> jnp.nanstd(x)
    Array(1.9843135, dtype=float32)

    If ``axis=0``, computes standard deviation along axis 0.

    >>> jnp.nanstd(x, axis=0)
    Array([0.5, 0.5, 1. , 1. ], dtype=float32)

    To preserve the dimensions of input, you can set ``keepdims=True``.

    >>> jnp.nanstd(x, axis=0, keepdims=True)
    Array([[0.5, 0.5, 1. , 1. ]], dtype=float32)

    If ``ddof=1``:

    >>> with jnp.printoptions(precision=2, suppress=True):
    ...   print(jnp.nanstd(x, axis=0, keepdims=True, ddof=1))
    [[0.71 0.71 1.41 1.41]]

    To include specific elements of the array to compute standard deviation, you
    can use ``where``.

    >>> where=jnp.array([[1, 0, 1, 0],
    ...                  [0, 1, 0, 1],
    ...                  [1, 1, 0, 1]], dtype=bool)
    >>> jnp.nanstd(x, axis=0, keepdims=True, where=where)
    Array([[0.5, 0.5, 0. , 0. ]], dtype=float32)
  nanstdz2The 'out' argument to jnp.nanstd is not supported.)rU   r:   rD  rW   rO   r  )r   rP   r   rj   rf   r   rT  rt  rw  s           r)   r~  r~    sp    T x#!
h
&%
44UHEE_
R
SS	&U"*%dD 
E Er+   c                  $    e Zd Z	 	 d	 	 	 	 	 ddZy)CumulativeReductionNc                     y r@   ra   )selfr2   rU   r:   rV   s        r)   __call__zCumulativeReduction.__call__  s    KNr+   NNN)
r2   r   rU   Axisr:   DTypeLike | NonerV   Nonereturnr   )__name__
__module____qualname__r  ra   r+   r)   r  r    s)    04;?O&O48ODIOr+   r  c	                   t        | |      }|t        d|  d      |t        |      r*t        j                  |t        j                  |      f      }|d}t        t        j                  |            }	t        |	      }
t        ||
      }|r4t        t        j                  |      t        j                  ||      |      }|j                  }|4|}|s$t        j                   |t
        j"                        rQt%        |      }nEt        j&                  ||       }t        j                   |t
        j"                        rt%        |      }|t
        j"                  k7  rE|t
        j"                  k(  r2t        j(                  |      j+                  t
        j"                        }t        j,                  ||      } |||      }|Ht        j                   |t
        j"                        r$t        j,                  |t
        j"                        }|S )z7Helper function for implementing cumulative reductions.r]   z is not supportedr   )r   rf   r*   r   r7  r&   r  r   rd   r   r   r   r_  r]  r:   r   rA   rB   rI   rj   ro   r   rl   )rN   	reductionr2   rU   r:   rV   fill_nan
fill_valuer[   r  num_dimsa_typeresult_typerw   s                 r)   _cumulative_reductionr    s    tQ!_
 :4&@QR
SS	\Yq\A
}%A	\D!'\(	4	*$szz!}cjjJ7;A''&
]K6,,["((C*;7k::5$GKbhh/*;7krxxERXX-Abhh'A	q+.!Q& 6,,UBHH=%%fbhh7F	-r+   rU   r:   c                >    t        dt        j                  | |||      S )aY  Cumulative sum of elements along an axis.

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

  Args:
    a: N-dimensional array to be accumulated.
    axis: integer axis along which to accumulate. If None (default), then
      array will be flattened and accumulated along the flattened axis.
    dtype: optionally specify the dtype of the output. If not specified,
      then the output dtype will match the input dtype.
    out: unused by JAX

  Returns:
    An array containing the accumulated sum along the given axis.

  See also:
    - :func:`jax.numpy.cumulative_sum`: cumulative sum via the array API standard.
    - :meth:`jax.numpy.add.accumulate`: cumulative sum via ufunc methods.
    - :func:`jax.numpy.nancumsum`: cumulative sum ignoring NaN values.
    - :func:`jax.numpy.sum`: sum along axis

  Examples:
    >>> x = jnp.array([[1, 2, 3],
    ...                [4, 5, 6]])
    >>> jnp.cumsum(x)  # flattened cumulative sum
    Array([ 1,  3,  6, 10, 15, 21], dtype=int32)
    >>> jnp.cumsum(x, axis=1)  # cumulative sum along axis 1
    Array([[ 1,  3,  6],
           [ 4,  9, 15]], dtype=int32)
  cumsumr  r   r  r2   rU   r:   rV   s       r)   r  r    s     D 
x)<)<auc	RRr+   c                >    t        dt        j                  | |||      S )a8  Cumulative product of elements along an axis.

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

  Args:
    a: N-dimensional array to be accumulated.
    axis: integer axis along which to accumulate. If None (default), then
      array will be flattened and accumulated along the flattened axis.
    dtype: optionally specify the dtype of the output. If not specified,
      then the output dtype will match the input dtype.
    out: unused by JAX

  Returns:
    An array containing the accumulated product along the given axis.

  See also:
    - :meth:`jax.numpy.multiply.accumulate`: cumulative product via ufunc methods.
    - :func:`jax.numpy.nancumprod`: cumulative product ignoring NaN values.
    - :func:`jax.numpy.prod`: product along axis

  Examples:
    >>> x = jnp.array([[1, 2, 3],
    ...                [4, 5, 6]])
    >>> jnp.cumprod(x)  # flattened cumulative product
    Array([  1,   2,   6,  24, 120, 720], dtype=int32)
    >>> jnp.cumprod(x, axis=1)  # cumulative product along axis 1
    Array([[  1,   2,   6],
           [  4,  20, 120]], dtype=int32)
  cumprodr  r   r  r  s       r)   r  r  :  s!    B 
y,*>*>4PS	TTr+   c           
     D    t        dt        j                  | |||dd      S )av  Cumulative sum of elements along an axis, ignoring NaN values.

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

  Args:
    a: N-dimensional array to be accumulated.
    axis: integer axis along which to accumulate. If None (default), then
      array will be flattened and accumulated along the flattened axis.
    dtype: optionally specify the dtype of the output. If not specified,
      then the output dtype will match the input dtype.
    out: unused by JAX

  Returns:
    An array containing the accumulated sum along the given axis.

  See also:
    - :func:`jax.numpy.cumsum`: cumulative sum without ignoring NaN values.
    - :func:`jax.numpy.cumulative_sum`: cumulative sum via the array API standard.
    - :meth:`jax.numpy.add.accumulate`: cumulative sum via ufunc methods.
    - :func:`jax.numpy.sum`: sum along axis

  Examples:
    >>> x = jnp.array([[1., 2., jnp.nan],
    ...                [4., jnp.nan, 6.]])

    The standard cumulative sum will propagate NaN values:

    >>> jnp.cumsum(x)
    Array([ 1.,  3., nan, nan, nan, nan], dtype=float32)

    :func:`~jax.numpy.nancumsum` will ignore NaN values, effectively replacing
    them with zeros:

    >>> jnp.nancumsum(x)
    Array([ 1.,  3.,  3.,  7.,  7., 13.], dtype=float32)

    Cumulative sum along axis 1:

    >>> jnp.nancumsum(x, axis=1)
    Array([[ 1.,  3.,  3.],
           [ 4.,  4., 10.]], dtype=float32)
  	nancumsumTr   r  r  r  r  s       r)   r  r  ^  s*    \ 
{L,?,?D%QT(,
< <r+   c           
     D    t        dt        j                  | |||dd      S )aJ  Cumulative product of elements along an axis, ignoring NaN values.

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

  Args:
    a: N-dimensional array to be accumulated.
    axis: integer axis along which to accumulate. If None (default), then
      array will be flattened and accumulated along the flattened axis.
    dtype: optionally specify the dtype of the output. If not specified,
      then the output dtype will match the input dtype.
    out: unused by JAX

  Returns:
    An array containing the accumulated product along the given axis.

  See also:
    - :func:`jax.numpy.cumprod`: cumulative product without ignoring NaN values.
    - :meth:`jax.numpy.multiply.accumulate`: cumulative product via ufunc methods.
    - :func:`jax.numpy.prod`: product along axis

  Examples:
    >>> x = jnp.array([[1., 2., jnp.nan],
    ...                [4., jnp.nan, 6.]])

    The standard cumulative product will propagate NaN values:

    >>> jnp.cumprod(x)
    Array([ 1.,  2., nan, nan, nan, nan], dtype=float32)

    :func:`~jax.numpy.nancumprod` will ignore NaN values, effectively replacing
    them with ones:

    >>> jnp.nancumprod(x)
    Array([ 1.,  2.,  2.,  8.,  8., 48.], dtype=float32)

    Cumulative product along axis 1:

    >>> jnp.nancumprod(x, axis=1)
    Array([[ 1.,  2.,  2.],
           [ 4.,  4., 24.]], dtype=float32)
  
nancumprodTr`   r  r  r  s       r)   r  r    s*    Z 
|\-A-A1dESV(,
< <r+   c           	     B    t        dt        j                  | |||d      S )z:Utility function to compute cumsum with integer promotion._cumsum_with_promotionT)r[   r  r  s       r)   r  r    s)     
79L9L $sT
K Kr+   )rU   r:   include_initialc                 t        d|       } | j                  dk(  rt        d      |*d}| j                  dkD  rt        d| j                   d      t        || j                        }|t	        j
                  |      }t        | ||      }|rSt        | j                        }d||<   t        j                  t        j                  |d|j                        |g|	      }|S )
a  Cumulative sum along the axis of an array.

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

  Args:
    x: N-dimensional array
    axis: integer axis along which to accumulate. If ``x`` is one-dimensional,
      this argument is optional and defaults to zero.
    dtype: optional dtype of the output.
    include_initial: if True, then include the initial value in the cumulative
      sum. Default is False.

  Returns:
    An array containing the accumulated values.

  See Also:
    - :func:`jax.numpy.cumsum`: alternative API for cumulative sum.
    - :func:`jax.numpy.nancumsum`: cumulative sum while ignoring NaN values.
    - :func:`jax.numpy.add.accumulate`: cumulative sum via the ufunc API.

  Examples:
    >>> x = jnp.array([[1, 2, 3],
    ...                [4, 5, 6]])
    >>> jnp.cumulative_sum(x, axis=1)
    Array([[ 1,  3,  6],
           [ 4,  9, 15]], dtype=int32)
    >>> jnp.cumulative_sum(x, axis=1, include_initial=True)
    Array([[ 0,  1,  3,  6],
           [ 0,  4,  9, 15]], dtype=int32)
  cumulative_sumr   zhThe input must be non-scalar to take a cumulative sum, however a scalar value or scalar array was given.r`   The input array has rank o, however axis was not set to an explicit value. The axis argument is only optional for one-dimensional arrays.r  r=   	dimension)r   r.   rM   r   r   rj   r  r   rd   r   concatenater4  r:   r   rU   r:   r  rV   zeros_shapes         r)   r  r    s    F '+!VVq[
0  
\Dvvz
#AFF8 , 	 
 
4	($
44U;Eqt59#qww-KK
//
xxQcii0#6C 
*r+   c                 t        d|       } | j                  dk(  rt        d      |*d}| j                  dkD  rt        d| j                   d      t        || j                        }|t	        j
                  |      }t        dt        j                  | ||      }|rSt        | j                        }d||<   t        j                  t        j                  |d|j                        |g|      }|S )	a.  Cumulative product along the axis of an array.

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

  Args:
    x: N-dimensional array
    axis: integer axis along which to accumulate. If ``x`` is one-dimensional,
      this argument is optional and defaults to zero.
    dtype: optional dtype of the output.
    include_initial: if True, then include the initial value in the cumulative
      product. Default is False.

  Returns:
    An array containing the accumulated values.

  See Also:
    - :func:`jax.numpy.cumprod`: alternative API for cumulative product.
    - :func:`jax.numpy.nancumprod`: cumulative product while ignoring NaN values.
    - :func:`jax.numpy.multiply.accumulate`: cumulative product via the ufunc API.

  Examples:
    >>> x = jnp.array([[1, 2, 3],
    ...                [4, 5, 6]])
    >>> jnp.cumulative_prod(x, axis=1)
    Array([[  1,   2,   6],
           [  4,  20, 120]], dtype=int32)
    >>> jnp.cumulative_prod(x, axis=1, include_initial=True)
    Array([[  1,   1,   2,   6],
           [  1,   4,  20, 120]], dtype=int32)
  cumulative_prodr   zlThe input must be non-scalar to take a cumulative product, however a scalar value or scalar array was given.r`   r  r  r=   r  )r   r.   rM   r   r   rj   r  r   r  r   rd   r   r  r4  r:   r  s         r)   r  r  	  s    F (!,!VVq[
0  
\Dvvz
#AFF8 , 	 
 
4	($
44U;E/1E1Eq$PUV#qww-KK
//
xxQcii0#6C 
*r+   )rU   overwrite_inputinterpolationrW   method)r  c                   t        d| |      \  } }|s|t        d      t        |t              st	        d      t        t        j                  |       t        j                  |      |||d      S )a  Compute the quantile of the data along the specified axis.

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

  Args:
    a: N-dimensional array input.
    q: scalar or 1-dimensional array specifying the desired quantiles. ``q``
      should contain floating-point values between ``0.0`` and ``1.0``.
    axis: optional axis or tuple of axes along which to compute the quantile
    out: not implemented by JAX; will error if not None
    overwrite_input: not implemented by JAX; will error if not False
    method: specify the interpolation method to use. Options are one of
      ``["linear", "lower", "higher", "midpoint", "nearest"]``.
      default is ``linear``.
    keepdims: if True, then the returned array will have the same number of
      dimensions as the input. Default is False.

  Returns:
    An array containing the specified quantiles along the specified axes.

  See also:
    - :func:`jax.numpy.nanquantile`: compute the quantile while ignoring NaNs
    - :func:`jax.numpy.percentile`: compute the percentile (0-100)

  Examples:
    Computing the median and quartiles of an array, with linear interpolation:

    >>> x = jnp.arange(10)
    >>> q = jnp.array([0.25, 0.5, 0.75])
    >>> jnp.quantile(x, q)
    Array([2.25, 4.5 , 6.75], dtype=float32)

    Computing the quartiles using nearest-value interpolation:

    >>> jnp.quantile(x, q, method='nearest')
    Array([2., 4., 7.], dtype=float32)
  quantilezGjax.numpy.quantile does not support overwrite_input=True or out != NonezPquantile() argument interpolation was removed in JAX v0.8.0. Use method instead.Fr   rM   r   r   r   	_quantiler   ro   r2   qrU   rV   r  r  rW   r  s           r)   r  r  G	  ss    T 
*a	+$!Q
 & ' ' 
M=	1
 3 4 4	3;;q>3;;q>45	QQr+   c                   t        d| |      \  } }|s|d}t        |      t        |t              st	        d      t        t        j                  |       t        j                  |      |||d      S )a  Compute the quantile of the data along the specified axis, ignoring NaNs.

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

  Args:
    a: N-dimensional array input.
    q: scalar or 1-dimensional array specifying the desired quantiles. ``q``
      should contain floating-point values between ``0.0`` and ``1.0``.
    axis: optional axis or tuple of axes along which to compute the quantile
    out: not implemented by JAX; will error if not None
    overwrite_input: not implemented by JAX; will error if not False
    method: specify the interpolation method to use. Options are one of
      ``["linear", "lower", "higher", "midpoint", "nearest"]``.
      default is ``linear``.
    keepdims: if True, then the returned array will have the same number of
      dimensions as the input. Default is False.

  Returns:
    An array containing the specified quantiles along the specified axes.

  See also:
    - :func:`jax.numpy.quantile`: compute the quantile without ignoring nans
    - :func:`jax.numpy.nanpercentile`: compute the percentile (0-100)

  Examples:
    Computing the median and quartiles of a 1D array:

    >>> x = jnp.array([0, 1, 2, jnp.nan, 3, 4, 5, 6])
    >>> q = jnp.array([0.25, 0.5, 0.75])

    Because of the NaN value, :func:`jax.numpy.quantile` returns all NaNs,
    while :func:`~jax.numpy.nanquantile` ignores them:

    >>> jnp.quantile(x, q)
    Array([nan, nan, nan], dtype=float32)
    >>> jnp.nanquantile(x, q)
    Array([1.5, 3. , 4.5], dtype=float32)
  nanquantilezJjax.numpy.nanquantile does not support overwrite_input=True or out != NonezSnanquantile() argument interpolation was removed in JAX v0.8.0. Use method instead.Tr  )	r2   r  rU   rV   r  r  rW   r  rN  s	            r)   r  r  |	  sp    V 
-A	.$!QC
S/	M=	1
 3 4 4	3;;q>3;;q>44	PPr+   c                Z  ! |dvrt        d      t        |       \  } g }t        j                  | j                  t
        j                        rt        d      &|rdg| j                  z  }| j                         } dnt        t              r`t        | j                        }| j                  !t        !fdD              t        t                    t              k7  rt        d      D ]  }d||<   	 t        t        !            t              z
  }t        t        !            }	t!        t#        |            D ]  \  }
}|	|   |	|
   c|	|
<   |	|<    t        fdt!        | j                        D              }t        fd	t!        | j                        D              }t%        j&                  | |t)        j*                  |      fz   |	      } t-        d
| j                        nt-        | j                        |j                  }|j                  }|dkD  rt        d|j                         | j                  }|rt/        t%        j0                  |       t
        j2                  |       } t%        j4                  |       } t7        t%        j8                  t%        j0                  |             |j                  |      }|j                  }t%        j:                  |t        t        |t        |      |z                     }t%        j:                  |t        t        |                  }t%        j<                  |t%        j>                  |t%        j@                  |d                  }t%        jB                  |      }t%        jD                  |      }t%        j>                  ||      }t%        j>                  t%        j@                  |d      |      }t%        jF                  t%        j@                  |d      t%        jH                  ||dz
              }t%        jF                  t%        j@                  |d      t%        jH                  ||dz
              }t%        jJ                  |tL              }t%        jJ                  |tL              }||z   }t        t        |            D cg c]   }t%        jN                  tL        |||z         " }}|r||<   n|jQ                  |       | t        |         }||<   | t        |         }ntS        jT                  d      5  t/        tW        t%        j0                  |       d      t
        j2                  |       } d d d        t%        j4                  |       } t%        jJ                  |   t%        jX                  |            }t%        j<                  ||dz
        }t%        jB                  |      }t%        jD                  |      }t%        j>                  ||      }t%        j>                  t%        j@                  |d      |      }t%        jZ                  t%        j@                  |d      ||dz
        }t%        jZ                  t%        j@                  |d      ||dz
        }t%        jJ                  |tL              }t%        jJ                  |tL              }t        |      }d|<   t]        j^                  t        t        ||rt        |      |z   nt        |      |z   dz
              |rdnff      }t]        j`                  | |d   ||      }t]        j`                  | |d   ||      }|dk(  rDt%        jb                  ||j                  d      }t%        jb                  ||j                  d      }|dk(  rqt%        jd                  t%        j<                  |jg                  |j                        |      t%        j<                  |jg                  |j                        |            }n|dk(  r|}n|dk(  r|}n|dk(  rBt%        jh                  |t%        j@                  |d            } t%        jj                  | ||      }nS|dk(  r?t%        j<                  t%        jd                  ||      t%        j@                  |d            }nt        d|d      |r3|r1|dkD  rt        j                  |      d   g|}|j'                  |      }t%        jJ                  || j                        S c c}w # 1 sw Y   xY w)N)linearlowerhighermidpointnearestzHmethod can only be 'linear', 'lower', 'higher', 'midpoint', or 'nearest'zLquantile does not support complex input, as the operation is poorly defined.r`   r   c              3  6   K   | ]  }t        |        y wr@   r|   )rb   r0  nds     r)   re   z_quantile.<locals>.<genexpr>	  s     :r"2r*:s   zrepeated axisc              3  2   K   | ]  \  }}|vs|  y wr@   ra   rb   idxr   rU   s      r)   re   z_quantile.<locals>.<genexpr>	  s     TUSCtOqT   c              3  2   K   | ]  \  }}|v s|  y wr@   ra   r  s      r)   re   z_quantile.<locals>.<genexpr>	  s     Iec!SD[Ir  r   z$q must be have rank <= 1, got shape r  rE  FTr   ra   )offset_dimscollapsed_slice_dimsstart_index_map).N)dimension_numbersslice_sizes)r   )broadcast_dimensionsr  r  r  r        ?r  zmethod=z not recognized)6rM   r   r   rA   r:   r&   r   r.   ravelr   r   r   rd   r   r   r/   r6  sortedr   r7  r5  r   r   r   r_  rK  sortr   rp  rp   r   r   r]  floorceilr   r   rl   r   broadcasted_iotar0   r   rJ  r   _dtypeclamplax_slicingGatherDimensionNumbersgatherbroadcast_in_dimr   r   ry  r   )"r2   r  rU   r  rW   squash_nanskeepdimr0  keepr3  r5   sdo_not_touch_shapetouch_shapeq_shapeq_ndimr  countsshape_after_reductionlowhighhigh_weight
low_weight	out_shaper2  r   	low_value
high_valuenr  dnumsrw   predr  s"     `                              @r)   r  r  	  s=   GG
_
``a "!'qww 2 23
c
dd	\affg		AD$177mG	
B:T::D
3t9~T"'' gbk uRy>CI%DeBiJ&,' B1%/]JqM"jmZ]BTi.@TTIy'9IIKA)TYY{-C,EEzRAR(DT166*DGG'66&aZ
;AGG9E
FFGG'szz!}bffa(Ad#AA/d!''T\]F"LLuVS!67&@A	B	DA__VU5=%9:F37763::a#345A
))A,C88A;D''!S/KK3[AJ
''#**S!$cggc6A:&>
?C773::dA&fqj(ABD

"
"3
,C##D#.D//Ic"789; !!#y#,? ;E ;eDkll4%,IE$K5<J			5	! J
SZZ]=rvvq
IaJd#A  

1>A1q5A
))A,C88A;D''!S/KK3[AJ
))CJJsA&QU
3C99SZZa($A6D

"
"3
,C##D#.Dw-KK..!)Gvs7|f/Dq/HJ K "*2wgE ""1c)n/:<I##AtI%0;=J{''
IOO=ACj((j6F6F=ACk xWWSWWY--agg6
CWWZ..qww7EGFFF66+szz+s;<DZZi4FWWSWWY
3SZZ	35OPF
{/2
33'z!Q*'*g^^G$F		!	!&!''	22{;J Js   3%d,;d  d*c          	         t        d| |      \  } }t        |      \  }t        |t              st	        d      t        | |dz  |||||      S )a  Compute the percentile of the data along the specified axis.

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

  Args:
    a: N-dimensional array input.
    q: scalar or 1-dimensional array specifying the desired quantiles. ``q``
      should contain integer or floating point values between ``0`` and ``100``.
    axis: optional axis or tuple of axes along which to compute the quantile
    out: not implemented by JAX; will error if not None
    overwrite_input: not implemented by JAX; will error if not False
    method: specify the interpolation method to use. Options are one of
      ``["linear", "lower", "higher", "midpoint", "nearest"]``.
      default is ``linear``.
    keepdims: if True, then the returned array will have the same number of
      dimensions as the input. Default is False.

  Returns:
    An array containing the specified percentiles along the specified axes.

  See also:
    - :func:`jax.numpy.quantile`: compute the quantile (0.0-1.0)
    - :func:`jax.numpy.nanpercentile`: compute the percentile while ignoring NaNs

  Examples:
    Computing the median and quartiles of a 1D array:

    >>> x = jnp.array([0, 1, 2, 3, 4, 5, 6])
    >>> q = jnp.array([25, 50, 75])
    >>> jnp.percentile(x, q)
    Array([1.5, 3. , 4.5], dtype=float32)

    Computing the same percentiles with nearest rather than linear interpolation:

    >>> jnp.percentile(x, q, method='nearest')
    Array([1., 3., 4.], dtype=float32)
  
percentilezRpercentile() argument interpolation was removed in JAX v0.8.0. Use method instead.d   rU   rV   r  r  rW   )r   r   r   r   r   r  r  s           r)   r  r  /
  s_    V 
,1	-$!Qa "!	M=	1
 3 4 4	!QW4S/(
4 4r+   c          	         t        d| |      \  } }t        |      \  }|dz  }t        |t              st	        d      t        | ||||||      S )a  Compute the percentile of the data along the specified axis, ignoring NaN values.

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

  Args:
    a: N-dimensional array input.
    q: scalar or 1-dimensional array specifying the desired quantiles. ``q``
      should contain integer or floating point values between ``0`` and ``100``.
    axis: optional axis or tuple of axes along which to compute the quantile
    out: not implemented by JAX; will error if not None
    overwrite_input: not implemented by JAX; will error if not False
    method: specify the interpolation method to use. Options are one of
      ``["linear", "lower", "higher", "midpoint", "nearest"]``.
      default is ``linear``.
    keepdims: if True, then the returned array will have the same number of
      dimensions as the input. Default is False.

  Returns:
    An array containing the specified percentiles along the specified axes.

  See also:
    - :func:`jax.numpy.nanquantile`: compute the nan-aware quantile (0.0-1.0)
    - :func:`jax.numpy.percentile`: compute the percentile without special
      handling of NaNs.

  Examples:
    Computing the median and quartiles of a 1D array:

    >>> x = jnp.array([0, 1, 2, jnp.nan, 3, 4, 5, 6])
    >>> q = jnp.array([25, 50, 75])

    Because of the NaN value, :func:`jax.numpy.percentile` returns all NaNs,
    while :func:`~jax.numpy.nanpercentile` ignores them:

    >>> jnp.percentile(x, q)
    Array([nan, nan, nan], dtype=float32)
    >>> jnp.nanpercentile(x, q)
    Array([1.5, 3. , 4.5], dtype=float32)
  nanpercentiler  zUnanpercentile() argument interpolation was removed in JAX v0.8.0. Use method instead.r  )r   r   r   r   r   r  r  s           r)   r  r  e
  sd    Z 
/1a	0$!Qa "!#g!	M=	1
 3 4 4	Q#"X
7 7r+   )rU   r  rW   c           	     >    t        d|       } t        | d||||d      S )aF  Return the median of array elements along a given axis.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      median to be computed. If None, median is computed for the flattened array.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    out: Unused by JAX.
    overwrite_input: Unused by JAX.

  Returns:
    An array of the median along the given axis.

  See also:
    - :func:`jax.numpy.mean`: Compute the mean of array elements over a given axis.
    - :func:`jax.numpy.max`: Compute the maximum of array elements over given axis.
    - :func:`jax.numpy.min`: Compute the minimum of array elements over given axis.

  Examples:
    By default, the median is computed for the flattened array.

    >>> x = jnp.array([[2, 4, 7, 1],
    ...                [3, 5, 9, 2],
    ...                [6, 1, 8, 3]])
    >>> jnp.median(x)
    Array(3.5, dtype=float32)

    If ``axis=1``, the median is computed along axis 1.

    >>> jnp.median(x, axis=1)
    Array([3. , 4. , 4.5], dtype=float32)

    If ``keepdims=True``, ``ndim`` of the output is equal to that of the input.

    >>> jnp.median(x, axis=1, keepdims=True)
    Array([[3. ],
           [4. ],
           [4.5]], dtype=float32)
  medianr  r  rU   rV   r  rW   r  )r   r  r2   rU   rV   r  rW   s        r)   r  r  
  s-    ^ x#!	!Sto#J
8 8r+   c           	     >    t        d|       } t        | d||||d      S )a!  Return the median of array elements along a given axis, ignoring NaNs.

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

  Args:
    a: input array.
    axis: optional, int or sequence of ints, default=None. Axis along which the
      median to be computed. If None, median is computed for the flattened array.
    keepdims: bool, default=False. If true, reduced axes are left in the result
      with size 1.
    out: Unused by JAX.
    overwrite_input: Unused by JAX.

  Returns:
    An array containing the median along the given axis, ignoring NaNs. If all
    elements along the given axis are NaNs, returns ``nan``.

  See also:
    - :func:`jax.numpy.nanmean`: Compute the mean of array elements over a given
      axis, ignoring NaNs.
    - :func:`jax.numpy.nanmax`: Compute the maximum of array elements over given
      axis, ignoring NaNs.
    - :func:`jax.numpy.nanmin`: Compute the minimum of array elements over given
      axis, ignoring NaNs.

  Examples:
    By default, the median is computed for the flattened array.

    >>> nan = jnp.nan
    >>> x = jnp.array([[2, nan, 7, nan],
    ...                [nan, 5, 9, 2],
    ...                [6, 1, nan, 3]])
    >>> jnp.nanmedian(x)
    Array(4., dtype=float32)

    If ``axis=1``, the median is computed along axis 1.

    >>> jnp.nanmedian(x, axis=1)
    Array([4.5, 5. , 3. ], dtype=float32)

    If ``keepdims=True``, ``ndim`` of the output is equal to that of the input.

    >>> jnp.nanmedian(x, axis=1, keepdims=True)
    Array([[4.5],
           [5. ],
           [3. ]], dtype=float32)
  	nanmedianr  r  r  )r   r  r  s        r)   r  r  
  s.    h {A&!	Q$C%4x&
( (r+   )r(   r   r  rL   )r2   r   r3   r   r4   r   r  r   )r:   r   r  r   )r:   r   r  r   )rN   r   rO   ArrayLike | Noner  zArray | None)"r2   r   rN   r   rq   ReductionOprr   r   rQ   rL   rR   zCallable[[Array], Array] | NonerS   zReductionOp | NonerT   rL   rU   r  r:   r  rV   r  rW   rL   rX   r  rY   r  rZ   zCallable[..., Array] | Noner[   rL   r  r   )r2   r   rU   r  )r2   r   rr   r   r  z
np.ndarray)r   r   r  r   )r   r   r  r   )r   r  r  r  )NNNFNNT)r2   r   rU   r  r:   r  rV   r  rW   rL   rX   r  rO   r  r[   rL   r  r   )NNNFNN)r2   r   rU   r  r:   r  rV   r  rW   rL   rX   r  rO   r  r  r   )NNFNN)r2   r   rU   r  rV   r  rW   rL   rX   r  rO   r  r  r   )NNF)r2   r   rU   r  rV   r  rW   rL   rO   r  r  r   )r2   r   rU   zint | Sequence[int])NNNF)r2   r   rU   r  r:   r  rV   r  rW   rL   rO   r  r  r   )
r2   r   rU   r  rW   rL   rO   r  r:   r   )r2   r   rU   r  r:   r  rV   r  rW   rL   rT   rL   rO   r  r  r   )NNFF)r2   r   rU   r  r&  r  r'  zLiteral[False]rW   rL   r  r   )NN)r2   r   rU   r  r&  r  r'  zLiteral[True]rW   rL   r  r   )r2   r   rU   r  r&  r  r'  rL   rW   rL   r  zArray | tuple[Array, Array])NNNr   F)r2   r   rU   r  r:   r  rV   r  rD  r   rW   rL   rO   r  r  r  r<  zint | float | Noner  r   )r2   r   rU   r  r:   r  rV   r  r<  int | floatrW   rL   rO   r  rA  r  r  r   )r   r   r:   r  r  ztuple[DType, DType])r2   r   rU   r  r:   r  rV   r  r<  r  rW   rL   rO   r  r  r  r  r   )
r2   r   rU   r  rV   r  rW   rL   r  r   )
r2   r   rU   r  rV   r  rW   rL   r  r   )NF)r2   r   rU   r  rW   rL   r  r   )NFN)r2   r   rN   r   r`  Callable[..., Array]rr   r   ra  rL   rU   r  rW   rL   rO   r  r  r   )NNNFN)NNNr   FNN)r2   r   rU   r  r:   r  rV   r  rD  r   rW   rL   rO   r  r  r  r  r   )r2   r   rU   r  r:   r  rV   r  rD  r   rW   rL   rO   r  rA  r  r  r   )NFr   F)rN   r   r  r  r2   r   rU   
int | Noner:   r  rV   r  r  rL   r  r   r[   rL   r  r   r  )
r2   r   rU   r  r:   r  rV   r  r  r   )
r   r   rU   r  r:   r  r  rL   r  r   )NNFr  F)r2   r   r  r   rU   int | tuple[int, ...] | NonerV   r  r  rL   r  r   rW   rL   r  r   r  r   )r2   r   r  r   rU   r  r  r   rW   rL   r  rL   r  r   )r2   r   rU   r  rV   r  r  rL   rW   rL   r  r   )
__future__r   builtinscollections.abcr   r   r5  r   typingr   r   r   r	   r
   numpyr&   jax._srcr   r   r   r   jax._src.numpy.utilr   r   r   r   r   jax._src.laxr   r   r   r   r   r   r   r  jax._src.typingr   r   r   r   r   jax._src.utilr   r   r    r!   exportr   ri   r   r  r*   r7   r>   rI   rP   r  ry   r   rh   rm   r   r   r   r   jitr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r(  r,  r?  rB  rG  rP  rR  rV  rW  r[  rc  re  rh  rk  rj  ro  rt  rv  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  ra   r+   r)   <module>r     s   #  .   : :     < < & # + 1 / M M b b 
K	 || S(3-%&B
 
 Sz3' (,:>-127 Dd %4*.>B(-AA $AA7AA +AA ,0	AA
 AA
 *:AA
 HLAA AA 1AAA (AA !<AA "&AA 38AAFV"G"5
,
	= 	JSWXKO38LP)-77,07)79I7 #'7 387 Y7 CGNRAEG8G8$(G8;KG8G8:>G8JOG8 G8V 	JSWXLP49MQ*.VV-1V*V:JV $(V 49V YV DH,1EI"&H9H9%)H9"H92BH9  H9 ,1H9 H9V 	-d;KO38LPVV,0V)V9IVUZV <V 59<@"&GFGF)9GFGF+0GF GFR 	6tDKO38LPVV,0V)V9IVUZV EV 59<@"&FFFF)9FFFF+0FF FFP 	-d;=A!&IEIII2BINSI <I 59/5=A/5/5*:/5FK/5 /5b 	-d;=A!&IEIII2BINSI <I 59/5=A/5/5*:/5FK/5 /5d 	6tDSW;@TX3!3483!13AQ3]b3 E3 	6tDRV;@TX3!3483!13AQ3]b3 E3 	6tDSW;@TX3!3483!13AQ3]b3 E3 	6tDSW;@TX3!3483!13AQ3]b3 E3 	6tDRV:?SW3 3373 03@P3\a3 E3 	6tDSW;@TX3!3483!13AQ3]b3 E3 KO27KOMM+/M(M8HMTYM. LP38LP88,08)89I8UZ8" 6:<@"&++)9+++0+ + 6:<@"&++)9+++0+ +	 DH,1@H#'@H@H%)@H @H,1@H @HD
  	
 " 	TEI-2-1$(&*&* " .3@ 
IM?DT$T8<TINT 
T	K6;K#K/3K@EK 
K	IM5:``.2`?Z` 
`IM5:6O6O.26O?Z6O 6Op 	9$GJN6;!!/3!@[! H!H CG:?X("&)-X(X( #X(37X(X(.>X( 'X( 38X( X(t 	67#'4Q#'D$$'2$BF$ $1A$MR$ 8$N9* CG:?T&"&)-T&T& #T&37T&T&.>T& 'T& 38T& T&l 	67#'4Q#'$BB'2BBFB B/?BKPB 8B 59'='=#('= '=R 	-.26$) / -.-1#('B 'B-2'B / 'BX Y]&8<04EU !&$ -.8<?C%)N6N6,<N6"N6.3N6 / N6b -.8<?C%)N6N6,<N6"N6.3N6 / N6b 67FJ?C%)Q6Q6Q6,<Q6 #Q6 /4Q6 8 Q6h 67Y]@D&*P6P6-=P6#P6/4P6 8 P6f 67Y]>BWW+;WGLW 8 Wt 67X\+0DHVmVm$(Vm"Vm1AVmMRVm 8 Vmp '+dX\5%)d11$(1"13C1OT16 67X\+0DHOEOE$(OE"OE1AOEMROE 8 OEdO( O JN45"	*
*.**"*+;*BF* * )2* 	* (-	*Z *+,07; S" S04 S@E S ,  SF *+-18<U#U15UAFU , UD *+/3:>-<%-<37-<CH-< , -<` *+04;?,<&,<48,<DI,< , ,<^ 	*+<@7;K"K04K@EK ,K +/"!::(:: : ',: :z +/"!::(:: : ',: :~ [\NRLT#0RGT0R0R040RFI0R0R7D0R[`0R ] 0Rf [\QUOW!&2QJW/2Q2Q372QIL2Q2Q:G2Q^c2Q ] 2Qhy3y3%)y38<y3AFy3z [\48NV %04 JW04104042604HK04 04 :G04 ^c04 ] 04h [\7;QY#(37 MZO37437375937KN37 !37 =J37 af37 ] 37l @A>B5:!/8/8.2/8/8&+/8 B /8d @AAE8=$5(5(155(5().5( B 5(r+   