U
    L?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 qe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 G/var/www/html/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rt|}|||< 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 )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  )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||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%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
