U
     ‹Ñh.a  ã                !   @   s.  d 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 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)h!Zd*d+d,d-œZd.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdBdCdDdEdFdGdHdIdJdKœZdLdMdLdNdOdOdPœZdQdR„ ZdSdT„ ZG dUdV„ dVeƒZG dWdX„ dXƒZG dYdZ„ dZeƒZejD ]Zeed[e eƒ �q6eD ]Zeed[e eƒ �qRd\d]„ Zd^ ¡ Zd_d`„ e ¡ D ƒZeefdadbdcdddedfdgdhdidjdkdldmœŽZdLdMdndodpdOdNdqdrœZdsdt„ Z G dudv„ dveƒZ!e!jD ]Zee!d[e eƒ �qæeD ]Zee!d[e eƒ �qG dwdx„ dxeƒZ"dyS )zzy
Python code printers

This module contains Python code printers for plain Python as well as NumPy & SciPy enabled code.
é    )Údefaultdict)Úchain)ÚS)ÚModé   )Ú
precedence)ÚCodePrinterÚandÚasÚassertÚbreakÚclassÚcontinueÚdefÚdelÚelifÚelseÚexceptÚfinallyÚforÚfromÚglobalÚifÚimportÚinÚisÚlambdaÚnotÚorÚpassÚraiseÚreturnÚtryÚwhileÚwithÚyieldÚNoneÚFalseÚnonlocalÚTrueÚabsÚminÚmax)ZAbsZMinZMaxÚacosÚacoshÚasinÚasinhÚatanÚatan2ÚatanhÚceilÚcosÚcoshÚerfÚerfcÚexpÚexpm1Ú	factorialÚfloorÚgammaÚhypotÚisnanÚlgammaÚlogÚlog10Úlog1pÚlog2ÚsinÚsinhÚsqrtÚtanÚtanh)r-   r.   r/   r0   r1   r2   r3   Zceilingr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   ÚloggammarA   ÚlnrB   rC   rD   rE   rF   ZSqrtrH   rI   ÚeÚpiÚinfÚnan)ÚExp1ÚPiÚEÚInfinityÚNaNZComplexInfinityc                    s8   ˆ j |jj }djˆ  |¡d ‡ fdd„|jD ƒ¡d�S )Nz{name}({args})ú, c                 3   s   | ]}ˆ   |¡V  qd S ©N©Ú_print©Ú.0Úarg©Úself© úC/tmp/user/2202/pip-unpacked-wheel-qe3242ng/sympy/printing/pycode.pyÚ	<genexpr>F   s     z$_print_known_func.<locals>.<genexpr>)ÚnameÚargs)Úknown_functionsÚ	__class__Ú__name__ÚformatÚ_module_formatÚjoinrb   ©r]   ÚexprZknownr^   r\   r_   Ú_print_known_funcC   s    ÿrk   c                 C   s   | j |jj }|  |¡S rV   )Úknown_constantsrd   re   rg   ri   r^   r^   r_   Ú_print_known_constI   s    rm   c                	       s¬  e Zd ZdZdZeZdZdZe	e
e ¡ dd„ e ¡ D ƒƒƒZdd„ e ¡ D ƒZd	d
ddœZe	eji dddddd�ZdU‡ fdd„	Zdd„ ZdVd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!‡ fd.d/„Z"d0d1„ Z#d2d3„ Z$d4d5„ Z%d6d7„ Z&d8d9„ Z'd:d;„  Z( Z) Z* Z+ Z, Z- Z.Z/d<d=„ Z0d>d?„ Z1d@dA„ Z2dBdC„ Z3dDdE„ Z4dFdG„ Z5dHdI„ Z6dJdK„ Z7dLdM„ Z8dNdO„ Z9dPdQ„ Z:dWdSdT„Z;‡  Z<S )XÚAbstractPythonCodePrinterZ_pythoncodeÚPythonNz    c                 C   s   g | ]\}}|d | f‘qS ©zmath.r^   ©rZ   ÚkÚvr^   r^   r_   Ú
<listcomp>V   s     z$AbstractPythonCodePrinter.<listcomp>c                 C   s   i | ]\}}|d | “qS rp   r^   rq   r^   r^   r_   Ú
<dictcomp>X   s      z$AbstractPythonCodePrinter.<dictcomp>r	   r   r   )r	   r   r   é   TFÚpython3)Úuser_functionsZ	precisionÚinlineÚfully_qualified_modulesZcontractÚstandardc                    s�   t ƒ  |¡ | jd }|d kr4dd l}d |jj¡}|dkrDtdƒ‚|| _t	t
ƒ| _t| jf|pbi  di ¡Ž| _t| jf|p~i  di ¡Ž| _d S )Nr{   r   zpython{}rw   zOnly Python 3 is supported.rx   Zuser_constants)ÚsuperÚ__init__Ú	_settingsÚsysrf   Úversion_infoÚmajorÚ
ValueErrorr{   r   ÚsetÚmodule_importsÚdictÚ_kfÚgetrc   Ú_kcrl   )r]   ÚsettingsZstdr   ©rd   r^   r_   r}   d   s"    

 ÿ ÿz"AbstractPythonCodePrinter.__init__c                 C   s   d||f S ©Nz%s = %sr^   )r]   ra   Úvaluer^   r^   r_   Ú_declare_number_constx   s    z/AbstractPythonCodePrinter._declare_number_constc                 C   sp   |  d¡}|r<t|ƒdkr<| jd |d d… ¡  |d ¡ | jd rJ|S |  d¡d   d¡d   d¡d S d S )NÚ.r   éÿÿÿÿrz   ú(r   ú[)ÚsplitÚlenr„   rh   Úaddr~   )r]   ZfqnÚregisterÚpartsr^   r^   r_   rg   {   s    
"
z(AbstractPythonCodePrinter._module_formatc                 C   s   |S rV   r^   )r]   Úlinesr^   r^   r_   Ú_format_code…   s    z&AbstractPythonCodePrinter._format_codec                 C   s
   d  |¡S )Nz{}©rf   ©r]   Z
codestringr^   r^   r_   Ú_get_statementˆ   s    z(AbstractPythonCodePrinter._get_statementc                 C   s
   d  |¡S )Nz  # {}r™   )r]   Útextr^   r^   r_   Ú_get_comment‹   s    z&AbstractPythonCodePrinter._get_commentc                 C   sL   t |ƒdkr|  |d ¡S d|  |¡|  ||dd… ¡|  |d ¡f S dS )zø
        This method expands a fold on binary operations.

        ``functools.reduce`` is an example of a folded operation.

        For example, the expression

        `A + B + C + D`

        is folded into

        `((A + B) + C) + D`
        r   r   ú
%s(%s, %s)Nr�   )r“   rX   rg   Ú_expand_fold_binary_op)r]   Úoprb   r^   r^   r_   rŸ   Ž   s    ýz0AbstractPythonCodePrinter._expand_fold_binary_opc                 C   s^   t |ƒdkr|  |d ¡S t |ƒ}|d }d|  |¡|  |d|… ¡|  ||d… ¡f S dS )zú
        This method expands a reductin on binary operations.

        Notice: this is NOT the same as ``functools.reduce``.

        For example, the expression

        `A + B + C + D`

        is reduced into:

        `(A + B) + (C + D)`
        r   r   é   rž   N)r“   rX   rg   Ú_expand_reduce_binary_op)r]   r    rb   ÚNZNhalfr^   r^   r_   r¢   ¥   s    ýz2AbstractPythonCodePrinter._expand_reduce_binary_opc                 C   s   dS )Nzfloat('nan')r^   ©r]   rj   r^   r^   r_   Ú
_print_NaN¾   s    z$AbstractPythonCodePrinter._print_NaNc                 C   s   dS )Nzfloat('inf')r^   r¤   r^   r^   r_   Ú_print_InfinityÁ   s    z)AbstractPythonCodePrinter._print_Infinityc                 C   s   dS )Nzfloat('-inf')r^   r¤   r^   r^   r_   Ú_print_NegativeInfinityÄ   s    z1AbstractPythonCodePrinter._print_NegativeInfinityc                 C   s
   |   |¡S rV   )r¥   r¤   r^   r^   r_   Ú_print_ComplexInfinityÇ   s    z0AbstractPythonCodePrinter._print_ComplexInfinityc                    s$   t |ƒ‰ dj‡ ‡fdd„|jD ƒŽ S )Nz{} % {}c                 3   s   | ]}ˆ  |ˆ ¡V  qd S rV   )Úparenthesize)rZ   Úx©ÚPRECr]   r^   r_   r`   Ì   s     z7AbstractPythonCodePrinter._print_Mod.<locals>.<genexpr>)r   rf   rb   r¤   r^   r«   r_   Ú
