o
    *ήc                     @   s  d dl Z d dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZmZ d dlZd dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmZmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZR d dlSmTZTmUZU d dlVmWZW d dlXmYZY d dlZm[Z[ dd	d
Z\dd Z]dd Z^dd Z_e_G dd dZ`dS )    N)product)AnyDictTupleListOptionalUnionCallable)EMulAddPow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pir   GreaterThanStrictGreaterThanStrictLessThanLessThanEqualityOrAndLambdaIntegerDummysymbols)sympify_sympify)airybiprime)li)sympy_deprecation_warningc                 C   s$   t dddd t|}t|| S )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)rR   MathematicaParserrN   
_parse_old)sadditional_translationsparser rZ   @/tmp/pip-target-vg8gfxp4/lib/python/sympy/parsing/mathematica.pymathematica   s   r\   c                 C   s   t  }|| S )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   rZ   rZ   r[   parse_mathematica   s   2
r^   c                     s   t | dkrB| d }td | }dd |D }t|}t|tr=td| td}t||	 fdd	t
|D S td
|S t | dkrU| d }| d }t||S td)N   r   Slotc                 S   s   g | ]}|j d  qS )r   )args).0arZ   rZ   r[   
<listcomp>Z       z#_parse_Function.<locals>.<listcomp>zdummy0:clsc                    s   i | ]\}} |d  |qS )r_   rZ   )rb   ivr`   rZ   r[   
<dictcomp>^   s    z#_parse_Function.<locals>.<dictcomp>rZ      z&Function node expects 1 or 2 arguments)lenr@   atomsmax
isinstancerK   rM   rL   rJ   xreplace	enumerateSyntaxError)ra   argslotsnumbersnumber_of_arguments	variablesbodyrZ   rj   r[   _parse_FunctionU   s   

"

rz   c                 C   s   |    | S N)_initialize_classrf   rZ   rZ   r[   _decoh   s   r}   c                !   @   s*  e Zd ZU dZi dddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)Zed*d+d,D ])\ZZZee e d- Z	erdd.e
  e d/ Zne
 e d/ Zee	ei qJd0d1d2d3d4Zed5ejd6fed7ejd6fed8ejd9fed:ejd;fd<Zed=ejZed>ejZd?Zi Zi Zi Zed@dA Zd"dCdDZedEdF ZdGdH ZdIdJ ZedKdL ZedMdN ZedOdP Z edQdR Z!dSdT Z"dUdV Z#dWZ$dXZ%dYZ&dZZ'd[Z(d\Z)e&dBd]d^d_ ife$e'd]d`ife$e(dadbdcdddedfdgfe$e)dhdid_ ife&dBdjdkife$e)dldmife$e(dndodpfe$e)dqdrife$e'dsdtife&dBdudvdwfe$e'dxdyife$e'dzd{ife%dBd|d}ife$e'd~ddfe$e'dddddddfe$dBddife$e'dddfe$e'dddfe$e'ddife%dBdd_ dd_ dfe$e(ddife$e(ddddd_ dfe&dBdddddfe$dBdd_ dd_ dfe%dBdd_ dd_ dfe$dBddife&dBdd_ dd_ dd_ dd_ dfe$dBddd_ ife%dBdddfgZ*e+e,e-e.e- e/e-e0e-e1f f f  e2d< dd_ dd_ dZ3dZ4dZ5g dZ6g dZ7edd Z8edd Z9dBZ:dd Z;de-fddZ<de0e-e=f de>fddĄZ?de0e-e=f de>fddƄZ@de0e-e=f de>fddȄZAde=fdd˄ZBde=de=de>fddτZCde=fddфZDd#de=de>fddԄZEde-de-fdd؄ZFde-fddۄZGde=fddބZHeId$i deJdeKdeLddd_ ddd_ ddd_ deMdeNdeOdePdeQdeRdeSdeTdeUdeVddd_ deWdeXdeYdeZde[de\de]de^de_de`deadebdecdeddeed efdegdehjdeidejdekdeldemdend	eod
