a
    khi                      @   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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-d/d0d0d1Zd2d3 Zd4d5 ZG d6d7 d7eZG d8d9 d9ZG d:d; d;eZejD ]Zeed<e e qe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d@Zd.d-d/dAdBZG dCdD dDeZejD ]Zeed<e ej qeD ]Zeed<e ej qdE ZdFdG e D Z e!e fi dHdIdJdKdLdMdNdOdPdQdRdSdTZ"d-d.dUdVdWd0d/dXdYZ#dZd[ Z$G d\d] d]eZ%e%jD ]Zee%d<e e qDe#D ]Zee%d<e e q`G d^d_ d_eZ&d`S )azy
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>!   classinandnonlocalimportyieldforglobalislambdawithasorreturnexceptpasseliffinallyfromelsenotifNoneFalseraisedeltrywhileTruecontinuedefassertbreak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isinf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?   r@   loggammarB   lnrC   rD   rE   rF   rG   ZSqrtrI   rJ   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/var/www/auris/lib/python3.9/site-packages/sympy/printing/pycode.py	<genexpr>G       z$_print_known_func.<locals>.<genexpr>)nameargs)known_functions	__class____name__format_module_formatjoinrd   r^   exprZknownr_   r]   r`   _print_known_funcD   s    rm   c                 C   s   | j |jj }| |S rW   )known_constantsrf   rg   ri   rk   r_   r_   r`   _print_known_constJ   s    ro   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_   r[   kvr_   r_   r`   
<listcomp>W   rb   z$AbstractPythonCodePrinter.<listcomp>c                 C   s   i | ]\}}|d | qS rr   r_   rs   r_   r_   r`   
<dictcomp>Y   rb   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 u r4dd l}d|jj}|dkrDtd|| _t	t
| _t| jfi |pdi di | _t| jfi |pi di | _d S )Nr}   r   zpython{}ry   zOnly Python 3 is supported.rz   Zuser_constants)super__init__	_settingssysrh   version_infomajor
ValueErrorr}   r   setmodule_importsdict_kfgetre   _kcrn   )r^   settingsZstdr   rf   r_   r`   r   e   s    


z"AbstractPythonCodePrinter.__init__c                 C   s   d||f S Nz%s = %sr_   )r^   rc   valuer_   r_   r`   _declare_number_consty   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   rj   addr   )r^   Zfqnregisterpartsr_   r_   r`   ri   |   s    
"
z(AbstractPythonCodePrinter._module_formatc                 C   s   |S rW   r_   )r^   linesr_   r_   r`   _format_code   s    z&AbstractPythonCodePrinter._format_codec                 C   s
   d |S )Nz{}rh   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   rY   ri   _expand_fold_binary_op)r^   oprd   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 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   rY   ri   _expand_reduce_binary_op)r^   r   rd   NZNhalfr_   r_   r`   r      s    z2AbstractPythonCodePrinter._expand_reduce_binary_opc                 C   s   dS )Nzfloat('nan')r_   r^   rl   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 rW   )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 rW   )parenthesize)r[   xPRECr^   r_   r`   ra      rb   z7AbstractPythonCodePrinter._print_Mod.<locals>.<genexpr>)r   rh   rd   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) )rd   rl   condappendrY   rj   )r^   rl   resultir\   rM   cr_   r_   r`   _print_Piecewise   s(    







z*AbstractPythonCodePrinter._print_Piecewisec                    sR   ddddddd}|j |v 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_oprY   r   r   rh   r~   _print_Relational)r^   rl   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   rY   Zrewrite)r^   rl   r   r_   r_   r`   
_print_ITE   s    z$AbstractPythonCodePrinter._print_ITEc                    s:    fdd|j d d d 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)rh   rY   )r[   r   r   r   r]   r_   r`   ra      s   z7AbstractPythonCodePrinter._print_Sum.<locals>.<genexpr>r   z"(builtins.sum({function} {loops})) )functionloops)limitsrh   rY   r   rj   )r^   rl   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   )rd   rh   rY   )r^   rl   r   r   r_   r_   r`   _print_KroneckerDelta  s
    
