
    Pi                     X   d dl 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
  e j        d           dd	            Ze j        dd            Ze j        j         e j        d           dd
ddedee j        ef         dedede
j        de j        fd                        Zee_        dS )    N)partial   )util)Unionc                     dfd	S )Nint32c                       | ||          S )Ndtype )exprs_inexpr_outbackendr   cs       b/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/einx/op/arange.py<lambda>z<lambda>.<locals>.<lambda>
   s"    qq(%P P P     )Nr   r   tr   s    `r   r   r   
   s          r   )tracer   c                    t          t                    rt          j                                      |                                 D ]5}t          |t          j        j        j                  rt          d          6| |fD ]L}|                                D ]5}t          |t          j        j        j
                  rt          d          6Md |                                D             }t          |          dk    rt          dt          |                     t          |          dk    r|d         j        ndt          j        | g          d         } t          j        |g          d         }fd}t          j        j                            ||          }	t          j        j                            |	          }	t          j        d | D             fd	| D             |	g
          \  \  }
}t          j        |g|
g|g
          \  }
|
t          j        j                            |          fS )Nz&Marker in input expression not allowedzConcatenation not allowedc                     g | ]L}t          |t          j        j        j                  &t          j        j                            |          J|MS r   )
isinstanceeinxexprstage3Axis	is_marked.0r   s     r   
<listcomp>z!arange_stage3.<locals>.<listcomp>   s^       dDI,122 8<y7G7Q7QRV7W7W  r   r   &Expected at most one marked axis, got r   c                 ^   t          | t          j        j        j                  rt          j        j                            |           rct          j        j                            d t                    D                       } t          j        j                            |           } | S d S d S )Nc                 X    g | ]'}t           j        j                            d d          (S )Nr   )r   r   r   r   )r    _s     r   r!   z2arange_stage3.<locals>.replace.<locals>.<listcomp>'   s:     3 3 334	 %%dA..3 3 3r   )	r   r   r   r   r   r   ConcatenationrangeComposition)r   ndims    r   replacezarange_stage3.<locals>.replace%   s    dDI,122 	ty7G7Q7QRV7W7W 	9#11 3 38=d3 3 3  D 9#//55DK	 	 	 	r   c                 6    g | ]}|                                 S r   )__deepcopy__)r    axiss     r   r!   z!arange_stage3.<locals>.<listcomp>1   s$    111				111r   c                 H    g | ]}                     |j                   S )r
   )arangevalue)r    r-   r   r   s     r   r!   z!arange_stage3.<locals>.<listcomp>2   s+    EEET
%	0	0EEEr   )r   )r   strr   r   getallr   r   Marker
ValueErrorr&   lenr0   r   flattenr*   demarkrearrange_stage3	unflatten)expr_inr   r   r   r   rootmarked_axesexpr_out_flatr*   expr_out_flat_withconcattensorr%   r)   s     ``        @r   arange_stage3rA   	   sp    '3 ,,""7++ G GdDI,344 	GEFFF	G(# > >HHJJ 	> 	>D$	 0 >?? > !<===>	> LLNN  K
 ;!T#kBRBRTTUUU#&{#3#3q#8#8;q>aDlG9%%a(GL(,,Q/M      $y/77wOO#y/667OPP(11111EEEEEWEEE	!"	  LIVq 		
 

  IV 49#**84444r   Tc                    t           j        j                            | |          \  } }t           j        j                            |           }t          |          dk    r}t           j        j                            t           j        j        	                    t           j        j        
                    |d         d                   g          |d         g          }t          |d                   dk    r%t          dt          |d                              t          |d                   dk    r%t          dt          |d                              t           j        j                            t           j        j                            |d         d                             fd}t           j                            t           j                            |d         d                   gt           j                            |d         d                   gz   d |                                D             z   ||          d d         \  }}||fS )	Nr   r   z%Expected 1 input expression, but got z&Expected 1 output expression, but got c                 r   | d         }d |                                 D             }d |D             }t          |          dk    rt          dt          |                     t          |          t          |          z
  }t          j                            t          j        |g                    gS )Nr   c                     g | ]>}t          |t          j        j        j        t          j        j        j        f          <|?S r   )r   r   r   stage2	NamedAxisUnnamedAxisr   s     r   r!   z/parse.<locals>.after_stage2.<locals>.<listcomp>^   sM     
 
 
