
    i)                     x    S r SSKrSSKJr  SSKr/ SQrSrS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rg)zC
A functionally equivalent parser of the numpy.einsum input parser
    N)OrderedDict)
is_valid_einsum_charhas_valid_einsum_chars_only
get_symbolgen_unused_symbolsconvert_to_valid_einsum_charsalpha_canonicalizefind_output_strfind_output_shapepossibly_convert_to_numpyparse_einsum_input4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZc                 ,    U [         ;   =(       d    U S;   $ )u   Check if the character ``x`` is valid for numpy einsum.

Examples
--------
>>> is_valid_einsum_char("a")
True

>>> is_valid_einsum_char("Ǵ")
False
z,->.)_einsum_symbols_basexs    Q/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/opt_einsum/parser.pyr   r      s     %%71;7    c                 4    [        [        [        U 5      5      $ )u   Check if ``einsum_str`` contains only valid characters for numpy einsum.

Examples
--------
>>> has_valid_einsum_chars_only("abAZ")
True

>>> has_valid_einsum_chars_only("Över")
False
)allmapr   )
einsum_strs    r   r   r   #   s     s'455r   c                 <    U S:  a	  [         U    $ [        U S-   5      $ )u   Get the symbol corresponding to int ``i`` - runs through the usual 52
letters before resorting to unicode characters, starting at ``chr(192)``.

Examples
--------
>>> get_symbol(2)
'c'

>>> get_symbol(200)
'Ŕ'

>>> get_symbol(20000)
'京'
4      )r   chr)is    r   r   r   1   s$     	2v#A&&q3w<r   c              #   l   #    S=p#X1:  a(  [        U5      nUS-  nX@;   a  M  Uv   US-  nX1:  a  M'  gg7f)zGenerate ``n`` symbols that are not already in ``used``.