epddd_ deqderdesdetdeudevdewdexdeydezde{de|de}de~dedededededededyed{edkeZeedZdd Zd d! ZdBS (%  rU   ap  
    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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	airybi(x)zairybiprime(x)z li(x)z
primepi(x)zprime(x)z
isprime(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]rc   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])
                rB   )
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])
                c                 C   s   |  | j}| j| d S r{   )_compile_dictionaryCORRESPONDENCESTRANSLATIONSupdate)rg   drZ   rZ   r[   r|      s   z#MathematicaParser._initialize_classNc                 C   sl   i | _ | j | j |d u ri }| jj|kr,t|tstd| |}|| j_|| j_	| j | jj	 d S )NzThe argument must be dict type)
translationsr   r   	__class__cache_originalrp   dict
ValueErrorr   cache_compiled)selfrX   r   rZ   rZ   r[   __init__   s   

zMathematicaParser.__init__c                 C   sV  i }|  D ]\}}| | | | | |d}| |d}| |d}| |d}| j|}|d u r@dj|d}t|| }| 	|\}}	|
 dksW|	t|kradj|d}t||d d dkrld}
nt|}
||
f}dd	 |D }d
d| d }| jj|d}t|tj}i ||< ||| d< ||| d< ||| d< q|S )Nr   r   '{f}' function form is invalid.fr   r   c                 S   s$   g | ]}|d  dkr|nd| qS )r   r   \rZ   )rb   xrZ   rZ   r[   rd   A  s   $ z9MathematicaParser._compile_dictionary.<locals>.<listcomp>z(?:(|z)))	argumentsfsra   pat)items_check_input_apply_rules_replace
FM_PATTERNsearchformatr   group	_get_argsstartrm   joinARGS_PATTERN_TEMPLATErecompileVERBOSE)rg   dicr   fmr   merrfm_namera   endkey_argkeyre_argsxyzpatStrr   rZ   rZ   r[   r     s<   

z%MathematicaParser._compile_dictionaryc           
      C   s   | j }d}d}	 ||}|du r||7 }	 |S | }| |\}}| }	| ||||	|}|	}||d| 7 }||d }q)z'Parse Mathematica function to SymPy oner~   r   TN)r   r   r   r   r   _convert_one_function)
r   rW   r   scannedcurr   r   ra   r   bgnrZ   rZ   r[   _convert_functionS  s"   
z#MathematicaParser._convert_functionc                 C   sd  |t |f| jv r!|t |f}| j| d }dd t||D }nA|df| jv rX|df}| j| d }i }t|D ]\}	}
|
d dkrPd||	d  ||
<  n||	 ||
< q9n
dj|d}t|| j| d	 }| j| d
 }d}d}	 ||}|d u r||7 }n| }
|	 }||d | ||
  7 }|
 }||d  }qu|d | | ||d   }|S )Nra   c                 S   s   i | ]\}}||qS rZ   rZ   )rb   kri   rZ   rZ   r[   rk     re   z;MathematicaParser._convert_one_function.<locals>.<dictcomp>r   r   ,z'{f}' is out of the whitelist.r   r   r   r~   )rm   r   ziprr   r   r   r   r   r   r   r   )r   rW   r   ra   r   r   r   x_argsr   rh   r   r   templater   r   r   r   xbgnrZ   rZ   r[   r   z  sB   
z'MathematicaParser._convert_one_functionc                 C   s   |j }| d }g g }}g }|}t||d |D ]K\}}	|	dkr4|s4|s4||||  |d }|	dkr>||	 n|	dkrF|  |	dkrP||	 q|	dkrf|r[|  q||||   nq|d }
||
fS )z'Get arguments of a Mathematica functionr_   Nr   r   r   r   r   )stringr   rr   appendpop)rg   r   rW   ancsquarecurlyra   r   rh   cfunc_endrZ   rZ   r[   r     s.   

