
    wgܩ                     *   U d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlZddlZ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mZmZ ddlm Z  ddl!m"Z" dd	l#m$Z$ dd
l%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ dZ,ee-e.f   Z/ee.ef   Z0eee/   e0e0gee/   f   Z1de.de2fdZ3dBde/de0de0fdZ4de.dee/   dee/   fdZ5 G d dee/         Z6 G d d      Z7deee/e7f      fdZ8de1fdZ9deee/e6f      de0de0fd Z:deee/e7f      de0de0fd!Z;deee/e7f      de0de0fd"Z<dee/   de0de0fd#Z=d$ee.ge2f   fd%Z> e>e3      Z?dee/   de0de0dee/   fd&Z@dee/   de0de0dee/   fd'ZAdee/   de0de0dee/   fd(ZBdee/   de0de0fd)ZCdee/   de0de0fd*ZDdee/   de0de0fd+ZEdee/   de0de0fd,ZFdee/   de0de0fd-ZGdee/   de0de0fd.ZHdee/   de0de0fd/ZIdee/   de0de0fd0ZJdee/   de0de0dee/   fd1ZKeDeCeGeHeEfZLee1d2f   eMd3<   d4e.de0de0d5ee1d2f   de.f
d6ZNde0de0fd7ZOdeLdd8fd4e.dee0   d5eee1d2f   e.f   dee0   fd9ZPd4e.fd:ZQ G d; d<ej                        ZSeDeCeGeHeEeBeFeAe@eKe=eId=ZTd>j                  d? eTj                         D              ZW G d@ dA      ZX eX       ZYy)CzGTransform a string with Python-like source code into SymPy expression.     )
generate_tokens
untokenize
TokenErrorNUMBERSTRINGNAMEOP	ENDMARKER
ERRORTOKENNEWLINE)	iskeywordN)StringIO)TupleDictAnyCallableListOptionalUnion)AssumptionKeys)Basic)Symbol)Function	func_name)MaxMin 
token_namereturnc                 z    d| v ry	 t        j                  d| z          S # t        $ r t        |       dkD  cY S w xY w)a  
    Predicate for whether a token name can be split into multiple tokens.

    A token is splittable if it does not contain an underscore character and
    it is not the name of a Greek letter. This is used to implicitly convert
    expressions like 'xyz' into 'x*y*z'.
    _FzGREEK SMALL LETTER    )unicodedatalookupKeyErrorlen)r   s    _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/parsing/sympy_parser.py_token_splittabler)      sL     j#%%&;j&HIII #:""#s     ::token
local_dictglobal_dictc                     |j                  | d         }|s|j                  | d         }t        |      xr t        |t               S )z
    Predicate for whether a token name represents a callable function.

    Essentially wraps ``callable``, but looks up the token name in the
    locals and globals.
    r#   )getcallable
isinstancer   )r*   r+   r,   	nextTokenfuncs        r(   _token_callabler3   .   sD     >>%(#DuQx(D>:*T6":::    nameresultc                    |g k(  s|d   d   dk(  r
t               t        | ft        dfg}t        dfg}d}t        |      }t	        |d d d         D ]r  \  }}|\  }}	||z
  dz
  }
|	dk(  r|dz  }n
|	dk(  r|dz  }|dk(  s.|
dz
  dk\  r+||
dz
     d   t        k(  r|d |
dz
   |z   ||
dz
  d  z   |z   c S |d |
 |z   ||
d  z   |z   c S  |S )Nr#   ()r   )r   r   r	   r'   	enumerate)r5   r6   	beginningenddifflengthindexr*   toknumtokvalis              r(   _add_factorial_tokensrD   ;   s   |vbz!}+lCy)I9+CD[F!&2,/ AuUNQS=AIDs]AID191uzfQUmA.$6fq1u~	1F1q56NBSHHbqzI-qr
:S@@A Mr4   c                       e Zd ZdZy)ParenthesisGroupz9List of tokens representing an expression in parentheses.N)__name__
__module____qualname____doc__ r4   r(   rF   rF   W   s    Cr4   rF   c                   @    e Zd ZdZd
dedefdZdee   fdZd Z	d	 Z
y)AppliedFunctionz
    A group of tokens representing a function and its arguments.

    `exponent` is for handling the shorthand sin^2, ln^2, etc.
    Nfunctionargsc                 H    |g }|| _         || _        || _        g d| _        y )NrN   rO   exponent)rN   rO   rR   items)selfrN   rO   rR   s       r(   __init__zAppliedFunction.__init__b   s)    H 	 5
