o
    §ƒðj¨i  ã                   @   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 h d	£Zd
dddœZi dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“d d!d"d#d$d$d%d&d'd(d)d*d+d,d-œ¥Zd.d/d.d0d1d1d2œZd3d4„ Zd5d6„ ZG d7d8„ d8eƒZG d9d:„ d:ƒZG d;d<„ d<eƒZejD ]
Zeed=e eƒ q¡eD ]
Zeed=e eƒ q®d>d?„ 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dAœZd/d.d0dBdCœZG dDdE„ dEeƒZejD ]Zeed=e ejƒ qíeD ]Zeed=e ejƒ qûdF ¡ ZdGdH„ e ¡ D ƒZ e!e fi dIdJdKdLdMdNdOdPdQdRdSdTdUœ¤ŽZ"d.d/dVdWdXd1d0dYdZœZ#d[d\„ Z$G d]d^„ d^eƒZ%e%jD ]Zee%d=e eƒ �qCe#D ]Zee%d=e eƒ �qQG d_d`„ d`eƒZ&daS )bzy
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>!   ÚasÚifÚinÚisÚorÚandÚdefÚdelÚforÚnotÚtryÚNoneÚTrueÚelifÚelseÚfromÚpassÚwithÚFalseÚbreakÚclassÚraiseÚwhileÚyieldÚassertÚexceptÚglobalÚimportÚlambdaÚreturnÚfinallyÚcontinueÚnonlocalÚabsÚminÚmax)ÚAbsÚMinÚMaxÚacosÚacoshÚasinÚasinhÚatanÚatan2ÚatanhÚceilingÚceilÚcosÚcoshÚerfÚerfcÚexpÚexpm1Ú	factorialÚfloorÚgammaÚhypotÚisinfÚisnanÚlgammaÚlogÚlog10Úlog1pÚlog2ÚsinÚsinhÚsqrtÚtanÚtanh)rB   rC   rD   ÚloggammarF   ÚlnrG   rH   rI   rJ   rK   ÚSqrtrM   rN   ÚeÚpiÚinfÚnan)ÚExp1ÚPiÚEÚInfinityÚNaNÚComplexInfinityc                    s8   ˆ j |jj }djˆ  |¡d ‡ fdd„|jD ƒ¡d�S )Nz{name}({args})ú, c                 3   ó   � | ]}ˆ   |¡V  qd S ©N©Ú_print©Ú.0Úarg©Úself© ú—/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/printing/pycode.pyÚ	<genexpr>G   ó   € z$_print_known_func.<locals>.<genexpr>)ÚnameÚargs)Úknown_functionsÚ	__class__Ú__name__ÚformatÚ_module_formatÚjoinrk   ©re   ÚexprÚknownrf   rd   rg   Ú_print_known_funcD   s   ÿru   c                 C   s   | j |jj }|  |¡S r^   )Úknown_constantsrm   rn   rp   rr   rf   rf   rg   Ú_print_known_constJ   s   
rw   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ÚAbstractPythonCodePrinterÚ_pythoncodeÚPythonNz    c                 C   ó   g | ]
\}}|d | f‘qS ©zmath.rf   ©rb   ÚkÚvrf   rf   rg   Ú
<listcomp>W   ó    z$AbstractPythonCodePrinter.<listcomp>c                 C   ó   i | ]	\}}|d | “qS r|   rf   r}   rf   rf   rg   Ú
<dictcomp>Y   ó    z$AbstractPythonCodePrinter.<dictcomp>r   r   r   )r   r   r   é   TFÚpython3)Úuser_functionsÚ	precisionÚinlineÚfully_qualified_modulesÚcontractÚstandardc                    s˜   t ƒ  |¡ | jd }|d u rdd l}d |jj¡}|dkr"tdƒ‚|| _t	t
ƒ| _t| jfi |p2i  di ¡¤Ž| _t| jfi |pBi  di ¡¤Ž| _d S )NrŒ   r   zpython{}r†   zOnly Python 3 is supported.r‡   Úuser_constants)ÚsuperÚ__init__Ú	_settingsÚsysro   Úversion_infoÚmajorÚ
ValueErrorrŒ   r   ÚsetÚmodule_importsÚdictÚ_kfÚgetrl   Ú_kcrv   )re   ÚsettingsÚstdr‘   ©rm   rf   rg   r�   e   s   


