o
    *ήc>                     @   sN  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m Z 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/m0Z0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8m9Z9 d dl:m;Z;m<Z< d dl=m>Z>m?Z?m@Z@mAZAmBZB d dlCmDZD d dlEmFZF d dlGmHZH ed\ZIZJZKe;dZLdd ZMdd ZNdd ZOdd ZPdd  ZQd!d" ZRd#d$ ZSd%d& ZTd'd( ZUd)d* ZVG d+d, d,eZWd-d. ZXd/d0 ZYd1d2 ZZd3d4 Z[d5d6 Z\d7d8 Z]d9d: Z^d;d< Z_d=d> Z`d?d@ ZadAdB ZbdCdD ZcdES )F    )
Assignment)none)expm1log1p)cosm1)MatrixSolve)
ExprModsymbolsEqLeGtzooooRationalPow)pi)S)acosKroneckerDelta	PiecewisesignsqrtMinMaxcotacschaseccoth)AndOr)SparseMatrixMatrixSymbolIdentity)MpmathPrinterPythonCodePrinterpycodeSymPyPrinter)TensorflowPrinter)NumPyPrinterSciPyPrinter)raisesskip)IndexedBaseIdx)ArraySymbolArrayDiagonalArrayContraction	ZeroArrayOneArray)import_module)loggamma)parse_latexz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s9J | tttdksEJ | tttdksQJ | dtt  d	ks^J | jrcJ | td
kslJ | jddhiksvJ | tt	dd dksJ | t
tdksJ | jdddhiksJ | ttdksJ | ttdksJ | ttdksJ | ttdksJ | ttdksJ | 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sJ | ttdksJ | td dks#J | tttdks0J | d d!ks:J | ddgd"ksFJ | tttd#ksSJ | tttd$ks`J d S )%Nzx**y   zx % 2z-(x % y)z(-x) % yzx and yzx or y   z	1/(x + y)zmath.pimathr   math.sqrt(x)r   zmath.acos(x)z1/math.tan(x)z9(math.exp(x) + math.exp(-x))/(math.exp(x) - math.exp(-x))zmath.acos(1/x)z&math.log(math.sqrt(1 + x**(-2)) + 1/x)zx = 2r      z/((1) if (x == 0) else (2) if (x > 6) else None)   Fevaluatez/((2) if (x <= 0) else (3) if (x > 0) else None)z((0.0 if x == 0 else math.copysign(1, x))r   r9   zp[0, 1]z(1 if x == y else 0)r8   r=   z(2, 3)z[2, 3]z	min(x, y)z	max(x, y))r%   module_importsdoprintxyr	   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r   r   r   prntr rH   G/tmp/pip-target-vg8gfxp4/lib/python/sympy/printing/tests/test_pycode.pytest_PythonCodePrinter   sL   



