o
    8VaE3                     @   s  d dl mZ d dlmZ d dlmZ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mZmZmZ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 d d
l m!Z!m"Z" d dl#m$Z$m%Z%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2 d dl/m3Z3 d dl4m5Z5 d dl6m7Z7 ed\Z8Z9Z:e2dZ;dd Z<dd Z=dd Z>dd Z?dd Z@dd  ZAd!d" ZBd#d$ ZCd%d& ZDG d'd( d(eZEd)d* ZFd+d, ZGd-d. ZHd/d0 ZId1d2 ZJd3d4 ZKd5d6 ZLd7d8 ZMd9d: ZNd;d< ZOd=d> ZPd?S )@    )
Assignment)none)expm1log1p)cosm1)MatrixSolve)
ExprModsymbolsEqLeGtzooooRationalPow)pi)S)acosKroneckerDelta	Piecewisesignsqrt)AndOr)SparseMatrixMatrixSymbolIdentity)MpmathPrinterPythonCodePrinterpycodeSymPyPrinter)NumPyPrinterSciPyPrinter)raises)IndexedBase)skip)import_module)loggammazx y zpc                  C   s  t  } | jrJ | tt dksJ | ttddksJ | tttdks+J | tttdks7J | jr<J | tdksEJ | jddhiksOJ | tt	d	d d
ks]J | t
td
kshJ | jdddhikssJ | ttdks~J | ttddksJ | td	ttdfdtdkfdksJ | tdttdfdttdfdddksJ | ttdksJ | td dksJ | tttdksJ d S )Nzx**y   zx % 2zx and yzx or yzmath.piZmathr      math.sqrt(x)r   zmath.acos(x)zx = 2r      z/((1) if (x == 0) else (2) if (x > 6) else None)   FZevaluatez/((2) if (x <= 0) else (3) if (x > 0) else None)z((0.0 if x == 0 else math.copysign(1, x))r   r+   zp[0, 1]z(1 if x == y else 0))r   module_importsdoprintxyr	   r   r   r   r   r   r   r   r   r   r   r   r   r)   r   prntr r7   B/usr/lib/python3/dist-packages/sympy/printing/tests/test_pycode.pytest_PythonCodePrinter   s6   


