
    Vpf 
                        d Z ddlmZ ddlmZ ddlZddlmZ ddlmZ	  G d d          Z
ej                            e
e
j        e
j                   dS )zA bounded functional stack implementation.

Used as a helper for expressing recursive algorithms such as QDWH-eig for
Eigendecomposition on TPU.
    )annotations)AnyN)laxc                  j    e Zd ZdZd Zd Zedd	            Zdd
ZddZ	ddZ
d Zed             ZdS )StackzCA bounded functional stack implementation. Elements may be pytrees.c                "    || _         || _        dS )zPrivate constructor.N_size_data)selfsizedatas      R/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/lax/stack.py__init__zStack.__init__   s    DJDJJJ    c                (    d| j          d| j         dS )NzStack(z, )r	   r   s    r   __repr__zStack.__repr__$   s    /DJ//$*////r   capacityint	prototyper   returnc                     t          t          j        dt          j                  t          j                             fd|                    S )zCreates a stack with size `capacity` with elements like `prototype`.

    `prototype` can be any JAX pytree. This function looks only at its
    structure; the specific values are ignored.
    r   c                d    t          j        ft          | j                  z   | j                  S N)jnpzerostupleshapedtype)xr   s    r   <lambda>zStack.create.<locals>.<lambda>1   s$    #)XK%..8!'BB r   )r   r   arrayint32jax	tree_utiltree_map)r   r   s   ` r   createzStack.create'   sP     	i39	mBBBBIO OP P Pr   c                    | j         dk    S )z#Returns true if the stack is empty.r   )r
   r   s    r   emptyzStack.empty3   s    :?r   elemc                     t           j        dz   t          j                             fd j        |                    S )z:Pushes `elem` onto the stack, returning the updated stack.   c                <    t          j        | |j        d          S )Nr   )r   dynamic_update_index_in_dimr
   )r"   yr   s     r   r#   zStack.push.<locals>.<lambda><   s    S4Q4:qII r   )r   r
   r&   r'   r(   r   r   r,   s   ` r   pushz
Stack.push7   sF    
j1n	mIIII
D   r   tuple[Any, Stack]c                     t           j                             fd j                  }|t	           j        dz
   j                  fS )z=Pops from the stack, returning an (elem, updated stack) pair.c                D    t          j        | j        dz
  dd          S )Nr.   r   F)keepdims)r   dynamic_index_in_dimr
   )r"   r   s    r   r#   zStack.pop.<locals>.<lambda>B   s!    (DJNANNN r   r.   )r&   r'   r(   r   r   r
   r2   s   ` r   popz	Stack.pop?   sJ    =!!NNNN
j D tzA~tz2222r   c                j    t           j                            | j                  \  }}| j        g|z   |fS r   )r&   r'   tree_flattenr   r
   )r   leavestreedefs      r   flattenzStack.flattenF   s1    m00<<OFGZL6!G++r   c           	     z    t          |d         t          j                            | |dd                              S )Nr   r.   )r   r&   r'   tree_unflatten)r=   r<   s     r   	unflattenzStack.unflattenJ   s0    CM88&*MMNNNr   N)r   r   r   r   r   r   )r   r   )r,   r   r   r   )r   r4   )__name__
__module____qualname____doc__r   r   staticmethodr)   r+   r3   r9   r>   rA    r   r   r   r      s        KK  
0 0 0 	P 	P 	P <	P      3 3 3 3, , , O O <O O Or   r   )rE   
__future__r   typingr   r&   r   	jax.numpynumpyr   r   r'   register_pytree_noder>   rA   rG   r   r   <module>rM      s     # " " " " "       



            /O /O /O /O /O /O /O /Ob  " "5%- I I I I Ir   