o
    8VaK                     @   sX  d dl mZ ddl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i dd	d
dddddddddZdddddddZdd e D Zdd e D ZG dd deZeD ]Zeed e e q`eD ]Zeed e e qni d!d!d"d"d#d$d%d&d'd(d)d*d+d+d,d,d-d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCZdDddEZdFd e D ZdGd e D ZG dHdI dIeZeD ]Zeed e e qeD ]Zeed e e qdJd e D ZdKd e D ZG dLdM dMeZeD ]Zeed e e qeD ]Zeed e e qdNS )O    )S   )PythonCodePrinter_known_functions_math_print_known_const_print_known_func_unpack_integral_limits)CodePrinterz!erf erfc factorial gamma loggammac                 C   s    g | ]\}}|t vr||fqS  )_not_in_numpy.0kvr
   r
   6/usr/lib/python3/dist-packages/sympy/printing/numpy.py
<listcomp>        r   ZarccosZarccoshZarcsinZarcsinhZarctanZarctan2Zarctanhexp2sign	logaddexp
logaddexp2)ZacosZacoshZasinZasinhZatanZatan2Zatanhr   r   r   r   eZpiZeuler_gammananZPINFZNINF)ZExp1PiZ
EulerGammaNaNZInfinityZNegativeInfinityc                 C      i | ]	\}}|d | qS znumpy.r
   r   r
   r
   r   
<dictcomp>       r   c                 C   r   r   r
   r   r
   r
   r   r      r   c                       sl  e Zd ZdZdZeZeZdO 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 fd&d'Zd(d) Zd*d+ Zd,d- ZdPd/d0Zd1d2 Zd3d4 Z d5d6 Z!d7d8 Z"d9d: Z#d;d< Z$d=d> Z%d?d@ Z&dAdB Z'dCdD Z(dEdF Z)dGdH Z*dIdJ Z+dKdL Z,dMdN Z-e.j/Z0e.j/Z1e.j/Z2e.j/Z3  Z4S )QNumPyPrinterza
    Numpy printer which handles vectorized piecewise functions,
    logical operators, etc.
    ZnumpyNc                    s@   d | j| _d | j| _i tj| j| _t j|d dS )z
        `settings` is passed to CodePrinter.__init__()
        `module` specifies the array module to use, currently 'NumPy' or 'CuPy'
        zPython with {}z_{}codesettingsN)format_modulelanguageZprintmethodr   _kfsuper__init__selfr!   	__class__r
   r   r'   +   s   zNumPyPrinter.__init__c                    s"   d}d | fdd|D S )z+General sequence printer: converts to tuple, z({},)c                 3       | ]}  |V  qd S N_print)r   itemr)   r
   r   	<genexpr>=       z*NumPyPrinter._print_seq.<locals>.<genexpr>)r"   join)r)   seqZ	delimiterr
   r2   r   
_print_seq8   s   zNumPyPrinter._print_seqc                    sj   |  d tjur%|  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   r-   r.   r/   r   ir2   r
   r   r3   C   r4   z-NumPyPrinter._print_MatMul.<locals>.<genexpr>c                 3   r-   r.   r/   r9   r2   r
   r   r3   D   r4   )Zas_coeff_matricesr   ZOner"   r5   args)r)   exprZ	expr_listr
   r2   r   _print_MatMul?   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#   r0   r;   r)   r<   r
   r
   r   _print_MatPowF   s   zNumPyPrinter._print_MatPowc                 C   $   d | | jd | |jd S )zMatrix inverse printer{}({})z.linalg.invr   r>   r@   r
   r
   r   _print_InverseK   s   zNumPyPrinter._print_Inversec                 C   sX   |j \}}|jd dkr|j}|jd dkr|j}d| | jd | || |f S )Nr   r   
%s(%s, %s)z.dot)r;   shapeTr?   r#   r0   )r)   r<   Zarg1Zarg2r
   r
   r   _print_DotProductP   s   
zNumPyPrinter._print_DotProductc                 C   s*   d|  | jd | |j| |jf S )NrE   z.linalg.solve)r?   r#   r0   ZmatrixZvectorr@   r
   r
   r   _print_MatrixSolve]   s   

zNumPyPrinter._print_MatrixSolvec                 C       d | | jd | |jS )NrC   z.zerosr"   r?   r#   r0   rF   r@   r
   r
   r   _print_ZeroMatrixb      
zNumPyPrinter._print_ZeroMatrixc                 C   rJ   )NrC   z.onesrK   r@   r
   r
   r   _print_OneMatrixf   rM   zNumPyPrinter._print_OneMatrixc                    s   ddl m} ddlm}m} |j}t||s |||f|||}d  j	d d
 fdd|jd D  |jd	  |jS )
Nr   )Lambda)r:   jz{}(lambda {}: {}, {})z.fromfunctionr,   c                 3   r-   r.   r/   r   argr2   r
   r   r3   q   r4   z5NumPyPrinter._print_FunctionMatrix.<locals>.<genexpr>r   )Zsympy.core.functionrO   Z	sympy.abcr:   rP   lamda
isinstancer"   r?   r#   r5   r;   r0   rF   )r)   r<   rO   r:   rP   rS   r
   r2   r   _print_FunctionMatrixj   s   
z"NumPyPrinter._print_FunctionMatrixc                    Z    jd  d fdd|jd d D d|jd dt|jd   S )	N	.multiply c                 3   "    | ]}d   |V  qdS z{}({}, Nr"   r0   rQ   funcr)   r
   r   r3   v       z6NumPyPrinter._print_HadamardProduct.<locals>.<genexpr>{}{})r   r?   r#   r5   r;   r"   r0   lenr@   r
   r\   r   _print_HadamardProductt      z#NumPyPrinter._print_HadamardProductc                    rV   )	Nz.kronrX   c                 3   rY   rZ   r[   rQ   r\   r
   r   r3   |   r^   z7NumPyPrinter._print_KroneckerProduct.<locals>.<genexpr>r_   r`   ra   r   rb   r@   r
   r\   r   _print_KroneckerProductz   re   z$NumPyPrinter._print_KroneckerProductc                 C   s2   d | | jd | | jd | |jd S )Nz
{}({}({}))z
.conjugatez
.transposer   r>   r@   r
   r
   r   _print_Adjoint   s
   zNumPyPrinter._print_Adjointc                 C   s8   d | | jd | |j}d | | jd |S )NrC   z.diagz{}({}, (-1, 1))z.reshape)r"   r?   r#   r0   rR   )r)   r<   Zvectr
   r
   r   _print_DiagonalOf   s   