_print_ModÊ   s    z$AbstractPythonCodePrinter._print_Modc                 C   sÆ   g }d}|j D ]r}|j}|j}|dkr0| d¡ | d¡ | |  |¡¡ | d¡ | d¡ | |  |¡¡ | d¡ |d7 }q|d d… }|d dkr²|d d	… }| d¡ n
| d
¡ d |¡S )Nr   r�   ú)z if z else r   r�   r)   éþÿÿÿz else None)Ú )rb   rj   ÚcondÚappendrX   rh   )r]   rj   ÚresultÚir[   rL   Úcr^   r^   r_   Ú_print_PiecewiseÎ   s(    







z*AbstractPythonCodePrinter._print_Piecewisec                    sR   dddddddœ}|j |krF|  |j¡}|  |j¡}dj|j ||d	�S tƒ  |¡S )
z.Relational printer for Equality and UnequalityÚequalÚ	not_equalZlessZ
less_equalZgreaterZgreater_equal)z==z!=ú<z<=ú>z>=z({lhs} {op} {rhs}))r    ÚlhsÚrhs)Zrel_oprX   r»   r¼   rf   r|   Ú_print_Relational)r]   rj   r    r»   r¼   rŠ   r^   r_   r½   å   s    ú
z+AbstractPythonCodePrinter._print_Relationalc                 C   s   ddl m} |  | |¡¡S )Nr   )Ú	Piecewise)Z$sympy.functions.elementary.piecewiser¾   rX   Zrewrite)r]   rj   r¾   r^   r^   r_   Ú
_print_ITEõ   s    z$AbstractPythonCodePrinter._print_ITEc                    s0   ‡ fdd„|j D ƒ}djˆ  |j¡d |¡d�S )Nc                 3   s6   | ].\}}}d j ˆ  |¡ˆ  |¡ˆ  |¡d�V  qdS )zfor {i} in range({a}, {b}+1))r´   ÚaÚbN)rf   rX   )rZ   r´   rÀ   rÁ   r\   r^   r_   r`   ú   s   üýz7AbstractPythonCodePrinter._print_Sum.<locals>.<genexpr>z"(builtins.sum({function} {loops}))ú )ÚfunctionÚloops)Úlimitsrf   rX   rÃ   rh   )r]   rj   rÄ   r^   r\   r_   Ú
_print_Sumù   s    
û
þz$AbstractPythonCodePrinter._print_Sumc                 C   s   dS )NZ1jr^   r¤   r^   r^   r_   Ú_print_ImaginaryUnit  s    z.AbstractPythonCodePrinter._print_ImaginaryUnitc                 C   s$   |j \}}dj|  |¡|  |¡d�S )Nz(1 if {a} == {b} else 0))rÀ   rÁ   )rb   rf   rX   )r]   rj   rÀ   rÁ   r^   r^   r_   Ú_print_KroneckerDelta  s
    
þz/AbstractPythonCodePrinter._print_KroneckerDeltac                 C   s,   |j j}| j ||¡}d||  | ¡ ¡f S )Nú%s(%s))rd   re   rc   r‡   rX   Útolist)r]   rj   ra   Úfuncr^   r^   r_   Ú_print_MatrixBase  s    z+AbstractPythonCodePrinter._print_MatrixBasec                 C   s
   |   |¡S rV   )rÌ   r¤   r^   r^   r_   Ú<lambda>  ó    z"AbstractPythonCodePrinter.<lambda>c                    s   d  ‡ fdd„| d¡D ƒ¡S )NÚ
c                    s   g | ]}ˆ j | ‘qS r^   )Útab)rZ   Úliner\   r^   r_   rt     s     z@AbstractPythonCodePrinter._indent_codestring.<locals>.<listcomp>)rh   r’   rš   r^   r\   r_   Ú_indent_codestring  s    z,AbstractPythonCodePrinter._indent_codestringc                    sN   d  ‡ fdd„|jD ƒ¡}djˆ  |j¡d  ‡ fdd„|jD ƒ¡ˆ  |¡d�S )	NrÏ   c                 3   s   | ]}ˆ   |¡V  qd S rV   rW   rY   r\   r^   r_   r`   "  s     zFAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<genexpr>z def {name}({parameters}):
{body}rU   c                    s   g | ]}ˆ   |j¡‘qS r^   )rX   Úsymbol)rZ   Úvarr\   r^   r_   rt   %  s     zGAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<listcomp>)ra   Ú
parametersÚbody)rh   rÖ   rf   rX   ra   rÕ   rÒ   )r]   ÚfdrÖ   r^   r\   r_   Ú_print_FunctionDefinition!  s    
ýz3AbstractPythonCodePrinter._print_FunctionDefinitionc                    s6   d  ‡ fdd„|jD ƒ¡}djˆ  |j¡ˆ  |¡d�S )NrÏ   c                 3   s   | ]}ˆ   |¡V  qd S rV   rW   rY   r\   r^   r_   r`   *  s     z9AbstractPythonCodePrinter._print_While.<locals>.<genexpr>zwhile {cond}:
{body})r±   rÖ   )rh   rÖ   rf   rX   Ú	conditionrÒ   )r]   ZwhlrÖ   r^   r\   r_   Ú_print_While)  s
    
þz&AbstractPythonCodePrinter._print_Whilec                 C   s    d|   |jj¡|   |jj¡f S r‹   )rX   ÚvariablerÓ   rŒ   )r]   Údeclr^   r^   r_   Ú_print_Declaration0  s    þz,AbstractPythonCodePrinter._print_Declarationc                 C   s   dS )Nr   r^   )r]   Zbtr^   r^   r_   Ú_print_BreakToken6  s    z+AbstractPythonCodePrinter._print_BreakTokenc                 C   s   |j \}d|  |¡ S )Nz	return %s©rb   rX   )r]   Úretr[   r^   r^   r_   Ú_print_Return9  s    z'AbstractPythonCodePrinter._print_Returnc                 C   s   |j \}d|  |¡ S )Nzraise %srß   )r]   Úrsr[   r^   r^   r_   Ú_print_Raise=  s    z&AbstractPythonCodePrinter._print_Raisec                 C   s   |j \}d|  |¡ S )NzRuntimeError(%s)rß   )r]   ÚreÚmessager^   r^   r_   Ú_print_RuntimeError_A  s    z.AbstractPythonCodePrinter._print_RuntimeError_c                    sj   d  ‡ fdd„|jD ƒ¡}ddlm} |j|krDd ˆ  |j¡|¡}|jd krb|dˆ  |j¡ 7 }d| S )	NrU   c                 3   s   | ]}ˆ   |¡V  qd S rV   rW   rY   r\   r^   r_   r`   F  s     z9AbstractPythonCodePrinter._print_Print.<locals>.<genexpr>r   )Únonez{} % ({}), end=""z	, file=%sz	print(%s))rh   Ú
print_argsZsympy.codegen.astrç   Úformat_stringrf   rX   Úfile)r]   Zprntrè   rç   r^   r\   r_   Ú_print_PrintE  s    

