o
    8Va0                     @   s  d Z ddlmZmZmZmZ ddlmZmZm	Z	 ddl
mZ ddlmZ i ddd d	fgd
dd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgdd d d!fgd"d#d d$fgd%d&d d'fgd(d)d d*fgd+d,d d-fgd.d/d d0fgd1d2d d'fgd3d4d d5fgd6d7d d8fgi d9d:d d;fgd<d=d d>fgd?d@d dAfgdBdCd dDfgdEdFd dGfgdHdId dJfgdKdLd dMfgdNdOd dPfgdQdRd dSfgdTdUd dVfgdWdXd dYfgdZd[d dZfgd\d]d d\fgd^d_d d`fgdadbd d`fgdcddd defgdfdgd dhfgi didjd dkfgdldmd dnfgdodpd dkfgdqdrd dsfgdtdud dvfgdwdxd dyfgdzd{d d|fgd}d~d dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgi ddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgi ddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgddd dfgdd dfgdd d fgdd dfgdd dfgdd dfgdd dfgd	d d
fgdd dfgdd dfgdd dfgdd dfgdd dfgdd dfgdd dfgdZG dd deZdd ZdS (  z
Mathematica code printer
    )AnyDictSetTuple)BasicExprFloat)CodePrinter)
precedenceexpc                 C      dS NT xr   r   </usr/lib/python3/dist-packages/sympy/printing/mathematica.py<lambda>       r   ZExplogc                 C   r   r   r   r   r   r   r   r      r   ZLogZsinc                 C   r   r   r   r   r   r   r   r      r   ZSinZcosc                 C   r   r   r   r   r   r   r   r      r   ZCosZtanc                 C   r   r   r   r   r   r   r   r      r   ZTanZcotc                 C   r   r   r   r   r   r   r   r      r   ZCotZsecc                 C   r   r   r   r   r   r   r   r      r   ZSecZcscc                 C   r   r   r   r   r   r   r   r      r   ZCscZasinc                 C   r   r   r   r   r   r   r   r      r   ZArcSinZacosc                 C   r   r   r   r   r   r   r   r      r   ZArcCosZatanc                 C   r   r   r   r   r   r   r   r      r   ZArcTanZacotc                 C   r   r   r   r   r   r   r   r      r   ZArcCotZasecc                 C   r   r   r   r   r   r   r   r      r   ZArcSecZacscc                 C   r   r   r   r   r   r   r   r      r   ZArcCscZatan2c                  G   r   r   r   r   r   r   r   r      r   Zsinhc                 C   r   r   r   r   r   r   r   r      r   ZSinhZcoshc                 C   r   r   r   r   r   r   r   r      r   ZCoshZtanhc                 C   r   r   r   r   r   r   r   r      r   ZTanhZcothc                 C   r   r   r   r   r   r   r   r       r   ZCothZsechc                 C   r   r   r   r   r   r   r   r   !   r   ZSechZcschc                 C   r   r   r   r   r   r   r   r   "   r   ZCschZasinhc                 C   r   r   r   r   r   r   r   r   #   r   ZArcSinhZacoshc                 C   r   r   r   r   r   r   r   r   $   r   ZArcCoshZatanhc                 C   r   r   r   r   r   r   r   r   %   r   ZArcTanhZacothc                 C   r   r   r   r   r   r   r   r   &   r   ZArcCothZasechc                 C   r   r   r   r   r   r   r   r   '   r   ZArcSechZacschc                 C   r   r   r   r   r   r   r   r   (   r   ZArcCsch	conjugatec                 C   r   r   r   r   r   r   r   r   )   r   Z	ConjugateZMaxc                  G   r   r   r   r   r   r   r   r   *   r   ZMinc                  G   r   r   r   r   r   r   r   r   +   r   Zerfc                 C   r   r   r   r   r   r   r   r   ,   r   ZErfZerf2c                  G   r   r   r   r   r   r   r   r   -   r   Zerfcc                 C   r   r   r   r   r   r   r   r   .   r   ZErfcZerfic                 C   r   r   r   r   r   r   r   r   /   r   ZErfiZerfinvc                 C   r   r   r   r   r   r   r   r   0   r   Z
InverseErfZerfcinvc                 C   r   r   r   r   r   r   r   r   1   r   ZInverseErfcZerf2invc                  G   r   r   r   r   r   r   r   r   2   r   Zexpintc                  G   r   r   r   r   r   r   r   r   3   r   ZExpIntegralEZEic                 C   r   r   r   r   r   r   r   r   4   r   ZExpIntegralEiZfresnelcc                 C   r   r   r   r   r   r   r   r   5   r   ZFresnelCZfresnelsc                 C   r   r   r   r   r   r   r   r   6   r   ZFresnelSZgammac                 C   r   r   r   r   r   r   r   r   7   r   ZGammaZ
uppergammac                  G   r   r   r   r   r   r   r   r   8   r   Z	polygammac                  G   r   r   r   r   r   r   r   r   9   r   Z	PolyGammaZloggammac                 C   r   r   r   r   r   r   r   r   :   r   ZLogGammaZbetac                  G   r   r   r   r   r   r   r   r   ;   r   ZBetaZCic                 C   r   r   r   r   r   r   r   r   <   r   ZCosIntegralZSic                 C   r   r   r   r   r   r   r   r   =   r   ZSinIntegralZChic                 C   r   r   r   r   r   r   r   r   >   r   ZCoshIntegralZShic                 C   r   r   r   r   r   r   r   r   ?   r   ZSinhIntegralZlic                 C   r   r   r   r   r   r   r   r   @   r   ZLogIntegralZ	factorialc                 C   r   r   r   r   r   r   r   r   A   r   Z	FactorialZ
factorial2c                 C   r   r   r   r   r   r   r   r   B   r   Z
Factorial2Zsubfactorialc                 C   r   r   r   r   r   r   r   r   C   r   ZSubfactorialcatalanc                 C   r   r   r   r   r   r   r   r   D   r   ZCatalanNumberZharmonicc                  G   r   r   r   r   r   r   r   r   E   r   ZHarmonicNumberZRisingFactorialc                  G   r   r   r   r   r   r   r   r   F   r   Z
PochhammerZFallingFactorialc                  G   r   r   r   r   r   r   r   r   G   r   ZFactorialPowerZlaguerrec                  G   r   r   r   r   r   r   r   r   H   r   Z	LaguerreLZassoc_laguerrec                  G   r   r   r   r   r   r   r   r   I   r   Zhermitec                  G   r   r   r   r   r   r   r   r   J   r   ZHermiteHZjacobic                  G   r   r   r   r   r   r   r   r   K   r   ZJacobiPZ
gegenbauerc                  G   r   r   r   r   r   r   r   r   L   r   ZGegenbauerCZ
chebyshevtc                  G   r   r   r   r   r   r   r   r   M   r   Z
ChebyshevTZ
chebyshevuc                  G   r   r   r   r   r   r   r   r   N   r   Z
ChebyshevUZlegendrec                  G   r   r   r   r   r   r   r   r   O   r   Z	LegendrePZassoc_legendrec                  G   r   r   r   r   r   r   r   r   P   r   Zmathieucc                  G   r   r   r   r   r   r   r   r   Q   r   ZMathieuCZmathieusc                  G   r   r   r   r   r   r   r   r   R   r   ZMathieuSZmathieucprimec                  G   r   r   r   r   r   r   r   r   S   r   ZMathieuCPrimeZmathieusprimec                  G   r   r   r   r   r   r   r   r   T   r   ZMathieuSPrimeZ	stieltjesc                 C   r   r   r   r   r   r   r   r   U   r   ZStieltjesGammaZ
elliptic_ec                  G   r   r   r   r   r   r   r   r   V   r   Z	EllipticEZ
elliptic_fc                  G   r   r   r   r   r   r   r   r   W   r   Z
elliptic_kc                 C   r   r   r   r   r   r   r   r   X   r   Z	EllipticKZelliptic_pic                  G   r   r   r   r   r   r   r   r   Y   r   Z
EllipticPiZzetac                  G   r   r   r   r   r   r   r   r   Z   r   ZZetaZbesselic                  G   r   r   r   r   r   r   r   r   [   r   ZBesselIZbesseljc                  G   r   r   r   r   r   r   r   r   \   r   ZBesselJZbesselkc                  G   r   r   r   r   r   r   r   r   ]   r   ZBesselKZbesselyc                  G   r   r   r   r   r   r   r   r   ^   r   ZBesselYZhankel1c                  G   r   r   r   r   r   r   r   r   _   r   ZHankelH1Zhankel2c                  G   r   r   r   r   r   r   r   r   `   r   ZHankelH2Zairyaic                 C   r   r   r   r   r   r   r   r   a   r   ZAiryAiZairybic                 C   r   r   r   r   r   r   r   r   b   r   ZAiryBic                 C   r   r   r   r   r   r   r   r   c   r   ZAiryAiPrimec                 C   r   r   r   r   r   r   r   r   d   r   ZAiryBiPrimec                  G   r   r   r   r   r   r   r   r   e   r   ZPolyLogc                  G   r   r   r   r   r   r   r   r   f   r   ZLerchPhic                  G   r   r   r   r   r   r   r   r   g   r   ZGCDc                  G   r   r   r   r   r   r   r   r   h   r   ZLCMc                  G   r   r   r   r   r   r   r   r   i   r   ZSphericalBesselJc                  G   r   r   r   r   r   r   r   r   j   r   ZSphericalBesselYc                  G   r   r   r   r   r   r   r   r   k   r   ZHypergeometricPFQc                  G   r   r   r   r   r   r   r   r   l   r   ZMeijerGc                  G   r   r   r   r   r   r   r   r   m   r   ZAppellF1c                 C   r   r   r   r   r   r   r   r   n   r   
DiracDeltac                 C   r   r   r   r   r   r   r   r   o   r   ZHeavisideThetac                  G   r   r   r   r   r   r   r   r   p   r   KroneckerDelta)ZairyaiprimeZairybiprimeZpolylogZlerchphiZgcdZlcmZjnZynZhyperZmeijergZappellf1r   Z	Heavisider   c                       sF  e Zd ZdZdZdZdddi ddd	Ze Ze Z	i fd
dZ
dd Zdd Z fddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 ZeZeZ d4d5 Z!d6d7 Z"d8d9 Z#d:d; Z$d<d= Z%e%Z&d>d? Z'd@dA Z(dBdC Z)dDdE Z*dFdG Z+  Z,S )HMCodePrinterz]A printer to convert python expressions to
    strings of the Wolfram's Mathematica code
    Z_mcodezWolfram LanguageNauto   TF)ZorderZ	full_precZ	precisionuser_functionsZhumanZallow_unknown_functionsc                 C   sd   t | | tt| _|di  }| D ]\}}t|ts)dd |fg||< q| j	| dS )z+Register function mappings supplied by userr   c                  W   r   r   r   r   r   r   r   r      r   z'MCodePrinter.__init__.<locals>.<lambda>N)
r	   __init__dictknown_functionsgetcopyitems
isinstancelistupdate)selfsettingsZ	userfuncskvr   r   r   r      s   

zMCodePrinter.__init__c                 C   s   |S Nr   )r&   linesr   r   r   _format_code      zMCodePrinter._format_codec                 C   s(   t |}d| |j|| |j|f S )Nz%s^%s)r
   parenthesizebaser   )r&   exprPRECr   r   r   
_print_Pow   s   zMCodePrinter._print_Powc                    sT   t | | \}}t |j| }|r(|d7 }|d fdd|D 7 }|S )N*z**c                 3   s    | ]	} | V  qd S r*   )r.   .0ar1   r&   r   r   	<genexpr>   s    z*MCodePrinter._print_Mul.<locals>.<genexpr>)r
   Zargs_cncsuper
_print_Mulfuncjoin)r&   r0   cZncres	__class__r7   r   r:      s   zMCodePrinter._print_Mulc                 C   s,   |  |j}|  |j}|j}d|||S )Nz{} {} {})_printZlhsZrhsZrel_opformat)r&   r0   Zlhs_codeZrhs_codeopr   r   r   _print_Relational   s   zMCodePrinter._print_Relationalc                 C   r   )N0r   r&   r0   r   r   r   _print_Zero   r-   zMCodePrinter._print_Zeroc                 C   r   )N1r   rF   r   r   r   
_print_One   r-   zMCodePrinter._print_Onec                 C   r   )Nz-1r   rF   r   r   r   _print_NegativeOne   r-   zMCodePrinter._print_NegativeOnec                 C   r   )Nz1/2r   rF   r   r   r   _print_Half   r-   zMCodePrinter._print_Halfc                 C   r   )NIr   rF   r   r   r   _print_ImaginaryUnit   r-   z!MCodePrinter._print_ImaginaryUnitc                 C   r   )NZInfinityr   rF   r   r   r   _print_Infinity   r-   zMCodePrinter._print_Infinityc                 C   r   )Nz	-Infinityr   rF   r   r   r   _print_NegativeInfinity   r-   z$MCodePrinter._print_NegativeInfinityc                 C   r   )NZComplexInfinityr   rF   r   r   r   _print_ComplexInfinity   r-   z#MCodePrinter._print_ComplexInfinityc                 C   r   )NZIndeterminater   rF   r   r   r   
_print_NaN   r-   zMCodePrinter._print_NaNc                 C   r   )NEr   rF   r   r   r   _print_Exp1   r-   zMCodePrinter._print_Exp1c                 C   r   )NZPir   rF   r   r   r   	_print_Pi   r-   zMCodePrinter._print_Pic                 C   r   )NZGoldenRatior   rF   r   r   r   _print_GoldenRatio   r-   zMCodePrinter._print_GoldenRatioc                 C   s    |j dd}t|}| ||S )NT)r;   )expandr
   r.   )r&   r0   Zexpandedr1   r   r   r   _print_TribonacciConstant   s   z&MCodePrinter._print_TribonacciConstantc                 C   r   )NZ
EulerGammar   rF   r   r   r   _print_EulerGamma   r-   zMCodePrinter._print_EulerGammac                 C   r   )NZCatalanr   rF   r   r   r   _print_Catalan   r-   zMCodePrinter._print_Catalanc                    s    dd  fdd|D  d S )N{, c                 3       | ]}  |V  qd S r*   doprintr4   r&   r   r   r8          z+MCodePrinter._print_list.<locals>.<genexpr>}r<   rF   r   r_   r   _print_list   s    zMCodePrinter._print_listc                 C      |  | S r*   r^   tolistrF   r   r   r   _print_ImmutableDenseMatrix      z(MCodePrinter._print_ImmutableDenseMatrixc                    sF   ddl m  fdd fdd}fdd}d	| | S )
Nr   )default_sort_keyc                    s,   d  | d d | d d f |S )N{} -> {}r      rB   r^   posvalr_   r   r   
print_rule   s   $z=MCodePrinter._print_ImmutableSparseMatrix.<locals>.print_rulec                     s4   t    d} ddfdd| D  d S )N)keyrZ   r[   c                 3   s    | ]
\}} ||V  qd S r*   r   )r5   r(   r)   )rp   r   r   r8      s    zPMCodePrinter._print_ImmutableSparseMatrix.<locals>.print_data.<locals>.<genexpr>ra   )sortedZtodokr"   r<   )r"   )ri   r0   rp   r   r   
print_data   s   z=MCodePrinter._print_ImmutableSparseMatrix.<locals>.print_datac                           jS r*   r^   shaper   r0   r&   r   r   
print_dims   s   z=MCodePrinter._print_ImmutableSparseMatrix.<locals>.print_dimsSparseArray[{}, {}])Zsympy.core.compatibilityri   rB   r&   r0   rs   rx   r   )ri   r0   rp   r&   r   _print_ImmutableSparseMatrix   s
   z)MCodePrinter._print_ImmutableSparseMatrixc                 C   rd   r*   re   rF   r   r   r   _print_ImmutableDenseNDimArray   rh   z+MCodePrinter._print_ImmutableDenseNDimArrayc                    sL   dd dd fdd fdd} fd	d
}d | | S )Nc                 S   s   dd dd | D  d S )NrZ   r[   c                 s   s    | ]}|V  qd S r*   r   r4   r   r   r   r8      s    zZMCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_string_list.<locals>.<genexpr>ra   rb   )Zstring_listr   r   r   print_string_list   s   zGMCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_string_listc                  W   s   t dd | D S )zHelper function to change Python style indexing to
            Pathematica indexing.

            Python indexing (0, 1 ... n-1)
            -> Mathematica indexing (1, 2 ... n)
            c                 s   s    | ]}|d  V  qdS )rk   Nr   r5   ir   r   r   r8     s    z]MCodePrinter._print_ImmutableSparseNDimArray.<locals>.to_mathematica_index.<locals>.<genexpr>)tuple)argsr   r   r   to_mathematica_index   s   zJMCodePrinter._print_ImmutableSparseNDimArray.<locals>.to_mathematica_indexc                    s   d  |  |S )z.Helper function to print a rule of Mathematicarj   rl   rm   r_   r   r   rp     s   z@MCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_rulec                      s$    fddt  j D S )a/  Helper function to print data part of Mathematica
            sparse array.

            It uses the fourth notation ``SparseArray[data,{d1,d2,...}]``
            from
            https://reference.wolfram.com/language/ref/SparseArray.html

            ``data`` must be formatted with rule.
            c                    s$   g | ]\}}  | |qS r   )Z_get_tuple_index)r5   rq   value)r0   rp   r   r   r   
<listcomp>  s    zTMCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_data.<locals>.<listcomp>)rr   Z_sparse_arrayr"   r   )r0   rp   r}   r   r   r   rs     s
   
z@MCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_datac                      rt   )a  Helper function to print dimensions part of Mathematica
            sparse array.

            It uses the fourth notation ``SparseArray[data,{d1,d2,...}]``
            from
            https://reference.wolfram.com/language/ref/SparseArray.html
            ru   r   rw   r   r   rx     s   z@MCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_dimsry   rB   rz   r   )r0   rp   r}   r&   r   r   _print_ImmutableSparseNDimArray   s   	
z,MCodePrinter._print_ImmutableSparseNDimArrayc                 C   s   |j j| jv r(| j|j j }|D ]\}}||j r&d|| |jdf   S qn|j j| jv rF| j|j j | jv rF| || j|j j S |j jd| |jd  S )Nz%s[%s]r[   z[%s])r;   __name__r   r   Z	stringifyZ_rewriteable_functionsrA   Zrewrite)r&   r0   Z
cond_mfuncZcondZmfuncr   r   r   _print_Function(  s   
zMCodePrinter._print_Functionc                 C   sH   t |jdkrd| |jd S d| |jd | |jd S )Nrk   zProductLog[{}]r   zProductLog[{}, {}])lenr   rB   rA   rF   r   r   r   _print_LambertW6  s
   zMCodePrinter._print_LambertWc                    s\   t |jdkr|jd dd  s|jd |jd g}n|j}dd fdd|D  d S )Nrk   r   zHold[Integrate[r[   c                 3   r\   r*   r]   r4   r_   r   r   r8   A  r`   z/MCodePrinter._print_Integral.<locals>.<genexpr>]])r   Z	variablesZlimitsr   r<   )r&   r0   r   r   r_   r   _print_Integral<  s     zMCodePrinter._print_Integralc                    s"   dd  fdd|jD  d S )Nz	Hold[Sum[r[   c                 3   r\   r*   r]   r4   r_   r   r   r8   D  r`   z*MCodePrinter._print_Sum.<locals>.<genexpr>r   )r<   r   rF   r   r_   r   
