Ë
    ~\;jý)  ã                   óx   — d Z ddlZddlmZ ddlZg d¢ZdZd„ Zd„ Z	d„ Z
d	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)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_inputÚ4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZc                 ó   — | t         v xs | dv 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    úZG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\opt_einsum/parser.pyr   r      s   € ð Ô%Ð%Ò7¨1°¨;Ð7ó    c                 ó4   — t        t        t        | «      «      S )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                 ó:   — | dk  r	t         |    S t        | dz   «      S )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$   € ð 	ˆ2‚vÜ# AÑ&Ð&Üˆq�3‰w‹<Ðr   c              #   óh   K  — dx}}||k  r%t        |«      }|dz  }|| v rŒ|–— |dz  }||k  rŒ$yy­w)zœGenerate ``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   sK   è ø€ ð €K€AˆØ
�Š'Ü�q‹MˆØ	ˆQ‰ˆØ�‰9ØØŠØˆq‰ˆð ��'ùs   ‚-2°2c                 óÔ   ‡— t        t        | «      t        d«      z
  «      }t        |«      D ��ci c]  \  }}|t        |«      “Œ c}}Šdj	                  ˆfd„| D «       «      S c c}}w )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   óB   •K  — | ]  }‰j                  ||«      –— Œ y ­w©N©Úget)Ú.0r   Úreplacers     €r   Ú	<genexpr>z0convert_to_valid_einsum_chars.<locals>.<genexpr>c   s   øè ø€ Ð:©z¨!�8—<‘<  1×%©zùó   ƒ)ÚsortedÚsetÚ	enumerater   Újoin)r   Úsymbolsr   r   r+   s       @r   r   r   W   s_   ø€ ô ”S˜“_¤s¨5£zÑ1Ó2€GÜ-6°wÔ-?Ô@Ñ-?¡T Q¨�”:˜a“=Ñ Ð-?Ò@€HØ�7‰7Ó:©zÓ:Ó:Ð:ùó As   °A$c                 óœ   ‡— t        «       Š| D ]#  }|dv rŒ|‰vsŒt        t        ‰«      «      ‰|<   Œ% dj                  ˆfd„| D «       «      S )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   óB   •K  — | ]  }‰j                  ||«      –— Œ y ­wr'   r(   )r*   r   Úrenames     €r   r,   z%alpha_canonicalize.<locals>.<genexpr>w   s   øè ø€ Ð6©X¨�6—:‘:˜a ×#©Xùr-   )r   r   Úlenr1   )ÚequationÚnamer5   s     @r   r	   r	   f   sQ   ø€ ô ‹]€FÛˆØ�6‰>ØØ�vÒÜ%¤c¨&£kÓ2ˆF�4ŠLð	 ð
 �7‰7Ó6©XÓ6Ó6Ð6r   c                 ó€   ‡— | j                  dd«      Šdj                  ˆfd„t        t        ‰«      «      D «       «      S )aU  
    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   óL   •K  — | ]  }‰j                  |«      d k(  sŒ|–— Œ y­w)r   N)Úcount)r*   r#   Útmp_subscriptss     €r   r,   z"find_output_str.<locals>.<genexpr>Š   s(   øè ø€ ÐZÑ9˜¸^×=QÑ=QÐRSÓ=TÐXYÓ=Y”1Ñ9ùs   ƒ$�$)Úreplacer1   r.   r/   )Ú
subscriptsr=   s    @r   r
   r
   z   s6   ø€ ð  ×'Ñ'¨¨RÓ0€NØ�7‰7ÓZœf¤S¨Ó%8Ô9ÓZÓZÐZr   c                 ó0   ‡ ‡— t        ˆ ˆfd„|D «       «      S )aO  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   óš   •K  — | ]=  }t        d „ t        ‰‰D �cg c]  }|j                  |«      ‘Œ c}«      D «       «      –— Œ? yc c}w ­w)c              3   ó8   K  — | ]  \  }}|d k\  sŒ||   –— Œ y­w)r   N© )r*   ÚshapeÚlocs      r   r,   z.find_output_shape.<locals>.<genexpr>.<genexpr>›   s$   è ø€ Ð^Ñ)Q™:˜5 #ÐUXÐ\]ÓU]ˆE�#�JÑ)Qùs   ‚�
N)ÚmaxÚzipÚfind)r*   Úcr   ÚinputsÚshapess      €€r   r,   z$find_output_shape.<locals>.<genexpr>š   sD   øè ø€ ð pÙhnÐcdŒÑ^¬¨VÉÓ5PÉÀA°a·f±f¸QµiÈÑ5PÔ)QÓ^×^ÑhnùÒ5Pùs   ƒA™A±A)Útuple)rJ   rK   Úoutputs   `` r   r   r   �   s#   ù€ ô ô pÙhnópó pð pr   c                 óH   — t        | d«      st        j                  | «      S | S )aT  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>
    rD   )ÚhasattrÚnpÚ
asanyarrayr   s    r   r   r   ž   s"   € ô2 �1�gÔÜ�}‰}˜QÓÐàˆr   c                 óD   — d}| D ]  }|t         u r|dz  }Œ|||   z  }Œ |S )a  Convert 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_subscriptsrX   ½   s;   € ð €GÛˆØ”‰=Ø�uÑ‰Gð �z !‘}Ñ$‰Gð ð €Nr   c                 óº  ‡— t        | «      }g }g }t        t        | «      dz  «      D ]B  }|j                  |j	                  d«      «       |j                  |j	                  d«      «       ŒD t        |«      r|d   nd}|D �cg c]  }t        |«      ‘Œ } }	 t        t        j                  j                  |«      «      }|j                  t        «       t        t        |«      «      D ��	ci c]  \  }}	|	t        |«      “Œ c}	}Šdj!                  ˆfd„|D «       «      }
|�|
dz  }
|
t#        |‰«      z  }
|
| fS c c}w c c}	}w # t        $ r t        d«      ‚w xY w)	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   ó6   •K  — | ]  }t        |‰«      –— Œ y ­wr'   )rX   )r*   ÚsubrV   s     €r   r,   z,convert_interleaved_input.<locals>.<genexpr>î   s   øè ø€ ÐXÉÀ#Ô,¨S°*×=Éùs   ƒú->)ÚlistÚranger6   ÚappendÚpopr   r/   Ú	itertoolsÚchainÚfrom_iterableÚdiscardrT   r0   r.   r   Ú	TypeErrorr1   rX   )ÚoperandsÚtmp_operandsÚoperand_listÚsubscript_listÚpÚoutput_listr   Ú
symbol_setÚidxÚsymbolr?   rV   s              @r   Úconvert_interleaved_inputrq   Ñ   sg  ø€ ô ˜“>€LØ€LØ€NÜ”3�x“= AÑ%Ö&ˆØ×Ñ˜L×,Ñ,¨QÓ/Ô0Ø×Ñ˜l×.Ñ.¨qÓ1Õ2ð 'ô '*¨,Ô&7�,˜rÒ"¸T€KÙ6BÓC±l°Ô)¨!Õ,°l€HÐCðNäœŸ™×6Ñ6°~ÓFÓGˆ
ð 	×Ñœ8Ô$ô BKÌ6ÐR\ÓK]ÔA^Ô_ÑA^±+°#°v�fœj¨›oÑ-ÐA^Ò_ˆ
ð —‘ÓXÉÓXÓX€JØÐØ�dÑˆ
ØÔ(¨°jÓAÑAˆ
à�xÐÐùò/ Dùó `øäò NÜð Mó Nð 	NðNús%   ÂD:ÂAE Ã,D?ÄE Ä?E ÅEc           	      ó†  — t        | «      dk(  rt        d«      ‚t        | d   t        «      r1| d   j	                  dd«      }| dd D �cg c]  }t        |«      ‘Œ } }nt        | «      \  }} d|v sd|v rK|j                  d«      dkD  xs |j                  d«      dkD  }|s|j                  d	«      dk7  rt        d
«      ‚d|v �rù|j	                  dd«      j	                  dd«      j	                  d	d«      }dj                  t        |t        d„ | D «       «      «      «      }d}d	|v r(|j                  d	«      \  }}|j                  d«      }	d}
n|j                  d«      }	d}
t        |	«      D ]Î  \  }}d|v sŒ|j                  d«      dk7  s|j                  d«      dk7  rt        d«      ‚| |   j                  dk(  rd}n1t        t        | |   j                  «      d«      t        |«      dz
  z
  }||kD  r|}|dk  rt        d«      ‚|dk(  r|j	                  dd«      |	|<   Œ¶|j	                  d|| d «      |	|<   ŒÐ dj                  |	«      }|dk(  rd}n|| d }|
r|d	j	                  d|«      z   z  }nEt        |«      }dj                  t        t!        |«      t!        |«      z
  «      «      }|d	|z   |z   z  }d	|v r|j                  d	«      \  }}n|t        |«      }}|D ]   }||vsŒt        dj#                  |«      «      ‚ t        |j                  d«      «      t        | «      k7  rt        d«      ‚||| fS c c}w )af  
    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Ú-Ú>r^   z%Subscripts can only contain one '->'.Ú.r:   c              3   óF   K  — | ]  }t        |j                  «      –— Œ y ­wr'   )r6   rD   )r*   r   s     r   r,   z%parse_einsum_input.<locals>.<genexpr>#  s   è ø€ Ð;[ÑRZÈQ¼CÀÇÁ¿LÑRZùs   ‚!TFé   rS   zInvalid Ellipses.rC   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.)r6   Ú
ValueErrorÚ
isinstanceÚstrr>   r   rq   r<   r1   r   rF   Úsplitr0   rD   r
   r.   r/   Úformat)rh   r?   r   Úinvalidr    Úellipse_indsÚlongestÚ	input_tmpÚ
output_subÚsplit_subscriptsÚout_subÚnumr]   Úellipse_countÚout_ellipseÚoutput_subscriptÚnormal_indsÚinput_subscriptsÚchars                      r   r   r   ö   sˆ  € ô4 ˆ8ƒ}˜ÒÜÐ,Ó-Ð-ä�(˜1‘+œsÔ#Ø˜a‘[×(Ñ(¨¨bÓ1ˆ
Ø:BÀ1À2¹,ÓG¹,°QÔ-¨aÕ0¸,ˆÑGô  9¸ÓBÑˆ
�Hð 	ˆzÑ˜s jÑ0Ø×#Ñ# CÓ(¨1Ñ,ÒL°*×2BÑ2BÀ3Ó2GÈ!Ñ2KˆÙ�z×'Ñ'¨Ó-°Ò2ÜÐDÓEÐEð ˆjÒØ×!Ñ! # rÓ*×2Ñ2°3¸Ó;×CÑCÀDÈ"ÓMˆØ—w‘wÔ1°$¼Ñ;[ÑRZÓ;[Ó8[Ó\Ó]ˆØˆð �:ÑØ$.×$4Ñ$4°TÓ$:Ñ!ˆI�zØ(Ÿ™¨sÓ3ÐØ‰Gà)×/Ñ/°Ó4ÐØˆGä!Ð"2Ö3‰HˆC�Ø�cŠzØ—I‘I˜c“N aÒ'¨S¯Y©Y°uÓ-=ÀÒ-BÜ$Ð%8Ó9Ð9ð ˜C‘=×&Ñ&¨"Ò,Ø$%‘Mä$'¬¨H°S©M×,?Ñ,?Ó(@À!Ó$DÌÈCËÐSTÉÑ$U�Mà  7Ò*Ø+�Gà  1Ò$Ü$Ð%EÓFÐFØ" aÒ'Ø,/¯K©K¸¸rÓ,BÐ$ SÒ)à,/¯K©K¸¸|È]ÈNÈOÐ?\Ó,]Ð$ SÒ)ð' 4ð* —X‘XÐ.Ó/ˆ
ð �aŠ<Ø‰Kà&¨ x yÐ1ˆKáØ˜$ ×!3Ñ!3°E¸;Ó!GÑGÑG‰Jô  /¨zÓ:ÐØŸ'™'¤&¬Ð-=Ó)>ÄÀ[ÓAQÑ)QÓ"RÓSˆKà˜$ Ñ,¨{Ñ:Ñ:ˆJð ˆzÑØ-7×-=Ñ-=¸dÓ-CÑ*ÐÑ*à-7¼ÈÓ9TÐ*Ðó !ˆØÐ'Ò'ÜÐP×WÑWÐX\Ó]Ó^Ð^ð !ô
 Ð×!Ñ! #Ó&Ó'¬3¨x«=Ò8ÜÐbÓcÐcàÐ-¨xÐ7Ð7ùò_ Hs   Á	L>)Ú__doc__rc   Úcollectionsr   ÚnumpyrP   Ú__all__r   r   r   r   r   r   r	   r
   r   r   rX   rq   r   rC   r   r   Ú<module>r�      se   ðñó Ý #ã ò€ð NÐ ò8ò6òò(ò$;ò7ò([ò&pò"ò>ò(" óJn8r   