U
    L?hN                     @   s  d dl mZ d dlmZ d dlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZ d Zdd	 e D Zeefd
ddddddddddddZddddddZdd e D Zdd e D ZG d d! d!eeZeD ]Zeed"e e
 qeD ]Zeed"e e	 qd#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<Zd=dd>Zd?d e D Zd@d e D ZG dAdB dBeZ eD ]Zee d"e e
 qeD ]Zee d"e e	 qdCd e D Z!dDd e D Z"G dEdF dFeZ#e!D ]Zee#d"e e
 qe"D ]Zee#d"e e	 qdGd e D Z$dHd e D Z%G dIdJ dJeZ&e$D ]Zee&d"e e
 qhe%D ]Zee&d"e e	 qdKS )L    )S)Lambda)Pow   )PythonCodePrinter_known_functions_math_print_known_const_print_known_func_unpack_integral_limitsArrayPrinter)CodePrinterz!erf erfc factorial gamma loggammac                 C   s    g | ]\}}|t kr||fqS  )_not_in_numpy.0kvr   r   F/var/www/html/venv/lib/python3.8/site-packages/sympy/printing/numpy.py
<listcomp>	   s      r   ZarccosZarccoshZarcsinZarcsinhZarctanZarctan2Zarctanhexp2sign	logaddexp
logaddexp2isnan)acosacoshasinasinhatanatan2atanhr   r   r   r   r   epiZeuler_gammananinf)ZExp1PiZ
EulerGammaNaNInfinityc                 C   s   i | ]\}}|d | qS znumpy.r   r   r   r   r   
<dictcomp>    s      r)   c                 C   s   i | ]\}}|d | qS r(   r   r   r   r   r   r)   !   s      c                       sl  e Zd ZdZdZeZeZdN f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d Zdd Zd d! Zd"d# Zd$d% Zd&d' Z fd(d)Zd*d+ Zd,d- Zd.d/ ZdOd1d2Zd3d4 Z d5d6 Z!d7d8 Z"d9d: Z#d;d< Z$d=d> Z%d?d@ Z&dAdB Z'dCdD Z(dEdF Z) fdGdHZ*dIZ+dJZ,dKZ-dLZ.dMZ/e0j1Z2e0j1Z3e0j1Z4e0j1Z5  Z6S )PNumPyPrinterza
    Numpy printer which handles vectorized piecewise functions,
    logical operators, etc.
    numpyNc                    s<   d | j| _d | j| _tj| j| _t j|d dS )z
        `settings` is passed to CodePrinter.__init__()
        `module` specifies the array module to use, currently 'NumPy', 'CuPy'
        or 'JAX'.
        zPython with {}z_{}codesettingsN)format_modulelanguageprintmethodr   _kfsuper__init__selfr-   	__class__r   r   r4   -   s    zNumPyPrinter.__init__c                    s"   d}d | fdd|D S )z+General sequence printer: converts to tuple, z({},)c                 3   s   | ]}  |V  qd S N_print)r   itemr6   r   r   	<genexpr>@   s     z*NumPyPrinter._print_seq.<locals>.<genexpr>)r.   join)r6   seq	delimiterr   r>   r   
_print_seq;   s    zNumPyPrinter._print_seqc                 C   s   d|  tj S )N-)r<   r   r'   r6   exprr   r   r   _print_NegativeInfinityB   s    z$NumPyPrinter._print_NegativeInfinityc                    sj   |  d tjk	rJ|  d |  d g }dd fdd|D S dd fdd|jD S )zMatrix multiplication printerr   r   ({})z).dot(c                 3   s   | ]}  |V  qd S r:   r;   r   ir>   r   r   r?   I   s     z-NumPyPrinter._print_MatMul.<locals>.<genexpr>c                 3   s   | ]}  |V  qd S r:   r;   rI   r>   r   r   r?   J   s     )Zas_coeff_matricesr   ZOner.   r@   args)r6   rF   Z	expr_listr   r>   r   _print_MatMulE   s    zNumPyPrinter._print_MatMulc                 C   s2   d | | jd | |jd | |jd S )zMatrix power printerz
{}({}, {})z.linalg.matrix_powerr   r   r.   _module_formatr/   r<   rK   rE   r   r   r   _print_MatPowL   s     zNumPyPrinter._print_MatPowc                 C   s$   d | | jd | |jd S )zMatrix inverse printer{}({})z.linalg.invr   rM   rE   r   r   r   _print_InverseQ   s    zNumPyPrinter._print_Inversec                 C   sX   |j \}}|jd dkr|j}|jd dkr2|j}d| | jd | || |f S )Nr   r   
%s(%s, %s)z.dot)rK   shapeTrN   r/   r<   )r6   rF   Zarg1Zarg2r   r   r   _print_DotProductV   s    
zNumPyPrinter._print_DotProductc                 C   s*   d|  | jd | |j| |jf S )NrR   z.linalg.solve)rN   r/   r<   matrixZvectorrE   r   r   r   _print_MatrixSolvec   s    

