
    9?iZ                     v    U d dl mZ d dlZd dlZd dlmZmZmZmZm	Z	 dZ
eed<    G d de      Z G d d	      Zy)
    )EinopsErrorN)ListOptionalSetTupleUnionu   …	_ellipsisc                   "    e Zd ZdZdefdZd Zy)AnonymousAxiszHImportant thing: all instances of this class are not equal to each othervaluec                     t        |      | _        | j                  dk  r>| j                  dk(  rt        d      t        dj                  | j                              y )N   zENo need to create anonymous axis of length 1. Report this as an issuez2Anonymous axis should have positive length, not {})intr   r   format)selfr   s     d/var/www/html/leadgen/airagagent/ocr_fallback/ocr_env/lib/python3.12/site-packages/einops/parsing.py__init__zAnonymousAxis.__init__   sR    Z
::?zzQ!"ijj!"V"]"]^b^h^h"ijj	     c                 J    dj                  t        | j                              S )Nz{}-axis)r   strr   )r   s    r   __repr__zAnonymousAxis.__repr__   s    DJJ00r   N)__name__
__module____qualname____doc__r   r   r    r   r   r   r   	   s    Rkc k1r   r   c            
           e Zd ZdZddddededefdZdefd	Zdefd
Z	e
ddededeeef   fd       Ze
dedefd       Zy)ParsedExpressionz
    non-mutable structure that contains information about one side of expression (e.g. 'b c (h w)')
    and keeps some information important for downstream
    F)allow_underscoreallow_duplicates
expressionr   r    c                    d _         d  _        t                _        d _        g  _        d|v rid|vrt        d      t        j                  |d      dk7  st        j                  |d      dk7  rt        d      |j                  dt              }d _         d  fd	}d }|D ]  }|d
v rR| ||       d }|dk(  rt        d      g (|dk(  s.t        d       j
                  j                         d Yt        j                  |      s
|dt        fv r||}}||z  }t        dj                  |             t        dj                  |            |	 ||       y y )NF.z...z6Expression may contain dots only inside ellipsis (...)r      zUExpression may contain dots only inside ellipsis (...); only one ellipsis for tensor Tc                    | j                   v r#r| dk(  sst        dj                  |             | t        k(  rej                   j	                  t               'j
                  j                  t               d_        y j                  t               d_        y t        j                  |       }|r.t        |       dk(  r j
                  j                  g        y 	 y j                  |       \  }}|s|st        dj                  | |            |rt        |       } j                   j	                  |        |rd_        j
                  j                  | g       y j                  |        y )N_z5Indexing expression contains duplicate dimension "{}"FTr   )r   zInvalid axis identifier: {}
{})identifiersr   r   r	   addcompositionappendhas_ellipsis_parenthesizedr   	isdecimalr   check_axis_name_return_reasonr   has_non_unitary_anonymous_axes)x	is_numberis_axis_namereasonr    r   bracket_groupr   s       r   add_axis_namez0ParsedExpression.__init__.<locals>.add_axis_name2   sZ   D$$$(Q#X?O%&]&d&def&ghhI~  $$Y/ ($$++I66;D3!((36:D3MM!,	Q1$,((//3  '+'I'I!^n'I'o$f!\%&G&N&NqRX&YZZ%a(A  $$Q':>D7 ($$++QC0!((+r   z() (zDAxis composition is one-level (brackets inside brackets not allowed))zBrackets are not balancedr&   zUnknown character '{}'z*Imbalanced parentheses in expression: "{}")has_ellipsisr+   setr'   r.   r)   r   r   countreplacer	   r*   isalnumr   )r   r!   r   r    r4   current_identifiercharr3   s   ` ``   @r   r   zParsedExpression.__init__   s   "':>'%(U49+8:*J&!"Z[[yyU+q0CIIj#4NRS4S!k  $++E9=J $D-1 	,D " 	IDu}%1!"45%)"3;$0)*pqq$&MS[$,)*EFF$$++M:$(MT"dsI.>&>%-)-&&$.&!":"A"A$"GHH)	I, $JQQR\]^^),- *r   returnc                     g }| j                   D ]1  }t        |t              sJ d       |D ]  }|j                  |        3 |S )Nzdoes not work with ellipsis)r)   
isinstancelistr*   )r   resultcomposed_axisaxiss       r   flat_axes_orderz ParsedExpression.flat_axes_orderp   sT    !-- 	$MmT2Q4QQ2% $d#$	$ r   c                 h    | j                   D ]#  }t        |t              st        |      dkD  s# y y)Nr   TF)r)   r@   rA   len)r   axess     r   has_composed_axesz"ParsedExpression.has_composed_axesx   s3    $$ 	D$%#d)a-	 r   namec                    t         j                  |       sy| d   dk(  s| d   dk(  r	| dk(  r|ryyt        j                  |       r)t	        j
                  dj                  |       t               | dv rt	        j
                  d	t               y)
N)Fznot a valid python identifierr   r&   )T )Fz8axis name should should not start or end with underscorez9It is discouraged to use axes names that are keywords: {})rD   zQIt is discouraged to use 'axis' as an axis name and will raise an error in future)	r   isidentifierkeyword	iskeywordwarningswarnr   RuntimeWarningFutureWarning)rJ   r   s     r   r-   z.ParsedExpression.check_axis_name_return_reason   s    %9!W^tBx3s{/T  &Y``aefhvwxj! r   c                 6    t         j                  |       \  }}|S )z
        Valid axes names are python identifiers except keywords,
        and additionally should not start or end with underscore
        )r   r-   )rJ   is_valid_reasons      r   check_axis_namez ParsedExpression.check_axis_name   s     -JJ4P'r   N)F)r   r   r   r   r   boolr   r   rE   rI   staticmethodr   r-   rX   r   r   r   r   r      s    
 EJdi P.3 P.T P.]a P.d 4  C 4 TYZ^`cZcTd  " c d  r   r   )einopsr   rO   rQ   typingr   r   r   r   r   r	   r   __annotations__objectr   r   r   r   r   <module>r_      s6       4 4	3 1F 1@ @r   