a
    <bFN                     @   s  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 qeD ]Zeed e e qd!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7Zd8dd9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 qheD ]Zeed e e qd>d e D Zd?d e D ZG d@dA dAeZeD ]Zeed e e qeD ]Zeed e e qdBS )C    )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
   d/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/printing/numpy.py
<listcomp>       r   ZarccosZarccoshZarcsinZarcsinhZarctanZarctan2Zarctanhexp2sign	logaddexp
logaddexp2)acosacoshasinasinhatanatan2atanhr   r   r   r   epiZeuler_gammananZPINFZNINF)ZExp1PiZ
EulerGammaNaNInfinityNegativeInfinityc                 C   s   i | ]\}}|d | qS znumpy.r
   r   r
   r
   r   
<dictcomp>   r   r&   c                 C   s   i | ]\}}|d | qS r%   r
   r   r
   r
   r   r&      r   c                       st  e Zd ZdZdZeZeZdQ 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dRd/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-dOdP Z.e/j0Z1e/j0Z2e/j0Z3e/j0Z4  Z5S )S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   s   | ]}  |V  qd S N_print)r   itemr1   r
   r   	<genexpr>=   r   z*NumPyPrinter._print_seq.<locals>.<genexpr>)r*   join)r1   seq	delimiterr
   r9   r   
_print_seq8   s    zNumPyPrinter._print_seqc                    sj   |  d tju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 r5   r6   r   ir9   r
   r   r:   C   r   z-NumPyPrinter._print_MatMul.<locals>.<genexpr>c                 3   s   | ]}  |V  qd S r5   r6   r@   r9   r
   r   r:   D   r   )Zas_coeff_matricesr   ZOner*   r;   args)r1   exprZ	expr_listr
   r9   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+   r7   rB   r1   rC   r
   r
   r   _print_MatPowF   s    zNumPyPrinter._print_MatPowc                 C   s$   d | | jd | |jd S )zMatrix inverse printer{}({})z.linalg.invr   rE   rG   r
   r
   r   _print_InverseK   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)rB   shapeTrF   r+   r7   )r1   rC   Zarg1Zarg2r
   r
   r   _print_DotProductP   s    
zNumPyPrinter._print_DotProductc                 C   s*   d|  | jd | |j| |jf S )NrK   z.linalg.solve)rF   r+   r7   ZmatrixZvectorrG   r
   r
   r   _print_MatrixSolve]   s    

zNumPyPrinter._print_MatrixSolvec                 C   s    d | | jd | |jS )NrI   z.zerosr*   rF   r+   r7   rL   rG   r
   r
   r   _print_ZeroMatrixb   s    
zNumPyPrinter._print_ZeroMatrixc                 C   s    d | | jd | |jS )NrI   z.onesrP   rG   r
   r
   r   _print_OneMatrixf   s    
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)rA   jz{}(lambda {}: {}, {})z.fromfunctionr4   c                 3   s   | ]}  |V  qd S r5   r6   r   argr9   r
   r   r:   q   r   z5NumPyPrinter._print_FunctionMatrix.<locals>.<genexpr>r   )Zsympy.core.functionrS   Z	sympy.abcrA   rT   lamda