z/AbstractPythonCodePrinter._print_KroneckerDeltac                 C   s,   |j j}| j||}d|| | f S )N%s(%s))rf   rg   re   r   rY   tolist)r^   rl   rc   funcr_   r_   r`   _print_MatrixBase  s    z+AbstractPythonCodePrinter._print_MatrixBasec                 C   s
   |  |S rW   )r   r   r_   r_   r`   <lambda>  rb   z"AbstractPythonCodePrinter.<lambda>c                    s   d  fdd|dD S )N
c                    s   g | ]} j | qS r_   )tab)r[   liner]   r_   r`   rv      rb   z@AbstractPythonCodePrinter._indent_codestring.<locals>.<listcomp>)rj   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 rW   rX   rZ   r]   r_   r`   ra   #  rb   zFAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<genexpr>z def {name}({parameters}):
{body}rV   c                    s   g | ]}  |jqS r_   )rY   symbol)r[   varr]   r_   r`   rv   &  rb   zGAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<listcomp>)rc   
parametersbody)rj   r   rh   rY   rc   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 rW   rX   rZ   r]   r_   r`   ra   +  rb   z9AbstractPythonCodePrinter._print_While.<locals>.<genexpr>zwhile {cond}:
{body})r   r   )rj   r   rh   rY   	conditionr   )r^   Zwhlr   r_   r]   r`   _print_While*  s
    
z&AbstractPythonCodePrinter._print_Whilec                 C   s    d|  |jj|  |jjf S r   )rY   variabler   r   )r^   declr_   r_   r`   _print_Declaration1  s    z,AbstractPythonCodePrinter._print_Declarationc                 C   s   dS )Nr)   r_   )r^   Zbtr_   r_   r`   _print_BreakToken7  s    z+AbstractPythonCodePrinter._print_BreakTokenc                 C   s   |j \}d| | S )Nz	return %srd   rY   )r^   retr\   r_   r_   r`   _print_Return:  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_B  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 )	NrV   c                 3   s   | ]}  |V  qd S rW   rX   rZ   r]   r_   r`   ra   G  rb   z9AbstractPythonCodePrinter._print_Print.<locals>.<genexpr>r   )nonez{} % ({}), end=""z	, file=%sz	print(%s))rj   
print_argsZsympy.codegen.astr   format_stringrh   rY   file)r^   Zprntr   r   r_   r]   r`   _print_PrintF  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)strrc   ri   rY   )r^   strmr_   r_   r`   _print_StreamR  s
    

z'AbstractPythonCodePrinter._print_Streamc                 C   s   dS )Nr   r_   )r^   r\   r_   r_   r`   _print_NoneTokenZ  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u r| |}| tj	}| |j}| d| d| dS |jtj
u 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Halfri   rY   baserh   Zis_commutativeZOneZNegativeOner   )
r^   rl   rationalrH   r   r   r\   numZbase_strZexp_strr_   r_   r`   _hprint_Pow]  s$    +


z%AbstractPythonCodePrinter._hprint_Pow)N)T)Fr   )=rg   
__module____qualname__printmethodlanguage_kwreserved_wordsmodulesr   r   r   _known_functionsitems_known_functions_mathr   _known_constants_mathr   
_operatorsr   _default_settingsr   r   ri   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`   rp   O   s   


rp   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   s2   ddl m} z
||W S  ty,   | Y S 0 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 ].}
||v rR|	||  n
|	| |d7 }q6||	 q&i }g }g }|D ]f}
|
D ]T}||vrt|}|||< n|| }||7 }||v r||vr|| q|| q|d7 }q|d d }|||fS )Nr   r   c                 S   s    i | ]}|D ]}|t |qqS r_   )r+   )r[   r   jr_   r_   r`   rw     rb   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 )Na   {   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  )r[   r  r  r_   r`   rv     rb   zEArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>.<listcomp>)rj   r  r[   r   r#  r_   r`   rv     rb   z:ArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>%s("%s", %s)r   rV   c                    s   g | ]}  |qS r_   rX   rZ   r]   r_   r`   rv     rb   )r  rj   r  ri   _module_einsumrd   )r^   rl   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||rd fdd|jD }n
 |}d  jd  j d	|d