þ
z&AbstractPythonCodePrinter._print_Printc                 C   s@   t |jƒdkr|  d¡S t |jƒdkr0|  d¡S |  |j¡S d S )NÚstdoutz
sys.stdoutÚstderrz
sys.stderr)Ústrra   rg   rX   )r]   Ústrmr^   r^   r_   Ú_print_StreamQ  s
    

z'AbstractPythonCodePrinter._print_Streamc                 C   s   dS )Nr&   r^   )r]   r[   r^   r^   r_   Ú_print_NoneTokenY  s    z*AbstractPythonCodePrinter._print_NoneTokenú	math.sqrtc           
      C   sô   t |ƒ}|jtjkr<|s<|  |¡}|  |j¡}dj||d�S |jrÄ|sÄ|j tjkrŒ|  |¡}|  tj	¡}|  |j¡}|› d|› d|› d�S |jtj
krÄ|  tj	¡}| j|j|dd�}|› d|› �S | j|j|dd�}| j|j|dd�}	d ||	¡S )	a   Printing helper function for ``Pow``

        Notes
        =====

        This preprocesses the ``sqrt`` as math formatter and prints division

        Examples
        ========

        >>> from sympy import sqrt
        >>> from sympy.printing.pycode import PythonCodePrinter
        >>> from sympy.abc import x

        Python code printer automatically looks up ``math.sqrt``.

        >>> printer = PythonCodePrinter()
        >>> printer._hprint_Pow(sqrt(x), rational=True)
        'x**(1/2)'
        >>> printer._hprint_Pow(sqrt(x), rational=False)
        'math.sqrt(x)'
        >>> printer._hprint_Pow(1/sqrt(x), rational=True)
        'x**(-1/2)'
        >>> printer._hprint_Pow(1/sqrt(x), rational=False)
        '1/math.sqrt(x)'
        >>> printer._hprint_Pow(1/x, rational=False)
        '1/x'
        >>> printer._hprint_Pow(1/x, rational=True)
        'x**(-1)'

        Using sqrt from numpy or mpmath

        >>> printer._hprint_Pow(sqrt(x), sqrt='numpy.sqrt')
        'numpy.sqrt(x)'
        >>> printer._hprint_Pow(sqrt(x), sqrt='mpmath.sqrt')
        'mpmath.sqrt(x)'

        See Also
        ========

        sympy.printing.str.StrPrinter._print_Pow
        z{func}({arg}))rË   r[   ú/r�   r®   F©Ústrictz{}**{})r   r9   r   ZHalfrg   rX   Úbaserf   Zis_commutativeZOneZNegativeOner©   )