ÿÿz"AbstractPythonCodePrinter.__init__c                 C   s   d||f S ©Nz%s = %srf   )re   rj   Úvaluerf   rf   rg   Ú_declare_number_consty   s   z/AbstractPythonCodePrinter._declare_number_constc                 C   sl   |  d¡}|rt|ƒdkr| jd |d d… ¡  |d ¡ | jd r%|S |  d¡d   d¡d   d¡d S )NÚ.r   éÿÿÿÿrŠ   ú(r   ú[)ÚsplitÚlenr–   rq   Úaddr�   )re   ÚfqnÚregisterÚpartsrf   rf   rg   rp   |   s   
"
"z(AbstractPythonCodePrinter._module_formatc                 C   s   |S r^   rf   )re   Úlinesrf   rf   rg   Ú_format_code†   ó   z&AbstractPythonCodePrinter._format_codec                 C   ó
   d  |¡S )Nz{}©ro   ©re   Ú
codestringrf   rf   rg   Ú_get_statement‰   ó   
z(AbstractPythonCodePrinter._get_statementc                 C   r®   )Nz  # {}r¯   )re   Útextrf   rf   rg   Ú_get_commentŒ   r³   z&AbstractPythonCodePrinter._get_commentc                 C   sH   t |ƒdkr|  |d ¡S d|  |¡|  ||dd… ¡|  |d ¡f 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¦   r`   rp   Ú_expand_fold_binary_op)re   Úoprk   rf   rf   rg   r·   �   s   ýz0AbstractPythonCodePrinter._expand_fold_binary_opc                 C   sZ   t |ƒdkr|  |d ¡S t |ƒ}|d }d|  |¡|  |d|… ¡|  ||d… ¡f S )zû
        This method expands a reduction 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¦   r`   rp   Ú_expand_reduce_binary_op)re   r¸   rk   ÚNÚNhalfrf   rf   rg   rº   ¦   s   ýz2AbstractPythonCodePrinter._expand_reduce_binary_opc                 C   ó   dS )Nzfloat('nan')rf   ©re   rs   rf   rf   rg   Ú
_print_NaN¿   r­   z$AbstractPythonCodePrinter._print_NaNc                 C   r½   )Nzfloat('inf')rf   r¾   rf   rf   rg   Ú_print_InfinityÂ   r­   z)AbstractPythonCodePrinter._print_Infinityc                 C   r½   )Nzfloat('-inf')rf   r¾   rf   rf   rg   Ú_print_NegativeInfinityÅ   r­   z1AbstractPythonCodePrinter._print_NegativeInfinityc                 C   ó
   |   |¡S r^   )r¿   r¾   rf   rf   rg   Ú_print_ComplexInfinityÈ   r³   z0AbstractPythonCodePrinter._print_ComplexInfinityc                    s$   t |ƒ‰ dj‡ ‡fdd„|jD ƒŽ S )Nz{} % {}c                 3   s   � | ]	}ˆ  |ˆ ¡V  qd S r^   )Úparenthesize)rb   Úx©ÚPRECre   rf   rg   rh   Í   s   € z7AbstractPythonCodePrinter._print_Mod.<locals>.<genexpr>)r   ro   rk   r¾   rf   rÆ   rg   Ú
_print_ModË   s   z$AbstractPythonCodePrinter._print_Modc                 C   sÆ   g }d}|j D ]9}|j}|j}|dkr| d¡ | d¡ | |  |¡¡ | d¡ | d¡ | |  |¡¡ | d¡ |d7 }q|d d… }|d dkrY|d d	… }| d¡ n| d
¡ d |¡S )Nr   r£   ú)z if z else r   r¢   r   éþÿÿÿz else None)Ú )rk   rs   ÚcondÚappendr`   rq   )re   rs   ÚresultÚirc   rR   Úcrf   rf   rg   Ú_print_PiecewiseÏ   s(   








z*AbstractPythonCodePrinter._print_Piecewisec                    sR   dddddddœ}|j |v r#|  |j¡}|  |j¡}dj|j ||d	�S tƒ  |¡S )
z.Relational printer for Equality and UnequalityÚequalÚ	not_equalÚlessÚ
less_equalÚgreaterÚgreater_equal)z==z!=ú<z<=ú>z>=z({lhs} {op} {rhs}))r¸   ÚlhsÚrhs)Úrel_opr`   rÚ   rÛ   ro   rŽ   Ú_print_Relational)re   rs   r¸   rÚ   rÛ   r�   rf   rg   rÝ   æ   s   ú
z+AbstractPythonCodePrinter._print_Relationalc                 C   s   ddl m} |  | |¡¡S )Nr   )Ú	Piecewise)Ú$sympy.functions.elementary.piecewiserÞ   r`   Úrewrite)re   rs   rÞ   rf   rf   rg   Ú
_print_ITEö   s   z$AbstractPythonCodePrinter._print_ITEc                    s:   ‡ fdd„|j d d d… D ƒ}djˆ  |j¡d |¡d�S )Nc                 3   s8   � | ]\}}}d j ˆ  |¡ˆ  |¡ˆ  |¡d�V  qdS )zfor {i} in range({a}, {b}+1))rÏ   ÚaÚbN)ro   r`   )rb   rÏ   râ   rã   rd   rf   rg   rh   û   s   € üý
ÿz7AbstractPythonCodePrinter._print_Sum.<locals>.<genexpr>r¢   z"(builtins.sum({function} {loops}))ú )ÚfunctionÚloops)Úlimitsro   r`   rå   rq   )re   rs   ræ   rf   rd   rg   Ú
_print_Sumú   s   
û
þz$AbstractPythonCodePrinter._print_Sumc                 C   r½   )NÚ1jrf   r¾   rf   rf   rg   Ú_print_ImaginaryUnit  r­   z.AbstractPythonCodePrinter._print_ImaginaryUnitc                 C   s$   |j \}}dj|  |¡|  |¡d�S )Nz(1 if {a} == {b} else 0))râ   rã   )rk   ro   r`   )re   rs   râ   rã   rf   rf   rg   Ú_print_KroneckerDelta  s
   
þz/AbstractPythonCodePrinter._print_KroneckerDeltac                 C   s,   |j j}| j ||¡}d||  | ¡ ¡f S )Nú%s(%s))rm   rn   rl   r™   r`   Útolist)re   rs   rj   Úfuncrf   rf   rg   Ú_print_MatrixBase  s   z+AbstractPythonCodePrinter._print_MatrixBasec                 C   rÂ   r^   )rï   r¾   rf   rf   rg   Ú<lambda>  s   
 z"AbstractPythonCodePrinter.<lambda>c                    s   d  ‡ fdd„| d¡D ƒ¡S )NÚ
