o
    *ήc1                     @   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 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/d0d	 d1fgd2d3d	 d(fgd4d5d	 d6fgd7d8d	 d9fgi d:d;d	 d<fgd=d>d	 d?fgd@dAd	 dBfgdCdDd	 dEfgdFdGd	 dHfgdIdJd	 dKfgdLdMd	 dNfgdOdPd	 dQfgdRdSd	 dTfgdUdVd	 dWfgdXdYd	 dZfgd[d\d	 d]fgd^d_d	 d^fgd`dad	 d`fgdbdcd	 ddfgdedfd	 ddfgdgdhd	 difgi djdkd	 dlfgdmdnd	 dofgdpdqd	 drfgdsdtd	 dofgdudvd	 dwfgdxdyd	 dzfgd{d|d	 d}fgd~dd	 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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	 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	 d'fgd(d)d	 d*fgd+d,d	 d+fgd-d.d	 d/fgd0d	 d1fgd2d	 d3fgd4ZG d5d6 d6eZd7d8 Zd9S (:  z
Mathematica code printer
    )AnyDictSetTuple)BasicExprFloat)default_sort_key)CodePrinter)
precedenceexpc                 C      dS NT xr   r   A/tmp/pip-target-vg8gfxp4/lib/python/sympy/printing/mathematica.py<lambda>       r   Explogc                 C   r   r   r   r   r   r   r   r      r   Logsinc                 C   r   r   r   r   r   r   r   r      r   Sincosc                 C   r   r   r   r   r   r   r   r      r   Costanc                 C   r   r   r   r   r   r   r   r      r   Tancotc                 C   r   r   r   r   r   r   r   r      r   Cotsecc                 C   r   r   r   r   r   r   r   r      r   Seccscc                 C   r   r   r   r   r   r   r   r      r   Cscasinc                 C   r   r   r   r   r   r   r   r      r   ArcSinacosc                 C   r   r   r   r   r   r   r   r      r   ArcCosatanc                 C   r   r   r   r   r   r   r   r      r   ArcTanacotc                 C   r   r   r   r   r   r   r   r      r   ArcCotasecc                 C   r   r   r   r   r   r   r   r      r   ArcSecacscc                 C   r   r   r   r   r   r   r   r      r   ArcCscatan2c                  G   r   r   r   r   r   r   r   r      r   sinhc                 C   r   r   r   r   r   r   r   r      r   Sinhcoshc                 C   r   r   r   r   r   r   r   r      r   Coshtanhc                 C   r   r   r   r   r   r   r   r       r   Tanhcothc                 C   r   r   r   r   r   r   r   r   !   r   Cothsechc                 C   r   r   r   r   r   r   r   r   "   r   Sechcschc                 C   r   r   r   r   r   r   r   r   #   r   Cschasinhc                 C   r   r   r   r   r   r   r   r   $   r   ArcSinhacoshc                 C   r   r   r   r   r   r   r   r   %   r   ArcCoshatanhc                 C   r   r   r   r   r   r   r   r   &   r   ArcTanhacothc                 C   r   r   r   r   r   r   r   r   '   r   ArcCothasechc                 C   r   r   r   r   r   r   r   r   (   r   ArcSechacschc                 C   r   r   r   r   r   r   r   r   )   r   ArcCschsincc                 C   r   r   r   r   r   r   r   r   *   r   Sinc	conjugatec                 C   r   r   r   r   r   r   r   r   +   r   	ConjugateMaxc                  G   r   r   r   r   r   r   r   r   ,   r   Minc                  G   r   r   r   r   r   r   r   r   -   r   erfc                 C   r   r   r   r   r   r   r   r   .   r   Erferf2c                  G   r   r   r   r   r   r   r   r   /   r   erfcc                 C   r   r   r   r   r   r   r   r   0   r   Erfcerfic                 C   r   r   r   r   r   r   r   r   1   r   Erfierfinvc                 C   r   r   r   r   r   r   r   r   2   r   
InverseErferfcinvc                 C   r   r   r   r   r   r   r   r   3   r   InverseErfcerf2invc                  G   r   r   r   r   r   r   r   r   4   r   expintc                  G   r   r   r   r   r   r   r   r   5   r   ExpIntegralEEic                 C   r   r   r   r   r   r   r   r   6   r   ExpIntegralEifresnelcc                 C   r   r   r   r   r   r   r   r   7   r   FresnelCfresnelsc                 C   r   r   r   r   r   r   r   r   8   r   FresnelSgammac                 C   r   r   r   r   r   r   r   r   9   r   Gamma
uppergammac                  G   r   r   r   r   r   r   r   r   :   r   	polygammac                  G   r   r   r   r   r   r   r   r   ;   r   	PolyGammaloggammac                 C   r   r   r   r   r   r   r   r   <   r   LogGammabetac                  G   r   r   r   r   r   r   r   r   =   r   BetaCic                 C   r   r   r   r   r   r   r   r   >   r   CosIntegralSic                 C   r   r   r   r   r   r   r   r   ?   r   SinIntegralChic                 C   r   r   r   r   r   r   r   r   @   r   CoshIntegralShic                 C   r   r   r   r   r   r   r   r   A   r   SinhIntegrallic                 C   r   r   r   r   r   r   r   r   B   r   LogIntegral	factorialc                 C   r   r   r   r   r   r   r   r   C   r   	Factorial
factorial2c                 C   r   r   r   r   r   r   r   r   D   r   
Factorial2subfactorialc                 C   r   r   r   r   r   r   r   r   E   r   Subfactorialcatalanc                 C   r   r   r   r   r   r   r   r   F   r   CatalanNumberharmonicc                  G   r   r   r   r   r   r   r   r   G   r   HarmonicNumberlucasc                 C   r   r   r   r   r   r   r   r   H   r   LucasLRisingFactorialc                  G   r   r   r   r   r   r   r   r   I   r   
PochhammerFallingFactorialc                  G   r   r   r   r   r   r   r   r   J   r   FactorialPowerlaguerrec                  G   r   r   r   r   r   r   r   r   K   r   	LaguerreLassoc_laguerrec                  G   r   r   r   r   r   r   r   r   L   r   hermitec                  G   r   r   r   r   r   r   r   r   M   r   HermiteHjacobic                  G   r   r   r   r   r   r   r   r   N   r   JacobiP
gegenbauerc                  G   r   r   r   r   r   r   r   r   O   r   GegenbauerC
chebyshevtc                  G   r   r   r   r   r   r   r   r   P   r   
ChebyshevT
chebyshevuc                  G   r   r   r   r   r   r   r   r   Q   r   
ChebyshevUlegendrec                  G   r   r   r   r   r   r   r   r   R   r   	LegendrePassoc_legendrec                  G   r   r   r   r   r   r   r   r   S   r   mathieucc                  G   r   r   r   r   r   r   r   r   T   r   MathieuCmathieusc                  G   r   r   r   r   r   r   r   r   U   r   MathieuSmathieucprimec                  G   r   r   r   r   r   r   r   r   V   r   MathieuCPrimemathieusprimec                  G   r   r   r   r   r   r   r   r   W   r   MathieuSPrime	stieltjesc                 C   r   r   r   r   r   r   r   r   X   r   StieltjesGamma
elliptic_ec                  G   r   r   r   r   r   r   r   r   Y   r   	EllipticE
elliptic_fc                  G   r   r   r   r   r   r   r   r   Z   r   
elliptic_kc                 C   r   r   r   r   r   r   r   r   [   r   	EllipticKelliptic_pic                  G   r   r   r   r   r   r   r   r   \   r   
EllipticPizetac                  G   r   r   r   r   r   r   r   r   ]   r   Zetadirichlet_etac                 C   r   r   r   r   r   r   r   r   ^   r   DirichletEta
riemann_xic                 C   r   r   r   r   r   r   r   r   _   r   	RiemannXibesselic                  G   r   r   r   r   r   r   r   r   `   r   BesselIbesseljc                  G   r   r   r   r   r   r   r   r   a   r   BesselJbesselkc                  G   r   r   r   r   r   r   r   r   b   r   BesselKbesselyc                  G   r   r   r   r   r   r   r   r   c   r   BesselYhankel1c                  G   r   r   r   r   r   r   r   r   d   r   HankelH1hankel2c                  G   r   r   r   r   r   r   r   r   e   r   HankelH2airyaic                 C   r   r   r   r   r   r   r   r   f   r   AiryAiairybic                 C   r   r   r   r   r   r   r   r   g   r   AiryBiairyaiprimec                 C   r   r   r   r   r   r   r   r   h   r   AiryAiPrimeairybiprimec                 C   r   r   r   r   r   r   r   r   i   r   AiryBiPrimepolylogc                  G   r   r   r   r   r   r   r   r   j   r   PolyLoglerchphic                  G   r   r   r   r   r   r   r   r   k   r   LerchPhigcdc                  G   r   r   r   r   r   r   r   r   l   r   GCDlcmc                  G   r   r   r   r   r   r   r   r   m   r   LCMjnc                  G   r   r   r   r   r   r   r   r   n   r   SphericalBesselJync                  G   r   r   r   r   r   r   r   r   o   r   SphericalBesselYhyperc                  G   r   r   r   r   r   r   r   r   p   r   HypergeometricPFQmeijergc                  G   r   r   r   r   r   r   r   r   q   r   MeijerGappellf1c                  G   r   r   r   r   r   r   r   r   r   r   AppellF1
DiracDeltac                 C   r   r   r   r   r   r   r   r   s   r   	Heavisidec                 C   r   r   r   r   r   r   r   r   t   r   HeavisideThetac                  G   r   r   r   r   r   r   r   r   u   r   KroneckerDeltac                 C   r   r   r   r   r   r   r   r   v   r   Sqrt)r   sqrtc                       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
    _mcodezWolfram LanguageNauto   TF)order	full_prec	precisionuser_functionshuman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settings	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r   r   r   r   	<genexpr>   s    z*MCodePrinter._print_Mul.<locals>.<genexpr>)r   args_cncsuper
_print_Mulfuncjoin)r   r   cncres	__class__r   r   r     s   zMCodePrinter._print_Mulc                 C   s,   |  |j}|  |j}|j}d|||S )Nz{} {} {})_printlhsrhsrel_opformat)r   r   lhs_coderhs_codeopr   r   r   _print_Relational   s   zMCodePrinter._print_Relationalc                 C   r   )N0r   r   r   r   r   r   _print_Zero   r   zMCodePrinter._print_Zeroc                 C   r   )N1r   r  r   r   r   
_print_One   r   zMCodePrinter._print_Onec                 C   r   )Nz-1r   r  r   r   r   _print_NegativeOne   r   zMCodePrinter._print_NegativeOnec                 C   r   )Nz1/2r   r  r   r   r   _print_Half   r   zMCodePrinter._print_Halfc                 C   r   )NIr   r  r   r   r   _print_ImaginaryUnit   r   z!MCodePrinter._print_ImaginaryUnitc                 C   r   )NInfinityr   r  r   r   r   _print_Infinity   r   zMCodePrinter._print_Infinityc                 C   r   )Nz	-Infinityr   r  r   r   r   _print_NegativeInfinity   r   z$MCodePrinter._print_NegativeInfinityc                 C   r   )NComplexInfinityr   r  r   r   r   _print_ComplexInfinity   r   z#MCodePrinter._print_ComplexInfinityc                 C   r   )NIndeterminater   r  r   r   r   
_print_NaN   r   zMCodePrinter._print_NaNc                 C   r   )NEr   r  r   r   r   _print_Exp1   r   zMCodePrinter._print_Exp1c                 C   r   )NPir   r  r   r   r   	_print_Pi   r   zMCodePrinter._print_Pic                 C   r   )NGoldenRatior   r  r   r   r   _print_GoldenRatio   r   zMCodePrinter._print_GoldenRatioc                 C   s    |j dd}t|}| ||S )NT)r  )expandr   r   )r   r   expandedr   r   r   r   _print_TribonacciConstant   s   z&MCodePrinter._print_TribonacciConstantc                 C   r   )N
EulerGammar   r  r   r   r   _print_EulerGamma   r   zMCodePrinter._print_EulerGammac                 C   r   )NCatalanr   r  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   doprintr   r   r   r   r          z+MCodePrinter._print_list.<locals>.<genexpr>}r  r  r   r5  r   _print_list   s    zMCodePrinter._print_listc                 C      |  | S r   r4  tolistr  r   r   r   _print_ImmutableDenseMatrix      z(MCodePrinter._print_ImmutableDenseMatrixc                    s8   fdd fdd} fdd}d | | S )Nc                    s,   d  | d d | d d f |S )N{} -> {}r      r  r4  posvalr5  r   r   
print_rule   s   $z=MCodePrinter._print_ImmutableSparseMatrix.<locals>.print_rulec                     s4   t    td} ddfdd| D  d S )N)keyr0  r1  c                 3   s    | ]
\}} ||V  qd S r   r   )r   r   r   )rE  r   r   r      s    zPMCodePrinter._print_ImmutableSparseMatrix.<locals>.print_data.<locals>.<genexpr>r7  )sortedtodokr   r	   r  )r   )r   rE  r   r   
print_data   s   z=MCodePrinter._print_ImmutableSparseMatrix.<locals>.print_datac                           jS r   r4  shaper   r   r   r   r   
print_dims   s   z=MCodePrinter._print_ImmutableSparseMatrix.<locals>.print_dimsSparseArray[{}, {}]r  r   r   rI  rN  r   )r   rE  r   r   _print_ImmutableSparseMatrix   s   z)MCodePrinter._print_ImmutableSparseMatrixc                 C   r:  r   r;  r  r   r   r   _print_ImmutableDenseNDimArray   r>  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 )Nr0  r1  c                 s   s    | ]}|V  qd S r   r   r   r   r   r   r     s    zZMCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_string_list.<locals>.<genexpr>r7  r8  )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 )r@  Nr   r   ir   r   r   r   
  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 Mathematicar?  rA  rB  r5  r   r   rE    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   )_get_tuple_index)r   rF  value)r   rE  rZ  r   r   
<listcomp>  s    zTMCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_data.<locals>.<listcomp>)rG  _sparse_arrayr   r   )r   rE  rU  rZ  r   r   rI    s
   
z@MCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_datac                      rJ  )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
            rK  r   rM  r   r   rN  !  s   z@MCodePrinter._print_ImmutableSparseNDimArray.<locals>.print_dimsrO  rP  rQ  r   )r   rE  rU  r   rZ  r   _print_ImmutableSparseNDimArray   s   	
z,MCodePrinter._print_ImmutableSparseNDimArrayc                    s   |j j jv r( j|j j }|D ]\}}||j r&d| |jdf   S qn(|j j jv rP j|j j \}} |rPt fdd|D rP |	|S |j jd |jd  S )Nz%s[%s]r1  c                 3   r2  r   )
_can_print)r   fr5  r   r   r   6  r6  z/MCodePrinter._print_Function.<locals>.<genexpr>z[%s])
r  __name__r   rY  	stringify_rewriteable_functionsr`  allr
  rewrite)r   r   