r]   rj   ÚrationalrG   r¬   rË   r[   ÚnumÚbase_strZexp_strr^   r^   r_   Ú_hprint_Pow\  s$    +


z%AbstractPythonCodePrinter._hprint_Pow)N)T)Frò   )=re   Ú
__module__Ú__qualname__ÚprintmethodÚlanguageÚ_kwÚreserved_wordsÚmodulesrÐ   r…   r   Ú_known_functionsÚitemsÚ_known_functions_mathr†   Ú_known_constants_mathrˆ   Ú
_operatorsr   Ú_default_settingsr}   r�   rg   r˜   r›   r�   rŸ   r¢   r¥   r¦   r§   r¨   r­   r¶   r½   r¿   rÆ   rÇ   rÈ   rÌ   Z_print_SparseRepMatrixZ_print_MutableSparseMatrixZ_print_ImmutableSparseMatrixZ_print_MatrixZ_print_DenseMatrixZ_print_MutableDenseMatrixZ_print_ImmutableMatrixZ_print_ImmutableDenseMatrixrÒ   rØ   rÚ   rÝ   rÞ   rá   rã   ræ   rë   rð   rñ   rú   Ú__classcell__r^   r^   rŠ   r_   rn   N   s„   þù


øÿþýüûrn   c                   @   sl   e Z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S )ÚArrayPrinterc                 C   s4   ddl m} z
||ƒW S  tk
r.   | Y S X d S )Nr   )Úconvert_indexed_to_array)Z4sympy.tensor.array.expressions.from_indexed_to_arrayr
  Ú	Exception)r]   Zindexedr
  r^   r^   r_   Ú	_arrayify¡  s
    