zMathematicaParser._get_argsc                 C   s   | j | }|||}|S r{   )REPLACEMENTSreplace)rg   rW   befaftrZ   rZ   r[   r     s   
zMathematicaParser._replacec                 C   s   | j | \}}|||S r{   )RULESsub)rg   rW   r   r   r   rZ   rZ   r[   r     s   zMathematicaParser._apply_rulesc                 C   sR   dD ]}| |d | |d krdj|d}t|qd|v r'd}t|d S )N))r   r   )r   r   )()r   r_   r   r   r   z Currently list is not supported.)countr   r   )rg   rW   bracketr   rZ   rZ   r[   r     s   zMathematicaParser._check_inputc                 C   s`   |  | | |d}| |d}| |d}| |d}| |}| |d}| |d}|S )Nr   r   r   r   r   r   )r   r   r   r   )r   rW   rZ   rZ   r[   rV     s   

zMathematicaParser._parse_oldc                 C   s"   |  |}| |}| |}|S r{   )_from_mathematica_to_tokens_from_tokens_to_fullformlist_from_fullformlist_to_sympy)r   rW   s2s3s4rZ   rZ   r[   r]     s   


zMathematicaParser.parseInfixPrefixPostfixFlatRightLeft;c                 C   s.   t | tr| r| d dkr| dg S d| dgS )Nr   CompoundExpressionNull)rp   listr   rZ   rZ   r[   <lambda>#  s   . zMathematicaParser.<lambda>r   Set
SetDelayedAddToSubtractFromTimesByDivideBy)=z:=z+=z-=z*=z/=z//c                 C   s   | |gS r{   rZ   r   yrZ   rZ   r[   r   &      &r@   z/.
ReplaceAllRuleRuleDelayed)z->z:>z/;	Conditionr   AlternativesRepeatedRepeatedNull)z..z...z||rH   z&&rI   !NotSameQUnsameQ)z===z=!=EqualUnequal	LessEqualLessGreaterEqualGreater)z==z!=z<=<z>=>z;;SpanPlus+-Times)r   /.Dotc                 C   s
   t | S r{   )rU   _get_negr   rZ   rZ   r[   r   6     
 c                 C   s   | S r{   rZ   r   rZ   rZ   r[   r   7  s    )r  r  r   PowerApplyMapMapAllc                 C   s   d| |ddggS )Nr  r   1rZ   r   rZ   rZ   r[   r   9      )z@@z/@z//@z@@@
Derivative	Factorial
Factorial2	Decrement)'r  z!!z--c                 C   s
   | g|S r{   rZ   r   rZ   rZ   r[   r   ;  r  c                 C   s   d| g|S )NPartrZ   r   rZ   rZ   r[   r   ;      )r   [[c                 C   s
   dg| S )Nr   rZ   r   rZ   rZ   r[   r   <  r  c                 C   s   | d S )Nr   rZ   r   rZ   rZ   r[   r   <  r   )r   r   ?PatternTestc                 C      d| dggS NPatternBlankrZ   r   rZ   rZ   r[   r   ?  r'  c                 C   s   dd| dgggS )Nr   r-  r.  rZ   r   rZ   rZ   r[   r   @  r   c                 C   r+  )Nr-  BlankSequencerZ   r   rZ   rZ   r[   r   A  r'  c                 C   r+  )Nr-  BlankNullSequencerZ   r   rZ   rZ   r[   r   B  r'  )_z_._____r1  c                 C   s   d| d|ggS r,  rZ   r   rZ   rZ   r[   r   D  s    r`   SlotSequence)#z##_mathematica_op_precedencec                   C      ddgS )Nr`   r  rZ   rZ   rZ   rZ   r[   r   I  r   c                   C   r7  )Nr4  r  rZ   rZ   rZ   rZ   r[   r   J  r   z[A-Za-z][A-Za-z0-9]*z (?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+))r   r   r(  r   )r   r   ]]r   c                 C   s,   t |trttj|rd| S dd|gS )Nr  r  -1)rp   strr   matchrU   _numberrg   r   rZ   rZ   r[   r  S  s   ,zMathematicaParser._get_negc                 C   s
   d|dgS )Nr  r9  rZ   r=  rZ   rZ   r[   _get_invW  s   
zMathematicaParser._get_invc                 C   s   | j d ur| j S | j| jg}| jd d  | jd d   }| jD ]\}}}|D ]}|| q&q|jdd d |t	t
j| |d |d t
dd| d }|| _ | j S )	Nc                 S   s
   t |  S r{   )rm   r   rZ   rZ   r[   r   f  r  z2MathematicaParser._get_tokenizer.<locals>.<lambda>)r   r   