t||f S )Nr   ArrayTensorProductr  c                    s   g | ]}d   | qS z%srX   rZ   r]   r_   r`   rv     rb   z8ArrayPrinter._print_ArrayContraction.<locals>.<listcomp>c                    s   g | ]}d   | qS r+  rX   rZ   r]   r_   r`   rv     rb   r%  r   {}->{}r   )0sympy.tensor.array.expressions.array_expressionsr*  rl   r  
isinstancerj   rd   r  rY   r   shaper  ri   r&  r'  rh   sorted)
r^   rl   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_   rX   r$  r]   r_   r`   rv      rb   z5ArrayPrinter._print_ArrayDiagonal.<locals>.<listcomp>r%  r   r,  r   rV   )r-  r*  listdiagonal_indicesr.  rl   r  rd   r  ri   r&  r'  rh   rj   )	r^   rl   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   )ri   r&  Z
_transposerY   rl   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_addrd   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  )ri   r&  Z_onesrj   maprY   rd   r   r_   r_   r`   _print_OneArray  s    zArrayPrinter._print_OneArrayc                 C   s.   d|  | jd | j dt| j|jf S r8  )ri   r&  Z_zerosrj   r9  rY   rd   r   r_   r_   r`   _print_ZeroArray  s    zArrayPrinter._print_ZeroArrayc                 C   s0   |  | |j}|  | |j}d||f S r   )rY   r  r   r   )r^   rl   r   r   r_   r_   r`   _print_Assignment  s    zArrayPrinter._print_Assignmentc                 C   s
   |  |S rW   )Z_print_ArraySymbolr   r_   r_   r`   _print_IndexedBase$  s    zArrayPrinter._print_IndexedBaseN)rg   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   )frM   rh   ri   rY   rd   r^   rM   r_   r_   r`   _print_sign*  s    zPythonCodePrinter._print_signc                 C   s(   t |}| jd d | |jd | S )Nr   r   r   )r   r  r   rd   )r^   rl   r   r_   r_   r`   
_print_Not.  s    zPythonCodePrinter._print_Notc                 C   s   |j S rW   )rc   r   r_   r_   r`   r=  2  s    z$PythonCodePrinter._print_IndexedBasec                    s<   |j d }|j dd  }dt|d fdd|D S )Nr   r   z{}[{}]rV   c                    s   g | ]}  |qS r_   rX   )r[   indr]   r_   r`   rv   8  rb   z4PythonCodePrinter._print_Indexed.<locals>.<listcomp>)rd   rh   r   rj   )r^   rl   r   indexr_   r]   r`   _print_Indexed5  s    
z PythonCodePrinter._print_IndexedFc                 C   s   | j ||dS )N)r   r   r^   rl   r   r_   r_   r`   
_print_Pow:  s    zPythonCodePrinter._print_Powc                 C   s   d |j|jS )Nz{}/{})rh   pqr   r_   r_   r`   _print_Rational=  s    z!PythonCodePrinter._print_Rationalc                 C   s
   |  |S rW   rL  r   r_   r_   r`   _print_Half@  s    zPythonCodePrinter._print_Halfc                 C   s   |  t|jd dS )Nr   r   )r   r   rd   r   r_   r_   r`   _print_fracC  s    zPythonCodePrinter._print_fracc                    sd   t  |}|| jv r@| jd r2d}t|||| jd  S d|v 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   rh   replace)r^   rl   rc   msgr   r_   r`   rR  F  s    

zPythonCodePrinter._print_Symbol)F)rg   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)rl   r   r_   r_   r`   pycodeb  s    rW  )r>  )r9   rH   rB   r5   rF   rI   r-   r/   r1   r6   rG   rJ   r.   r0   r3   z-inf)rR   rS   rT   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 Z
_cmathcodezPython with cmathc                 C   s   i | ]\}}|d | qS )cmath.r_   rs   r_   r_   r`   rw     rb   zCmathPrinter.<dictcomp>Fc                 C   s   | j ||ddS )Nz
cmath.sqrtr   rH   rG  rH  r_   r_   r`   rI    s    zCmathPrinter._print_Powc                 C   s   dj | d| |dS )Nz{func}({val})z	cmath.mpf)r   valrh   ri   rY   rA  r_   r_   r`   _print_Float  s    zCmathPrinter._print_Floatc                    sF   |j j}|| jv r:d| j|  ddt| j|j dS t |S )NrZ  r   rV   r   )	r   rg   r   rj   r9  rY   rd   r~   _print_Function)r^   rl   	func_namer   r_   r`   rm     s    
(zCmathPrinter._print_known_funcc                 C   s   | j |jj S rW   )r   rf   rg   r   r_   r_   r`   ro     s    zCmathPrinter._print_known_constc                 C   s   d|  |jd  dS )zPrints `re(z)` as `z.real`r   r   z).realrY   rd   r   r_   r_   r`   	_print_re  s    zCmathPrinter._print_rec                 C   s   d|  |jd  dS )zPrints `im(z)` as `z.imag`r   r   z).imagra  r   r_   r_   r`   	_print_im  s    zCmathPrinter._print_im)F)rg   r   r   __doc__r   r   r   r   _known_functions_cmathr  r   _known_constants_cmathr   rI  r^  rm   ro   rb  rc  r  r_   r_   r   r`   rY    s   
rY  z
log1p log2c                 C   s    g | ]\}}|t vr||fqS r_   )_not_in_mpmathrs   r_   r_   r`   rv     rb   rv   betafracfresnelcfresnelssignrK   hypermeijergbesseljbesselybesselibesselk)rh  ri  rj  rk  rl  rK   rm  rn  ro  rp  rq  rr  phiZeulercatalanZninf)rQ   rR   ZGoldenRatioZ
EulerGammaZCatalanrU   rT   rX  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   )Zintegral_exprintegration_varsr   Zintegration_rangeZintegration_varZlower_limitZupper_limitr_   r_   r`   _unpack_integral_limits  s    

