a
    <bN                     @   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	 h dZ
d	d
i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'Zd(d)d(d*d+d+d,Zd-d. Zd/d0 ZG d1d2 d2e	ZG d3d4 d4eZejD ]Zeed5e e qeD ]Zeed5e e qd6d7 Zd8 Zd9d: e D Zeefi d;d<d=d>d?d@dAdBdCdDdEdFdGZd(d)dHdIdJd+d*dKdLZdMdN ZG dOdP dPeZejD ]Zeed5e e qeD ]Zeed5e e qG dQdR dReZdSS )Tzy
Python code printers

This module contains Python code printers for plain Python as well as NumPy & SciPy enabled code.
    )defaultdict)chain)S   )
precedence)CodePrinter>!   elseelifTrueasserttrywithfromfordelNoneexceptimportFalsebreakreturnwhilecontinuenotiflambdayieldispassdefnonlocalfinallyasinraiseorclassandglobalZAbsabsacosacoshasinasinhatanatan2atanhceilcoscosherferfcexpexpm1	factorialfloorgammahypotlgammaloglog10log1plog2sinsinhsqrttantanh)r*   r+   r,   r-   r.   r/   r0   Zceilingr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   loggammar=   lnr>   r?   r@   rA   rB   ZSqrtrD   rE   epiinfnan)Exp1PiEInfinityNaNZComplexInfinityc                    s8    j |jj }dj |dt fdd|jdS )Nz{name}({args}), c                    s
     | S N_printargself e/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/printing/pycode.py<lambda>B       z#_print_known_func.<locals>.<lambda>)nameargs)known_functions	__class____name__format_module_formatjoinmapr^   rX   exprZknownrY   rW   rZ   _print_known_func?   s    rh   c                 C   s   | j |jj }| |S rR   )known_constantsr`   ra   rc   rf   rY   rY   rZ   _print_known_constE   s    rj   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dQ fdd	Zdd ZdRd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" fd0d1Z#d2d3 Z$d4d5 Z%d6d7 Z&d8d9 Z'd:d; Z(d<d=  Z) Z* Z+ Z, Z- Z. Z/Z0d>d? Z1d@dA Z2dBdC Z3dDdE Z4dFdG Z5dHdI Z6dJdK Z7dLdM Z8dSdOdPZ9  Z:S )TAbstractPythonCodePrinterZ_pythoncodePythonNz    c                 C   s   g | ]\}}|d | fqS zmath.rY   .0kvrY   rY   rZ   
<listcomp>R   r\   z$AbstractPythonCodePrinter.<listcomp>c                 C   s   i | ]\}}|d | qS rm   rY   rn   rY   rY   rZ   
<dictcomp>T   r\   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 )Nry   r   zpython{}ru   zOnly Python 3 is supported.rv   Zuser_constants)super__init__	_settingssysrb   version_infomajor
ValueErrorry   r   setmodule_importsdict_kfgetr_   _kcri   )rX   settingsZstdr}   r`   rY   rZ   r{   `   s    


z"AbstractPythonCodePrinter.__init__c                 C   s   d||f S Nz%s = %srY   )rX   r]   valuerY   rY   rZ   _declare_number_constt   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   rx   (r   [)splitlenr   rd   addr|   )rX   ZfqnregisterpartsrY   rY   rZ   rc   w   s    
"
z(AbstractPythonCodePrinter._module_formatc                 C   s   |S rR   rY   )rX   linesrY   rY   rZ   _format_code   s    z&AbstractPythonCodePrinter._format_codec                 C   s
   d |S )Nz{}rb   rX   Z
codestringrY   rY   rZ   _get_statement   s    z(AbstractPythonCodePrinter._get_statementc                 C   s
   d |S )Nz  # {}r   )rX   textrY   rY   rZ   _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   rT   rc   _expand_fold_binary_op)rX   opr^   rY   rY   rZ   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   rT   rc   _expand_reduce_binary_op)rX   r   r^   NZNhalfrY   rY   rZ   r      s    z2AbstractPythonCodePrinter._expand_reduce_binary_opc                 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 )N r   c                 S   s    i | ]}|D ]}|t |qqS rY   )min)ro   ijrY   rY   rZ   rs      r\   z@AbstractPythonCodePrinter._get_einsum_string.<locals>.<dictcomp>r   ,r   )Z _get_letter_generator_for_einsumrangeappendnext)rX   ZsubranksZcontraction_indiceslettersZcontraction_stringcounterdindicesZrank_argZlindicesr   mappingZletters_freeZletters_dumr   lrY   rY   rZ   _get_einsum_string   s<    