c                    s   g | ]}ˆ j | ‘qS rf   )Útab)rb   Úlinerd   rf   rg   r€      ó    z@AbstractPythonCodePrinter._indent_codestring.<locals>.<listcomp>)rq   r¥   r°   rf   rd   rg   Ú_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   r]   r^   r_   ra   rd   rf   rg   rh   #  ri   zFAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<genexpr>z def {name}({parameters}):
{body}r\   c                    s   g | ]}ˆ   |j¡‘qS rf   )r`   Úsymbol)rb   Úvarrd   rf   rg   r€   &  s    zGAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<listcomp>)rj   Ú
parametersÚbody)rq   rù   ro   r`   rj   rø   rõ   )re   Úfdrù   rf   rd   rg   Ú_print_FunctionDefinition"  s   
ýz3AbstractPythonCodePrinter._print_FunctionDefinitionc                    s6   d  ‡ fdd„|jD ƒ¡}djˆ  |j¡ˆ  |¡d�S )Nrñ   c                 3   r]   r^   r_   ra   rd   rf   rg   rh   +  ri   z9AbstractPythonCodePrinter._print_While.<locals>.<genexpr>zwhile {cond}:
{body})rÌ   rù   )rq   rù   ro   r`   Ú	conditionrõ   )re   Úwhlrù   rf   rd   rg   Ú_print_While*  s
   
þz&AbstractPythonCodePrinter._print_Whilec                 C   s    d|   |jj¡|   |jj¡f S rž   )r`   Úvariablerö   rŸ   )re   Údeclrf   rf   rg   Ú_print_Declaration1  s   þz,AbstractPythonCodePrinter._print_Declarationc                 C   r½   )Nr   rf   )re   Úbtrf   rf   rg   Ú_print_BreakToken7  r­   z+AbstractPythonCodePrinter._print_BreakTokenc                 C   ó   |j \}d|  |¡ S )Nz	return %s©rk   r`   )re   Úretrc   rf   rf   rg   Ú_print_Return:  ó   z'AbstractPythonCodePrinter._print_Returnc                 C   r  )Nzraise %sr  )re   Úrsrc   rf   rf   rg   Ú_print_Raise>  r  z&AbstractPythonCodePrinter._print_Raisec                 C   r  )NzRuntimeError(%s)r  )re   ÚreÚmessagerf   rf   rg   Ú_print_RuntimeError_B  r  z.AbstractPythonCodePrinter._print_RuntimeError_c                    sj   d  ‡ fdd„|jD ƒ¡}ddlm} |j|kr"d ˆ  |j¡|¡}|jd kr1|dˆ  |j¡ 7 }d| S )	Nr\   c                 3   r]   r^   r_   ra   rd   rf   rg   rh   G  ri   z9AbstractPythonCodePrinter._print_Print.<locals>.<genexpr>r   )Únonez{} % ({}), end=""z	, file=%sz	print(%s))rq   Ú
print_argsÚsympy.codegen.astr  Úformat_stringro   r`   Úfile)re   Úprntr  r  rf   rd   rg   Ú_print_PrintF  s   