zNumPyPrinter._print_DiagonalOfc                 C   rB   )NrC   z	.diagflatr   r>   r@   r
   r
   r   _print_DiagMatrix   s   zNumPyPrinter._print_DiagMatrixc              
   C   sJ   d | | jd | |j| | jd | |jd | |jd S )Nz{}({}, {}({}, {}))rW   .eyer   r   )r"   r?   r#   r0   rR   rF   r@   r
   r
   r   _print_DiagonalMatrix   s   z"NumPyPrinter._print_DiagonalMatrixc                    sd   d d fdd|jD }d d fdd|jD }d   jd || tjS )zPiecewise function printerz[{}],c                 3       | ]	}  |jV  qd S r.   )r0   r<   rQ   r2   r
   r   r3          z0NumPyPrinter._print_Piecewise.<locals>.<genexpr>c                 3   rm   r.   )r0   ZcondrQ   r2   r
   r   r3      rn   z{}({}, {}, default={})z.select)r"   r5   r;   r?   r#   r0   r   r   )r)   r<   ZexprsZcondsr
   r2   r   _print_Piecewise   s     
zNumPyPrinter._print_Piecewisec                    sf   ddddddd}|j |v r-| |j}| |j}dj| | jd	 ||j   ||d
S t |S )z.Relational printer for Equality and UnequalityZequalZ	not_equalZlessZ
less_equalZgreaterZgreater_equal)z==z!=<z<=>z>=z{op}({lhs}, {rhs}).)oplhsrhs)	Zrel_opr0   rt   ru   r"   r?   r#   r&   _print_Relational)r)   r<   rs   rt   ru   r*   r
   r   rv      s   