z,AbstractPythonCodePrinter._get_einsum_stringc                 C   s   dS )Nzfloat('nan')rY   rX   rg   rY   rY   rZ   
_print_NaN   s    z$AbstractPythonCodePrinter._print_NaNc                 C   s   dS )Nzfloat('inf')rY   r   rY   rY   rZ   _print_Infinity   s    z)AbstractPythonCodePrinter._print_Infinityc                 C   s   dS )Nzfloat('-inf')rY   r   rY   rY   rZ   _print_NegativeInfinity   s    z1AbstractPythonCodePrinter._print_NegativeInfinityc                 C   s
   |  |S rR   )r   r   rY   rY   rZ   _print_ComplexInfinity   s    z0AbstractPythonCodePrinter._print_ComplexInfinityc                    s$   t | djt fdd|j S )Nz{} % {}c                    s    |  S rR   )parenthesize)xPRECrX   rY   rZ   r[      r\   z6AbstractPythonCodePrinter._print_Mod.<locals>.<lambda>)r   rb   re   r^   r   rY   r   rZ   
_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)r   )r^   rg   condr   rT   rd   )rX   rg   resultr   rV   rH   crY   rY   rZ   _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_oprT   r   r   rb   rz   _print_Relational)rX   rg   r   r   r   r   rY   rZ   r     s    
z+AbstractPythonCodePrinter._print_Relationalc                 C   s   ddl m} | ||S )Nr   )	Piecewise)Z$sympy.functions.elementary.piecewiser   rT   Zrewrite)rX   rg   r   rY   rY   rZ   
_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)rb   rT   )ro   r   r   r   rW   rY   rZ   	<genexpr>  s   z7AbstractPythonCodePrinter._print_Sum.<locals>.<genexpr>z"(builtins.sum({function} {loops})) )functionloops)limitsrb   rT   r   rd   )rX   rg   r   rY   rW   rZ   
_print_Sum  s    

z$AbstractPythonCodePrinter._print_Sumc                 C   s   dS )NZ1jrY   r   rY   rY   rZ   _print_ImaginaryUnit#  s    z.AbstractPythonCodePrinter._print_ImaginaryUnitc                 C   s$   |j \}}dj| || |dS )Nz(1 if {a} == {b} else 0))r   r   )r^   rb   rT   )rX   rg   r   r   rY   rY   rZ   _print_KroneckerDelta&  s
    