zArrayPrinter._arrayifyc                 C   s   |   ¡ }d}d}dd„ |D ƒ}g }|D ]J}g }	t|ƒD ].}
||krR|	 || ¡ n
|	 |¡ |d7 }q6| |	¡ q&i }g }g }|D ]f}
|
D ]T}||kr¨t|ƒ}|||< n|| }||7 }||krÔ||krÞ| |¡ qŠ| |¡ qŠ|d7 }q‚|d d… }|||fS )Nr°   r   c                 S   s    i | ]}|D ]}|t |ƒ“qqS r^   )r+   )rZ   r´   Újr^   r^   r_   ru   ¬  s
        z3ArrayPrinter._get_einsum_string.<locals>.<dictcomp>r   ú,r�   )Ú _get_letter_generator_for_einsumÚranger²   Únext)r]   ÚsubranksÚcontraction_indicesÚlettersÚcontraction_stringÚcounterÚdÚindicesZrank_argZlindicesr´   ÚmappingÚletters_freeÚletters_dumr  Úlr^   r^   r_   Ú_get_einsum_string¨  s<    



zArrayPrinter._get_einsum_stringc                 c   s@   t ddƒD ]}t|ƒV  q
t ddƒD ]}t|ƒV  q$tdƒ‚d S )Néa   é{   éA   é[   zout of letters)r  Úchrr‚   )r]   r´   r^   r^   r_   r  Ë  s
    z-ArrayPrinter._get_letter_generator_for_einsumc                    sX   ˆ  ¡ ‰ d ‡ fdd„|jD ƒ¡}dˆ ˆjd ˆj ¡|d ‡fdd„|jD ƒ¡f S )Nr  c                    s(   g | ] }d   ‡ fdd„t|ƒD ƒ¡‘qS )r°   c                    s   g | ]}t ˆ ƒ‘qS r^   )r  )rZ   r  ©r  r^   r_   rt   Ô  s     zEArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>.<listcomp>)rh   r  ©rZ   r´   r#  r^   r_   rt   Ô  s     z:ArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>ú%s("%s", %s)rŽ   rU   c                    s   g | ]}ˆ   |¡‘qS r^   rW   rY   r\   r^   r_   rt   Ø  s     )r  rh   r  rg   Ú_moduleÚ_einsumrb   )r]   rj   r  r^   )r  r]   r_   Ú_print_ArrayTensorProductÒ  s    ýz&ArrayPrinter._print_ArrayTensorProductc           
   	      sÜ   ddl m} |j}|j}t||ƒrDd ‡ fdd„|jD ƒ¡}|j}nˆ  |¡}t	|j