zNumPyPrinter._print_Relationalc                    .   d   jd d fdd|jD S )zLogical And printer{}.reduce(({}))z.logical_andrl   c                 3   r-   r.   r/   r9   r2   r
   r   r3      r4   z*NumPyPrinter._print_And.<locals>.<genexpr>r"   r?   r#   r5   r;   r@   r
   r2   r   
_print_And      .zNumPyPrinter._print_Andc                    rw   )zLogical Or printerrx   z.logical_orrl   c                 3   r-   r.   r/   r9   r2   r
   r   r3      r4   z)NumPyPrinter._print_Or.<locals>.<genexpr>ry   r@   r
   r2   r   	_print_Or   r{   zNumPyPrinter._print_Orc                    rw   )zLogical Not printerrC   z.logical_notrl   c                 3   r-   r.   r/   r9   r2   r
   r   r3      r4   z*NumPyPrinter._print_Not.<locals>.<genexpr>ry   r@   r
   r2   r   
_print_Not   r{   zNumPyPrinter._print_NotFc                 C   sH   ddl m} |jjr|jjr||j|j dd}| j||| jd dS )Nr   )PowF)Zevaluatez.sqrt)rationalZsqrt)	Zsympy.core.powerr~   Zexp
is_integerZis_negativebaseZevalfZ_hprint_Powr#   )r)   r<   r   r~   r
   r
   r   
_print_Pow   s   zNumPyPrinter._print_Powc                    rw   )N{}(({}), axis=0)z.aminrl   c                 3   r-   r.   r/   r9   r2   r
   r   r3      r4   z*NumPyPrinter._print_Min.<locals>.<genexpr>ry   r@   r
   r2   r   
_print_Min      .zNumPyPrinter._print_Minc                    rw   )Nr   z.amaxrl   c                 3   r-   r.   r/   r9   r2   r
   r   r3      r4   z*NumPyPrinter._print_Max.<locals>.<genexpr>ry   r@   r
   r2   r   
_print_Max   r   zNumPyPrinter._print_Maxc                 C   $   d|  | jd | |jd f S )N%s(%s)z.angler   r?   r#   r0   r;   r@   r
   r
   r   
_print_arg      $zNumPyPrinter._print_argc                 C   r   )Nr   z.imagr   r   r@   r
   r
   r   	_print_im   r   zNumPyPrinter._print_imc                    s.   d   jd dt fdd|jf S )Nr   z.modr,   c                    s
     | S r.   r/   )rR   r2   r
   r   <lambda>   s   
 z)NumPyPrinter._print_Mod.<locals>.<lambda>)r?   r#   r5   mapr;   r@   r
   r2   r   
_print_Mod   s   zNumPyPrinter._print_Modc                 C   r   )Nr   z.realr   r   r@   r
   r
   r   	_print_re   r   zNumPyPrinter._print_rec                 C   s*   d|  | jd | |jd tj f S )Nr   z.sincr   )r?   r#   r0   r;   r   r   r@   r
   r
   r   _print_sinc   s   *zNumPyPrinter._print_sincc                 C   s@   | j |jjd }|d u r| | jd }d|| | f S )Nz.arrayr   )Zknown_functionsgetr+   __name__r?   r#   r0   tolist)r)   r<   r]   r
   r
   r   _print_MatrixBase   s   zNumPyPrinter._print_MatrixBasec                 C   sD   |j }tdd |D rd| | jd | |j d f S td)Nc                 S   s   g | ]}|j qS r
   )Z
