U
    ZÊ¹j.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© úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/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_functionsÚ	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   éÿÿÿÿr{   ú(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_equalÚ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[   ÚnumZ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+  r5  r  r2  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'  Ú_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 r:  )rg   r'  Z_zerosrh   r;  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)  r3  r6  r7  r9  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   ©rN  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_   rT  E  s    

zPythonCodePrinter._print_Symbol)F)re   rü   rý   rD  rE  r?  rH  rK  rN  rP  rQ  rT  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    rY  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)r[  r\  r]  r^  r_  rJ   r`  ra  rb  rc  rd  re  Ú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    

rl  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 rn  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ð   Útupler;  Ú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})ro  )rÍ   rM  rL  )rf   rg   rX   rM  rL  rC  r^   r^   r_   rN  Â  s
    

ýzMpmathPrinter._print_Rationalc                 C   s
   |   |¡S rV   rO  rC  r^   r^   r_   rP  É  s    zMpmathPrinter._print_Halfc                 C   s4   d  |  d¡|  |jd ¡|  |jd ¡|  d¡¡S )Nz{}({}, {}, {})úmpmath.gammaincr   r   z
mpmath.infrB  rC  r^   r^   r_   rX  Ì  s    üzMpmathPrinter._print_uppergammac                 C   s,   d  |  d¡|  |jd ¡|  |jd ¡¡S )Nz{}({}, 0, {})rs  r   r   rB  rC  r^   r^   r_   rW  Ó  s
    ýzMpmathPrinter._print_lowergammac                 C   s   d  |  d¡|  |jd ¡¡S )Nz{0}({1})/{0}(2)z
mpmath.logr   rB  rC  r^   r^   r_   Ú_print_log2Ù  s     ÿzMpmathPrinter._print_log2c                 C   s   d  |  d¡|  |jd ¡¡S )Nz{}({})zmpmath.log1pr   rB  rC  r^   r^   r_   Ú_print_log1pÝ  s     ÿzMpmathPrinter._print_log1pFc                 C   s   | j ||dd�S )Nzmpmath.sqrt©rù   rG   rI  rJ  r^   r^   r_   rK  á  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)rp  r;  rX   )rZ   r  r\   r^   r_   r`   ë  s     z0MpmathPrinter._print_Integral.<locals>.<genexpr>)rl  rf   rg   rh   r;  rX   rb   )r]   rL   rk  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‰   rr  rN  rP  rX  rW  rt  ru  rK  rw  r^   r^   r^   r_   rm  ª  s"   þ

rm  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.sqrtrv  rI  rJ  r^   r^   r_   rK    s    zSymPyPrinter._print_PowN)F)	re   rü   rý   rÿ   r†   rn   r  r}  rK  r^   r^   r^   r_   r{  õ  s   þr{  N)#rx  Úcollectionsr   Ú	itertoolsr   Z
sympy.corer   Zsympy.core.modr   r   Zcodeprinterr   r   r  r  r  rk   rm   rn   r
  r@  r‡   rr   ÚsetattrrY  r“   rZ  r  Z
_in_mpmathr†   ry  rz  rl  rm  r{  r^   r^   r^   r_   Ú<module>   sþ                                üýâ"ú	  S 	3
ôøD