zNumPyPrinter._print_MatrixSolvec                 C   s    d | | jd | |jS )NrP   z.zerosr.   rN   r/   r<   rS   rE   r   r   r   _print_ZeroMatrixh   s    
zNumPyPrinter._print_ZeroMatrixc                 C   s    d | | jd | |jS )NrP   z.onesrX   rE   r   r   r   _print_OneMatrixl   s    
zNumPyPrinter._print_OneMatrixc                    s~   ddl m}m} |j}t|ts4t||f|||}d  jd d	 fdd|j
d D  |j
d  |jS )	Nr   )rJ   jz{}(lambda {}: {}, {})z.fromfunctionr9   c                 3   s   | ]}  |V  qd S r:   r;   r   argr>   r   r   r?   v   s     z5NumPyPrinter._print_FunctionMatrix.<locals>.<genexpr>r   )Z	sympy.abcrJ   r[   lamda
isinstancer   r.   rN   r/   r@   rK   r<   rS   )r6   rF   rJ   r[   r^   r   r>   r   _print_FunctionMatrixp   s    
 
z"NumPyPrinter._print_FunctionMatrixc                    sZ    jd  d fdd|jd d D d|jd dt|jd   S )	N	.multiply c                 3   s    | ]}d   |V  qdS z{}({}, Nr.   r<   r\   funcr6   r   r   r?   {   s   z6NumPyPrinter._print_HadamardProduct.<locals>.<genexpr>{}{})r   rN   r/   r@   rK   r.   r<   lenrE   r   re   r   _print_HadamardProducty   s    z#NumPyPrinter._print_HadamardProductc                    sZ    jd  d fdd|jd d D d|jd dt|jd   S )	Nz.kronrb   c                 3   s    | ]}d   |V  qdS rc   rd   r\   re   r   r   r?      s   z7NumPyPrinter._print_KroneckerProduct.<locals>.<genexpr>rg   rh   ri   r   rj   rE   r   re   r   _print_KroneckerProduct   s    z$NumPyPrinter._print_KroneckerProductc                 C   s2   d | | jd | | jd | |jd S )Nz
{}({}({}))z
.conjugatez
.transposer   rM   rE   r   r   r   _print_Adjoint   s
    zNumPyPrinter._print_Adjointc                 C   s8   d | | jd | |j}d | | jd |S )NrP   z.diagz{}({}, (-1, 1))z.reshape)r.   rN   r/   r<   r]   )r6   rF   Zvectr   r   r   _print_DiagonalOf   s    
 zNumPyPrinter._print_DiagonalOfc                 C   s$   d | | jd | |jd S )NrP   z	.diagflatr   rM   rE   r   r   r   _print_DiagMatrix   s    zNumPyPrinter._print_DiagMatrixc              
   C   sJ   d | | jd | |j| | jd | |jd | |jd S )Nz{}({}, {}({}, {}))ra   .eyer   r   )r.   rN   r/   r<   r]   rS   rE   r   r   r   _print_DiagonalMatrix   s    
  z"NumPyPrinter._print_DiagonalMatrixc                    s   ddl m m  fddddfdd|jD }ddfd	d|jD }d
jd ||t	j
S )zPiecewise function printerr   )ITEsimplify_logicc                    s&   |   r| S | S dS )z$ Problem having an ITE in the cond. N)hasr<   Zcond)rs   r6   rt   r   r   
print_cond   s    
z1NumPyPrinter._print_Piecewise.<locals>.print_condz[{}],c                 3   s   | ]}  |jV  qd S r:   )r<   rF   r\   r>   r   r   r?      s     z0NumPyPrinter._print_Piecewise.<locals>.<genexpr>c                 3   s   | ]} |j V  qd S r:   rv   r\   )rw   r   r   r?      s     z{}({}, {}, default={})z.select)Zsympy.logic.boolalgrs   rt   r.   r@   rK   rN   r/   r<   r   r&   )r6   rF   ZexprsZcondsr   )rs   rw   r6   rt   r   _print_Piecewise   s        
zNumPyPrinter._print_Piecewisec                    sf   ddddddd}|j |krZ| |j}| |j}dj| | jd	 ||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{op}({lhs}, {rhs}).)oplhsrhs)	Zrel_opr<   r   r   r.   rN   r/   r3   _print_Relational)r6   rF   r   r   r   r7   r   r   r      s    
 zNumPyPrinter._print_Relationalc                    s.   d   jd d fdd|jD S )Logical And printer{}.reduce(({}))z.logical_andrx   c                 3   s   | ]}  |V  qd S r:   r;   rI   r>   r   r   r?      s     z*NumPyPrinter._print_And.<locals>.<genexpr>r.   rN   r/   r@   rK   rE   r   r>   r   