_print_SumC  s   "zMCodePrinter._print_Sumc                    s<   |j }dd |jD }dd fdd|g| D  d S )Nc                 S   s$   g | ]}|d  d kr|d n|qS )rk   r   r   r~   r   r   r   r   H  s   $ z2MCodePrinter._print_Derivative.<locals>.<listcomp>zHold[D[r[   c                 3   r\   r*   r]   r4   r_   r   r   r8   I  r`   z1MCodePrinter._print_Derivative.<locals>.<genexpr>r   )r0   Zvariable_countr<   )r&   r0   ZdexprZdvarsr   r_   r   _print_DerivativeF  s   &zMCodePrinter._print_Derivativec                 C   s
   d |S )Nz(* {} *)r   )r&   textr   r   r   _get_commentL  s   
zMCodePrinter._get_comment)-r   
__module____qualname____doc__ZprintmethodlanguageZ_default_settingssetZ_number_symbolsZ_not_supportedr   r,   r2   r:   rD   rG   rI   rJ   rK   rM   rN   rO   rP   rQ   rS   rT   rU   rW   rX   rY   rc   Z_print_tupleZ_print_Tuplerg   r{   r|   r   r   Z_print_MinMaxBaser   r   r   r   r   __classcell__r   r   r?   r   r   t   s^    	
	.r   c                 K   s   t || S )a  Converts an expr to a string of the Wolfram Mathematica code

    Examples
    ========

    >>> from sympy import mathematica_code as mcode, symbols, sin
    >>> x = symbols('x')
    >>> mcode(sin(x).series(x).removeO())
    '(1/120)*x^5 - 1/6*x^3 + x'
    )r   r^   )r0   r'   r   r   r   mathematica_codeP  s   r   N)r   typingr   r   r   r   Z
sympy.corer   r   r   Zsympy.printing.codeprinterr	   Zsympy.printing.precedencer
   r   r   r   r   r   r   r   <module>   s   	
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUV
g ]