
    \h                       S SK Jr  S SKrS SKrS SKJr  S SKJrJr  S SKrS SKJ	r	J
r
JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5J6r6J7r7J8r8J9r9J:r:J;r;J<r<J=r=J>r>J?r?J@r@JArAJBrBJCrCJDrDJErEJFrFJGrGJHrHJIrIJJrJJKrKJLrLJMrMJNrN  S SKOJPrPJQrQ  S SKRJSrS  S SKTJUrU  S S	KVJWrW  SS
 jrXS rYS rZS r[\[ " S S5      5       r\g)    )annotationsN)product)AnyCallable)FMulAddPowRationallogexpsqrtcossintanasinacosacotasecacscsinhcoshtanhasinhacoshatanhacothasechacschexpandimflattenpolylogcancelexpand_trigsignsimplifyUnevaluatedExprSatanatan2ModMaxMinrfEiSiCiairyaiairyaiprimeairybiprimepiprimeisprimecotseccsccschsechcothFunctionIpiTupleGreaterThanStrictGreaterThanStrictLessThanLessThanEqualityOrAndLambdaIntegerDummysymbols)sympify_sympify)airybiprime)li)sympy_deprecation_warningc                b    [        SSSS9  [        U5      n[        UR                  U 5      5      $ )NzThe ``mathematica`` function for the Mathematica parser is now
deprecated. Use ``parse_mathematica`` instead.
The parameter ``additional_translation`` can be replaced by SymPy's
.replace( ) or .subs( ) methods on the output expression instead.z1.11zmathematica-parser-new)deprecated_since_versionactive_deprecations_target)rQ   MathematicaParserrM   
_parse_old)sadditional_translationsparsers      Q/var/www/auris/envauris/lib/python3.13/site-packages/sympy/parsing/mathematica.pymathematicar[      s;    	E "(#; 67F6$$Q'((    c                8    [        5       nUR                  U 5      $ )a  
Translate a string containing a Wolfram Mathematica expression to a SymPy
expression.

If the translator is unable to find a suitable SymPy expression, the
``FullForm`` of the Mathematica expression will be output, using SymPy
``Function`` objects as nodes of the syntax tree.

Examples
========

>>> from sympy.parsing.mathematica import parse_mathematica
>>> parse_mathematica("Sin[x]^2 Tan[y]")
sin(x)**2*tan(y)
>>> e = parse_mathematica("F[7,5,3]")
>>> e
F(7, 5, 3)
>>> from sympy import Function, Max, Min
>>> e.replace(Function("F"), lambda *x: Max(*x)*Min(*x))
21

Both standard input form and Mathematica full form are supported:

>>> parse_mathematica("x*(a + b)")
x*(a + b)
>>> parse_mathematica("Times[x, Plus[a, b]]")
x*(a + b)

To get a matrix from Wolfram's code:

>>> m = parse_mathematica("{{a, b}, {c, d}}")
>>> m
((a, b), (c, d))
>>> from sympy import Matrix
>>> Matrix(m)
Matrix([
[a, b],
[c, d]])

If the translation into equivalent SymPy expressions fails, an SymPy
expression equivalent to Wolfram Mathematica's "FullForm" will be created:

>>> parse_mathematica("x_.")
Optional(Pattern(x, Blank()))
>>> parse_mathematica("Plus @@ {x, y, z}")
Apply(Plus, (x, y, z))
>>> parse_mathematica("f[x_, 3] := x^3 /; x > 0")
SetDelayed(f(Pattern(x, Blank()), 3), Condition(x**3, x > 0))
)rU   parse)rW   rY   s     rZ   parse_mathematicar_       s    d  F<<?r\   c                    [        U 5      S:X  a  U S   n[        S5      nUR                  U5      nU Vs/ s H  oDR                  S   PM     nn[	        U5      n[        U[        5      (       aS  [        SU 3[        S9n[        XqR                  [        U5       VV	s0 s H  u  pU" US-   5      U	_M     sn	n5      5      $ [        SU5      $ [        U 5      S:X  a  U S   nU S   n
[        Xz5      $ [        S5      es  snf s  sn	nf )	N   r   Slotzdummy0:cls    z&Function node expects 1 or 2 arguments)lenr>   atomsargsmax
isinstancerJ   rL   rK   rI   xreplace	enumerateSyntaxError)ri   argrb   slotsanumbersnumber_of_arguments	variablesivbodys              rZ   _parse_Functionrx   V   s    
4yA~1g		$&+,e66!9e,!'l)733'*=)> ?UKI)\\IV_L`2aL`DA4!9a<L`2a%bccb#	TaG	Awi&&BCC - 3bs   C=Dc                &    U R                  5         U $ N)_initialize_classrc   s    rZ   _decor|   i   s    Jr\   c            !      0   \ rS rSr% Sr0 SS_SS_SS_S	S
_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS _S!S"_S#S$_S%S&S'S(S)S*S+.Er\" S,S-S.5       H\  u  rrr	\\-   \	-   S/-   r
\(       a  S0\R                  5       -   \	-   S1-   rO\R                  5       \	-   S1-   r\R                  \
\05        M^     S2S3S4S5S6.r\R                   " S7\R"                  5      S84\R                   " S9\R"                  5      S84\R                   " S:\R"                  5      S;4\R                   " S<\R"                  5      S=4S>.r\R                   " S?\R"                  5      r\R                   " S@\R"                  5      rSAr0 rSB\SC'   0 rSB\SD'   0 rSB\SE'   \SF 5       rGSSH jr\SI 5       rSJ rSK r\SL 5       r \SM 5       r!\SN 5       r"\SO 5       r#SP r$SQ r%SRr&SSr'STr(SUr)SVr*SWr+\(SGSXSY 04\&\)SXSZ04\&\*S[S\S]S^S_S`Sa.4\&\+SbSc 04\(SGSdSe04\&\+SfSg04\&\*ShSiSj.4\&\+SkSl04\&\)SmSn04\(SGSoSpSq.4\&\)SrSs04\&\)StSu04\'SGSvSw04\&\)SxSySz.4\&\)S{S|S}S~SSS.4\&SGSS04\&\)SSS.4\&\)SSS.4\&\)SS04\'SGS S S.4\&\*SS04\&\*SSSS S.4\(SGSSSSS.4\&SGS S S.4\'SGS S S.4\&SGSS04\(SGS S S S S.4\&SGSS 04\'SGSSS.4/r,S\S'   S S S.r-Sr.Sr// SQr0/ SQr1\S 5       r2\S 5       r3SGr4S r5GSS jr6GSS jr7GSS jr8GSS jr9GSS jr:GSS jr;GSS jr<GS	GS
S jjr=GSS jr>GSS jr?GSS jr@0 S\A_S\B_S\C_S\D_SS _SS _SS _S\E_S\F_S\G_S\H_S\I_S\J_S\K_S\L_S\M_S\N_0 SS _S\O_S\P_S\Q_S\R_S\S_S\T_S\U_S\V_S\W_S\X_S\Y_S\Z_S\[_S\\_S\]_S\^_E0 S\__S\`R                  _S\a_S\b_S\c_S\d_S\e_S\f_S\g_S\h_SS _S\i_S\j_S\k_S\l_S\m_S\n_E0 S\o_S\p_S\q_S\r_S\s_S\t_S\u_S\v_S\w_S\x_S\y_S\z_S~\{_S}\|_S{\}_Ss\~_Su\_ESe\0Er\\GS .rGS rGS rGSrgG(  rU   n   a8  
An instance of this class converts a string of a Wolfram Mathematica
expression to a SymPy expression.

The main parser acts internally in three stages:

1. tokenizer: tokenizes the Mathematica expression and adds the missing *
    operators. Handled by ``_from_mathematica_to_tokens(...)``
2. full form list: sort the list of strings output by the tokenizer into a
    syntax tree of nested lists and strings, equivalent to Mathematica's
    ``FullForm`` expression output. This is handled by the function
    ``_from_tokens_to_fullformlist(...)``.
3. SymPy expression: the syntax tree expressed as full form list is visited
    and the nodes with equivalent classes in SymPy are replaced. Unknown
    syntax tree nodes are cast to SymPy ``Function`` objects. This is
    handled by ``_from_fullformlist_to_sympy(...)``.

zSqrt[x]zsqrt(x)zRational[x,y]zRational(x,y)zExp[x]zexp(x)zLog[x]zlog(x)zLog[x,y]zlog(y,x)zLog2[x]zlog(x,2)zLog10[x]z	log(x,10)zMod[x,y]zMod(x,y)zMax[*x]zMax(*x)zMin[*x]zMin(*x)zPochhammer[x,y]zrf(x,y)zArcTan[x,y]z
atan2(y,x)zExpIntegralEi[x]zEi(x)zSinIntegral[x]zSi(x)zCosIntegral[x]zCi(x)z	AiryAi[x]z	airyai(x)zAiryAiPrime[x]zairyaiprime(x)z	airybi(x)zairybiprime(x)z li(x)z
primepi(x)zprime(x)z
isprime(x))z	AiryBi[x]zAiryBiPrime[x]zLogIntegral[x]z
PrimePi[x]zPrime[x]z	PrimeQ[x]) Arc)SinCosTanCotSecCsc)r   hz[x]rq   z(x)r   z**[]) ^{}z
                (?:(?<=[a-zA-Z\d])|(?<=\d\.))     # a letter or a number
                \s+                               # any number of whitespaces
                (?:(?=[a-zA-Z\d])|(?=\.\d))       # a letter or a number
                *z
                (?:(?<=[])\d])|(?<=\d\.))       # ], ) or a number
                                                # ''
                (?=[(a-zA-Z])                   # ( or a single letter
                z
                (?<=[a-zA-Z])       # a letter
                \(                  # ( as a character
                (?=.)               # any characters
                z*(z
                (?:
                \A|(?<=[^a-zA-Z])
                )
                Pi                  # 'Pi' is 3.14159... in Mathematica
                (?=[^a-zA-Z])
                r@   )
whitespaceadd*_1add*_2Piz
                (?:
                \A|(?<=[^a-zA-Z])   # at the top or a non-letter
                )
                [A-Z][a-zA-Z\d]*    # Function
                (?=\[)              # [ as a character
                z(
                \{.*\}
                z
                (?:
                \A|(?<=[^a-zA-Z])
                )
                {arguments}         # model argument like x, y,...
                (?=[^a-zA-Z])
                z%dict[tuple[str, int], dict[str, Any]]TRANSLATIONScache_originalcache_compiledc                p    U R                  U R                  5      nU R                  R                  U5        g rz   )_compile_dictionaryCORRESPONDENCESr   update)rd   ds     rZ   r{   #MathematicaParser._initialize_class   s.     ##C$7$78"r\   Nc                   0 U l         U R                   R                  U R                  5        Uc  0 nU R                  R                  U:w  aQ  [        U[        5      (       d  [        S5      eU R                  U5      nXR                  l        X R                  l	        U R                   R                  U R                  R                  5        g )NzThe argument must be dict type)
translationsr   r   	__class__r   rk   dict
ValueErrorr   r   )selfrX   r   s      rZ   __init__MathematicaParser.__init__   s     	  !2!23"*&(# >>((,CC5t<< !ABB (()@AA -DNN),-NN) 	  !>!>?r\   c                   0 nUR                  5        GH  u  p4U R                  U5        U R                  U5        U R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  R                  U5      nUc  SR                  US9n[        U5      eUR                  5       nU R                  U5      u  pUR                  5       S:w  d  U	[        U5      :w  a  SR                  US9n[        U5      eUS   S   S:X  a  Sn
O[        U5      n
Xz4nU Vs/ s H  oS   S:w  a  UOSU-   PM     nnS	S
R                  U5      -   S-   nU R                  R                  US9n[        R                  " U[        R                   5      n0 X+'   XBU   S'   XU   S'   UX+   S'   GM     U$ s  snf )Nr   r   '{f}' function form is invalid.fr   r   \z(?:(|z)))	argumentsfsri   pat)items_check_input_apply_rules_replace
FM_PATTERNsearchformatr   group	_get_argsstartrg   joinARGS_PATTERN_TEMPLATErecompileVERBOSE)rd   dicr   fmr   merrfm_nameri   endkey_argkeyxre_argsxyzpatStrr   s                    rZ   r   %MathematicaParser._compile_dictionary  s    iikFBR R  !!"l3B!!"l3B b#&Bb#&B %%b)A y7>>>D o% ggiG a(ID wwyA~B7>>>D o% Bx{c!d)$C @DDt!aDCKqTAX5tGD 388G,,t3C ..555DF**VRZZ0C AFcF4L!cF6NAF5Mo "r ! Es   <Gc                   U R                   nSnSn UR                  U5      nUc  X1-  n U$ UR                  5       nU R                  U5      u  pxUR	                  5       n	U R                  XXyU5      nU	nX1SU -  nXS nMp  )z'Parse Mathematica function to SymPy oner   r   N)r   r   r   r   r   _convert_one_function)
r   rW   r   scannedcurr   r   ri   r   bgns
             rZ   _convert_function#MathematicaParser._convert_functionU  s     oo

1Ay. ) B q)ID '')C **1$SAA C #wG $A7 r\   c                   U[        U5      4U R                  ;   a4  U[        U5      4nU R                  U   S   n[        [        Xs5      5      nOUS4U R                  ;   aS  US4nU R                  U   S   n0 n[	        U5       H+  u  pU
S   S:X  a  SR                  X9S  5      X'     O%X9   X'   M-     OSR                  US9n[        U5      eU R                  U   S   nU R                  U   S   nS	nSn UR                  U5      nUc  X-  nOBUR                  5       n
UR                  5       nXS U X   -   -  nUR                  5       nXS  nM[  US U U-   XS  -   nU$ )
Nri   r   r   ,z'{f}' is out of the whitelist.r   r   r   r   )rg   r   r   ziprm   r   r   r   r   r   r   r   )r   rW   r   ri   r   r   r   x_argsr   ru   r   r   templater   r   r   r   xbgns                     rZ   r   'MathematicaParser._convert_one_function|  s   D	?d///s4y/C &&s+F3F S&'A #Y$+++s)C &&s+F3F A!&)Q43;88DH-ADw	 * 399B9?CS/! $$S)$/ $U+

8$Ay# 	A 779D --G %%'C  ~H) . dsGg$'r\   c                   UR                   nUR                  5       S-   n/ / pT/ nUn[        X#S U5       H  u  pU	S:X  a&  U(       d  U(       d  UR                  X'U 5        US-   nU	S:X  a  UR                  U	5        OU	S:X  a  UR	                  5         U	S:X  a  UR                  U	5        Mx  U	S:X  d  M  U(       a  UR	                  5         M  UR                  X'U 5          O   WS-   n
Xj4$ )z'Get arguments of a Mathematica functionra   Nr   r   r   r   r   )stringr   rm   appendpop)rd   r   rW   ancsquarecurlyri   r   ru   cfunc_ends              rZ   r   MathematicaParser._get_args  s     HHeegkB ags+DACx%A!H%!e CxQc		 Cxa cJJLKKa)) ,. q5~r\   c                F    U R                   U   nUR                  X#5      nU$ rz   )REPLACEMENTSreplace)rd   rW   befafts       rZ   r   MathematicaParser._replace  s%    s#IIcr\   c                F    U R                   U   u  p4UR                  XA5      $ rz   )RULESsub)rd   rW   r   r   r   s        rZ   r   MathematicaParser._apply_rules  s    99S>wwsr\   c                    S HG  nUR                  US   5      UR                  US   5      :w  d  M/  SR                  US9n[        U5      e   SU;   a  Sn[        U5      eg )N))r   r   )r   r   )()r   ra   r   r   r   z Currently list is not supported.)countr   r   )rd   rW   bracketr   s       rZ   r   MathematicaParser._check_input  sh    ;Gwwwqz"agggaj&997>>>C o% <
 !84CS/! r\   c                "   U R                  U5        U R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      nU R                  U5      nU R                  US5      nU R                  US5      nU$ )Nr   r   r   r   r   r   )r   r   r   r   )r   rW   s     rZ   rV   MathematicaParser._parse_old  s    ! a. MM!S! a*a* ""1% MM!S! a& r\   c                l    U R                  U5      nU R                  U5      nU R                  U5      nU$ rz   )_from_mathematica_to_tokens_from_tokens_to_fullformlist_from_fullformlist_to_sympy)r   rW   s2s3s4s        rZ   r^   MathematicaParser.parse  s7    --a0..r2--b1	r\   InfixPrefixPostfixFlatRightLeft;c                b    [        U [        5      (       a  U (       a  U S   S:X  a  U S/-   $ SU S/$ )Nr   CompoundExpressionNull)rk   listr   s    rZ   <lambda>MathematicaParser.<lambda>%  sP    
1d8K8KPQVWXYVZ^rVrVH  )Z  zN  PQ  SY  yZ  )Zr\   r  Set
SetDelayedAddToSubtractFromTimesByDivideBy)=z:=z+=z-=z*=z/=z//c                    X/$ rz   re   r   ys     rZ   r  r	  (  s    1&r\   &r>   z/.
ReplaceAllRuleRuleDelayed)z->z:>z/;	Conditionr   AlternativesRepeatedRepeatedNull)z..z...z||rG   z&&rH   !NotSameQUnsameQ)z===z=!=EqualUnequal	LessEqualLessGreaterEqualGreater)z==z!=z<=<z>=>z;;SpanPlus+-Times)r   /.Dotc                ,    [         R                  U 5      $ rz   )rU   _get_negr  s    rZ   r  r	  8  s    '8'A'A!'Dr\   c                    U $ rz   re   r  s    rZ   r  r	  9  s    qr\   )r,  r+  r   PowerApplyMapMapAllc                    SXSS//$ )Nr5  List1re   r  s     rZ   r  r	  ;  s    ZacdjpruivYwr\   )z@@z/@z//@z@@@
Derivative	Factorial
Factorial2	Decrement)'r  z!!z--c                    U /UQ$ rz   re   r  s     rZ   r  r	  =  s
    !ar\   c                    SU /UQ$ )NPartre   r  s     rZ   r  r	  =  s    fa_RS_r\   )r   [[c                    S/U Q$ )Nr9  re   r  s    rZ   r  r	  >  s
    ||r\   c                    U S   $ )Nr   re   r  s    rZ   r  r	  >  s    AaDr\   )r   r   ?PatternTestc                    SU S//$ NPatternBlankre   r  s    rZ   r  r	  A  s    Iq7)4r\   c                    SSU S///$ )NOptionalrJ  rK  re   r  s    rZ   r  r	  B  s    Z)Q	)BCr\   c                    SU S//$ )NrJ  BlankSequencere   r  s    rZ   r  r	  C  s    YO+<=r\   c                    SU S//$ )NrJ  BlankNullSequencere   r  s    rZ   r  r	  D  s    i-@,ABr\   )_z_._____rR  c                    SU SU//$ rI  re   r  s     rZ   r  r	  F  s    )Q!)Er\   rb   SlotSequence)#z##z7list[tuple[str, str | None, dict[str, str | Callable]]]_mathematica_op_precedencec                 
    SS/$ )Nrb   r:  re   re   r\   rZ   r  r	  K  s    fc]r\   c                 
    SS/$ )NrV  r:  re   re   r\   rZ   r  r	  L  s	    ~s+r\   z[A-Za-z][A-Za-z0-9]*z (?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+))r   r   rC  r   )r   r   ]]r   c                    [        U[        5      (       a/  [        R                  " [        R
                  U5      (       a  SU 3$ SSU/$ )Nr,  r-  -1)rk   strr   matchrU   _numberrd   r   s     rZ   r2  MathematicaParser._get_negU  sA    $Q,,:K:S:SUV1W1W1#wo^egkmn]oor\   c                    SUS/$ )Nr4  r]  re   ra  s     rZ   _get_invMathematicaParser._get_invY  s    D!!r\   c                2   U R                   b  U R                   $ U R                  U R                  /nU R                  S S  U R                  S S  -   nU R
                   H   u  p4nU H  nUR                  U5        M     M"     UR                  S S9  UR                  [        [        R                  U5      5        UR                  S5        UR                  S5        [        R                  " SSR                  U5      -   S-   5      nXpl         U R                   $ )Nc                    [        U 5      * $ rz   )rg   r  s    rZ   r  2MathematicaParser._get_tokenizer.<locals>.<lambda>h  s
    #a&r\   )r   r   
r   r   r   )_regex_tokenizer_literalr`  _enclosure_open_enclosure_closerX  r   sortextendmapr   escaper   r   )r   tokenstokens_escapetypstratsymdictk	tokenizers           rZ   _get_tokenizer MathematicaParser._get_tokenizer_  s      ,(((--.,,Q/$2G2G2JJ#'#B#BC$$Q'  $C 	01c"))]34cdJJsSXXf%55;<	 )$$$r\   c                b   U R                  5       n/ n UR                  S5      nUS:X  a!  [        U5      S:  a  UR                  U5        O[        R
                  " SXS-   S  5      nUc  [        S5      eXER                  5       -   S-   nUS:  a  UR                  US U 5        UR                  SXS-   U R                  SS5      /5        XS-   S  nM  [        U5       Ho  u  px[        U[        5      (       a  M   UR                  S	5      n	U	S:X  a  O6UR                  S
5      n
U
S:X  d  X:  a  [        S5      eUS U	 XS-   S  -   nMN  XU'   Mq     U Vs/ s HA  n[        U[        5      (       a&  UR                  5       (       a  UR                  U5      OU/PMC     nnU VVs/ s H  ow  H  oPM     M     nnnU(       a,  US   S:X  a#  UR                  S5        U(       a  US   S:X  a  M#  U(       a,  US   S:X  a#  UR                  S5        U(       a  US   S:X  a  M#  U$ s  snf s  snnf )N"r   r   z(?<!\\)"ra   z"mismatch in string "  " expression_Strz\"z(*z*)zmismatch in comment (*  *) coderf   ri  )ry  findrg   r   r   r   rn   r   r   rm   rk   r  r^  isasciifindallr   )r   coderx  code_splitsstring_start	match_end
string_endru   
code_splitpos_comment_startpos_comment_endtoken_listsjrr  s                 rZ   r   -MathematicaParser._from_mathematica_to_tokensp  s@   '')	 )+99T?Lr!t9q=&&t,		+tNO/DEI !"FGG%(99A=Ja""4#67!^J(G(O(OPUWZ([\]1&D   '{3MA*d++$.OOD$9!$*",//$"7"b(O,O%&GHH'(:):;jYZIZI[>\\
  (N 4 eppdo_`z!S/A/Aaiikky((+XYWZZdop(4[!Q!!![4 d*JJqM d* t+JJrN t+  q4s   AH&'H+c                    [        U[        5      (       a  g[        R                  " U R                  U5      (       a  g[        R                  " SU R
                  -   U5      (       a  gg)NFz-?T)rk   r  r   r_  rk  r`  r   tokens     rZ   _is_opMathematicaParser._is_op  sJ    eT""88DMM5))88D4<<'//r\   c                <    US;   a  gU R                  U5      (       + $ )N)r   r   Tr  r  s     rZ   _is_valid_star1!MathematicaParser._is_valid_star1      J;;u%%%r\   c                <    US;   a  gU R                  U5      (       + $ )N)r   r   Tr  r  s     rZ   _is_valid_star2!MathematicaParser._is_valid_star2  r  r\   c                   / /n/ nSnU[        U5      :  Ga  X   nXPR                  ;   a8  US   R                  U5        UR                  U5        UR                  / 5        GO!US:X  a\  [        US   5      S:X  a   US   S   US   :X  a  [        SUS   -  5      eU R	                  US   5      US'   UR                  / 5        GOXPR
                  ;   Ga  U R
                  R                  U5      nU R                  U   US   :w  a  [        S5      nUS:X  ao  US   S:X  af  US   S:X  a  UR                  US	-   S
5        OIUS   S:X  a=  XS	-      S
:X  a  SXS	-   '   O-XS	-      S:X  a  SXS	-   '   UR                  US-   S
5        OUeOUe[        US   5      S:X  a  US   S   S:X  a  [        S5      eU R	                  US   S5      nXS'   / n	US   S   US   :w  a0  U	R                  UR                  5       5        US   S   US   :w  a  M0  U	R                  5         US   S:X  a&  [        U	5      S	:w  a  [        S[        U	5      -  5      eUS   R                  U	5        UR                  S5        OUS   R                  U5        US	-  nU[        U5      :  a  GM  [        U5      S	:w  a  [        S5      eU R	                  US   5      $ )Nr   r   r   z %s cannot be followed by comma ,zunmatched enclosurer[  r   ra   r   rC  rf   r   z( ) not valid syntaxTz1( must be followed by one expression, %i detectedz"Stack should have only one element)rg   rl  r   rn   _parse_after_bracesrm  indexinsertr   reverseRuntimeError)
r   rr  stackopen_seqpointerr  indunmatched_enclosure
last_stacknew_stack_elements
             rZ   r   .MathematicaParser._from_tokens_to_fullformlist  s   DF#OE,,,b	  '&R #uRy>Q&59R=HRL+H%&H8TV<&WXX 44U2Y?b	R ///++11%8'',<*56K*L'}")<#B<3.
 #MM'!)S9%b\T1%ai0C748qy 1!'	!2d!:48qy 1 &gai =&9 9 2 21uRy>Q&59R=C+?%&<==!55eBiF
&b	$&!Bimx|3%,,UYY[9 Bimx|3!))+B<3&3/@+AQ+F%&Y\_`q\r&rssb	  !23R b	  'qLG] F#^ u:?CDD''a11r\   c                6   Sn[        U5      nXE:  a  X$   nUS:X  a  U(       a  UR                  U5        US-  nM.  US:X  a  UR                  S5        US-  nML  US:  a   U R                  US U U5      nOUS   n[        U5      S:  a  US   S:X  a  UR	                  USS  5        OUR                  U5        [        U5       H  nUR                  S5        M     XT-  nSnM  US-  nXE:  a  M  g g ! [         a    UR                  U5        US-  n GM	  f = f)Nr   ri  ra   r  )rg   r   r  rn   ro  r   range)	r   linesrr  inside_enclosurer  sizer  	prev_exprru   s	            rZ   _util_remove_newlines'MathematicaParser._util_remove_newlines  s$   6{nOE}#JJw'AIDa<JJqMAIDQ;!$($<$<VHW=MO_$`	 !'q	Iy>A%)A,:N*NLL12/LL+wAJJqM (qLG= n ' !

7+	 !s   !C3 3 DDc                $   [        U5      nSnX2:  a~  US:  ak  U R                  XS-
     5      (       aP  U R                  X   5      (       a8  X   S:X  a  SX'   XS-      S   XS-   '   OUR                  US5        US-  nUS-  nUS-  nX2:  a  M}  g g )Nr   ra   r   r   )rg   r  r  r  )r   rr  r  r  s       rZ   _util_add_missing_asterisks-MathematicaParser._util_add_missing_asterisks  s    Kn!((!)<==((99
 ?c)&)FO*01*=a*@FQ;'MM'3/qLGAIDqLG! nr\   c                   Sn/ nU R                  XAU5        [        U R                  5       GH9  u  pVnSU;   a  U R                  U5        [	        U5      nSn	X:  d  M2  X   n
[        U
[        5      (       Ga  X;   Ga  Xz   n[        U[        5      (       a  U/nSnO/ nSnU
S;   a7  XPR                  :X  a(  U	S:  a"  U R                  XS-
     5      (       d  U	S-  n	M  XPR                  :X  aK  U	S:X  d>  XS-
  :X  d6  U R                  XS-
     5      (       d  U R                  XS-      5      (       a  U	S-  n	M  SnXU	'   XPR                  :X  Ga  UR                  U	S-
  5      nUR                  U	5      nU
S:X  a  U R                  U5      nOU
S:X  a  U R                  U5      nU	S-  n	US	-  nUR                  U5        UnX`R                  :X  a  U	S	-   U:  a  U R                  XS-      U
5      (       a  UR                  U5        UR                  U	S-   5      nUR                  U	S-   5      nUS:X  a  U R                  U5      nOUS:X  a  U R                  U5      nUS	-  nU	S	-   U:  a  U R                  XS-      U
5      (       a  M  UR                  U5        GONX`R                   :X  a  U	S	-   U:  ae  XS-      U
:X  aZ  UR                  X/5        US
   nUR                  U	S-   5        UR                  U	S-   5      nUS	-  nU	S	-   U:  a  XS-      U
:X  a  MZ  UR                  U5        GOX`R"                  :X  a  U	S-   U:  a~  XS-      U
:X  as  [        U[        5      (       a  UUU   U/UU'   OU" UU   U5      UU'   UR                  U	S-   5        UR                  U	S-   5      nUS	-  nU	S-   U:  a  XS-      U
:X  a  Ms  UR                  U5        GOUR                  U5        GOXPR                  :X  aq  Ub  [%        S5      eXS-
  :X  d  U R                  XS-      5      (       a  U R&                  U
   " 5       X'   OUR                  UR                  U	S-   5      5        US-  nOXPR(                  :X  as  Ub  [%        S5      eU	S:X  d  U R                  XS-
     5      (       a  U R&                  U
   " 5       X'   O-UR                  UR                  U	S-
  5      5        U	S-  n	US-  n[        U[*        5      (       a[  [,        R.                  " [*        U5      nU" U6 nUR1                  5         [        U[2        5      (       a  UR5                  U5        OUX'   U	S-  n	X:  a  GM  GM<     [	        U5      S:  d  [	        U5      S:X  a2  [	        U5      S:X  a#  U(       a  U R7                  X5      $ [9        S5      e[	        U5      S:  a'  US   (       a  US   S   S:X  a  US   SS  nS/UQUQnU$ US   $ )NFr   r   ra   r*  Tr.  r,  rf   r   z1'Prefix' op_type should not have a grouping stratz0unable to create a single AST for the expressionr  )r  reversedrX  r  rg   rk   r^  PREFIXr  INFIXr   rd  r2  r   FLAT_check_op_compatibleRIGHTLEFT	TypeError_missing_arguments_defaultPOSTFIXr   typingcastclearr  ro  r  rn   )r   rr  r  changedr  op_typegrouping_stratop_dictr  r  r  op_namenodefirst_indexarg1arg2node_pother_opop_callnew_nodecompound_expressions                        rZ   r  %MathematicaParser._parse_after_braces!  s   ""52BC089X9X0Y,GWg~008FDG.eS))e.>.5nG "'3// 'y&'!&'
*w++/E'TU+^b^i^ijp{|q|j}^~^~  1 **,"a<7Qh+>$++f_`U`NaBbBbfjfqfqrx  DE  zE  sF  gG  gG#qLG$"G&*7O**,%zz'!)4%zz'2 C<#'==#6D"c\#'==#6D1	D)!%)YY6")A+"49R9RSYbcZcSdfk9l9l &d 3+1::gai+@'-zz'!)'<#+s?+/==+>D%-_+/==+>D $	 #*A+"49R9RSYbcZcSdfk9l9l #MM$/+zz9")A+"4	9Je9S &wo >)/ &

719 5'-zz'!)'< $	 #*A+"4	9Je9S #MM$/+yy8")A+"4	9Je9S#-gs#;#;;BF;DWY]:^F;$7:A&BUW[:\F;$7 &

719 5'-zz'!)'< $	 #*A+"4	9Je9S #MM$/ KK- KK/)5"+,_"``"Qh.$++fq[>Q2R2R.2.M.Me.T.VFO KK

719(=> AID LL0)5"+,_"``"a<4;;vk7J+K+K.2.M.Me.T.VFO KK

719(=>#qLG AID!'844,2KK',J#*D>

%h55 KK1.6FO1u .. 1Z@ v;?s5zQ3v;!3C //IIPQQu:>ayVAYq\-AA12#7"I%"I&"I&&ayr\   c                L    X:X  a  gSS1nSS1nX;   a  X#;   a  gX;   a  X$;   a  gg)NTr   r.  r+  r,  Fre   )r   op1op2muldivaddsubs        rZ   r  &MathematicaParser._check_op_compatible  s7    :ss=S]=S]r\   c                   / nU/n[         R                  " SU5      nSnU GH  nUc    US   $ UR                  5       nXU R                  SS5      R                  SS5      R                  SS5      R	                  5       nUR                  5       S:X  a  US:w  a  US   R                  U5        OUR                  5       S:X  a+  US:w  a  US   R                  U5        UR                  5         O@UR                  5       S:X  a,  US   R                  U/5        UR                  US   S   5        UR                  5       nGM      US   $ )z8
Parses FullForm[Downvalues[]] generated by Mathematica
z[\[\],]r   r   r   r   r   r   )	r   finditerr   r   stripr   r   r   r   )	r   wmexproutr  	generatorlast_posr_  position	last_exprs	            rZ   _from_fullform_to_fullformlist0MathematicaParser._from_fullform_to_fullformlist  s0    KK
F3	E} 1v {{}H199#rBJJ3PRS[[\_acdjjlI{{}#?"I$$Y/#%?"I$$Y/		#%b	  )-U2Yr]+yy{H! " 1vr\   c                8   ^^^ SSK JmJm  UUU4S jmT" U5      $ )Nr   )r>   Symbolc                  > [        U [        5      (       aE  [        U 5      S:  a+  U S   nU SS   Vs/ s H  nT" U5      PM     nnT" U5      " U6 $ [        S5      e[        U [        5      (       a  T" U 5      $ [        U 5      $ s  snf )Nr   ra   zEmpty list of expressions)rk   r  rg   r   r^  rN   )exprheadro   ri   r>   r  	converters       rZ   r  HMathematicaParser._from_fullformlist_to_fullformsympy.<locals>.converter  s    $%%t9q=7D6:12h?hsIcNhD?#D>400$%@AAD#&&d|#~% @s   B)sympyr>   r  )r   pylistr>   r  r  s     @@@rZ   #_from_fullformlist_to_fullformsympy5MathematicaParser._from_fullformlist_to_fullformsympy  s    *	&   r\   r
   Logc                 $    [        [        U 5      6 $ rz   )r   r  rq   s    rZ   r  r	    s    #x{+r\   Log2c                    [        U S5      $ Nrf   r   r  s    rZ   r  r	    s
    #a)r\   Log10c                    [        U S5      $ )N
   r  r  s    rZ   r  r	    s
    3q":r\   ExpSqrtr   r   r   r   r   r   ArcSinArcCosArcTanc                 R    [        U 5      S:X  a  [        [        U 5      6 $ [        U 6 $ r  )rg   r*   r  r)   r  s    rZ   r  r	    s#    CFaKUHQK0MT1XMr\   ArcCotArcSecArcCscSinhCoshTanhCothSechCschArcSinhArcCoshArcTanhArcCothArcSechArcCschExpandImReFlattenPolylogCancel
TrigExpandSignSimplifyDeferIdentityr  c                 "    [         R                  $ rz   )r(   Zeror  s    rZ   r  r	    s    166r\   r+   r,   r-   
PochhammerExpIntegralEiSinIntegralCosIntegralAiryAiAiryAiPrimeAiryBiAiryBiPrimeLogIntegralPrimePiPrimePrimeQr9  )r?   r   c                $   ^ ^ UU 4S jmT" U5      $ )Nc                ^  > [        U [        5      (       an  [        U S   [        5      (       a  T" U S   5      nO+TR                  R                  U S   [	        U S   5      5      nU" U SS   Vs/ s H  nT" U5      PM     sn6 $ TR
                  R                  U [        U 5      5      $ s  snf )Nr   ra   )rk   r  _node_conversionsgetr>   _atom_conversionsrM   )r  r  ro   recurser   s      rZ   r)  >MathematicaParser._from_fullformlist_to_sympy.<locals>.recurse,  s    $%%d1gt,,"47+D1155d1gxQ?PQDd12h?hsgclh?@@--11$FF @s   /B*re   )r   full_form_listr)  s   ` @rZ   r   -MathematicaParser._from_fullformlist_to_sympy*  s    	G ~&&r\   c                    UnU R                   R                  5        H   u  p4UR                  [        U5      U5      nM"     U$ rz   )r&  r   r   r>   )r   mformr  mma_form
sympy_nodes        rZ   _from_fullformsympy_to_sympy.MathematicaParser._from_fullformsympy_to_sympy8  s>    $($:$:$@$@$B H<< 2J?D %Cr\   )rj  r   rz   )r  r^  )r  z
str | listreturnbool)rr  r  )r  r  rr  r  r  r4  )F)rr  r  r  r4  )r  r^  r  r^  )r  r^  )r  r  )__name__
__module____qualname____firstlineno____doc__r   r   arctrir   r   lowerr   r   r   r   r   r   r   r   ARG_MTRX_PATTERNr   r   __annotations__r   r   classmethodr{   r   r   r   r   r   r   r   r   rV   r^   r  r  r  r  r  r  rX  r  rk  r`  rl  rm  r2  rd  rj  ry  r   r  r  r  r   r  r  r  r  r  r  r   r   r	   r
   r   r   r   r   r   r8   r9   r:   r   r   r   r   r   r   r   r   r=   r<   r;   r   r   r   r   r   r   r   r    r  r!   r"   r#   r$   r%   r&   r'   r(   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   rO   rP   r5   r6   r7   rA   rC   rB   rD   rE   rF   rG   rH   rx   r&  r?   r@   r(  r   r1  __static_attributes__re   r\   rZ   rU   rU   n   s	   (9 	( 	(	
 	J 	: 	K 	J 	9 	9 	) 	l 	G 	' 	'  	[!" 	*#$ !*!"!/O6 { -67@BS!3Y]U"syy{"Q&.Bq5(BBx(B 	L JJ  ZZ	!
  JJ  ZZ	!
  JJ  ZZ	!
  JJ  ZZ! ;&ER   ZZ!J zz #ZZ!
 ;=L7< =?N9> =?N9># #
@0 = =~%N@D # #J  
   " ": EFGDED 
$  Z  [  	\	s012	U,g^clt~  	A	t012	$j)*	t\*+	fM:;	t[)*	sN+,	$z.AB	tTl#	tUm$	U|$	gi89	WI[v]kr{|}	tVn%	F01	G'23	sEl#	D(* 	+	W~&	gU8Lwxy	$lLXcde	08TUV	3.IJ	sM*+	$4C=B	
 	 
sEFG	V>:;G$[ W $N #+"
 'H1G+O,p p " " %"-^&
&
52n!F*w wr	6!$QQQ 	Q 	H	Q
 	+Q 	#Q 	%Q 	sQ 	Q 	sQ 	sQ 	sQ 	sQ 	sQ  	s!Q$ 	$%Q& 	$'Q( 	M)Q* 	$+Q, 	$-Q. 	$/Q2 	3Q4 	5Q6 	7Q8 	9Q: 	;Q< 	=Q@ 	5AQB 	5CQD 	5EQF 	5GQH 	5IQJ 	5KQN 	&OQP 	bQQR 	ehhSQT 	7UQV 	7WQX 	&YQ\ 	k]Q^ 	_Q` 	HaQb 	cQd 	AeQl 	!mQn 	soQp 	sqQr 	ssQt 	buQv 	wQx 	ryQz 	r{Q| 	&}Q~ 	{Q@ 	&AQB 	{CQD 	rEQF 	7GQH 	IQJ 	'KQN 	OQP 	$QQR 	SQT 	UQV 	XWQX 	YQZ 	b[Q\ 	s]Q` 	OaQh 
'r\   rU   rz   )]
__future__r   r   r  	itertoolsr   r   r   r  r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   sympy.core.sympifyrM   rN   sympy.functions.special.besselrO   'sympy.functions.special.error_functionsrP   sympy.utilities.exceptionsrQ   r[   r_   rx   r|   rU   re   r\   rZ   <module>rG     s    " 	     A A A A A A A A A A A A A A A A A A
 1 6 6 @
)3lD&
 N N Nr\   