ƒg}ˆ  ||¡\}}}	|szˆ  |¡S t||ƒr d ‡ fdd„|jD ƒ¡}n
ˆ  |¡}dˆ  ˆ jd ˆ j ¡d	 |d
 t|ƒ¡¡|f S )Nr   ©ÚArrayTensorProductr  c                    s   g | ]}d ˆ   |¡ ‘qS ©z%srW   rY   r\   r^   r_   rt   á  s     z8ArrayPrinter._print_ArrayContraction.<locals>.<listcomp>c                    s   g | ]}d ˆ   |¡ ‘qS r+  rW   rY   r\   r^   r_   rt   ì  s     r%  rŽ   ú{}->{}r°   )Ú0sympy.tensor.array.expressions.array_expressionsr*  rj   r  Ú
isinstancerh   rb   r  rX   r“   Úshaper  rg   r&  r'  rf   Úsorted)
r]   rj   r*  rö   r  ÚelemsZranksr  r  r  r^   r\   r_   Ú_print_ArrayContractionÛ  s&    




ýz$ArrayPrinter._print_ArrayContractionc           	   	      sž   ddl m} t|jƒ}t|j|ƒr4|jj}|jj}n|j}|jg}ˆ  ||¡\}}}‡ fdd„|D ƒ}dˆ  	ˆ j
d ˆ j ¡d |d || ¡¡d	 |¡f S )
Nr   r)  c                    s   g | ]}ˆ   |¡‘qS r^   rW   r$  r\   r^   r_   rt   ÿ  s     z5ArrayPrinter._print_ArrayDiagonal.<locals>.<listcomp>r%  rŽ   r,  r°   rU   )r-  r*  ÚlistÚdiagonal_indicesr.  rj   r  rb   r  rg   r&  r'  rf   rh   )	r]   rj   r*  r4  r  r1  Zdiagonal_stringr  r  r^   r\   r_   Ú_print_ArrayDiagonalõ  s    

ýz!ArrayPrinter._print_ArrayDiagonalc                 C   s2   d|   | jd | j ¡|  |j¡|  |jj¡f S )Nrž   rŽ   )rg   r&  Z
_transposerX   rj   ZpermutationZ
array_formr¤   r^   r^   r_   Ú_print_PermuteDims  s
    
ýzArrayPrinter._print_PermuteDimsc                 C   s   |   | jd | j |j¡S )NrŽ   )rŸ   r&  Z_addrb   r¤   r^   r^   r_   Ú_print_ArrayAdd  s    zArrayPrinter._print_ArrayAddc                 C   s.   d|   | jd | j ¡d t| j|jƒ¡f S ©Nz	%s((%s,))rŽ   r  )rg   r&  Z_onesrh   ÚmaprX   rb   r¤   r^   r^   r_   Ú_print_OneArray  s    þzArrayPrinter._print_OneArrayc                 C   s.   d|   | jd | j ¡d t| j|jƒ¡f S r8  )rg   r&  Z_zerosrh   r9  rX   rb   r¤   r^   r^   r_   Ú_print_ZeroArray  s    þzArrayPrinter._print_ZeroArrayc                 C   s0   |   |  |j¡¡}|   |  |j¡¡}d||f S r‹   )rX   r  r»   r¼   )r]   rj   r»   r¼   r^   r^   r_   Ú_print_Assignment  s    zArrayPrinter._print_Assignmentc                 C   s
   |   |¡S rV   )Z_print_ArraySymbolr¤   r^   r^   r_   Ú_print_IndexedBase#  s    zArrayPrinter._print_IndexedBaseN)re   rû   rü   r  r  r  r(  r2  r5  r6  r7  r:  r;  r<  r=  r^   r^   r^   r_   r	  Ÿ  s   #	r	  c                       sv   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zdd