r4   r    c                 4    | j                   g| j                  S )z1Return a list of tokens representing the function)rN   rO   rT   s    r(   expandzAppliedFunction.expandj   s    *		**r4   c                 4    t        | | j                  |         S N)getattrrS   )rT   r@   s     r(   __getitem__zAppliedFunction.__getitem__n   s    tTZZ.//r4   c                 V    d| j                   d| j                  d| j                  dS )NzAppliedFunction(z, r:   rQ   rW   s    r(   __repr__zAppliedFunction.__repr__q   s     04tyy04? 	?r4   rZ   )rG   rH   rI   rJ   TOKENrF   rU   r   rX   r\   r^   rK   r4   r(   rM   rM   \   s5    
6 6.> 6+U +0?r4   rM   c                     g }| D ]C  }t        |t              r |j                  |j                                3|j	                  |       E |S rZ   )r0   rM   extendrX   append)r6   result2toks      r(   _flattenre   v   sE    G  c?+NN3::<(NN3	 
 Nr4   recursorc                 F     dt         t           dt        dt        f fd}|S )Ntokensr+   r,   c                    g }g }d}| D ]  }|d   t         k(  r|d   dk(  r |j                  t        g              |dz  }n|d   dk(  r|d   j                  |       |j                         }t	        |      dkD  r|d   j                  |       n9|dd } 
|||      }|d   g|z   |d   gz   }	|j                  t        |	             |dz  }|r|d   j                  |       |j                  |        |rt        d      |S )zsGroup tokens between parentheses with ParenthesisGroup.

        Also processes those tokens recursively.

        r   r#   r9   r:   r8   zMismatched parentheses)r	   rb   rF   popr'   ra   r   )rh   r+   r,   r6   stacks