z/AbstractPythonCodePrinter._print_KroneckerDeltac                 C   s,   |j j}| j||}d|| | f S )N%s(%s))r`   ra   r_   r   rT   tolist)rX   rg   r]   funcrY   rY   rZ   _print_MatrixBase.  s    z+AbstractPythonCodePrinter._print_MatrixBasec                 C   s
   |  |S rR   )r   r   rY   rY   rZ   r[   ;  r\   z"AbstractPythonCodePrinter.<lambda>c                    s   d  fdd|dD S )N
c                    s   g | ]} j | qS rY   )tab)ro   linerW   rY   rZ   rr   >  r\   z@AbstractPythonCodePrinter._indent_codestring.<locals>.<listcomp>)rd   r   r   rY   rW   rZ   _indent_codestring=  s    z,AbstractPythonCodePrinter._indent_codestringc                    sN   d t fdd|j}dj |jd  fdd|jD  |dS )	Nr   c                    s
     | S rR   rS   rU   rW   rY   rZ   r[   A  r\   zEAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<lambda>z def {name}({parameters}):
{body}rQ   c                    s   g | ]}  |jqS rY   )rT   symbol)ro   varrW   rY   rZ   rr   D  r\   zGAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<listcomp>)r]   
parametersbody)rd   re   r   rb   rT   r]   r   r   )rX   fdr   rY   rW   rZ   _print_FunctionDefinition@  s    
z3AbstractPythonCodePrinter._print_FunctionDefinitionc                    s6   d t fdd|j}dj |j |dS )Nr   c                    s
     | S rR   rS   rU   rW   rY   rZ   r[   I  r\   z8AbstractPythonCodePrinter._print_While.<locals>.<lambda>zwhile {cond}:
{body})r   r   )rd   re   r   rb   rT   	conditionr   )rX   Zwhlr   rY   rW   rZ   _print_WhileH  s
    
z&AbstractPythonCodePrinter._print_Whilec                 C   s    d|  |jj|  |jjf S r   )rT   variabler   r   )rX   declrY   rY   rZ   _print_DeclarationO  s    z,AbstractPythonCodePrinter._print_Declarationc                 C   s   |j \}d| | S )Nz	return %s)r^   rT   )rX   retrV   rY   rY   rZ   _print_ReturnU  s    z'AbstractPythonCodePrinter._print_Returnc                    s^   d t fdd|j}|jd kr8d |j|}|jd krV|d |j 7 }d| S )NrQ   c                    s
     | S rR   rS   rU   rW   rY   rZ   r[   Z  r\   z8AbstractPythonCodePrinter._print_Print.<locals>.<lambda>z	{} % ({})z	, file=%sz	print(%s))rd   re   
print_argsformat_stringrb   rT   file)rX   Zprntr   rY   rW   rZ   _print_PrintY  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)strr]   rc   rT   )rX   strmrY   rY   rZ   _print_Streamc  s
    

z'AbstractPythonCodePrinter._print_Streamc                 C   s   dS )Nr   rY   )rX   rV   rY   rY   rZ   _print_NoneTokenk  s    z*AbstractPythonCodePrinter._print_NoneToken	math.sqrtc           
      C   s   t |}|jtjkr<|s<| |}| |j}dj||dS |jr|j tju r|s| |}| tj	}| |j}dj|||dS | j
|j|dd}| j
|j|dd}	d||	S )a  Printing helper function for ``Pow``

        Notes
        =====

        This only preprocesses the ``sqrt`` as math formatter

        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)'

        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   rV   z{num}/{func}({arg}))numr   rV   F)strictz{}**{})r   r6   r   ZHalfrc   rT   baserb   Zis_commutativeZOner   )
rX   rg   rationalrC   r   r   rV   r   Zbase_strZexp_strrY   rY   rZ   _hprint_Pown  s     '

z%AbstractPythonCodePrinter._hprint_Pow)N)T)Fr   );ra   
__module____qualname__printmethodlanguage_kwreserved_wordsmodulesr   r   r   _known_functionsitems_known_functions_mathr   _known_constants_mathr   
_operatorsr   Z_default_settingsr{   r   rc   r   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   __classcell__rY   rY   r   rZ   rk   J   s   


#
rk   c                       sn   e Z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   )frH   rb   rc   rT   r^   rX   rH   rY   rY   rZ   _print_sign  s    zPythonCodePrinter._print_signc                 C   s$   t |}| jd | |jd | S )Nr   r   )r   r  r   r^   )rX   rg   r   rY   rY   rZ   
_print_Not  s    zPythonCodePrinter._print_Notc                    s<   |j d }|j dd  }dt|d fdd|D S )Nr   r   z{}[{}]rQ   c                    s   g | ]}  |qS rY   rS   )ro   indrW   rY   rZ   rr     r\   z4PythonCodePrinter._print_Indexed.<locals>.<listcomp>)r^   rb   r   rd   )rX   rg   r   indexrY   rW   rZ   _print_Indexed  s    
z PythonCodePrinter._print_IndexedFc                 C   s   | j ||dS )N)r   r   rX   rg   r   rY   rY   rZ   
_print_Pow  s    zPythonCodePrinter._print_Powc                 C   s   d |j|jS )Nz{}/{})rb   pqr   rY   rY   rZ   _print_Rational  s    z!PythonCodePrinter._print_Rationalc                 C   s
   |  |S rR   r  r   rY   rY   rZ   _print_Half  s    zPythonCodePrinter._print_Halfc                 C   s"   ddl m} | ||jd dS )Nr   )Modr   )Zsympy.core.modr  r   r^   )rX   rg   r  rY   rY   rZ   _print_frac  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   })rz   _print_Symbolr   r|   r   rb   replace)rX   rg   r]   msgr   rY   rZ   r    s    

zPythonCodePrinter._print_Symbol)F)ra   r   r   r
  r  r  r  r  r  r  r  r   Z_print_not_supported_print_lowergamma_print_uppergammaZ_print_fresnelcZ_print_fresnelsr  rY   rY   r   rZ   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)rg   r   rY   rY   rZ   pycode  s    r   z
log1p log2c                 C   s    g | ]\}}|t vr||fqS rY   )_not_in_mpmathrn   rY   rY   rZ   rr     r\   rr   betafracfresnelcfresnelssignrF   hypermeijergbesseljbesselybesselibesselk)r"  r#  r$  r%  r&  rF   r'  r(  r)  r*  r+  r,  phiZeulercatalanZninf)rL   rM   ZGoldenRatioZ
EulerGammaZCatalanrP   rO   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_limitrY   rY   rZ   _unpack_integral_limits  s    

r3  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.rY   rn   rY   rY   rZ   rr   4  r\   zMpmathPrinter.<listcomp>c                 C   s   i | ]\}}|d | qS r5  rY   rn   rY   rY   rZ   rs   6  r\   zMpmathPrinter.<dictcomp>c                 C   s(   t ttt|j}dj| d|dS )Nz{func}({args})
mpmath.mpf)r   r^   )r   tuplere   intZ_mpf_rb   rc   )rX   rH   r^   rY   rY   rZ   _print_Float8  s    zMpmathPrinter._print_Floatc                 C   s&   dj | d| |j| |jdS )Nz{func}({p})/{func}({q})r6  )r   r  r  )rb   rc   rT   r  r  r	  rY   rY   rZ   r  B  s
    

zMpmathPrinter._print_Rationalc                 C   s
   |  |S rR   r  r	  rY   rY   rZ   r  I  s    zMpmathPrinter._print_Halfc                 C   s4   d | d| |jd | |jd | dS )Nz{}({}, {}, {})mpmath.gammaincr   r   z
mpmath.infr  r	  rY   rY   rZ   r  L  s    zMpmathPrinter._print_uppergammac                 C   s,   d | d| |jd | |jd S )Nz{}({}, 0, {})r:  r   r   r  r	  rY   rY   rZ   r  S  s
    zMpmathPrinter._print_lowergammac                 C   s   d | d| |jd S )Nz{0}({1})/{0}(2)
mpmath.logr   r  r	  rY   rY   rZ   _print_log2Y  s    zMpmathPrinter._print_log2c                 C   s   d | d| |jd S )Nz{}({}+1)r;  r   r  r	  rY   rY   rZ   _print_log1p]  s    zMpmathPrinter._print_log1pFc                 C   s   | j ||ddS )Nzmpmath.sqrtr   rC   r  r  rY   rY   rZ   r  a  s    zMpmathPrinter._print_Powc              
      sP   t |\}}d ddt j| |jd d fdd|D S )Nz{}(lambda {}: {}, {})zmpmath.quadrQ   r   c                 3   s"   | ]}d t t j| V  qdS )z(%s, %s)N)r7  re   rT   )ro   r   rW   rY   rZ   r   k  r\   z0MpmathPrinter._print_Integral.<locals>.<genexpr>)r3  rb   rc   rd   re   rT   r^   )rX   rH   r2  r   rY   rW   rZ   _print_Integrald  s    zMpmathPrinter._print_IntegralN)F)ra   r   r   __doc__r   r   r   r   r   r  _known_functions_mpmathr   _known_constants_mpmathr   r9  r  r  r  r  r<  r=  r  r?  rY   rY   rY   rZ   r4  *  s"   

r4  c                   @   s"   e Zd ZdZdd ZdddZdS )	SymPyPrinterzPython with SymPyc                    sH   |j jp
d}d ||rdnd |j j dt fdd|jf S )Nr   r   r   rQ   c                    s
     | S rR   rS   rU   rW   rY   rZ   r[   |  r\   z.SymPyPrinter._print_Function.<locals>.<lambda>)r   r   rc   ra   rd   re   r^   )rX   rg   modrY   rW   rZ   _print_Functiony  s    zSymPyPrinter._print_FunctionFc                 C   s   | j ||ddS )Nz
sympy.sqrtr>  r  r  rY   rY   rZ   r  ~  s    zSymPyPrinter._print_PowN)F)ra   r   r   r   rE  r  rY   rY   rY   rZ   rC  u  s   rC  N) r@  collectionsr   	itertoolsr   Z
sympy.corer   r   Zcodeprinterr   r   r   r  r  rh   rj   rk   r  r   rp   setattrr   r   r!  r  Z
_in_mpmathr   rA  rB  r3  r4  rC  rY   rY   rY   rZ   <module>   s   !	  a1

D