d„Zdd„ Zdd„ Z	dd„ Z
‡ fdd„ZejZejZejZejZ‡  ZS )ÚPythonCodePrinterc                 C   s    dj |  d¡|  |jd ¡d�S )Nz"(0.0 if {e} == 0 else {f}(1, {e}))zmath.copysignr   )ÚfrL   ©rf   rg   rX   rb   ©r]   rL   r^   r^   r_   Ú_print_sign)  s     ÿzPythonCodePrinter._print_signc                 C   s$   t |ƒ}| jd |  |jd |¡ S )Nr   r   )r   r  r©   rb   )r]   rj   r¬   r^   r^   r_   Ú
_print_Not-  s    zPythonCodePrinter._print_Notc                 C   s   |j S rV   )ra   r¤   r^   r^   r_   r=  1  s    z$PythonCodePrinter._print_IndexedBasec                    s<   |j d }|j dd … }d t|ƒd ‡ fdd„|D ƒ¡¡S )Nr   r   z{}[{}]rU   c                    s   g | ]}ˆ   |¡‘qS r^   rW   )rZ   Úindr\   r^   r_   rt   7  s     z4PythonCodePrinter._print_Indexed.<locals>.<listcomp>)rb   rf   rî   rh   )r]   rj   rö   Úindexr^   r\   r_   Ú_print_Indexed4  s    
z PythonCodePrinter._print_IndexedFc                 C   s   | j ||d�S )N)r÷   ©rú   ©r]   rj   r÷   r^   r^   r_   Ú
_print_Pow9  s    zPythonCodePrinter._print_Powc                 C   s   d  |j|j¡S )Nz{}/{})rf   ÚpÚqr¤   r^   r^   r_   Ú_print_Rational<  s    z!PythonCodePrinter._print_Rationalc                 C   s
   |   |¡S rV   ©rL  r¤   r^   r^   r_   Ú_print_Half?  s    zPythonCodePrinter._print_Halfc                 C   s   |   t|jd dƒ¡S )Nr   r   )r­   r   rb   r¤   r^   r^   r_   Ú_print_fracB  s    zPythonCodePrinter._print_fracc                    sd   t ƒ  |¡}|| jkr@| jd r2d}t| |¡ƒ‚|| jd  S d|kr\| dd¡ dd¡S |S d S )NZerror_on_reservedzVThis expression includes the symbol "{}" which is a reserved keyword in this language.Zreserved_word_suffixÚ{r°   Ú})r|   Ú_print_Symbolr   r~   r‚   rf   Úreplace)r]   rj   ra   ÚmsgrŠ   r^   r_   rR  E  s    

zPythonCodePrinter._print_Symbol)F)re   rû   rü   rB  rC  r=  rF  rI  rL  rN  rO  rR  r   Z_print_not_supportedÚ_print_lowergammaÚ_print_uppergammaZ_print_fresnelcZ_print_fresnelsr  r^   r^   rŠ   r_   r>  '  s   
r>  z	_print_%sc                 K   s   t |ƒ | ¡S )a)   Converts an expr to a string of Python code

    Parameters
    ==========

    expr : Expr
        A SymPy expression.
    fully_qualified_modules : bool
        Whether or not to write out full module names of functions
        (``math.sin`` vs. ``sin``). default: ``True``.
    standard : str or None, optional
        Only 'python3' (default) is supported.
        This parameter may be removed in the future.

    Examples
    ========

    >>> from sympy import pycode, tan, Symbol
    >>> pycode(tan(Symbol('x')) + 1)
    'math.tan(x) + 1'

    )r>  Zdoprint)rj   r‰   r^   r^   r_   Úpycodea  s    rW  z