_print_And   s    zNumPyPrinter._print_Andc                    s.   d   jd d fdd|jD S )Logical Or printerr   z.logical_orrx   c                 3   s   | ]}  |V  qd S r:   r;   rI   r>   r   r   r?      s     z)NumPyPrinter._print_Or.<locals>.<genexpr>r   rE   r   r>   r   	_print_Or   s    zNumPyPrinter._print_Orc                    s.   d   jd d fdd|jD S )zLogical Not printerrP   z.logical_notrx   c                 3   s   | ]}  |V  qd S r:   r;   rI   r>   r   r   r?      s     z*NumPyPrinter._print_Not.<locals>.<genexpr>r   rE   r   r>   r   
_print_Not   s    zNumPyPrinter._print_NotFc                 C   s<   |j jr&|j jr&t|j|j  dd}| j||| jd dS )NF)evaluatez.sqrt)rationalsqrt)exp
is_integerZis_negativer   baseZevalfZ_hprint_Powr/   )r6   rF   r   r   r   r   
_print_Pow   s    zNumPyPrinter._print_Powc              	      s8   d   jd   jd fdd|jD S )N{}({}.asarray([{}]), axis=0)z.aminrx   c                 3   s   | ]}  |V  qd S r:   r;   rI   r>   r   r   r?      s     z*NumPyPrinter._print_Min.<locals>.<genexpr>r   rE   r   r>   r   
_print_Min   s    zNumPyPrinter._print_Minc              	      s8   d   jd   jd fdd|jD S )Nr   z.amaxrx   c                 3   s   | ]}  |V  qd S r:   r;   rI   r>   r   r   r?      s     z*NumPyPrinter._print_Max.<locals>.<genexpr>r   rE   r   r>   r   
_print_Max   s    zNumPyPrinter._print_Maxc                 C   s$   d|  | jd | |jd f S )N%s(%s)z.angler   rN   r/   r<   rK   rE   r   r   r   
_print_arg   s    zNumPyPrinter._print_argc                 C   s$   d|  | jd | |jd f S )Nr   z.imagr   r   rE   r   r   r   	_print_im   s    zNumPyPrinter._print_imc                    s.   d   jd d fdd|jD f S )Nr   z.modr9   c                 3   s   | ]}  |V  qd S r:   r;   r\   r>   r   r   r?      s     z*NumPyPrinter._print_Mod.<locals>.<genexpr>)rN   r/   r@   rK   rE   r   r>   r   
_print_Mod   s    zNumPyPrinter._print_Modc                 C   s$   d|  | jd | |jd f S )Nr   z.realr   r   rE   r   r   r   	_print_re   s    zNumPyPrinter._print_rec                 C   s*   d|  | jd | |jd tj f S )Nr   z.sincr   )rN   r/   r<   rK   r   r%   rE   r   r   r   _print_sinc   s    zNumPyPrinter._print_sincc                 C   s@   | j |jjd }|d kr*| | jd }d|| | f S )Nz.arrayr   )Zknown_functionsgetr8   __name__rN   r/   r<   tolist)r6   rF   rf   r   r   r   _print_MatrixBase   s    zNumPyPrinter._print_MatrixBasec                 C   sH   |j }tdd |D r<d| | jd | |j d f S tdd S )Nc                 s   s   | ]}|j V  qd S r:   )Z
is_Integer)r   dimr   r   r   r?      s     z/NumPyPrinter._print_Identity.<locals>.<genexpr>r   rq   r   zFSymbolic matrix dimensions are not yet supported for identity matrices)rS   allrN   r/   r<   NotImplementedError)r6   rF   rS   r   r   r   _print_Identity   s    $zNumPyPrinter._print_Identityc                 C   s(   d | | jd | |jd  S )NrP   z.blockr   )r.   rN   r/   r<   rK   r   rE   r   r   r   _print_BlockMatrix   s    zNumPyPrinter._print_BlockMatrixc                    sV   t |jdkr,| jd | |jd  d S t |jdkrH| | S t | |S )Nr   z.array(r   ri      )rk   rS   r/   r<   rK   Ztomatrixr3   _print_not_supportedrE   r7   r   r   _print_NDimArray   s
    zNumPyPrinter._print_NDimArrayaddZeinsumZ	transposeZonesZzeros)N)F)7r   