þ
z&AbstractPythonCodePrinter._print_Printc                 C   s<   t |jƒdkr|  d¡S t |jƒdkr|  d¡S |  |j¡S )NÚstdoutz
sys.stdoutÚstderrz
sys.stderr)Ústrrj   rp   r`   )re   Ústrmrf   rf   rg   Ú_print_StreamR  s
   

z'AbstractPythonCodePrinter._print_Streamc                 C   r½   )Nr   rf   )re   rc   rf   rf   rg   Ú_print_NoneTokenZ  r­   z*AbstractPythonCodePrinter._print_NoneTokenú	math.sqrtc           
      C   sô   t |ƒ}|jtjkr|s|  |¡}|  |j¡}dj||d�S |jrb|sb|j tju rF|  |¡}|  tj	¡}|  |j¡}|› d|› d|› d�S |jtj
u rb|  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î   rc   ú/r£   rÉ   F©Ústrictz{}**{})r   r=   r   ÚHalfrp   r`   Úbasero   Úis_commutativeÚOneÚNegativeOnerÄ   )
re   rs   ÚrationalrL   rÇ   rî   rc   ÚnumÚbase_strÚexp_strrf   rf   rg   Ú_hprint_Pow]  s$   +


z%AbstractPythonCodePrinter._hprint_Powr^   )T)Fr  )=rn   Ú
__module__Ú__qualname__ÚprintmethodÚlanguageÚ_kwÚreserved_wordsÚmodulesrò   r—   r   Ú_known_functionsÚitemsÚ_known_functions_mathr˜   Ú_known_constants_mathrš   Ú
_operatorsr   Ú_default_settingsr�   r    rp   r¬   r²   rµ   r·   rº   r¿   rÀ   rÁ   rÃ   rÈ   rÑ   rÝ   rá   rè   rê   rë   rï   Ú_print_SparseRepMatrixÚ_print_MutableSparseMatrixÚ_print_ImmutableSparseMatrixÚ_print_MatrixÚ_print_DenseMatrixÚ_print_MutableDenseMatrixÚ_print_ImmutableMatrixÚ_print_ImmutableDenseMatrixrõ   rû   rþ   r  r  r  r
  r  r  r  r  r(  Ú__classcell__rf   rf   r�   rg   rx   O   s†    þù


øÿþýüûrx   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   s.   ddl m} z||ƒW S  ty   | Y S w )Nr   )Úconvert_indexed_to_array)Ú4sympy.tensor.array.expressions.from_indexed_to_arrayr@  Ú	Exception)re   Úindexedr@  rf   rf   rg   Ú	_arrayify¢  s   
ÿzArrayPrinter._arrayifyc                 C   s   |   ¡ }d}d}dd„ |D ƒ}g }|D ]%}g }	t|ƒD ]}
||v r)|	 || ¡ n|	 |¡ |d7 }q| |	¡ qi }g }g }|D ]3}
|
D ]*}||vrTt|ƒ}|||< n|| }||7 }||v rj||vri| |¡ qE| |¡ qE|d7 }qA|d d… }|||fS )NrË   r   c                 S   s    i | ]}|D ]}|t |ƒ“qqS rf   )r+   )rb   rÏ   Újrf   rf   rg   rƒ   ­  ó     z3ArrayPrinter._get_einsum_string.<locals>.<dictcomp>r   ú,r¢   )Ú _get_letter_generator_for_einsumÚrangerÍ   Únext)re   ÚsubranksÚcontraction_indicesÚlettersÚcontraction_stringÚcounterÚdÚindicesÚrank_argÚlindicesrÏ   ÚmappingÚletters_freeÚletters_dumrE  Úlrf   rf   rg   Ú_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ƒ‚)Néa   é{   éA   é[   zout of letters)rI  Úchrr”   )re   rÏ   rf   rf   rg   rH  Ì  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 )NrG  c                    s(   g | ]}d   ‡ fdd„t|ƒD ƒ¡‘qS )rË   c                    s   g | ]}t ˆ ƒ‘qS rf   )rJ  )rb   rE  ©rM  rf   rg   r€   Õ  s    zEArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>.<listcomp>)rq   rI  ©rb   rÏ   r^  rf   rg   r€   Õ  s   ( z:ArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>ú%s("%s", %s)r¡   r\   c                    ó   g | ]}ˆ   |¡‘qS rf   r_   ra   rd   rf   rg   r€   Ù  rô   )rH  rq   rK  rp   Ú_moduleÚ_einsumrk   )re   rs   rN  rf   )rM  re   rg   Ú_print_ArrayTensorProductÓ  s   ýz&ArrayPrinter._print_ArrayTensorProductc           
   	      sÜ   ddl m} |j}|j}t||ƒr"d ‡ fdd„|jD ƒ¡}|j}nˆ  |¡}t	|j