r9   c                  C   sZ   dd l } tdd i}| jj}|dkr|jdksJ |dkr$|jdks$J ttdd  d S )	Nr   standardr*   python2r.   python3c                   S   s   t ddiS )Nr:   Zpython4)r   r7   r7   r7   r8   <lambda>A   s    z1test_PythonCodePrinter_standard.<locals>.<lambda>)sysr   version_infomajorr:   r$   
ValueError)r>   r6   Zpython_versionr7   r7   r8   test_PythonCodePrinter_standard7   s   rB   c                  C   s   t  } | ttdksJ | tdddksJ | tjdks$J | tjdks.J | tjdks8J | tj	dksBJ | tj
d	ksLJ | tjd
ksVJ | tjdks`J | ttdkskJ d S )Nzmpmath.sign(x)r+   r*   zmpmath.mpf(1)/mpmath.mpf(2)zmpmath.ez	mpmath.piz
mpmath.phizmpmath.eulerz
mpmath.nanz
mpmath.infzmpmath.ninfzmpmath.loggamma(x))r   r2   r   r3   r   r   Exp1PiGoldenRatio
EulerGammaNaNInfinityNegativeInfinityr(   )r)   r7   r7   r8   test_MpmathPrinterC   s   rJ   c               	   C   s  ddl m} m}m}m}m}m}m}m}m	}m
}	 ddlm}
m} t }|ttdks.J tddd}tddd}tdd	d
}tddd}||d dksQJ ||d
 dks\J |tddksgJ tddd	}tddd	}|t||dksJ |t||| dksJ ||dddksJ ||dddksJ ||dd
| |
|f|
| dksJ ||||dksJ ||||dksJ |||dksJ |||dksJ |||dksJ ||	|dksJ |td dks	J |td  d!ksJ tddd"d#}||d$ks&J |tjd%ks1J |tjd&ks<J |tjd'ksGJ |tjd(ksRJ |tjd)ks]J |tjd*kshJ d S )+Nr   )
Lambda
ZeroMatrix	OneMatrixFunctionMatrixHadamardProductKroneckerProductAdjoint
DiagonalOf
DiagMatrixDiagonalMatrix)abznumpy.sign(x)Ar*   BCr+      Dr.      znumpy.linalg.inv(A)znumpy.linalg.matrix_power(A, 5)znumpy.eye(3)r3   r4   znumpy.linalg.solve(A, x)znumpy.linalg.solve(A, x) + yznumpy.zeros((2, 3))znumpy.ones((2, 3))z.numpy.fromfunction(lambda a, b: a + b, (4, 5))znumpy.multiply(A, B)znumpy.kron(A, B)z#numpy.conjugate(numpy.transpose(A))z%numpy.reshape(numpy.diag(A), (-1, 1))znumpy.diagflat(C)z"numpy.multiply(D, numpy.eye(3, 4))z	x**(-1.0)z	x**(-2.0)Fr/   z	2**(-1.0)numpy.eznumpy.piznumpy.euler_gammaz	numpy.nanz
numpy.PINFz
numpy.NINF)sympyrK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   Z	sympy.abcrU   rV   r"   r2   r   r3   r   r   r   r   r   rC   rD   rF   rG   rH   rI   )rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r)   rW   rX   rY   r[   uvexprr7   r7   r8   test_NumPyPrinterR   sH   0rd   c                  C   s   t d} | s
td ddlm}m}m} |dt d td dt d }|t|d}|| dddg d	k s;J |d
dksCJ |td td }|t|d}|| ddd
g dk scJ |d
dkskJ d S )Nnumpyznumpy not installed.r   )lambdifyMinMaxg?r.   r+   g      ?)g      ?g      ?g      @r\   r*   r]   )r+   r   r+      @   )	r'   r&   r`   rf   rg   rh   r3   Zlinspaceall)re   rf   rg   rh   expr1funcr7   r7   r8   test_issue_18770   s    ""rn   c                  C   s   t  } tt}d| jvsJ | |dksJ d| jv sJ tdd | jD r*J tddddi}| |d	ks;J d
| jv sBJ | tjdksLJ | tj	dksVJ | tj
dks`J d S )Nre   znumpy.arccos(x)c                 s   s    | ]}| d V  qdS )ZscipyN)
startswith).0mr7   r7   r8   	<genexpr>   s    z$test_SciPyPrinter.<locals>.<genexpr>r*   rZ   r0   r.   z8scipy.sparse.coo_matrix(([3], ([0], [1])), shape=(2, 5))zscipy.sparsezscipy.constants.golden_ratiozscipy.constants.pir_   )r#   r   r3   r1   r2   anyr   r   rE   rD   rC   )r)   rc   Zsmatr7   r7   r8   test_SciPyPrinter   s   rt   c                     s<   t d\ tt fdd t  } | dv sJ d S )Nzif elsec                      s   t   ddS )NT)Zerror_on_reserved)r    r7   s1s2r7   r8   r=      s    z,test_pycode_reserved_words.<locals>.<lambda>)zelse_ + if_zif_ + else_)r
   r$   rA   r    )Zpy_strr7   ru   r8   test_pycode_reserved_words   s   rx   c                  C   s  t  } | jttdddksJ | jdtt dddksJ t ddi} | jttddd	ks2J | jdtt ddd
ksAJ t ddi} | jttdddksTJ | jdtt dddkscJ t } | jttdddkssJ | jttdddksJ t } | jttdddksJ | jttdddksJ t } | jttdddksJ | jttdddksJ t } | jttdddksJ | jttdddksJ d S )NF)Zrationalr,   r+   z1/math.sqrt(x)r:   r;   Tz
x**(1./2.)zx**(-1./2.)r<   zx**(1/2)z	x**(-1/2)zmpmath.sqrt(x)z x**(mpmath.mpf(1)/mpmath.mpf(2))znumpy.sqrt(x)zsympy.sqrt(x))r   Z
_print_Powr   r3   r   r"   r#   r!   r5   r7   r7   r8   	test_sqrt   s.   ry   c                  C   s   ddl m}  | t}t }||dksJ t }||dks"J t }||dks.J t }||dks:J t }||dksFJ d S )Nr   )fracznumpy.mod(x, 1)zx % 1zmpmath.frac(x)z+sympy.functions.elementary.integers.frac(x))	r`   rz   r3   r"   r2   r#   r   r   r!   )rz   rc   r6   r7   r7   r8   	test_frac   s   r{   c                   @   s   e Zd Zdd Zdd ZdS )CustomPrintedObjectc                 C      dS )Nre   r7   selfZprinterr7   r7   r8   
_numpycode      zCustomPrintedObject._numpycodec                 C   r}   )Nmpmathr7   r~   r7   r7   r8   _mpmathcode   r   zCustomPrintedObject._mpmathcodeN)__name__
__module____qualname__r   r   r7   r7   r7   r8   r|      s    r|   c                  C   s2   t  } t | dksJ t | dksJ d S )Nre   r   )r|   r"   r2   r   )objr7   r7   r8   test_printmethod   s   r   c                   C   s   t tdksJ d S )NNone)r    r   r7   r7   r7   r8   test_codegen_ast_nodes   s   r   c                  C   s0   t  } | tdksJ | t dksJ d S )Nzfloat('nan')zfloat('-inf'))r   r2   r   r   r5   r7   r7   r8   test_issue_14283   s   r   c                  C   s    t  } | tddksJ d S )Nr*   z(0, 1,))r"   Z
_print_seqrange)nr7   r7   r8   test_NumPyPrinter_print_seq   s   r   c                  C   s   ddl m} m} td}| |t}||t}t }||dks"J ||dks+J t }d||v s7J d||v s@J t }d||v sLJ d||v sUJ d S )Nr   )
lowergamma
uppergammarU   z3scipy.special.gamma(a)*scipy.special.gammainc(a, x)z4scipy.special.gamma(a)*scipy.special.gammaincc(a, x)Not supported)	r`   r   r   r
   r3   r#   r2   r"   r   )r   r   rU   rl   expr2r6   r7   r7   r8   test_issue_16535_16536   s   

r   c                     s  ddl m} m} | |t tdtf}| td |tt  tt tftdtf}| td t| td tdf t |dksDJ |dksMJ t	t
fdd t	t
 fd	d t |d
ksmJ |dksvJ t	t
fdd t	t
 fdd d S )Nr   )Integralexpr*   r+   z?scipy.integrate.quad(lambda x: numpy.exp(-x), 0, numpy.PINF)[0]zMscipy.integrate.nquad(lambda x, y: x**2*numpy.exp(x*y), ((-z, z), (0, z)))[0]c                      
     S Nr2   r7   
indefiniter6   r7   r8   r=        
 ztest_Integral.<locals>.<lambda>c                      r   r   r   r7   
evaluateatr6   r7   r8   r=     r   z6mpmath.quad(lambda x: mpmath.exp(-x), (0, mpmath.inf))z?mpmath.quad(lambda x, y: x**2*mpmath.exp(x*y), (-z, z), (0, z))c                      r   r   r   r7   r   r7   r8   r=   !  r   c                      r   r   r   r7   r   r7   r8   r=   "  r   )r`   r   r   r3   r   r4   zr#   r2   r$   NotImplementedErrorr   )r   r   ZsingleZdoubler7   )r   r   r6   r8   test_Integral  s   *r   c                  C   s   ddl m} m} | t}|t}t }||dksJ ||dks%J t }d||v s1J d||v s:J t }d||v sFJ d||v sOJ t }||dks[J ||dksdJ d S )Nr   )fresnelcfresnelszscipy.special.fresnel(x)[1]zscipy.special.fresnel(x)[0]r   zmpmath.fresnelc(x)zmpmath.fresnels(x))	r`   r   r   r3   r#   r2   r"   r   r   )r   r   rl   r   r6   r7   r7   r8   test_fresnel_integrals%  s   r   c                  C   s   ddl m}  | tt}t }||dksJ t }||dks#J t }||dks/J tddi}||dks>J t }||dksJJ d S )Nr   )betazscipy.special.beta(x, y)z-math.gamma(x)*math.gamma(y)/math.gamma(x + y)Zallow_unknown_functionsTzmpmath.beta(x, y))	r`   r   r3   r4   r#   r2   r"   r   r   )r   rc   r6   r7   r7   r8   	test_beta<  s   
r   c                  C      ddl m} m} | t}|t}t }||dksJ ||dks%J t }d||v s1J d||v s:J t }d||v sFJ d||v sOJ d S )Nr   )airyaiairybizscipy.special.airy(x)[0]zscipy.special.airy(x)[2]r   )r`   r   r   r3   r#   r2   r"   r   )r   r   rl   r   r6   r7   r7   r8   	test_airyP     r   c                  C   r   )Nr   )airyaiprimeairybiprimezscipy.special.airy(x)[1]zscipy.special.airy(x)[3]r   )r`   r   r   r3   r#   r2   r"   r   )r   r   rl   r   r6   r7   r7   r8   test_airy_primeb  r   r   c                  C   sL   t  } | ttdksJ | ttdksJ | ttdks$J d S )Nznumpy.expm1(x)znumpy.log1p(x)zscipy.special.cosm1(x))r#   r2   r   r3   r   r   r5   r7   r7   r8   !test_numerical_accuracy_functionsu  s   r   N)QZsympy.codegenr   Zsympy.codegen.astr   Zsympy.codegen.cfunctionsr   r   Zsympy.codegen.scipy_nodesr   Zsympy.codegen.matrix_nodesr   Z
sympy.corer   r	   r
   r   r   r   r   r   r   r   Zsympy.core.numbersr   Zsympy.core.singletonr   Zsympy.functionsr   r   r   r   r   Zsympy.logicr   r   Zsympy.matricesr   r   r   Zsympy.printing.pycoder   r   r    r!   Zsympy.printing.numpyr"   r#   Zsympy.testing.pytestr$   Zsympy.tensorr%   r&   Zsympy.externalr'   Z'sympy.functions.special.gamma_functionsr(   r3   r4   r   r)   r9   rB   rJ   rd   rn   rt   rx   ry   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r7   r7   r7   r8   <module>   sR    0.