stacklevelr*   stackinner
parenGrouprf   s             r(   _innerz"_group_parentheses.<locals>._inner   s,    9;)+
 	%EQx2~8s?MM"22"67!OJ1X_2J%%e,"JJLE6{Q r
))%0 !&a ()3)4!6 ',AhZ%%759+%E
&6z&BC!OJr
!!%(e$7	%8 566r4   r   r_   DICT)rf   rp   s   ` r(   _group_parenthesesrs      s)    'tE{ ' '4 'P Mr4   rh   c                    g }d}| D ]x  }t        |t              r3|rt        |||      rt        ||      |d<   d}4|j	                  |       F|d   t
        k(  r|}|j                  |       fd}|j                  |       z |S )zConvert a NAME token + ParenthesisGroup into an AppliedFunction.

    Note that ParenthesisGroups, if not applied to any function, are
    converted back into lists of tokens.

    Nr8   r   )r0   rF   r3   rM   ra   r   rb   )rh   r+   r,   r6   symbolrd   s         r(   _apply_functionsrv      s     46FF c+,/&*kJ,VS9r
c"Vt^FMM#FMM# Mr4   c                 |   g }d}t        | | dd       D ]  \  }}|j                  |       |rd}|d   t        k(  r|d   dk(  r|d   t        k(  rd}@t	        |t
              rt	        |t
              r|j                  t        df       x|t        dfk(  r@|j                  d   d	k(  r|j                  d   d
f|_        |j                  t        df       |d   t        k(  s|j                  t        df       |t        dfk(  rtt	        |t
              r|j                  t        df       |d   t        k(  r|j                  t        df       A|t        dfk(  sN|j                  t        df       g|d   t        k(  sut        |||      rt	        |t
              s|d   t        k(  r&t        |||      r|j                  t        df       |t        dfk(  r|j                  t        df       |d   t        k(  s|j                  t        df        | r|j                  | d          |S )a  Implicitly adds '*' tokens.

    Cases:

    - Two AppliedFunctions next to each other ("sin(x)cos(x)")

    - AppliedFunction next to an open parenthesis ("sin x (cos x + 1)")

    - A close parenthesis next to an AppliedFunction ("(x+2)sin x")
    - A close parenthesis next to an open parenthesis ("(x+2)(x+3)")

    - AppliedFunction next to an implicitly applied function ("sin(x)cos x")

    Fr#   Nr   .T*r9   r   r   r:   r8   )ziprb   r	   r   r0   rM   rN   r3   )rh   r+   r,   r6   skiprd   nextToks          r(   _implicit_multiplicationr}      s     46FDFF12J/ )-WcDq6R<CFcMgajD.@Dc?+'?3r3i(RI%<<?j0$'LLOX#>CLr3i(t#r3i(r3ig7MM2s),QZ4'MM2s),S	)MM2s),Q4Z(Ug7QZ4'OGZQ\,]MM2s),S	)MM2s),QZ4'MM2s),S)-T fRj!Mr4   c                 0   g }d}d}d}t        | | dd       D ]G  \  }}|j                  |       |d   t        k(  rB|d   t        t        t
        fvr,t        ||||      sI|j                  t        df       |dz  }f|d   t        k(  r%|d   t        k(  r|d   dk(  rt        |||      sd}|rot        |t              s|d   t        k(  s|d   dk(  s|d   t        k(  r	|d   dk(  r|d   t        k(  r|d   dk(  s|j                  t        df       |dz  }d}|s|d   t        k(  r|d   d	v rd}#|r|dz  },|j                  t        d
f       |dz  }J | r|j                  | d          |r|j                  t        d
fg|z         |S )z+Adds parentheses as needed after functions.r   Fr#   Nr9   **Try   )^r   ry   r:   r8   )
rz   rb   r   r	   r
   r   r3   r0   rM   ra   )	rh   r+   r,   r6   appendParenr{   exponentSkiprd   r|   s	            r(   _implicit_applicationr     s   35FKDLFF12J/ !WcFdNwqz"i1IIsJWEr3i(q !fnr!1gajD6HsJ<#
 30FbLSVs]  
b(WQZ3->"1:+
c0Ar3i0#q(#(LqzRGAJ2B$B	MM2s)$1KC!F fRj!CykK/0Mr4   c                    g }g }d}d}t        | | dd       D ]$  \  }}|d   t        k(  r$|d   t        k(  r|d   dk(  rt        |||      rd}n|r|d   t        k(  r|d   dk(  rt        df}|j	                  |       |d   |d   cxk(  r	t        k(  rn n|d   d	k(  r
|d   d
k(  rd}|d   |d   cxk(  r	t        k(  rn n|d   dk(  r|d   d
k(  rd}|d= |rT|sR|d   t        k(  r|d   d
k(  r|dz  }n|d   d	k(  r|dz  }|dk(  r&|j	                  |       |j                  |       g }|j	                  |       ' | r|j	                  | d          |r|j                  |       |S )ap  Allows functions to be exponentiated, e.g. ``cos**2(x)``.

    Examples
    ========

    >>> from sympy.parsing.sympy_parser import (parse_expr,
    ... standard_transformations, function_exponentiation)
    >>> transformations = standard_transformations + (function_exponentiation,)
    >>> parse_expr('sin**4(x)', transformations=transformations)
    sin(x)**4
    Fr   r#   Nr   Tr   r   r:   r9   ry   r8   )rz   r   r	   r3   rb   ra   )	rh   r+   r,   r6   rR   consuming_exponentlevelrd   r|   s	            r(   function_exponentiationr   9  s    FHEFF12J/ Wq6T>gajB.71:3EsJ<%)"1v~#a&J"6X&OOC  1v)r)c!fm
c@Q%*"1v)r)c!fm
c@Q%*"RL01v|q6S=QJEVs]QJEzc"h'c9: fRj!hMr4   	predicatec                 F     dt         t           dt        dt        f fd}|S )a2  Creates a transformation that splits symbol names.

    ``predicate`` should return True if the symbol name is to be split.

    For instance, to retain the default behavior but avoid splitting certain
    symbol names, a predicate like this would work:


    >>> from sympy.parsing.sympy_parser import (parse_expr, _token_splittable,
    ... standard_transformations, implicit_multiplication,
    ... split_symbols_custom)
    >>> def can_split(symbol):
    ...     if symbol not in ('list', 'of', 'unsplittable', 'names'):
    ...             return _token_splittable(symbol)
    ...     return False
    ...
    >>> transformation = split_symbols_custom(can_split)
    >>> parse_expr('unsplittable', transformations=standard_transformations +
    ... (transformation, implicit_multiplication))
    unsplittable
    rh   r+   r,   c                 P   g }d}d}| D ]  }|rd}	d}|d   t         k(  r|d   dv rd}nf|rc|d   t         k(  rV|d   dd } |      rC|d   d   }|dd = d}	|	t        |      k  r||	   }
|
|v s|
|v r|j                  t         d|
z  f       n|
j                         r|
g}t	        |	dz   t        |            D ]0  }	||	   j                         s|	dz  }	 n|j                  ||	          2 d	j                  |      }
|j                  t         d
ft        dft         d|
z  ft        dfg       nB|	t        |      k(  r|nd}|j                  t         |ft        dft         d|
z  ft        dfg       |	dz  }	|	t        |      k  rd}d}d}|j                  |        |S )NFr   r#   )r   r   Tr8   z%sr   Numberr9   z'%s'r:   r   )r   r'   rb   isdigitrangejoinra   r	   )rh   r+   r,   r6   splitsplit_previousrd   ru   tok_typerC   charcharsuser   s                r(   _split_symbolsz,split_symbols_custom.<locals>._split_symbols  s     /	C$ N1v~#a&,B"B3q6T>Q"V$%bz!}HrsAc&k/%ay:-1D"MM4*=>!\\^%)FE%*1q5#f+%> 8'-ay'8'8':"#q&!"' %VAY 7	8
 $&775>D"MMD(+;b#Y,0&4-+@2s)+M N /03v;.>(HC"MMD#;S	,0&4-+@2s)+M NQ% c&k/, "E%)N "EMM#_/	b r4   rq   )r   r   s   ` r(   split_symbols_customr   m  s*    ,6tE{ 6 64 6p r4   c                 ~     t        t              | ||      }t        |||      }t        |||      }t	        |      }|S )a   Makes the multiplication operator optional in most cases.

    Use this before :func:`implicit_application`, otherwise expressions like
    ``sin 2x`` will be parsed as ``x * sin(2)`` rather than ``sin(2*x)``.

    Examples
    ========

    >>> from sympy.parsing.sympy_parser import (parse_expr,
    ... standard_transformations, implicit_multiplication)
    >>> transformations = standard_transformations + (implicit_multiplication,)
    >>> parse_expr('3 x y', transformations=transformations)
    3*x*y
    )rs   implicit_multiplicationrv   r}   re   rh   r+   r,   res1res2res3r6   s          r(   r   r     sE    " 756vz;WDD*k:D#D*kBDd^FMr4   c                 ~     t        t              | ||      }t        |||      }t        |||      }t	        |      }|S )a  Makes parentheses optional in some cases for function calls.

    Use this after :func:`implicit_multiplication`, otherwise expressions
    like ``sin 2x`` will be parsed as ``x * sin(2)`` rather than
    ``sin(2*x)``.

    Examples
    ========

    >>> from sympy.parsing.sympy_parser import (parse_expr,
    ... standard_transformations, implicit_application)
    >>> transformations = standard_transformations + (implicit_application,)
    >>> parse_expr('cot z + csc z', transformations=transformations)
    cot(z) + csc(z)
    )rs   implicit_applicationrv   r   re   r   s          r(   r   r     sE    " 423FJTDD*k:D z;?Dd^FMr4   c                 P    t         t        t        t        fD ]  } || ||      }  | S )an  Allows a slightly relaxed syntax.

    - Parentheses for single-argument method calls are optional.

    - Multiplication is implicit.

    - Symbol names can be split (i.e. spaces are not needed between
      symbols).

    - Functions can be exponentiated.

    Examples
    ========

    >>> from sympy.parsing.sympy_parser import (parse_expr,
    ... standard_transformations, implicit_multiplication_application)
    >>> parse_expr("10sin**2 x**2 + 3xyz + tan theta",
    ... transformations=(standard_transformations +
    ... (implicit_multiplication_application,)))
    3*x*y*z + 10*sin(x**2)**2 + tan(theta)

    )split_symbolsr   r   r   )r6   r+   r,   steps       r(   #implicit_multiplication_applicationr     s7    0  7%'>@ 7fj+67 Mr4   c                    g }d}| j                  d       t        | | dd       D ]  \  }}|\  }}|\  }	}
|t        k(  rg|}|dv sOt        |      sD|d   t        k(  r|d   dk(  s0|d   t        k(  r|d   dv r|	t        k(  r|
dk(  s||v r#||   t
        ur|j                  t        |f       ||v rg|j                  t
        t                     j                  |       |
d	k(  rt        |      ||<   nt        |      ||<   |j                  t        |f       ||v rD||   }t        |t        t        t        f      st        |      r|j                  t        |f       ;|j!                  t        |
d	k7  rd
ndft        d	ft        t#        t%        |            ft        dfg       n|j                  ||f       ||f} |S )zAInserts calls to ``Symbol``/``Function`` for undefined variables.)r8   r   r#   N)TrueFalseNoner   rx   )r9   ,=r9   r   r   r:   )rb   rz   r   r   r	   null
setdefaultsetaddr   r   r0   r   r   typer/   ra   reprstr)rh   r+   r,   r6   prevTokrd   r|   tokNumtokVal
nextTokNum
nextTokValr5   objs                r(   auto_symbolr     s   FG
MM(FF12J/ (#W!(
JT>D11 
b(WQZ3->
b(WQZ:-E&",s1Bz)j.>d.JtTl+#%%dCE266t<$'/~Jt$'-d|Jt$tTl+$!$'cNE4#@AXc]MM4,/MM:#4x*ES	tCI'S		  MM66*+6"Q(#T Mr4   c                    g }d}| d   \  }}t        |       }|t        k(  r|dk(  r|dk(  s|dk(  r"| d   d   t        k(  r|j                  |        |S |dkD  r|j                  t        dft        dft        dft        d	ft        d	fg       | dd
 D ]\  \  }}	|t        k(  r	|	dk(  rd}	d}|s|t        k(  r|	dv rt        d      |r|j                  d||	f       I|j                  d||	f       ^ |S |j                  |        |S )zSubstitutes "lambda" with its SymPy equivalent Lambda().
    However, the conversion does not take place if only "lambda"
    is passed because that is a syntax error.

    Fr   lambda      r#   Lambdar9   r:   N:r   T)ry   r   z)Starred arguments in lambda not supportedr8   r   )r'   r   r   ra   r	   r   insert)
rh   r+   r,   r6   flagrA   rB   tokLenr   r   s
             r(   lambda_notationr   I  s2    FDAYNFF[F~&H,Q;&A+&)A,'*A MM&!, M+ aZMMx S	S	S	S	  #)* 	8R<FcM FD";1F$%PQQMM"vv&67MM"vv&67	8 M 	fMr4   c                 2   g }d}| D ]  \  }}|t         k(  r|dk(  r|dz  }|t        k(  r'|}|dk(  r|dz  }0d}|j                  t         |f       J|dk(  rt        d|      }n|dk(  rt        d|      }n|dkD  rt        d}|j                  ||f        |S )z'Allows standard notation for factorial.r   !r#   	factorialr   
factorial2)r	   r   rb   rD   r   )rh   r+   r,   r6   
nfactorialrA   rB   ops           r(   factorial_notationr   q  s    FJ  ,R<FcM!OJz!BSya

r2h'Q.{FCq.|VDa  JMM66*+',( Mr4   c                     g }| D ]R  \  }}|t         k(  r1|dk(  r|j                  t         df       ,|j                  ||f       @|j                  ||f       T |S )z-Treats XOR, ``^``, as exponentiation, ``**``.r   r   )r	   rb   )rh   r+   r,   r6   rA   rB   s         r(   convert_xorr     sa    F  ,R<}r4j)vv./MM66*+, Mr4   c                    g }d }g }| D ]  \  }}|t         k(  r|s<d|v r8d|j                         vr&d|j                         vr|j                  ||f       n ||      r"t        |      dk(  r|j                  ||f       n ||      r0t        |      dk(  r" ||d   d         r|j                  ||f       ng }n|t        k(  rx|d	k(  r&t        |      dk(  r|j                  t        |f       nO|d
k(  r&t        |      dk\  r|j                  t        |f       n$|dk(  r|s|j                  t         df       ng }ng }|j                  ||f       |sM|d   d   d
k(  sZ|dt        |        }|d   d   j                  d      \  }}	|d   d   }
t        |      dk(  r|
|d   d   z  }
|j                  dd      }|	j                  dd      }	|
j                  dd      }
dt        |	      z  }|	|
fD cg c]  }|j                  d       c}\  }	}|xs d}|	xs dd|z   }}|dt        |
      z  |z   }}t        dft        dft        dft         |ft        dft        dft        dft        dft         |ft        dft         |ft        dft        dft        dft        dft         |ft        dft         |ft        dft        dfg}|j                  |       g } |S c c}w )zw
    Allows 0.2[1] notation to represent the repeated decimal 0.2111... (19/90)

    Run this before auto_number.

    c                 &    t        d | D              S )Nc              3   $   K   | ]  }|d v  
 yw)0123456789_NrK   ).0rC   s     r(   	<genexpr>z6repeated_decimals.<locals>.is_digit.<locals>.<genexpr>  s     1!1%1s   )all)ss    r(   is_digitz#repeated_decimals.<locals>.is_digit  s    1q111r4   rx   ejr   r   r8   r#   []z0.Nr      r"   r   019r9   Integerr:   +Rationalr   )
r   lowerrb   r'   r	   r   replacelstripr   ra   )rh   r+   r,   r6   r   numrA   rB   prepostrepetendzerosw	repetendsabcdr   seqs                       r(   repeated_decimalsr     s    F2 C  HVC6Mc.G6<<>)

FF+,&!c#h!m

FF+,&!c#h!mR8L 

FF+,r\}SQ