ƒg}ˆ  ||¡\}}}	|s=ˆ  |¡S t||ƒrPd ‡ fdd„|jD ƒ¡}nˆ  |¡}dˆ  ˆ jd ˆ j ¡d	 |d
 t|ƒ¡¡|f S )Nr   ©ÚArrayTensorProductrG  c                    ó   g | ]	}d ˆ   |¡ ‘qS ©z%sr_   ra   rd   rf   rg   r€   â  r„   z8ArrayPrinter._print_ArrayContraction.<locals>.<listcomp>c                    rg  rh  r_   ra   rd   rf   rg   r€   í  r„   r`  r¡   ú{}->{}rË   )Ú0sympy.tensor.array.expressions.array_expressionsrf  rs   rL  Ú
isinstancerq   rk   rK  r`   r¦   ÚshaperX  rp   rb  rc  ro   Úsorted)
re   rs   rf  r   rL  ÚelemsÚranksrN  rU  rV  rf   rd   rg   Ú_print_ArrayContractionÜ  s&   




ýz$ArrayPrinter._print_ArrayContractionc           	   	      sž   ddl m} t|jƒ}t|j|ƒr|jj}|jj}n|j}|jg}ˆ  ||¡\}}}‡ fdd„|D ƒ}dˆ  	ˆ j
d ˆ j ¡d |d || ¡¡d	 |¡f S )
Nr   re  c                    ra  rf   r_   r_  rd   rf   rg   r€      rô   z5ArrayPrinter._print_ArrayDiagonal.<locals>.<listcomp>r`  r¡   ri  rË   r\   )rj  rf  ÚlistÚdiagonal_indicesrk  rs   rK  rk   rX  rp   rb  rc  ro   rq   )	re   rs   rf  rr  rK  rn  Údiagonal_stringrU  rV  rf   rd   rg   Ú_print_ArrayDiagonalö  s   

ýz!ArrayPrinter._print_ArrayDiagonalc                 C   s2   d|   | jd | j ¡|  |j¡|  |jj¡f S )Nr¶   r¡   )rp   rb  Ú
_transposer`   rs   ÚpermutationÚ
array_formr¾   rf   rf   rg   Ú_print_PermuteDims  s
   