cond_mfunccondmfunctarget_frequired_fsr   r5  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 )Nr@  zProductLog[{}]r   zProductLog[{}, {}])lenrY  r  r
  r  r   r   r   _print_LambertW<  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 )Nr@  r   zHold[Integrate[r1  c                 3   r2  r   r3  r   r5  r   r   r   G  r6  z/MCodePrinter._print_Integral.<locals>.<genexpr>]])rm  	variableslimitsrY  r  )r   r   rY  r   r5  r   _print_IntegralB  s     zMCodePrinter._print_Integralc                    s"   dd  fdd|jD  d S )Nz	Hold[Sum[r1  c                 3   r2  r   r3  r   r5  r   r   r   J  r6  z*MCodePrinter._print_Sum.<locals>.<genexpr>ro  )r  rY  r  r   r5  r   
_print_SumI  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 )r@  r   r   rV  r   r   r   r]  N  s   $ z2MCodePrinter._print_Derivative.<locals>.<listcomp>zHold[D[r1  c                 3   r2  r   r3  r   r5  r   r   r   O  r6  z1MCodePrinter._print_Derivative.<locals>.<genexpr>ro  )r   variable_countr  )r   r   dexprdvarsr   r5  r   _print_DerivativeL  s   &zMCodePrinter._print_Derivativec                 C   s
   d |S )Nz(* {} *)rP  )r   textr   r   r   _get_commentR  s   
zMCodePrinter._get_comment)-rb  
__module____qualname____doc__printmethodlanguage_default_settingsset_number_symbols_not_supportedr   r   r   r  r  r  r  r  r  r  r  r  r   r"  r$  r&  r(  r+  r-  r/  r9  _print_tuple_print_Tupler=  rR  rS  r_  rl  _print_MinMaxBasern  rr  rs  rw  ry  __classcell__r   r   r  r   r   z   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   r4  )r   r   r   r   r   mathematica_codeV  s   r  N)r|  typingr   r   tDictr   tSetr   tTuple
sympy.corer   r   r   sympy.core.sortingr	   sympy.printing.codeprinterr
   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WXYZ[\]^_`abcdefg
l ]