B<(33s8q=

B<(3s

FD>*Cvv&'3r71:$ Jc#hY'FAq	,IC1vayH3x1}CF1I%++c2&C<<R(D''R0HD	ME7;X6FGqxx}GOD) 
sA;3eqAs3x=0E9qA S	9%III:&IS	II:&IS	IS	)C, MM#CQHT MA Hs   8K c           	         g }| D ]  \  }}|t         k(  r|}g }|j                  d      r|dd }t        dft        dfg}d|v sd|v sd|v rB|j	                  d	      s1t        d
ft        dft         t        t        |            ft        dfg}nt        dft        dft         |ft        dfg}|j                  ||z          |j                  ||f        |S )z
    Converts numeric literals to use SymPy equivalents.

    Complex numbers use ``I``, integer literals use ``Integer``, and float
    literals use ``Float``.

    )r   JNr8   ry   Irx   r   E)0x0XFloatr9   r:   r   )	r   endswithr	   r   
startswithr   r   ra   rb   )	rh   r+   r,   r6   rA   rB   numberpostfixr   s	            r(   auto_numberr     s     F  ,VFGz*9tSk2f}#-3&=**<8gS	T#f+./"c< i(2s)F6$&(#Y0 MM#-(MM66*+',* Mr4   c                     g }d}| D ]f  \  }}|t         k(  r|dk(  rd}d}|j                  ||f       ,|dk(  r#|t        k(  rd}|j                  t        |f       T|j                  ||f       h |S )z=Converts floats into ``Rational``. Run AFTER ``auto_number``.Fr   Tr   )r   rb   r   r   )rh   r+   r,   r6   passed_floatrA   rB   s          r(   rationalizer     s    FL  
,T> ##MM66*+T!f&6 LMM66*+MM66*+
, Mr4   c                 &   g }t         df| v r|j                  t        df       |j                  t         df       | D ]6  }|t         dfk(  r|j                  t         df       &|j                  |       8 |j                  t         df       |S | }|S )a  Transforms the equals sign ``=`` to instances of Eq.

    This is a helper function for ``convert_equals_signs``.
    Works with expressions containing one equals sign and no
    nesting. Expressions like ``(1=2)=False`` will not work with this
    and should be used with ``convert_equals_signs``.

    Examples: 1=2     to Eq(1,2)
              1*2=x   to Eq(1*2, x)

    This does not deal with function arguments yet.

    r   Eqr9   r   r:   )r	   rb   r   )rh   r+   r,   r6   r*   s        r(   _transform_equals_signr   +  s     F
CyFtTl#r3i  	!ES	!r3i(MM% 		!
 	r3i  M Mr4   c                 ~     t        t              | ||      }t        |||      }t        |||      }t	        |      }|S )a{   Transforms all the equals signs ``=`` to instances of Eq.

    Parses the equals signs in the expression and replaces them with
    appropriate Eq instances. Also works with nested equals signs.

    Does not yet play well with function arguments.
    For example, the expression ``(x=y)`` is ambiguous and can be interpreted
    as x being an argument to a function and ``convert_equals_signs`` will not
    work for this.

    See also
    ========
    convert_equality_operators

    Examples
    ========

    >>> from sympy.parsing.sympy_parser import (parse_expr,
    ... standard_transformations, convert_equals_signs)
    >>> parse_expr("1*2=x", transformations=(
    ... standard_transformations + (convert_equals_signs,)))
    Eq(2, x)
    >>> parse_expr("(1*2=x)=False", transformations=(
    ... standard_transformations + (convert_equals_signs,)))
    Eq(Eq(2, x), False)

    )rs   convert_equals_signsrv   r   re   r   s          r(   r  r  H  sE    : 423FJTDD*k:D!$
K@Dd^FMr4   .standard_transformationsr   transformationsc                     g }t        | j                               }t        |j                        D ]  \  }}}}}|j	                  ||f        |D ]  }	 |	|||      } t        |      S )zt
    Converts the string ``s`` to Python code, in ``local_dict``

    Generally, ``parse_expr`` should be used.
    )r   stripr   readlinerb   r   )