ýzArrayPrinter._print_PermuteDimsc                 C   s   |   | jd | j |j¡S )Nr¡   )r·   rb  Ú_addrk   r¾   rf   rf   rg   Ú_print_ArrayAdd  ó   zArrayPrinter._print_ArrayAddc                 C   ó.   d|   | jd | j ¡d t| j|jƒ¡f S ©Nz	%s((%s,))r¡   rG  )rp   rb  Ú_onesrq   Úmapr`   rk   r¾   rf   rf   rg   Ú_print_OneArray  ó   þzArrayPrinter._print_OneArrayc                 C   r|  r}  )rp   rb  Ú_zerosrq   r  r`   rk   r¾   rf   rf   rg   Ú_print_ZeroArray  r�  zArrayPrinter._print_ZeroArrayc                 C   s0   |   |  |j¡¡}|   |  |j¡¡}d||f S rž   )r`   rD  rÚ   rÛ   )re   rs   rÚ   rÛ   rf   rf   rg   Ú_print_Assignment  s   zArrayPrinter._print_Assignmentc                 C   rÂ   r^   )Ú_print_ArraySymbolr¾   rf   rf   rg   Ú_print_IndexedBase$  r³   zArrayPrinter._print_IndexedBaseN)rn   r)  r*  rD  rX  rH  rd  rp  rt  rx  rz  r€  rƒ  r„  r†  rf   rf   rf   rg   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   )ÚfrR   ©ro   rp   r`   rk   ©re   rR   rf   rf   rg   Ú_print_sign*  s   ÿzPythonCodePrinter._print_signc                 C   s(   t |ƒ}| jd d |  |jd |¡ S )Nr   rä   r   )r   r4  rÄ   rk   )re   rs   rÇ   rf   rf   rg   Ú
_print_Not.  s    zPythonCodePrinter._print_Notc                 C   s   |j S r^   )rj   r¾   rf   rf   rg   r†  2  s   z$PythonCodePrinter._print_IndexedBasec                    s<   |j d }|j dd … }d t|ƒd ‡ fdd„|D ƒ¡¡S )Nr   r   z{}[{}]r\   c                    ra  rf   r_   )rb   Úindrd   rf   rg   r€   8  rô   z4PythonCodePrinter._print_Indexed.<locals>.<listcomp>)rk   ro   r  rq   )re   rs   r   Úindexrf   rd   rg   Ú_print_Indexed5  s   
$z PythonCodePrinter._print_IndexedFc                 C   s   | j ||d�S )N)r$  ©r(  ©re   rs   r$  rf   rf   rg   Ú
_print_Pow:  ó   zPythonCodePrinter._print_Powc                 C   s   d  |j|j¡S )Nz{}/{})ro   ÚpÚqr¾   rf   rf   rg   Ú_print_Rational=  ó   z!PythonCodePrinter._print_Rationalc                 C   rÂ   r^   ©r–  r¾   rf   rf   rg   Ú_print_Half@  r³   zPythonCodePrinter._print_Halfc                 C   s   |   t|jd dƒ¡S )Nr   r   )rÈ   r   rk   r¾   rf   rf   rg   Ú_print_fracC  s   zPythonCodePrinter._print_fracc                    s`   t ƒ  |¡}|| jv r | jd rd}t| |¡ƒ‚|| jd  S d|v r.| dd¡ dd¡S |S )NÚerror_on_reservedzVThis expression includes the symbol "{}" which is a reserved keyword in this language.Úreserved_word_suffixÚ{rË   Ú})rŽ   Ú_print_Symbolr.  r�   r”   ro   Úreplace)re   rs   rj   Úmsgr�   rf   rg   rŸ  F  s   

zPythonCodePrinter._print_Symbol©F)rn   r)  r*  r‹  rŒ  r†  r�  r’  r–  r™  rš  rŸ  r   Ú_print_not_supportedÚ_print_lowergammaÚ_print_uppergammaÚ_print_fresnelcÚ_print_fresnelsr>  rf   rf   r�   rg   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‡  Údoprint)rs   r›   rf   rf   rg   Úpycodeb  s   r©  )r‡  )r=   rL   rF   r9   rJ   rM   r0   r2   r4   r:   rK   rN   r1   r3   r6   z-inf)rW   rX   rY   ÚNegativeInfinityc                       st   e Zd ZdZdZdZeee 	¡ ƒƒZ
dd„ e 	¡ D ƒZddd„Zd	d
„ Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚCmathPrinterz# Printer for Python's cmath module Ú
_cmathcodezPython with cmathc                 C   r‚   )úcmath.rf   r}   rf   rf   rg   rƒ   ¡  r„   zCmathPrinter.<dictcomp>Fc                 C   ó   | j ||dd�S )Nz
cmath.sqrt©r$  rL   r�  r‘  rf   rf   rg   r’  £  r—  zCmathPrinter._print_Powc                 C   s   dj |  d¡|  |¡d�S )Nz{func}({val})z	cmath.mpf)rî   Úval©ro   rp   r`   rŠ  rf   rf   rg   Ú_print_Float¦  r{  zCmathPrinter._print_Floatc                    sF   |j j}|| jv rd| j| › dd t| j|jƒ¡› d�S tƒ  |¡S )Nr­  r£   r\   rÉ   )	rî   rn   r˜   rq   r  r`   rk   rŽ   Ú_print_Function)re   rs   Ú	func_namer�   rf   rg   ru   ©  s   
(zCmathPrinter._print_known_funcc                 C   s   | j |jj S r^   )rš   rm   rn   r¾   rf   rf   rg   rw   ¯  r“  zCmathPrinter._print_known_constc                 C   ó   d|   |jd ¡› d�S )zPrints `re(z)` as `z.real`r£   r   z).real©r`   rk   r¾   rf   rf   rg   Ú	_print_re²  ó   zCmathPrinter._print_rec                 C   rµ  )zPrints `im(z)` as `z.imag`r£   r   z).imagr¶  r¾   rf   rf   rg   Ú	_print_im¶  r¸  zCmathPrinter._print_imr¢  )rn   r)  r*  Ú__doc__r+  r,  r—   r   Ú_known_functions_cmathr1  r˜   Ú_known_constants_cmathrš   r’  r²  ru   rw   r·  r¹  r>  rf   rf   r�   rg   r«  ˜  s    ÿ
r«  z
log1p log2c                 C   s    g | ]\}}|t vr||f‘qS rf   )Ú_not_in_mpmathr}   rf   rf   rg   r€   Ã  rF  r€   ÚbetaÚfracÚfresnelcÚfresnelsÚsignrO   ÚhyperÚmeijergÚbesseljÚbesselyÚbesseliÚbesselk)r¾  r¿  rÀ  rÁ  rÂ  rO   rÃ  rÄ  rÅ  rÆ  rÇ  rÈ  ÚphiÚeulerÚcatalanÚninf)rV   rW   ÚGoldenRatioÚ
EulerGammaÚCatalanrZ   rY   rª  c                 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Í   )Úintegral_exprÚintegration_varsrç   Úintegration_rangeÚintegration_varÚlower_limitÚupper_limitrf   rf   rg   Ú_unpack_integral_limitsÞ  s   