r   r   r   )_regex_tokenizer_literalr<  _enclosure_open_enclosure_closer6  r   sortextendmapr   escaper   r   )r   tokenstokens_escapetypstratsymdictr   	tokenizerrZ   rZ   r[   _get_tokenizer]  s   


z MathematicaParser._get_tokenizercodec                    s  |    g }	 |d}|dkrt|dkr|| nCtd||d d  }|d u r0td||  d }|dkrE||d |  |d||d | d	dg ||d d  }qt	|D ]9\}}t
|trmqc	 |d
}|dkrxn |d}	|	dks|	|k rtd|d | ||	d d   }qn|||< qc fdd|D }
dd |
D }|r|d dkr|d |r|d dks|r|d dkr|d |r|d dks|S )NT"r   r   z(?<!\\)"r_   z"mismatch in string "  " expression_Strz\"z(*z*)zmismatch in comment (*  *) coderl   c                    s&   g | ]}t |tr |n|gqS rZ   )rp   r:  findall)rb   rh   rM  rZ   r[   rd     s   & zAMathematicaParser._from_mathematica_to_tokens.<locals>.<listcomp>c                 S   s   g | ]	}|D ]}|qqS rZ   rZ   )rb   rh   jrZ   rZ   r[   rd     s    r?  )rN  findrm   r   r   r   rs   r   r   rr   rp   r   r   )r   rO  code_splitsstring_start	match_end
string_endrh   
code_splitpos_comment_startpos_comment_endtoken_listsrH  rZ   rS  r[   r   n  sN   

"





z-MathematicaParser._from_mathematica_to_tokenstokenreturnc                 C   s:   t |trdS t| j|rdS td| j |rdS dS )NFz-?T)rp   r   r   r;  rA  r<  r   r^  rZ   rZ   r[   _is_op  s   
zMathematicaParser._is_opc                 C      |dv rdS |  | S )N)r   r   Tra  r`  rZ   rZ   r[   _is_valid_star1     z!MathematicaParser._is_valid_star1c                 C   rb  )N)r   r   Trc  r`  rZ   rZ   r[   _is_valid_star2  re  z!MathematicaParser._is_valid_star2rH  c           
      C   s|  g g}g }d}|t |k r.|| }|| jv r)|d | || |g  n|dkrVt |d dkrG|d d |d krGtd|d  | |d |d< |g  n|| jv r| j|}| j| |d krtd}|dkr|d dkr|d dkr||d	 d
 n1|d dkr||d	  d