__module____qualname____doc__r/   _numpy_known_functionsr2   _numpy_known_constants_kcr4   rC   rG   rL   rO   rQ   rU   rW   rY   rZ   r`   rl   rm   rn   ro   rp   rr   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   _addZ_einsumZ
_transposeZ_onesZ_zerosr   r   _print_lowergamma_print_uppergamma_print_fresnelc_print_fresnels__classcell__r   r   r7   r   r*   #   s^   	
r*   Z_print_ZexpierferfcZjvZyvZivkvcosm1powm1	factorialgammaZgammalnpsi	polygammaZpochZeval_jacobiZeval_gegenbauerZeval_chebytZeval_chebyuZeval_legendreZeval_hermiteZeval_laguerreZeval_genlaguerrebetaZlambertw)ZEir   r   ZbesseljZbesselyZbesseliZbesselkr   r   r   r   ZloggammaZdigammar   ZRisingFactorialZjacobiZ
gegenbauerZ
chebyshevtZ
chebyshevuZlegendreZhermiteZlaguerreZassoc_laguerrer   ZLambertWZgolden_ratio)ZGoldenRatior%   c                 C   s   i | ]\}}|d | qS )zscipy.special.r   r   r   r   r   r)   :  s      c                 C   s   i | ]\}}|d | qS )zscipy.constants.r   r   r   r   r   r)   ;  s      c                       s   e Zd ZejeZejeZd& fdd	Zdd Z	e	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d Zd d! Zd"d# Zd$d% Z  ZS )'SciPyPrinterNc                    s   t  j|d d| _d S )Nr,   zPython with SciPy and NumPy)r3   r4   r0   r5   r7   r   r   r4   B  s    zSciPyPrinter.__init__c                 C   sd   g g g   }}}|   D ]*\\}}}|| || || qdj| d||||jdS )Nz+{name}(({data}, ({i}, {j})), shape={shape})zscipy.sparse.coo_matrix)namedatarJ   r[   rS   )Ztodokitemsappendr.   rN   rS   )r6   rF   rJ   r[   r   rcr   r   r   r   _print_SparseRepMatrixF  s    

   z#SciPyPrinter._print_SparseRepMatrixc              	   C   s:   d | d| |jd | |jd | |jd S )Nz{0}({2}, {1}, {3})zscipy.special.lpmvr   r   r   r.   rN   r<   rK   rE   r   r   r   _print_assoc_legendreU  s    z"SciPyPrinter._print_assoc_legendrec              	   C   s4   d | d| d| |jd | |jd S )N{0}({2})*{1}({2}, {3})scipy.special.gammazscipy.special.gammaincr   r   r   rE   r   r   r   r   \  s    zSciPyPrinter._print_lowergammac              	   C   s4   d | d| d| |jd | |jd S )Nr   r   zscipy.special.gammainccr   r   r   rE   r   r   r   r   c  s    zSciPyPrinter._print_uppergammac                    s     d}  d} fdd|jD }d| d|d  d|d  d|d	  d
| d|d  d|d  d|d  d| d|d  d|d  dS )Nscipy.special.betainczscipy.special.betac                    s   g | ]}  |qS r   r;   r\   r>   r   r   r   m  s     z/SciPyPrinter._print_betainc.<locals>.<listcomp>(r   r9   r      z) - r   z))             * ri   )rN   rK   )r6   rF   Zbetaincr   rK   r   r>   r   _print_betaincj  s    

JzSciPyPrinter._print_betaincc              
   C   sH   d | d| |jd | |jd | |jd | |jd S )Nz'{0}({1}, {2}, {4}) - {0}({1}, {2}, {3})r   r   r   r   r   r   rE   r   r   r   _print_betainc_regularizedq  s    z'SciPyPrinter._print_betainc_regularizedc                 C   s   d | d| |jd S )N	{}({})[0]scipy.special.fresnelr   r   rE   r   r   r   r   y  s    zSciPyPrinter._print_fresnelsc                 C   s   d | d| |jd S )N	{}({})[1]r   r   r   rE   r   r   r   r   ~  s    zSciPyPrinter._print_fresnelcc                 C   s   d | d| |jd S )Nr   scipy.special.airyr   r   rE   r   r   r   _print_airyai  s    zSciPyPrinter._print_airyaic                 C   s   d | d| |jd S )Nr   r   r   r   rE   r   r   r   _print_airyaiprime  s    zSciPyPrinter._print_airyaiprimec                 C   s   d | d| |jd S )Nz	{}({})[2]r   r   r   rE   r   r   r   _print_airybi  s    zSciPyPrinter._print_airybic                 C   s   d | d| |jd S )Nz	{}({})[3]r   r   r   rE   r   r   r   _print_airybiprime  s    zSciPyPrinter._print_airybiprimec                 C   s   |  |j|j S r:   r<   Z_eval_rewrite_as_zetarK   rE   r   r   r   _print_bernoulli  s    zSciPyPrinter._print_bernoullic                 C   s   |  |j|j S r:   r   rE   r   r   r   _print_harmonic  s    zSciPyPrinter._print_harmonicc                    s   t |\}}t|dkr< d}dtt j|d  }n( d}dd fdd	|D }d
|dt j| |jd |S )Nr   zscipy.integrate.quadz%s, %sr   zscipy.integrate.nquadrH   r9   c                 3   s"   | ]}d t t j| V  qdS )z(%s, %s)N)tuplemapr<   )r   lr>   r   r   r?     s    z/SciPyPrinter._print_Integral.<locals>.<genexpr>z{}(lambda {}: {}, {})[0])	r
   rk   rN   r   r   r<   r.   r@   rK   )r6   r!   Zintegration_varsZlimitsZ
module_strZ	limit_strr   r>   r   _print_Integral  s    


zSciPyPrinter._print_Integralc                 C   s   d | d| |jd S )Nr   scipy.special.sicir   r   rE   r   r   r   	_print_Si  s    zSciPyPrinter._print_Sic                 C   s   d | d| |jd S )Nr   r   r   r   rE   r   r   r   	_print_Ci  s    zSciPyPrinter._print_Ci)N)r   r   r   r*   r2   _scipy_known_functionsr   _scipy_known_constantsr4   r   Z_print_ImmutableSparseMatrixr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r   r   =  s*   

r   c                 C   s   i | ]\}}|d | qS zcupy.r   r   r   r   r   r)     s      c                 C   s   i | ]\}}|d | qS r   r   r   r   r   r   r)     s      c                       s.   e Zd ZdZdZeZeZd fdd	Z	  Z
S )CuPyPrinterz`
    CuPy printer which handles vectorized piecewise functions,
    logical operators, etc.
    ZcupyNc                    s   t  j|d d S )Nr,   )r3   r4   r5   r7   r   r   r4     s    zCuPyPrinter.__init__)N)r   r   r   r   r/   _cupy_known_functionsr2   _cupy_known_constantsr   r4   r   r   r   r7   r   r     s
   r   c                 C   s   i | ]\}}|d | qS z