rØ  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d„ ZdS )ÚMpmathPrinterzH
    Lambda printer for mpmath which maintains precision for floats
    Ú_mpmathcodezPython with mpmathc                 C   r{   ©zmpmath.rf   r}   rf   rf   rg   r€   û  r�   zMpmathPrinter.<listcomp>c                 C   r‚   rÛ  rf   r}   rf   rf   rg   rƒ   ý  r„   zMpmathPrinter.<dictcomp>c                 C   s(   t ttt|jƒƒƒ}dj|  d¡|d�S )Nz{func}({args})ú
mpmath.mpf)rî   rk   )r  Útupler  ÚintÚ_mpf_ro   rp   )re   rR   rk   rf   rf   rg   r²  ÿ  s   zMpmathPrinter._print_Floatc                 C   s&   dj |  d¡|  |j¡|  |j¡d�S )Nz{func}({p})/{func}({q})rÜ  )rî   r•  r”  )ro   rp   r`   r•  r”  rŠ  rf   rf   rg   r–  	  s
   

ýzMpmathPrinter._print_Rationalc                 C   rÂ   r^   r˜  rŠ  rf   rf   rg   r™    r³   zMpmathPrinter._print_Halfc                 C   s4   d  |  d¡|  |jd ¡|  |jd ¡|  d¡¡S )Nz{}({}, {}, {})úmpmath.gammaincr   r   z
mpmath.infr‰  rŠ  rf   rf   rg   r¥    s   üzMpmathPrinter._print_uppergammac                 C   s,   d  |  d¡|  |jd ¡|  |jd ¡¡S )Nz{}({}, 0, {})rà  r   r   r‰  rŠ  rf   rf   rg   r¤    s
   ýzMpmathPrinter._print_lowergammac                 C   ó   d  |  d¡|  |jd ¡¡S )Nz{0}({1})/{0}(2)z
mpmath.logr   r‰  rŠ  rf   rf   rg   Ú_print_log2   ó   ÿzMpmathPrinter._print_log2c                 C   rá  )Nz{}({})zmpmath.log1pr   r‰  rŠ  rf   rf   rg   Ú_print_log1p$  rã  zMpmathPrinter._print_log1pFc                 C   r®  )Nzmpmath.sqrtr¯  r�  r‘  rf   rf   rg   r’  (  r—  zMpmathPrinter._print_Powc              
      sP   t |ƒ\}}d ˆ  d¡d tˆ j|ƒ¡ˆ  |jd ¡d ‡ fdd„|D ƒ¡¡S )Nz{}(lambda {}: {}, {})zmpmath.quadr\   r   c                 3   s$   � | ]}d t tˆ j|ƒƒ V  qdS )z(%s, %s)N)rÝ  r  r`   )rb   rW  rd   rf   rg   rh   2  s   €" z0MpmathPrinter._print_Integral.<locals>.<genexpr>)rØ  ro   rp   rq   r  r`   rk   )re   rR   rÓ  rç   rf   rd   rg   Ú_print_Integral+  s   üzMpmathPrinter._print_Integralc                 C   s&   |\}|\}d  |  d¡|  |¡|¡S )Nz{}({}, derivative={})zmpmath.zetar±  )re   rk   Ú