r   r+   r,   r  rh   
input_coderA   rB   r"   	transforms
             r(   stringify_exprr
  t  s{     F!'')$J#2:3F3F#G (1avv&'( % <	6:{;< fr4   c                      t        | ||      }|S )zn
    Evaluate Python code generated by ``stringify_expr``.

    Generally, ``parse_expr`` should be used.
    )eval)coder+   r,   exprs       r(   	eval_exprr    s     k:'DKr4   Tc                 0   |i }n.t        |t              st        d      t        |v rt	        d      |hi }t        d|       t        t              }|j                         D ]%  \  }}t        |t        j                        s!|||<   ' t        |d<   t        |d<   nt        |t              st        d      |xs d}t        |t              r)|d	k(  r
t        dd }n|d
k(  r
t        dd }nt	        d      |}t        | |||      }	|st!        t#        |	      dd      }		 t%        |	||      }
|j'                  t        d      D ]  }t        ||<    |
S # t(        $ r8}|j'                  t        d      D ]  }t        ||<    |t	        d|	      d}~ww xY w)a  Converts the string ``s`` to a SymPy expression, in ``local_dict``.

    Parameters
    ==========

    s : str
        The string to parse.

    local_dict : dict, optional
        A dictionary of local variables to use when parsing.

    global_dict : dict, optional
        A dictionary of global variables. By default, this is initialized
        with ``from sympy import *``; provide this parameter to override
        this behavior (for instance, to parse ``"Q & S"``).

    transformations : tuple or str
        A tuple of transformation functions used to modify the tokens of the
        parsed expression before evaluation. The default transformations
        convert numeric literals into their SymPy equivalents, convert
        undefined variables into SymPy symbols, and allow the use of standard
        mathematical factorial notation (e.g. ``x!``). Selection via
        string is available (see below).

    evaluate : bool, optional
        When False, the order of the arguments will remain as they were in the
        string and automatic simplification that would normally occur is
        suppressed. (see examples)

    Examples
    ========

    >>> from sympy.parsing.sympy_parser import parse_expr
    >>> parse_expr("1/2")
    1/2
    >>> type(_)
    <class 'sympy.core.numbers.Half'>
    >>> from sympy.parsing.sympy_parser import standard_transformations,\
    ... implicit_multiplication_application
    >>> transformations = (standard_transformations +
    ...     (implicit_multiplication_application,))
    >>> parse_expr("2x", transformations=transformations)
    2*x

    When evaluate=False, some automatic simplifications will not occur:

    >>> parse_expr("2**3"), parse_expr("2**3", evaluate=False)
    (8, 2**3)

    In addition the order of the arguments will not be made canonical.
    This feature allows one to tell exactly how the expression was entered:

    >>> a = parse_expr('1 + x', evaluate=False)
    >>> b = parse_expr('x + 1', evaluate=0)
    >>> a == b
    False
    >>> a.args
    (1, x)
    >>> b.args
    (x, 1)

    Note, however, that when these expressions are printed they will
    appear the same:

    >>> assert str(a) == str(b)

    As a convenience, transformations can be seen by printing ``transformations``:

    >>> from sympy.parsing.sympy_parser import transformations

    >>> print(transformations)
    0: lambda_notation
    1: auto_symbol
    2: repeated_decimals
    3: auto_number
    4: factorial_notation
    5: implicit_multiplication_application
    6: convert_xor
    7: implicit_application
    8: implicit_multiplication
    9: convert_equals_signs
    10: function_exponentiation
    11: rationalize

    The ``T`` object provides a way to select these transformations:

    >>> from sympy.parsing.sympy_parser import T

    If you print it, you will see the same list as shown above.

    >>> str(T) == str(transformations)
    True

    Standard slicing will return a tuple of transformations:

    >>> T[:5] == standard_transformations
    True

    So ``T`` can be used to specify the parsing transformations:

    >>> parse_expr("2x", transformations=T[:5])
    Traceback (most recent call last):
    ...
    SyntaxError: invalid syntax
    >>> parse_expr("2x", transformations=T[:6])
    2*x
    >>> parse_expr('.3', transformations=T[3, 11])
    3/10
    >>> parse_expr('.3x', transformations=T[:])
    3*x/10

    As a further convenience, strings 'implicit' and 'all' can be used
    to select 0-5 and all the transformations, respectively.

    >>> parse_expr('.3x', transformations='all')
    3*x/10

    See Also
    ========

    stringify_expr, eval_expr, standard_transformations,
    implicit_multiplication_application

    Nz!expecting local_dict to be a dictzcannot use "" in local_dictzfrom sympy import *maxminz"expecting global_dict to be a dictrK   r   implicit   z!unknown transformation group namez<string>r  z-Error from parse_expr with transformed code: )r0   dict	TypeErrorr   