rJ   c                  C   s&   t  } | jdks
J ttdd  d S )Npython3c                   S   s   t ddiS )Nstandardpython4)r%   rH   rH   rH   rI   <lambda>N   s    z1test_PythonCodePrinter_standard.<locals>.<lambda>)r%   rL   r+   
ValueErrorrF   rH   rH   rI   test_PythonCodePrinter_standardI   s   rP   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)r9   r8   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$   rC   r   rD   r   r   Exp1PiGoldenRatio
EulerGammaNaNInfinityNegativeInfinityr5   )r7   rH   rH   rI   test_MpmathPrinterQ   s   rX   c               	   C   s  ddl m}  ddlm} ddlm}m}m} ddlm	} ddl
m} ddlm} ddlm}m}	 dd	lm}
m} t }|ttd
ksFJ tddd}tddd}tddd}tddd}||d dksiJ ||d dkstJ |tddksJ tddd}tddd}|t||dksJ |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sJ |||d$ksJ |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sKJ |tjd,ksVJ |tjd-ksaJ |tj d.kslJ |tj!d/kswJ |tj"d0ksJ d S )1Nr   )Lambda)Adjoint)
DiagMatrixDiagonalMatrix
DiagonalOf)FunctionMatrix)HadamardProduct)KroneckerProduct)	OneMatrix
ZeroMatrix)abznumpy.sign(x)Ar8   BCr9      Dr=      znumpy.linalg.inv(A)znumpy.linalg.matrix_power(A, 5)znumpy.eye(3)rD   rE   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)#sympy.core.functionrY   "sympy.matrices.expressions.adjointrZ   #sympy.matrices.expressions.diagonalr[   r\   r]   %sympy.matrices.expressions.funcmatrixr^   #sympy.matrices.expressions.hadamardr_   $sympy.matrices.expressions.kroneckerr`   "sympy.matrices.expressions.specialra   rb   	sympy.abcrc   rd   r)   rC   r   rD   r"   r#   r   r   r   rQ   rR   rT   rU   rV   rW   )rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   r7   re   rf   rg   ri   uvexprrH   rH   rI   test_NumPyPrinter`   sT   ry   c                  C   s   t d} | s
td ddlm}m} ddlm} |dt d td d	t d }|t|d}|| dddg d
k	 s?J |ddksGJ |td td }|t|d}|| dddg dk	 sgJ |ddksoJ d S )Nnumpyznumpy not installed.r   )r   r   )lambdifyg?r=   r9   g      ?)g      ?g      ?g      @rj   r8   rk   )r9   r   r9      @   )
r4   r,   (sympy.functions.elementary.miscellaneousr   r   sympy.utilities.lambdifyr{   rD   linspaceall)rz   r   r   r{   expr1funcrH   rH   rI   test_issue_18770   s    ""r   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 )Nrz   znumpy.arccos(x)c                 s   s    | ]}| d V  qdS )scipyN)
startswith).0mrH   rH   rI   	<genexpr>   s    z$test_SciPyPrinter.<locals>.<genexpr>r8   rh   r@   r=   z8scipy.sparse.coo_matrix(([3], ([0], [1])), shape=(2, 5))zscipy.sparsezscipy.constants.golden_ratiozscipy.constants.pirm   )r*   r   rD   rB   rC   anyr!   r   rS   rR   rQ   )r7   rx   smatrH   rH   rI   test_SciPyPrinter   s   r   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)error_on_reserved)r&   rH   s1s2rH   rI   rN      s    z,test_pycode_reserved_words.<locals>.<lambda>)zelse_ + if_zif_ + else_)r
   r+   rO   r&   )py_strrH   r   rI   test_pycode_reserved_words   s   r   c                  C   sH   t d} | s
td td}t|dksJ td}t|dks"J d S )Nantlr4zantlr not installed.za_b \cdot bza_b*bza_{11} \cdot bza_11*b)r4   r,   r6   r&   )r   rx   rH   rH   rI   test_issue_20762   s   r   c                  C   sn  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 } | jttdddksQJ | jttdddks^J t } | jttdddksnJ | jttddd	ks{J 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)rationalr;   r9   z1/math.sqrt(x)rL   rK   Tz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%   
_print_Powr   rD   r$   r)   r*   r'   rF   rH   rH   rI   	test_sqrt   s(   r   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))	#sympy.functions.elementary.integersr   rD   r)   rC   r*   r%   r$   r'   )r   rx   rG   rH   rH   rI   	test_frac   s   r   c                   @   s   e Zd Zdd Zdd ZdS )CustomPrintedObjectc                 C      dS )Nrz   rH   selfprinterrH   rH   rI   
_numpycode      zCustomPrintedObject._numpycodec                 C   r   )NmpmathrH   r   rH   rH   rI   _mpmathcode   r   zCustomPrintedObject._mpmathcodeN)__name__
__module____qualname__r   r   rH   rH   rH   rI   r      s    r   c                  C   s2   t  } t | dksJ t | dksJ d S )Nrz   r   )r   r)   rC   r$   )objrH   rH   rI   test_printmethod   s   r   c                   C   s   t tdksJ d S )NNone)r&   r   rH   rH   rH   rI   test_codegen_ast_nodes  s   r   c                  C   s0   t  } | tdksJ | t dksJ d S )Nzmath.nanzfloat('-inf'))r%   rC   r   r   rF   rH   rH   rI   test_issue_14283	  s   r   c                  C   s    t  } | tddksJ d S )Nr8   z(0, 1,))r)   
_print_seqrange)nrH   rH   rI   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
uppergammarc   z3scipy.special.gamma(a)*scipy.special.gammainc(a, x)z4scipy.special.gamma(a)*scipy.special.gammaincc(a, x)Not supported)	'sympy.functions.special.gamma_functionsr   r   r
   rD   r*   rC   r)   r%   )r   r   rc   r   expr2rG   rH   rH   rI   test_issue_16535_16536  s   