krd||d	 < n||d	  dkrd||d	 < ||d d
 n|n|t |d dkr|d d dkrtd| |d d}||d< g }	|d d |d kr|	|  |d d |d ks|		  |d dkrt |	d	krtdt |	 |d |	 |d n|d | |d	7 }|t |k st |d	ks7J | |d S )Nr   r   r   z %s cannot be followed by comma ,zunmatched enclosurer8  r   r_   r   r(  rl   r   z( ) not valid syntaxTz1( must be followed by one expression, %i detected)
rm   rB  r   rs   _parse_after_bracesrC  indexinsertr   reverse)
r   rH  stackopen_seqpointerr^  indunmatched_enclosure
last_stacknew_stack_elementrZ   rZ   r[   r     s`   

$	 /z.MathematicaParser._from_tokens_to_fullformlistlinesinside_enclosurec           	      C   s  d}t |}||k r|| }|dkr|r|| |d8 }q|dkr,|d |d8 }q|dkrOz| |d | |}W n tyN   || |d8 }Y qw |d }t |dkri|d dkri||dd   n|| t|D ]}|d qr||8 }d}q|d7 }||k s
d S d S )Nr   r?  r_   r   )rm   r   rh  rs   rE  r   range)	r   rs  rH  rt  rn  sizer^  	prev_exprrh   rZ   rZ   r[   _util_remove_newlines  s@   



z'MathematicaParser._util_remove_newlinesc                 C   s   t |}d}||k rM|dkrC| ||d  rC| || rC|| dkr5d||< ||d  d ||d < n||d |d7 }|d7 }|d7 }||k s
d S d S )Nr   r_   r   r   )rm   rd  rf  rj  )r   rH  rv  rn  rZ   rZ   r[   _util_add_missing_asterisks	  s    z-MathematicaParser._util_add_missing_asterisksFc                 C   s\  d}g }|  ||| t| jD ]P\}}}d|v r| | t|}d}	|	|k ra||	 }
t|
trY|
|v rY||
 }t|trH|g}d}ng }d}|
dv rg|| jkrg|	dkrg| ||	d  sg|	d7 }	q%|| j	kr|	dks|	|d ks| ||	d  s| ||	d  r|	d7 }	q%d}|||	< || j	kr|
|	d }|
|	}|
dkr| |}n	|
dkr| |}|	d8 }	|d	8 }|| |}|| jkr"|	d	 |k r| ||	d  |
r|| |
|	d }|
|	d }|dkr| |}n
|dkr| |}|d	8 }|	d	 |k r| ||	d  |
s|| n|| jkrk|	d	 |k re||	d  |
kre|||g |d
 }|
|	d  |
|	d }|d	8 }|	d	 |k re||	d  |
ks8|| n|| jkr|	d |k r||	d  |
krt|tr||| |g||< n	||| |||< |
|	d  |
|	d }|d	8 }|	d |k r||	d  |
ks|| ns|| nm|| jkr|d u sJ |	|d ks| ||	d  r| j|
  ||	< nF||
|	d  |d8 }n7|| jkr5|d u sJ |	dks| ||	d  r#| j|
  ||	< n||
|	d  |	d8 }	|d8 }t|trYtt|}|| }|  t|trU|| n|||	< |	d7 }	|	|k s*qt|dkswt|dkrt|dkr|r| ||S tdt|dkr|d r|d d dkr|d dd  }dg||}|S |d S )NFr   r   r_   r  Tr  r  rl   r   z0unable to create a single AST for the expressionr   )rx  reversedr6  ry  rm   rp   r:  PREFIXra  INFIXr   r>  r  r   FLAT_check_op_compatibleRIGHTLEFT_missing_arguments_defaultPOSTFIXr	   typingcastclearr   rE  rh  rs   )r   rH  rt  changedrs  op_typegrouping_stratop_dictrv  rn  r^  op_namenodefirst_indexarg1arg2node_pother_opop_callnew_nodecompound_expressionrZ   rZ   r[   rh    s   


,
8


$


"	    "
*Yz%MathematicaParser._parse_after_bracesop1op2c                 C   sH   ||krdS ddh}ddh}||v r||v rdS ||v r"||v r"dS dS )NTr   r  r  r  FrZ   )r   r  r  muldivaddsubrZ   rZ   r[   r~    s   z&MathematicaParser._check_op_compatiblewmexprc           	      C   s   g }|g}t d|}d}|D ]f}|du r |d S | }||| dddddd }| dkrD|dkrC|d | n-| dkrZ|dkrU|d | |  n| dkrq|d |g ||d d  | }q|d S )	zH
        Parses FullForm[Downvalues[]] generated by Mathematica
        z[\[\],]r   Nr   r~   r   r   r   )	r   finditerr   r   stripr   r   r   r   )	r   r  outrl  	generatorlast_posr;  position	last_exprrZ   rZ   r[   _from_fullform_to_fullformlist  s.   (

z0MathematicaParser._from_fullform_to_fullformlistpylistc                    s(   ddl m m  fdd|S )Nr   )r@   Symbolc                    sf   t | tr&t| dkr"| d }fdd| dd  D } || S tdt | tr/| S t| S )Nr   c                    s   g | ]} |qS rZ   rZ   rb   rt   )	converterrZ   r[   rd     s    z\MathematicaParser._from_fullformlist_to_fullformsympy.<locals>.converter.<locals>.<listcomp>r_   zEmpty list of expressions)rp   r   rm   r   r:  rO   )exprheadra   r@   r  r  rZ   r[   r    s   

zHMathematicaParser._from_fullformlist_to_fullformsympy.<locals>.converter)sympyr@   r  )r   r  rZ   r  r[   #_from_fullformlist_to_fullformsympy  s   z5MathematicaParser._from_fullformlist_to_fullformsympyLogc                  G   s   t t|  S r{   )r   rz  rc   rZ   rZ   r[   r     r'  Log2c                 C   
   t | dS Nrl   r   r   rZ   rZ   r[   r     r  Log10c                 C   r  )N
   r  r   rZ   rZ   r[   r     r  ExpSqrtr   r   r   r   r   r   ArcSinArcCosArcTanc                  G   s    t | dkrtt|  S t|  S r  )rm   r,   rz  r+   r  rZ   rZ   r[   r     s     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                  G   s   t jS r{   )r*   Zeror  rZ   rZ   r[   r     s    r-   r.   r/   
PochhammerExpIntegralEiSinIntegralCosIntegralAiryAiAiryAiPrimeAiryBiAiryBiPrimeLogIntegralPrimePiPrimePrimeQr   )rA   r   c                    s    fdd  |S )Nc                    st   t | tr1t | d tr | d }nj| d t| d }|t fdd| dd  D  S j| t| S )Nr   c                 3   s    | ]} |V  qd S r{   rZ   r  )recurserZ   r[   	<genexpr>,  s    zQMathematicaParser._from_fullformlist_to_sympy.<locals>.recurse.<locals>.<genexpr>r_   )rp   r   _node_conversionsgetr@   _atom_conversionsrN   )r  r  r  r   rZ   r[   r  &  s   
"z>MathematicaParser._from_fullformlist_to_sympy.<locals>.recurserZ   )r   full_form_listrZ   r  r[   r   $  s   
z-MathematicaParser._from_fullformlist_to_sympyc                 C   s,   |}| j  D ]\}}|t||}q|S r{   )r  r   r   r@   )r   mformr  mma_form
sympy_noderZ   rZ   r[   _from_fullformsympy_to_sympy2  s   z.MathematicaParser._from_fullformsympy_to_sympyr{   )FrZ   )__name__
__module____qualname____doc__r   r   arctrir   r   lowerr   r   r   r   r   r   r   r   ARG_MTRX_PATTERNr   r   r   r   classmethodr|   r   r   r   r   r   r   r   r   rV   r]   r|  r{  r  r}  r  r  r6  r   tTupler:  r   tDicttUnionr	   __annotations__r  rA  r<  rB  rC  r  r>  r@  rN  r   r   boolra  rd  rf  r   rx  ry  rh  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-   r.   r/   r0   r1   r2   r3   r4   r5   r6   rP   rQ   r7   r8   r9   r   rD   rC   rE   rF   rG   rH   rI   rz   r  rA   rB   r  r   r  rZ   rZ   rZ   r[   rU   m   sr  
 	




)		

?'B
%




.'

/	6#w

 !"#$&'()*+-./01
56789:;<=>?@ABCDFGHIJKLMOSrU   r{   )ar   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*   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   rM   sympy.core.sympifyrN   rO   sympy.functions.special.besselrP   'sympy.functions.special.error_functionsrQ   sympy.utilities.exceptionsrR   r\   r^   rz   r}   rU   rZ   rZ   rZ   r[   <module>   s"    $ 
6