ValueErrorexecvarsbuiltinsrS   typesBuiltinFunctionTyper   r   r   Tr
  compileevaluateFalser  rj   	Exception)r   r+   r  r,   evaluatebuiltins_dictr5   r   _transformationsr  rvrC   r   s                r(   
parse_exprr%    s   B 

D);<<		677"K0X&,,. 	(ID##u889$'D!	( !E ET*<==%+O/3'e# t
* !u@AA*!Z6FGD}T*J?
ZtZ5b) 	!A JqM	!	 Zb) 	!A JqM	!Z"OPTx XYY	Zs   !2E 	F3FFc                     t        j                  |       }t               j                  |      }t        j                  |j
                  d   j                        }t        j                  |      S )zO
    Replaces operators with the SymPy equivalent and sets evaluate=False.
    r   )astparseEvaluateFalseTransformervisit
Expressionbodyvaluefix_missing_locations)r   nodetransformed_nodes      r(   r  r  E  sX     99Q<D/177=~~&6&;&;A&>&D&DE$$%566r4   c                      e Zd Zej                  dej
                  dej                  dej                  dej                  dej                  dej                  dej                  diZdZej                  dej                  d	ej                   d
ej"                  dej$                  dej&                  diZd Zd Zd Zd Zy)r)  AddMulPowOrAndNot)#Absimresignarg	conjugateacosacotacscasecasinatanacoshacothacschasechasinhatanhcoscotcscsecsintancoshcothcschsechsinhtanhexplnlogsqrtcbrtNeLtLeGtGer   c           
         |j                   d   j                  | j                  v r| j                  |j                   d   j                     }| j                  |j                  d         }| j                  |j
                        }t        j                  t        j                  |t        j                               ||gt        j                  dt        j                  d            g      }|S |S )Nr   idctxr!  Fr-  r<  r-  r2   rO   keywords)ops	__class__relational_operatorsr*  comparatorsleftr'  CallNameLoadkeywordConstant)rT   r/  sympy_classrightrl  new_nodes         r(   visit_Comparez&EvaluateFalseTransformer.visit_Comparem  s    88A;  D$=$==33DHHQK4I4IJKJJt//23E::dii(DxxXX#((*=E]++*CLLu<UVWH
 Or4   c                 l   g }|D ]  }t        |t        j                        r|j                  }t        |t        j                        r|j                  }|j                  |k(  r,|j                  | j                  |j                  |             |j                  |       |j                  |        |S rZ   )	r0   r'  rm  r2   rb  ra   flattenrO   rb   )rT   rO   r2   r6   r<  arg_funcs         r(   rw  z EvaluateFalseTransformer.flattenz  s     
	#C#sxx(88h1'}}H;;$&MM$,,sxx">?MM#&c"
	# r4   c           
         |j                   j                  | j                  v rX| j                  |j                   j                     }| j                  |j                        }| j                  |j
                        }d}t        |j                   t        j                        rt        j                  t        j                  dt        j                               t        j                  t        j                         t        j                  d            |gt        j                  dt        j                  d            g	      }nt        |j                   t        j                         rut        |j
                  t        j                        r||}}d
}t        j                  t        j                  dt        j                               |t        j                  t        j                         t        j                  d            gt        j                  dt        j                  d            g	      }nt        j                  t        j                  dt        j                               |t        j                  t        j                         t        j                  d            gt        j                  dt        j                  d            g	      }|r||}}t        j                  t        j                  |t        j                               ||gt        j                  dt        j                  d            g	      }|dv r!| j#                  |j$                  |      |_        |S |S )NFr3  ra  r#   )r   operandr!  rd  re  rf  Tr4  )r2  r3  )r   ri  	operatorsr*  rs  rl  r0   r'  Subrm  rn  ro  UnaryOpUSubrq  rp  Divrw  rO   )rT   r/  rr  rs  rl  revrt  s          r(   visit_BinOpz$EvaluateFalseTransformer.visit_BinOp  s8   77...):):;KJJtzz*E::dii(DC$''377+U
;++S\\!_MuU!kkjSX@YZ[
 DGGSWW-dii5"'%DC88U
;sxxz3<<PQ? ST!kkjSX@YZ[D  HHU
;
CLLQRO!TU!kkjSX@YZ[E #TexxXX#((*=E]++*CLLu<UVWH n, $X]]K HOr4   c                 <   | j                  |      }t        |j                  t        j                        rf|j                  j
                  | j                  v rD|j                  j                  t        j                  dt        j                  d                   |S )Nr!  Frd  re  )generic_visitr0   r2   r'  rn  rb  	functionsrg  rb   rp  rq  )rT   r/  rt  s      r(   
visit_Callz#EvaluateFalseTransformer.visit_Call  se    %%d+dii*tyy||t~~/M$$S[[Zs||Z_G`%abr4   N)rG   rH   rI   r'  r2  Multr4  r|  r  BitOrBitAndBitXorr{  r  NotEqr\  LtEr^  GtEr   rj  ru  rw  r  r  rK   r4   r(   r)  r)  Q  s    %		4

E

E	II 			4*Xr4   r)  )r   r#   r   r      r   r        	   
      