r   c                     s   ddl m}  ddl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sHJ 	|dksQJ t
tfdd	 t
t fd
d	 t 	|dksqJ 	|dkszJ t
tfdd	 t
t fdd	 d S )Nr   )exp)Integralr8   r9   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rC   rH   
indefiniterG   rH   rI   rN   6     
 ztest_Integral.<locals>.<lambda>c                      r   r   r   rH   
evaluateatrG   rH   rI   rN   7  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   rH   r   rH   rI   rN   <  r   c                      r   r   r   rH   r   rH   rI   rN   =  r   )&sympy.functions.elementary.exponentialr   sympy.integrals.integralsr   rD   r   rE   zr*   rC   r+   NotImplementedErrorr$   )r   r   singledoublerH   )r   r   rG   rI   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))	'sympy.functions.special.error_functionsr   r   rD   r*   rC   r)   r%   r$   )r   r   r   r   rG   rH   rH   rI   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)allow_unknown_functionsTzmpmath.beta(x, y))	&sympy.functions.special.beta_functionsr   rD   rE   r*   rC   r)   r%   r$   )r   rx   rG   rH   rH   rI   	test_betaW  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   )sympy.functions.special.besselr   r   rD   r*   rC   r)   r%   )r   r   r   r   rG   rH   rH   rI   	test_airyk     r   c                  C   r   )Nr   )airyaiprimeairybiprimezscipy.special.airy(x)[1]zscipy.special.airy(x)[3]r   )r   r   r   rD   r*   rC   r)   r%   )r   r   r   r   rG   rH   rH   rI   test_airy_prime}  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*   rC   r   rD   r   r   rF   rH   rH   rI   !test_numerical_accuracy_functions  s   r   c                  C   s
  t dd} td}tddtddtdd	}}}t }|td
dks(J |td
dks3J |t| ddgdksAJ ||dksJJ |t| g ddksXJ |t| ddgddgdksiJ |t| dgdgdksxJ |t	||||f ||||f dksJ t
 }|td
dksJ |td
dksJ |t| ddgdksJ ||dksJ |t| g ddksJ |t| ddgddgdksJ |t| dgdgdksJ |t	||||f ||||f dksJ d S )Nre   )rj   rj   r<   r<   r<   Iir@   jrA   k)rj   rh   rh   znumpy.zeros((5,))znumpy.ones((5,))r8   r=   znumpy.einsum("abccd->abd", A))r8   r=   rj   znumpy.einsum("abccc->abc", A)r   r9   znumpy.einsum("aabbc->cab", A)znumpy.einsum("abcde->abe", A)zI = Iztensorflow.zeros((5,))ztensorflow.ones((5,))z)tensorflow.linalg.einsum("abccd->abd", A)z)tensorflow.linalg.einsum("abccc->abc", A)z)tensorflow.linalg.einsum("aabbc->cab", A)z)tensorflow.linalg.einsum("abcde->abe", A))r/   r-   r.   r)   rC   r2   r3   r1   r0   r   r(   )re   r   r   r   r   rG   rH   rH   rI   test_array_printer  s*   
"","2r   N)dsympy.codegenr   sympy.codegen.astr   sympy.codegen.cfunctionsr   r   sympy.codegen.scipy_nodesr   sympy.codegen.matrix_nodesr   
sympy.corer   r	   r
   r   r   r   r   r   r   r   sympy.core.numbersr   sympy.core.singletonr   sympy.functionsr   r   r   r   r   r   r   r   r   r   r   sympy.logicr   r    sympy.matricesr!   r"   r#   sympy.printing.pycoder$   r%   r&   r'   sympy.printing.tensorflowr(   sympy.printing.numpyr)   r*   sympy.testing.pytestr+   r,   sympy.tensorr-   r.   0sympy.tensor.array.expressions.array_expressionsr/   r0   r1   r2   r3   sympy.externalr4   r   r5   sympy.parsing.latexr6   rD   rE   r   r7   rJ   rP   rX   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   rH   rH   rI   <module>   sZ    04,2