$!1!;TY=M=Y Z[[

 
 
r   c                 Z    g | ](}t           j        j                            |          &|)S r   )r   r   rE   r   r   s     r   r!   z/parse.<locals>.after_stage2.<locals>.<listcomp>c   s1    YYYD	8H8R8RSW8X8XY4YYYr   r"   )r3   r6   r5   r   r   Equationnpasarray)exprs1exprs2r   out_axesmarked_out_axesr)   marked_expr_outs         r   after_stage2zparse.<locals>.after_stage2\   s    !9
 
 
 
 

 ZYHYYY!##\c/FZFZ\\]]]8}}s?333	""?BJv4F4FGGHHr   c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS ).N)depth1depth2)r   r   rI   rJ   rK   newaxis)r    kvs      r   r!   zparse.<locals>.<listcomp>l   sY     
 
 
1 Iq"*Q--RZ"@VZ[[
 
 
r   )cserQ      )r   opr   !_clean_description_and_parametersr   stage1parse_opr6   OpArgsget_unmarkedr5   r(   
get_markedsolverI   items)descriptionrX   
parametersrZ   rQ   r;   r   rP   s          @r   parserf   D   s   "glLLZ K 
		"	";	/	/B 2ww!||Y  	 %%ty'7'D'DRU1X'N'N&OPP1
 
 2a5zzQMRUMMNNN
2a5zzQN#be**NNOOOi&22493C3N3NrRSuUVx3X3XYYOI I I I I 				BqE!H	%	%&9beAh''
(	)
 
"((**
 
 
	
 ! ( 	 	 	qb	
GX Hr   c                     dfd	S )Nc                      | fi |S Nr   )rd   r   kwargsr   s      r   r   z<lambda>.<locals>.<lambda>x   s    ;IaIaZ`IaIa r   ri   r   r   s    `r   r   r   x   s    aaaaa r   )r   rX   rd   r   r   rX   re   returnc                V    t          | fd|i|\  }}t          ||||          \  }}|S )a  n-dimensional ``arange`` operation.

    *This function might be removed in a future version.*

    Runs ``arange`` for every axis in ``input``, and stacks the results along the single
    marked axis in ``output``. Always uses ``start=0`` and ``step=1``.

    The `description` argument must meet one of the following formats:

    1. ``input -> output``
        Runs ``backend.arange`` for every axis in ``input``, and stacks the results along the
        marked axis in ``output``. The values are stacked in the order that the axes appear
        in ``input``.

    2. ``output``
        Implicitly determines the input expression by removing the marked axis from ``output``.

        Example: ``a b [2]`` resolves to ``a b -> a b [2]``

    Args:
        description: Description string in Einstein notation (see above).
        backend: Backend to use for all operations.
        cse: Whether to apply common subexpression elimination to the expressions. Defaults
            to True.
        graph: Whether to return the graph representation of the operation instead of computing
            the result. Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.

    Returns:
        The result of the n-dimensional arange operation if `graph=False`, otherwise the graph
        representation of the operation.

    Examples:
        Arange two-dimensional coordinates:

        >>> tensor = einx.arange("a b [2]", a=5, b=6, backend="numpy")
        >>> tensor.shape
        (5, 6, 2)
        >>> tensor[2, 3]
        array([2, 3], dtype=int32)

        Arange two-dimensional coordinates with inverted coordinates (`Cartesian ordering
        <https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html>`_:
        First axis of tensor corresponds to second coordinate along stacked axis and vice versa.):

        >>> tensor = einx.arange("a b -> b a [2]", a=5, b=6, backend="numpy")
        >>> tensor.shape
        (6, 5, 2)
        >>> tensor[2, 3]
        array([3, 2], dtype=int32)

        Arange one-dimensional coordinates:

        >>> einx.arange("a", a=5, backend="numpy").shape
        (5,)
    rX   )r   r   )rf   rA   )rd   r   r   rX   re   r;   r   r@   s           r   r/   r/   w   sD    D kAAsAjAAGX$WhuUUUFHMr   )r   )T)r   	functoolsr    r   numpyrJ   typingr   numpy.typingnptjitrA   	lru_cacherf   traceback_utilfilterr1   Backendbool	ArrayLikeTensorr/   r   r   r   <module>r{      s                                
   
35 35 35 
35l / / / /d 	aabbb
 B B BB 4<$%B 	B
 
B -B 
[B B B cb BJ r   