is_Integer)r   Zdimr
   r
   r   r      s    z0NumPyPrinter._print_Identity.<locals>.<listcomp>r   rj   r   zFSymbolic matrix dimensions are not yet supported for identity matrices)rF   allr?   r#   r0   NotImplementedError)r)   r<   rF   r
   r
   r   _print_Identity   s   $zNumPyPrinter._print_Identityc                 C   s(   d | | jd | |jd  S )NrC   z.blockr   )r"   r?   r#   r0   r;   r   r@   r
   r
   r   _print_BlockMatrix   s   zNumPyPrinter._print_BlockMatrixc                    s6    fddt |jD }d  jd d|f S )Nc                    s<   g | ]\}}  |d d| d| d f fD ]}|qqS )z[%i, %i]   r   r/   )r   r:   rR   rP   r2   r
   r   r      s     z:NumPyPrinter._print_ArrayTensorProduct.<locals>.<listcomp>r   .einsumr,   )	enumerater;   r?   r#   r5   )r)   r<   Z
array_listr
   r2   r   _print_ArrayTensorProduct   s   z&NumPyPrinter._print_ArrayTensorProductc                    s   ddl m} |j}|j}|s |S t||rhd}dd |D }g }|jD ]%}g }	t|D ]}
||v r<|	||  n|	| |d7 }q.||	 q& fddt	|j
|D }d	  jd
 d|f S t )Nr   )ArrayTensorProductr   c                 S   s    i | ]}|D ]}|t |qqS r
   )min)r   r:   rP   r
   r
   r   r     r   z8NumPyPrinter._print_ArrayContraction.<locals>.<dictcomp>r   c                    s"   g | ]\}}d   ||f qS )%s, %sr/   )r   rR   Zindr2   r
   r   r     s   " z8NumPyPrinter._print_ArrayContraction.<locals>.<listcomp>r   r   r,   )Z*tensor.array.expressions.array_expressionsr   r<   contraction_indicesr0   rT   Zsubranksrangeappendzipr;   r?   r#   r5   r   )r)   r<   r   r   r   ZcounterdindicesZrank_argZlindicesr:   Zelemsr
   r2   r   _print_ArrayContraction   s.   




z$NumPyPrinter._print_ArrayContractionc                 C   s\   t |j}t|dkrtt|d dkrtd| d| |j|d d |d d f S )Nr   r   r   z%s(%s, 0, axis1=%s, axis2=%s)znumpy.diagonal)listdiagonal_indicesrc   r   r?   r0   r<   )r)   r<   r   r
   r
   r   _print_ArrayDiagonal  s   



z!NumPyPrinter._print_ArrayDiagonalc                 C   s&   d|  d| |j| |jjf S )NrE   znumpy.transpose)r?   r0   r<   ZpermutationZ
array_formr@   r
   r
   r   _print_PermuteDims&  s
   
zNumPyPrinter._print_PermuteDimsc                 C   s   |  | jd |jS )Nz.add)Z_expand_fold_binary_opr#   r;   r@   r
   r
   r   _print_ArrayAdd-  s   zNumPyPrinter._print_ArrayAddr.   )F)5r   
__module____qualname____doc__r#   _numpy_known_functionsr%   _numpy_known_constants_kcr'   r7   r=   rA   rD   rH   rI   rL   rN   rU   rd   rf   rg   rh   ri   rk   ro   rv   rz   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   Z_print_not_supported_print_lowergamma_print_uppergamma_print_fresnelc_print_fresnels__classcell__r
   r
   r*   r   r   !   s\    

r   Z_print_ZerfZerfcZbesseljZjvZbesselyZyvZbesseliZivZbesselkZkvZcosm1Z	factorialZgammaZloggammaZgammalnZdigammaZpsiZRisingFactorialZpochZjacobiZeval_jacobiZ
gegenbauerZeval_gegenbauerZ
chebyshevtZeval_chebytZ
chebyshevuZeval_chebyuZlegendreZeval_legendreZeval_hermiteZeval_laguerreZeval_genlaguerrebetaZlambertw)ZhermiteZlaguerreZassoc_laguerrer   ZLambertWZgolden_ratio)ZGoldenRatior   c                 C   r   )zscipy.special.r
   r   r
   r
   r   r   Y  r   c                 C   r   )zscipy.constants.r
   r   r
   r
   r   r   Z  r   c                       s   e Zd Zi ejeZi 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  ZS )SciPyPrinterNc                    s   t  j|d d| _d S )Nr    zPython with SciPy and NumPy)r&   r'   r$   r(   r*   r
   r   r'   a  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datar:   rP   rF   )Ztodokitemsr   r"   r?   rF   )r)   r<   r:   rP   r   rcr   r
   r
   r   _print_SparseMatrixe  s   