log1p log2c                 C   s    g | ]\}}|t kr||f‘qS r^   )Ú_not_in_mpmathrq   r^   r^   r_   rt   |  s      rt   ÚbetaÚfracÚfresnelcÚfresnelsÚsignrJ   ÚhyperÚmeijergÚbesseljÚbesselyÚbesseliÚbesselk)rY  rZ  r[  r\  r]  rJ   r^  r_  r`  ra  rb  rc  ÚphiZeulerÚcatalanZninf)rP   rQ   ZGoldenRatioZ
EulerGammaZCatalanrT   rS   ÚNegativeInfinityc                 C   sT   g }g }| j D ]<}t|ƒdkr*|\}}}ntdƒ‚| |¡ | ||f¡ q||fS )zì helper function for _print_Integral that
        - accepts an Integral expression
        - returns a tuple of
           - a list variables of integration
           - a list of tuples of the upper and lower limits of integration
    é   z%Only definite integrals are supported)rÅ   r“   ÚNotImplementedErrorr²   )Zintegral_exprÚintegration_varsrÅ   Zintegration_rangeZintegration_varZlower_limitZupper_limitr^   r^   r_   Ú_unpack_integral_limits—  s    

rj  c                   @   s”   e Zd ZdZdZdZeee 	¡ dd„ e
 	¡ D ƒƒƒZdd„ e 	¡ 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d„Zdd„ ZdS )ÚMpmathPrinterzH
    Lambda printer for mpmath which maintains precision for floats
    Z_mpmathcodezPython with mpmathc                 C   s   g | ]\}}|d | f‘qS ©zmpmath.r^   rq   r^   r^   r_   rt   ´  s     zMpmathPrinter.<listcomp>c                 C   s   i | ]\}}|d | “qS rl  r^   rq   r^   r^   r_   ru   ¶  s      zMpmathPrinter.<dictcomp>c                 C   s(   t ttt|jƒƒƒ}dj|  d¡|d�S )Nz{func}({args})ú
mpmath.mpf)rË   rb   )rî   Útupler9  ÚintZ_mpf_rf   rg   )r]   rL   rb   r^   r^   r_   Ú_print_Float¸  s    zMpmathPrinter._print_Floatc                 C   s&   dj |  d¡|  |j¡|  |j¡d�S )Nz{func}({p})/{func}({q})rm  )rË   rK  rJ  )rf   rg   rX   rK  rJ  rA  r^   r^   r_   rL  Â  s
    

ýzMpmathPrinter._print_Rationalc                 C   s
   |   |¡S rV   rM  rA  r^   r^   r_   rN  É  s    zMpmathPrinter._print_Halfc                 C   s4   d  |  d¡|  |jd ¡|  |jd ¡|  d¡¡S )Nz{}({}, {}, {})úmpmath.gammaincr   r   z
mpmath.infr@  rA  r^   r^   r_   rV  Ì  s    üzMpmathPrinter._print_uppergammac                 C   s,   d  |  d¡|  |jd ¡|  |jd ¡¡S )Nz{}({}, 0, {})rq  r   r   r@  rA  r^   r^   r_   rU  Ó  s
    ýzMpmathPrinter._print_lowergammac                 C   s   d  |  d¡|  |jd ¡¡S )Nz{0}({1})/{0}(2)z
mpmath.logr   r@  rA  r^   r^   r_   Ú_print_log2Ù  s     ÿzMpmathPrinter._print_log2c                 C   s   d  |  d¡|  |jd ¡¡S )Nz{}({})zmpmath.log1pr   r@  rA  r^   r^   r_   Ú_print_log1pÝ  s     ÿzMpmathPrinter._print_log1pFc                 C   s   | j ||dd�S )Nzmpmath.sqrt©r÷   rG   rG  rH  r^   r^   r_   rI  á  s    zMpmathPrinter._print_Powc              
      sP   t |ƒ\}}d ˆ  d¡d tˆ j|ƒ¡ˆ  |jd ¡d ‡ fdd„|D ƒ¡¡S )Nz{}(lambda {}: {}, {})zmpmath.quadrU   r   c                 3   s"   | ]}d t tˆ j|ƒƒ V  qdS )z(%s, %s)N)rn  r9  rX   )rZ   r  r\   r^   r_   r`   ë  s     z0MpmathPrinter._print_Integral.<locals>.<genexpr>)rj  rf   rg   rh   r9  rX   rb   )r]   rL   ri  rÅ   r^   r\   r_   Ú_print_Integralä  s    üzMpmathPrinter._print_IntegralN)F)re   rû   rü   Ú__doc__rý   rþ   r…   r   r  r  Ú_known_functions_mpmathr†   Ú_known_constants_mpmathrˆ   rp  rL  rN  rV  rU  rr  rs  rI  ru  r^   r^   r^   r_   rk  ª  s"   þ

rk  c                   @   s0   e Zd ZdZeejdd�Zdd„ Zd	dd„ZdS )
ÚSymPyPrinterzPython with SymPyFrô   c                    sH   |j jp
d}dˆ  ||rdnd |j j ¡d ‡ fdd„|jD ƒ¡f S )Nr°   rÉ   rŽ   rU   c                 3   s   | ]}ˆ   |¡V  qd S rV   rW   rY   r\   r^   r_   r`     s     z/SymPyPrinter._print_Function.<locals>.<genexpr>)rË   rû   rg   re   rh   rb   )r]   rj   Úmodr^   r\   r_   Ú_print_Functionþ  s    ÿzSymPyPrinter._print_Functionc                 C   s   | j ||dd�S )Nz
sympy.sqrtrt  rG  rH  r^   r^   r_   rI    s    zSymPyPrinter._print_PowN)F)	re   rû   rü   rþ   r…   rn   r  r{  rI  r^   r^   r^   r_   ry  õ  s   þry  N)#rv  Úcollectionsr   Ú	itertoolsr   Z
sympy.corer   Zsympy.core.modr   r   Zcodeprinterr   rÿ   r  r  r  rk   rm   rn   r	  r>  r†   rr   ÚsetattrrW  r’   rX  r  Z
_in_mpmathr…   rw  rx  rj  rk  ry  r^   r^   r^   r_   Ú<module>   sþ                                üýâ"ú	  S 	3
ôøD