Examples
--------
>>> list(oe.parser.gen_unused_symbols("abd", 2))
['c', 'e']
r      N)r   )usednr   cntss        r   r   r   E   sB      KA
'qM	Q9q 's   .44c                    ^ [        [        U 5      [        S5      -
  5      n[        U5       VVs0 s H  u  p#U[        U5      _M     snnmSR	                  U4S jU  5       5      $ s  snnf )u   Convert the str ``einsum_str`` to contain only the alphabetic characters
valid for numpy einsum. If there are too many symbols, let the backend
throw an error.

Examples
--------
>>> oe.parser.convert_to_valid_einsum_chars("Ĥěļļö")
'cbdda'
z,-> c              3   F   >#    U  H  nTR                  X5      v   M     g 7fNget).0r   replacers     r   	<genexpr>0convert_to_valid_einsum_chars.<locals>.<genexpr>c   s     :z!8<<%%z   !)sortedset	enumerater   join)r   symbolsr   r   r+   s       @r   r   r   W   s]     S_s5z12G-6w-?@-?TQ:a= -?@H77:z::: As   A'c                    ^ [        5       mU  H*  nUS;   a  M  UT;  d  M  [        [        T5      5      TU'   M,     SR                  U4S jU  5       5      $ )u   Alpha convert an equation in an order-independent canonical way.

Examples
--------
>>> oe.parser.alpha_canonicalize("dcba")
'abcd'

>>> oe.parser.alpha_canonicalize("Ĥěļļö")
'abccd'
z.,->r%   c              3   F   >#    U  H  nTR                  X5      v   M     g 7fr'   r(   )r*   r   renames     r   r,   %alpha_canonicalize.<locals>.<genexpr>w   s     6X6::a##Xr.   )r   r   lenr2   )equationnamer6   s     @r   r	   r	   f   sQ     ]F6>v%c&k2F4L	 
 776X666r   c                    ^ U R                  SS5      mSR                  U4S j[        [        T5      5       5       5      $ )a-  
Find the output string for the inputs ``subscripts`` under canonical einstein summation rules. That is, repeated indices are summed over by default.

Examples
--------
>>> oe.parser.find_output_str("ab,bc")
'ac'

>>> oe.parser.find_output_str("a,b")
'ab'

>>> oe.parser.find_output_str("a,a,b,b")
''
,r%   c              3   V   >#    U  H  nTR                  U5      S :X  d  M  Uv   M      g7f)r   N)count)r*   r#   tmp_subscriptss     r   r,   "find_output_str.<locals>.<genexpr>   s)     Z9^=Q=QRS=TXY=Y119s   )	))replacer2   r/   r0   )
subscriptsr?   s    @r   r
   r
   z   s6      ''R0N77ZfS%89ZZZr   c                 2   ^ ^ [        U U4S jU 5       5      $ )a+  Find the output shape for given inputs, shapes and output string, taking
into account broadcasting.

Examples
--------
>>> oe.parser.find_output_shape(["ab", "bc"], [(2, 3), (3, 4)], "ac")
(2, 4)

# Broadcasting is accounted for
>>> oe.parser.find_output_shape(["a", "a"], [(4, ), (1, )], "a")
(4,)
c              3      >#    U  H@  n[        S  [        TT Vs/ s H  o"R                  U5      PM     sn5       5       5      v   MB     gs  snf 7f)c              3   >   #    U  H  u  pUS :  d  M  X   v   M     g7f)r   N )r*   shapelocs      r   r,   .find_output_shape.<locals>.<genexpr>.<genexpr>   s!     ^)Q:5UX\]U]JEJ)Qs   N)maxzipfind)r*   cr   inputsshapess      r   r,   $find_output_shape.<locals>.<genexpr>   sD      phncd^V5PAffQi5P)Q^^^hn5Ps   AAA)tuple)rN   rO   outputs   `` r   r   r      s#      phnp p pr   c                 T    [        U S5      (       d  [        R                  " U 5      $ U $ )a  Convert things without a 'shape' to ndarrays, but leave everything else.

Examples
--------
>>> oe.parser.possibly_convert_to_numpy(5)
array(5)

>>> oe.parser.possibly_convert_to_numpy([5, 3])
array([5, 3])

>>> oe.parser.possibly_convert_to_numpy(np.array([5, 3]))
array([5, 3])

# Any class with a shape is passed through
>>> class Shape:
...     def __init__(self, shape):
...         self.shape = shape
...

>>> myshape = Shape((5, 5))
>>> oe.parser.possibly_convert_to_numpy(myshape)
<__main__.Shape object at 0x10f850710>
rG   )hasattrnp
asanyarrayr   s    r   r   r      s%    2 1g}}Qr   c                 J    SnU  H  nU[         L a  US-  nM  X!U   -  nM     U$ )zConvert user custom subscripts list to subscript string according to `symbol_map`.

Examples
--------
>>>  oe.parser.convert_subscripts(['abc', 'def'], {'abc':'a', 'def':'b'})
'ab'
>>> oe.parser.convert_subscripts([Ellipsis, object], {object:'a'})
'...a'
r%   ...)Ellipsis)old_sub
symbol_mapnew_subr#   s       r   convert_subscriptsr]      s9     G=uG !}$G  Nr   c                   ^ [        U 5      n/ n/ n[        [        U 5      S-  5       HC  nUR                  UR	                  S5      5        UR                  UR	                  S5      5        ME     [        U5      (       a  US   OSnU Vs/ s H  n[        U5      PM     n n [        [        R                  R                  U5      5      nUR                  [        5        [        [        U5      5       VV	s0 s H  u  pU	[        U5      _M     sn	nmSR!                  U4S jU 5       5      n
Ub  U
S-  n
U
[#        UT5      -  n
X4$ s  snf s  sn	nf ! [         a    [        S5      ef = f)	z:Convert 'interleaved' input to standard einsum input.
       r   NziFor this input type lists must contain either Ellipsis or hashable and comparable object (e.g. int, str).r<   c              3   <   >#    U  H  n[        UT5      v   M     g 7fr'   )r]   )r*   subr[   s     r   r,   ,convert_interleaved_input.<locals>.<genexpr>   s     X#,S*==s   ->)listranger8   appendpopr   r0   	itertoolschainfrom_iterablediscardrY   r1   r/   r   	TypeErrorr2   r]   )operandstmp_operandsoperand_listsubscript_listpoutput_listr   
symbol_setidxsymbolrB   r[   s              @r   convert_interleaved_inputrw      sf    >LLN3x=A%&L,,Q/0l..q12 ' '*,&7&7,r"TK6BCl)!,lHCN66~FG
 	8$ BK6R\K]A^_A^+#fjo-A^_
 XXXJd
(jAA
/ D ` N M N 	NNs%   
E"AE 6EE E E(c           	         [        U 5      S:X  a  [        S5      e[        U S   [        5      (       a4  U S   R	                  SS5      nU SS  Vs/ s H  n[        U5      PM     n nO[        U 5      u  pSU;   d  SU;   aV  UR                  S5      S:  =(       d    UR                  S5      S:  nU(       d  UR                  S	5      S:w  a  [        S
5      eSU;   Ga  UR	                  SS5      R	                  SS5      R	                  S	S5      nSR                  [        U[        S U  5       5      5      5      nSnS	U;   a'  UR                  S	5      u  pxUR                  S5      n	Sn
OUR                  S5      n	Sn
[        U	5       H  u  pSU;   d  M  UR                  S5      S:w  d  UR                  S5      S:w  a  [        S5      eX   R                  S:X  a  SnO0[        [        X   R                  5      S5      [        U5      S-
  -
  nX:  a  UnUS:  a  [        S5      eUS:X  a  UR	                  SS5      X'   M  UR	                  SX]* S 5      X'   M     SR                  U	5      nUS:X  a  SnOXV* S nU
(       a  US	WR	                  SU5      -   -  nOE[        U5      nSR                  [        [!        U5      [!        U5      -
  5      5      nUS	U-   U-   -  nS	U;   a  UR                  S	5      u  nnOU[        U5      nnU H#  nUU;  d  M  [        SR#                  U5      5      e   [        UR                  S5      5      [        U 5      :w  a  [        S5      eUX4$ s  snf )a  
A reproduction of einsum c side einsum parsing in python.

Returns
-------
input_strings : str
    Parsed input strings
output_string : str
    Parsed output string
operands : list of array_like
    The operands to use in the numpy contraction

Examples
--------
The operand list is simplified to reduce printing:

>>> a = np.random.rand(4, 4)
>>> b = np.random.rand(4, 4, 4)
>>> parse_einsum_input(('...a,...a->...', a, b))
('za,xza', 'xz', [a, b])

>>> parse_einsum_input((a, [Ellipsis, 0], b, [Ellipsis, 0]))
('za,xza', 'xz', [a, b])
r   zNo input operands r%   r   N->rd   z%Subscripts can only contain one '->'..r<   c              3   L   #    U  H  n[        UR                  5      v   M     g 7fr'   )r8   rG   )r*   r   s     r   r,   %parse_einsum_input.<locals>.<genexpr>#  s     ;[RZQCLLRZs   "$TF   rX   zInvalid Ellipses.rF   zEllipses lengths do not match.z1Output character '{}' did not appear in the inputzDNumber of einsum subscripts must be equal to the number of operands.)r8   
ValueError
isinstancestrrA   r   rw   r>   r2   r   rJ   splitr1   rG   r
   r/   r0   format)rn   rB   r   invalidr    ellipse_indslongest	input_tmp
output_subsplit_subscriptsout_subnumrb   ellipse_countout_ellipseoutput_subscriptnormal_indsinput_subscriptschars                      r   r   r      sw   4 8},--(1+s##a[((b1
:B12,G,Q-a0,G  9B
 	zsj0##C(1,L*2B2B32G!2Kz''-2DEE j!!#r*223;CCD"Mww1$;[RZ;[8[\] :$.$4$4T$:!I(s3G)//4G!"23HCczIIcNa'SYYu-=-B$%899 =&&",$%M$'HM,?,?(@!$DCST$UM *+G 1$$%EFF"a',/KKr,B$),/KK|NO?\,]$)' 4* XX./
 a<K&xy1K$!3!3E;!GGGJ  /z:''&-=)>[AQ)Q"RSK$,{::J z-7-=-=d-C**-79T* !''PWWX\]^^ !
 !!#&'3x=8bcc-77_ Hs   M()__doc__ri   collectionsr   numpyrU   __all__r   r   r   r   r   r   r	   r
   r   r   r]   rw   r   rF   r   r   <module>r      se     #  N 86($;7([&p">(" Jn8r   