rx  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
    Z_mpmathcodezPython with mpmathc                 C   s   g | ]\}}|d | fqS zmpmath.r_   rs   r_   r_   r`   rv     rb   zMpmathPrinter.<listcomp>c                 C   s   i | ]\}}|d | qS rz  r_   rs   r_   r_   r`   rw     rb   zMpmathPrinter.<dictcomp>c                 C   s(   t ttt|j}dj| d|dS )Nz{func}({args})
mpmath.mpf)r   rd   )r   tupler9  intZ_mpf_rh   ri   )r^   rM   rd   r_   r_   r`   r^    s    zMpmathPrinter._print_Floatc                 C   s&   dj | d| |j| |jdS )Nz{func}({p})/{func}({q})r{  )r   rK  rJ  )rh   ri   rY   rK  rJ  rA  r_   r_   r`   rL  	  s
    

zMpmathPrinter._print_Rationalc                 C   s
   |  |S rW   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, {})r~  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.sqrtr[  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.quadrV   r   c                 3   s"   | ]}d t t j| V  qdS )z(%s, %s)N)r|  r9  rY   )r[   r  r]   r_   r`   ra   2  rb   z0MpmathPrinter._print_Integral.<locals>.<genexpr>)rx  rh   ri   rj   r9  rY   rd   )r^   rM   rw  r   r_   r]   r`   _print_Integral+  s    zMpmathPrinter._print_Integralc                 C   s&   |\}|\}d | d| ||S )Nz{}({}, derivative={})zmpmath.zetar]  )r^   rd   Z
seq_ordersr\   Zderiv_orderr_   r_   r`   _print_Derivative_zeta5  s    
z$MpmathPrinter._print_Derivative_zetaN)F)rg   r   r   rd  r   r   r   r   r  r  _known_functions_mpmathr   _known_constants_mpmathr   r^  rL  rN  rV  rU  r  r  rI  r  r  r_   r_   r_   r`   ry    s$   


ry  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   rV   c                 3   s   | ]}  |V  qd S rW   rX   rZ   r]   r_   r`   ra   Q  rb   z/SymPyPrinter._print_Function.<locals>.<genexpr>)r   r   ri   rg   rj   rd   )r^   rl   modr_   r]   r`   r_  N  s    zSymPyPrinter._print_Functionc                 C   s   | j ||ddS )Nz
sympy.sqrtr[  rG  rH  r_   r_   r`   rI  S  s    zSymPyPrinter._print_PowN)F)	rg   r   r   r   r   rp   r  r_  rI  r_   r_   r_   r`   r  E  s   r  N)'rd  collectionsr   	itertoolsr   Z
sympy.corer   Zsympy.core.modr   r   Zcodeprinterr   r   r  r  r  rm   ro   rp   r	  r>  r   rt   setattrrW  Zsympy.printing.pycodere  rf  rY  r   rg  r  Z
_in_mpmathr   r  r  rx  ry  r  r_   r_   r_   r`   <module>   s   #	  S 	3
#

M