z SciPyPrinter._print_SparseMatrixc              	   C   s:   d | d| |jd | |jd | |jd S )Nz{0}({2}, {1}, {3})zscipy.special.lpmvr   r   r   r"   r?   r0   r;   r@   r
   r
   r   _print_assoc_legendret  s   z"SciPyPrinter._print_assoc_legendrec              	   C   4   d | d| d| |jd | |jd S )N{0}({2})*{1}({2}, {3})scipy.special.gammazscipy.special.gammaincr   r   r   r@   r
   r
   r   r   {     zSciPyPrinter._print_lowergammac              	   C   r   )Nr   r   zscipy.special.gammainccr   r   r   r@   r
   r
   r   r     r   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/   rQ   r2   r
   r   r     s    z/SciPyPrinter._print_betainc.<locals>.<listcomp>(r   r,   r      z) - r   z))             * ra   )r?   r;   )r)   r<   Zbetaincr   r;   r
   r2   r   _print_betainc  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   r@   r
   r
   r   _print_betainc_regularized  s   z'SciPyPrinter._print_betainc_regularizedc                 C      d | d| |jd S )N	{}({})[0]scipy.special.fresnelr   r   r@   r
   r
   r   r        zSciPyPrinter._print_fresnelsc                 C   r   )N	{}({})[1]r   r   r   r@   r
   r
   r   r     r   zSciPyPrinter._print_fresnelcc                 C   r   )Nr   scipy.special.airyr   r   r@   r
   r
   r   _print_airyai  r   zSciPyPrinter._print_airyaic                 C   r   )Nr   r   r   r   r@   r
   r
   r   _print_airyaiprime  r   zSciPyPrinter._print_airyaiprimec                 C   r   )Nz	{}({})[2]r   r   r   r@   r
   r
   r   _print_airybi  r   zSciPyPrinter._print_airybic                 C   r   )Nz	{}({})[3]r   r   r   r@   r
   r
   r   _print_airybiprime  r   zSciPyPrinter._print_airybiprimec                    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.quadr   r   zscipy.integrate.nquadr8   r,   c                 3   s$    | ]}d t t j| V  qdS )z(%s, %s)N)tupler   r0   )r   lr2   r
   r   r3     s    
z/SciPyPrinter._print_Integral.<locals>.<genexpr>z{}(lambda {}: {}, {})[0])	r   rc   r?   r   r   r0   r"   r5   r;   )r)   r   Zintegration_varsZlimitsZ
module_strZ	limit_strr
   r2   r   _print_Integral  s   


zSciPyPrinter._print_Integralr.   )r   r   r   r   r%   _scipy_known_functionsr   _scipy_known_constantsr'   r   Z_print_ImmutableSparseMatrixr   r   r   r   r   r   r   r   r   r   r   r   r   r
   r
   r*   r   r   \  s$    r   c                 C   r   zcupy.r
   r   r
   r
   r   r     r   c                 C   r   r   r
   r   r
   r
   r   r     r   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    )r&   r'   r(   r*   r
   r   r'     s   zCuPyPrinter.__init__r.   )r   r   r   r   r#   _cupy_known_functionsr%   _cupy_known_constantsr   r'   r   r
   r
   r*   r   r     s    r   N)Z
sympy.corer   Zpycoder   r   r   r   r   Zcodeprinterr	   splitr   r   Z	_in_numpydictZ_known_functions_numpyZ_known_constants_numpyr   r   r   r]   setattrconstZ_known_functions_scipy_specialZ _known_constants_scipy_constantsr   r   r   r   r   r   r
   r
   r
   r   <module>   s    
	  	
m