jax.numpy.r   r   r   r   r   r)     s      c                 C   s   i | ]\}}|d | qS r   r   r   r   r   r   r)     s      c                       s>   e Zd ZdZdZeZeZd
 fdd	Z	dd Z
dd	 Z  ZS )
JaxPrinterz_
    JAX printer which handles vectorized piecewise functions,
    logical operators, etc.
    z	jax.numpyNc                    s   t  j|d d| _d S )Nr,   Z_jaxcode)r3   r4   r1   r5   r7   r   r   r4     s    zJaxPrinter.__init__c              	      s8   d   jd   jd fdd|jD S )r   r   z.allrx   c                 3   s   | ]}  |V  qd S r:   r;   rI   r>   r   r   r?     s     z(JaxPrinter._print_And.<locals>.<genexpr>r   rE   r   r>   r   r     s
    
zJaxPrinter._print_Andc              	      s8   d   jd   jd fdd|jD S )r   r   z.anyrx   c                 3   s   | ]}  |V  qd S r:   r;   rI   r>   r   r   r?     s     z'JaxPrinter._print_Or.<locals>.<genexpr>r   rE   r   r>   r   r     s
    
zJaxPrinter._print_Or)N)r   r   r   r   r/   _jax_known_functionsr2   _jax_known_constantsr   r4   r   r   r   r   r   r7   r   r     s   r   N)'Z
sympy.corer   Zsympy.core.functionr   Zsympy.core.powerr   Zpycoder   r   r   r	   r
   r   Zcodeprinterr   splitr   r   Z	_in_numpydictZ_known_functions_numpyZ_known_constants_numpyr   r   r*   rf   setattrconstZ_known_functions_scipy_specialZ _known_constants_scipy_constantsr   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s     q}