isinstancer*   rF   r+   r;   rB   r7   rL   )r1   rC   rS   rA   rT   rW   r
   r9   r   _print_FunctionMatrixj   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*   r7   rU   funcr1   r
   r   r:   v   s   z6NumPyPrinter._print_HadamardProduct.<locals>.<genexpr>{}{})r   rF   r+   r;   rB   r*   r7   lenrG   r
   r^   r   _print_HadamardProductt   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.kronr[   c                 3   s    | ]}d   |V  qdS r\   r]   rU   r^   r
   r   r:   |   s   z7NumPyPrinter._print_KroneckerProduct.<locals>.<genexpr>r`   ra   rb   r   rc   rG   r
   r^   r   _print_KroneckerProductz   s    z$NumPyPrinter._print_KroneckerProductc                 C   s2   d | | jd | | jd | |jd S )Nz
{}({}({}))z
.conjugatez
.transposer   rE   rG   r
   r
   r   _print_Adjoint   s
    zNumPyPrinter._print_Adjointc                 C   s8   d | | jd | |j}d | | jd |S )NrI   z.diagz{}({}, (-1, 1))z.reshape)r*   rF   r+   r7   rV   )r1   rC   Zvectr
   r
   r   _print_DiagonalOf   s    
zNumPyPrinter._print_DiagonalOfc                 C   s$   d | | jd | |jd S )NrI   z	.diagflatr   rE   rG   r
   r
   r   _print_DiagMatrix   s    zNumPyPrinter._print_DiagMatrixc              
   C   sJ   d | | jd | |j| | jd | |jd | |jd S )Nz{}({}, {}({}, {}))rZ   .eyer   r   )r*   rF   r+   r7   rV   rL   rG   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)Zhasr7   Zcond)rl   r1   rm   r
   r   
print_cond   s    
z1NumPyPrinter._print_Piecewise.<locals>.print_condz[{}],c                 3   s   | ]}  |jV  qd S r5   )r7   rC   rU   r9   r
   r   r:      r   z0NumPyPrinter._print_Piecewise.<locals>.<genexpr>c                 3   s   | ]} |j V  qd S r5   rn   rU   )ro   r
   r   r:      r   z{}({}, {}, default={})z.select)Zsympy.logic.boolalgrl   rm   r*   r;   rB   rF   r+   r7   r   r"   )r1   rC   exprsZcondsr
   )rl   ro   r1   rm   r   _print_Piecewise   s      
zNumPyPrinter._print_Piecewisec                    sf   ddddddd}|j |v rZ| |j}| |j}dj| | jd	 ||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{op}({lhs}, {rhs}).)oplhsrhs)	Zrel_opr7   ry   rz   r*   rF   r+   r.   _print_Relational)r1   rC   rx   ry   rz   r2   r
   r   r{      s    
zNumPyPrinter._print_Relationalc                    s.   d   jd d fdd|jD S )zLogical And printer{}.reduce(({}))z.logical_andrp   c                 3   s   | ]}  |V  qd S r5   r6   r@   r9   r
   r   r:      r   z*NumPyPrinter._print_And.<locals>.<genexpr>r*   rF   r+   r;   rB   rG   r
   r9   r   
_print_And   s    zNumPyPrinter._print_Andc                    s.   d   jd d fdd|jD S )zLogical Or printerr|   z.logical_orrp   c                 3   s   | ]}  |V  qd S r5   r6   r@   r9   r
   r   r:      r   z)NumPyPrinter._print_Or.<locals>.<genexpr>r}   rG   r
   r9   r   	_print_Or   s    zNumPyPrinter._print_Orc                    s.   d   jd d fdd|jD S )zLogical Not printerrI   z.logical_notrp   c                 3   s   | ]}  |V  qd S r5   r6   r@   r9   r
   r   r:      r   z*NumPyPrinter._print_Not.<locals>.<genexpr>r}   rG   r
   r9   r   
_print_Not   s    zNumPyPrinter._print_NotFc                 C   sH   ddl m} |jjr2|jjr2||j|j dd}| j||| jd dS )Nr   )PowF)evaluatez.sqrt)rationalsqrt)	Zsympy.core.powerr   exp
is_integerZis_negativebaseZevalfZ_hprint_Powr+   )r1   rC   r   r   r
   r
   r   
_print_Pow   s    zNumPyPrinter._print_Powc                    s.   d   jd d fdd|jD S )N{}(({}), axis=0)z.aminrp   c                 3   s   | ]}  |V  qd S r5   r6   r@   r9   r
   r   r:      r   z*NumPyPrinter._print_Min.<locals>.<genexpr>r}   rG   r
   r9   r   
_print_Min   s    zNumPyPrinter._print_Minc                    s.   d   jd d fdd|jD S )Nr   z.amaxrp   c                 3   s   | ]}  |V  qd S r5   r6   r@   r9   r
   r   r:      r   z*NumPyPrinter._print_Max.<locals>.<genexpr>r}   rG   r
   r9   r   
_print_Max   s    zNumPyPrinter._print_Maxc                 C   s$   d|  | jd | |jd f S )N%s(%s)z.angler   rF   r+   r7   rB   rG   r
   r
   r   
_print_arg   s    zNumPyPrinter._print_argc                 C   s$   d|  | jd | |jd f S )Nr   z.imagr   r   rG   r
   r
   r   	_print_im   s    zNumPyPrinter._print_imc                    s.   d   jd dt fdd|jf S )Nr   z.modr4   c                    s
     | S r5   r6   )rV   r9   r
   r   <lambda>   r   z)NumPyPrinter._print_Mod.<locals>.<lambda>)rF   r+   r;   maprB   rG   r
   r9   r   
_print_Mod   s    zNumPyPrinter._print_Modc                 C   s$   d|  | jd | |jd f S )Nr   z.realr   r   rG   r
   r
   r   	_print_re   s    zNumPyPrinter._print_rec                 C   s*   d|  | jd | |jd tj f S )Nr   z.sincr   )rF   r+   r7   rB   r   r!   rG   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getr3   __name__rF   r+   r7   tolist)r1   rC   r_   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 r5   )Z
is_Integer)r   Zdimr
   r
   r   r:      r   z/NumPyPrinter._print_Identity.<locals>.<genexpr>r   rj   r   zFSymbolic matrix dimensions are not yet supported for identity matrices)rL   allrF   r+   r7   NotImplementedError)r1   rC   rL   r
   r
   r   _print_Identity   s    $zNumPyPrinter._print_Identityc                 C   s(   d | | jd | |jd  S )NrI   z.blockr   )r*   rF   r+   r7   rB   r   rG   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 | ]4\}}  |d d| d| d f fD ]}|q.qS )z[%i, %i]   r   r6   )r   rA   rV   rT   r9   r
   r   r      s   $z:NumPyPrinter._print_ArrayTensorProduct.<locals>.<listcomp>r   .einsumr4   )	enumeraterB   rF   r+   r;   )r1   rC   Z
array_listr
   r9   r   _print_ArrayTensorProduct   s    z&NumPyPrinter._print_ArrayTensorProductc                    s   ddl m} |j}|j}|s& |S t||rd}dd |D }g }|jD ]J}g }	t|D ].}
||v rx|	||  n
|	| |d7 }q\||	 qL fddt	|j
|D }d	  jd
 d|f S t d S )Nr   )ArrayTensorProductr   c                 S   s    i | ]}|D ]}|t |qqS r
   )min)r   rA   rT   r
   r
   r   r&   
  r   z8NumPyPrinter._print_ArrayContraction.<locals>.<dictcomp>r   c                    s"   g | ]\}}d   ||f qS )%s, %sr6   )r   rV   indr9   r
   r   r     r   z8NumPyPrinter._print_ArrayContraction.<locals>.<listcomp>r   r   r4   )Z*tensor.array.expressions.array_expressionsr   rC   contraction_indicesr7   rX   ZsubranksrangeappendziprB   rF   r+   r;   r   )r1   rC   r   r   r   counterdindicesZrank_argZlindicesrA   elemsr
   r9   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_indicesrd   r   rF   r7   rC   )r1   rC   r   r
   r
   r   _print_ArrayDiagonal  s    



z!NumPyPrinter._print_ArrayDiagonalc                 C   s&   d|  d| |j| |jjf S )NrK   znumpy.transpose)rF   r7   rC   ZpermutationZ
array_formrG   r
   r
   r   _print_PermuteDims-  s
    
zNumPyPrinter._print_PermuteDimsc                 C   s   |  | jd |jS )Nz.add)Z_expand_fold_binary_opr+   rB   rG   r
   r
   r   _print_ArrayAdd4  s    zNumPyPrinter._print_ArrayAddc                 C   sT   t |jdkr,| jd | |jd  d S t |jdkrH| | S t| |S )Nr   z.array(r   rb   r   )rd   rL   r+   r7   rB   Ztomatrixr	   _print_not_supportedrG   r
   r
   r   _print_NDimArray7  s
    zNumPyPrinter._print_NDimArray)N)F)6r   
__module____qualname____doc__r+   _numpy_known_functionsr-   _numpy_known_constants_kcr/   r>   rD   rH   rJ   rN   rO   rQ   rR   rY   re   rf   rg   rh   ri   rk   rr   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   _print_lowergamma_print_uppergamma_print_fresnelc_print_fresnels__classcell__r
   r
   r2   r   r'   !   s\   

r'   Z_print_erferfcZjvZyvZivkvcosm1	factorialgammaZgammalnpsiZpochZeval_jacobiZeval_gegenbauerZeval_chebytZeval_chebyuZeval_legendreZeval_hermiteZeval_laguerreZeval_genlaguerrebetaZlambertw)r   r   ZbesseljZbesselyZbesseliZbesselkr   r   r   ZloggammaZdigammaZ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&   h  r   c                 C   s   i | ]\}}|d | qS )zscipy.constants.r
   r   r
   r
   r   r&   i  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,   r0   r2   r
   r   r/   p  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datarA   rT   rL   )Ztodokitemsr   r*   rF   rL   )r1   rC   rA   rT   r   rcr   r
   r
   r   _print_SparseRepMatrixt  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*   rF   r7   rB   rG   r
   r
   r   _print_assoc_legendre  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   rG   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   rG   r
   r
   r   r     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
   r6   rU   r9   r
   r   r     r   z/SciPyPrinter._print_betainc.<locals>.<listcomp>(r   r4   r      z) - r   z))             * rb   )rF   rB   )r1   rC   Zbetaincr   rB   r
   r9   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   rG   r
   r
   r   _print_betainc_regularized  s    z'SciPyPrinter._print_betainc_regularizedc                 C   s   d | d| |jd S )N	{}({})[0]scipy.special.fresnelr   r   rG   r
   r
   r   r     s    zSciPyPrinter._print_fresnelsc                 C   s   d | d| |jd S )N	{}({})[1]r   r   r   rG   r
   r
   r   r     s    zSciPyPrinter._print_fresnelcc                 C   s   d | d| |jd S )Nr   scipy.special.airyr   r   rG   r
   r
   r   _print_airyai  s    zSciPyPrinter._print_airyaic                 C   s   d | d| |jd S )Nr   r   r   r   rG   r
   r
   r   _print_airyaiprime  s    zSciPyPrinter._print_airyaiprimec                 C   s   d | d| |jd S )Nz	{}({})[2]r   r   r   rG   r
   r
   r   _print_airybi  s    zSciPyPrinter._print_airybic                 C   s   d | d| |jd S )Nz	{}({})[3]r   r   r   rG   r
   r
   r   _print_airybiprime  s    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.nquadr?   r4   c                 3   s"   | ]}d t t j| V  qdS )z(%s, %s)N)tupler   r7   )r   lr9   r
   r   r:     s   z/SciPyPrinter._print_Integral.<locals>.<genexpr>z{}(lambda {}: {}, {})[0])	r   rd   rF   r   r   r7   r*   r;   rB   )r1   r   Zintegration_varsZlimitsZ
module_strZ	limit_strr
   r9   r   _print_Integral  s    


zSciPyPrinter._print_Integral)N)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
   r2   r   r   k  s"   r   c                 C   s   i | ]\}}|d | qS zcupy.r
   r   r
   r
   r   r&     r   c                 C   s   i | ]\}}|d | qS 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/   r0   r2   r
   r   r/     s    zCuPyPrinter.__init__)N)r   r   r   r   r+   _cupy_known_functionsr-   _cupy_known_constantsr   r/   r   r
   r
   r2   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