seq_ordersrc   Úderiv_orderrf   rf   rg   Ú_print_Derivative_zeta5  s   
þz$MpmathPrinter._print_Derivative_zetaNr¢  )rn   r)  r*  rº  r+  r,  r—   r   r0  r1  Ú_known_functions_mpmathr˜   Ú_known_constants_mpmathrš   r²  r–  r™  r¥  r¤  râ  rä  r’  rå  rè  rf   rf   rf   rg   rÙ  ñ  s&    þ


rÙ  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¡   r\   c                 3   r]   r^   r_   ra   rd   rf   rg   rh   Q  ri   z/SymPyPrinter._print_Function.<locals>.<genexpr>)rî   r)  rp   rn   rq   rk   )re   rs   Úmodrf   rd   rg   r³  N  s   ÿzSymPyPrinter._print_Functionc                 C   r®  )Nz
sympy.sqrtr¯  r�  r‘  rf   rf   rg   r’  S  r—  zSymPyPrinter._print_PowNr¢  )	rn   r)  r*  r,  r—   rx   r5  r³  r’  rf   rf   rf   rg   rë  E  s    þrë  N)'rº  Úcollectionsr   Ú	itertoolsr   Ú
sympy.corer   Úsympy.core.modr   r   Úcodeprinterr   r-  r0  r2  r3  ru   rw   rx   r?  r‡  r˜   r~   Úsetattrr©  Úsympy.printing.pycoder»  r¼  r«  r¥   r½  r1  Ú
_in_mpmathr—   ré  rê  rØ  rÙ  rë  rf   rf   rf   rg   Ú<module>   s   ýÿþýüûúùø	÷
öõôóòñðïá#ú	  S 	
3ñü
#
ôø
M