c              #   B   K   | ]  \  }}|d t        |        yw)z: Nr   )r   rC   fs      r(   r   r     s     ]TQ9Q<8]s   c                   "    e Zd ZdZd Zd Zd Zy)_Tzclass to retrieve transformations from a given slice

    EXAMPLES
    ========

    >>> from sympy.parsing.sympy_parser import T, standard_transformations
    >>> assert T[:5] == standard_transformations
    c                 ,    t        t              | _        y rZ   )r'   _transformationNrW   s    r(   rU   z_T.__init__  s    _%r4   c                     t         S rZ   )r  rW   s    r(   __str__z
_T.__str__  s    r4   c                    t        |      t        ur|f}g }|D ]  }t        |      t        u r(|j                  t	        | j
                        |          <t        |      t        u r1|j                  t	        |j                  | j
                                ~t        d       t        |D cg c]  }t        |    c}      S c c}w )Nzunexpected slice arg)r   tupleintrb   r   r  slicera   indicesr  r  )rT   trC   tir"   s        r(   r\   z_T.__getitem__  s    Aw%A 	8BBx3tvvr*+bU"

466 234 677	8 !4Qoa(4554s   ,CN)rG   rH   rI   rJ   rU   r  r\   rK   r4   r(   r  r    s    &6r4   r  rZ   )ZrJ   tokenizer   r   r   r   r   r   r	   r
   r   r   rp  r   r'  r$   ior   r  r  typingr   tTupler   tDictr   r   r   r   r   tUnionsympy.assumptions.askr   sympy.core.basicr   
sympy.corer   sympy.core.functionr   sympy.utilities.miscr   (sympy.functions.elementary.miscellaneousr   r   r   r  r   r_   rr   TRANSboolr)   r3   rD   rF   rM   re   rs   rv   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  __annotations__r
  r  r%  r  NodeTransformerr)  r  r   rS   r  r  r  rK   r4   r(   <module>r     s   M> > >  
    $ $ $ 1 "  ( * = 
sCxS#X$u+tT*DK78## #$ # 
;5 
;d 
; 
; T%[ T%[ 8	tE{ 	
? ?4T&!789 ) )XT&0@)@"AB PT cg 2>T&1G*H%I >W[ >jn >B0$ve_.D'E"F 0TX 0gk 0f1DK 1T 1PT 1hNHcUD[$9 Nn %%67DK T )-26u+0e $ &*/3E{0U  59>B5k>0U 0 0D 0f%DK %T % %PtE{  4 6U  D Xd5k Xt X$ XvU  D DU  D &4; D t :!e !$ !&*!/3E{!P %6 &, 
c t $ s
+03& 4  59--1DpZ# pZ8D> pZ &veSj'93'> ?pZ %TNpZf	7S 	7hs22 hX & ))]_EZEZE\]]6 68 Dr4   