a
    khG                     @   s  d dl mZ 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 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)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z; d dl<m=Z=m>Z>m?Z? d dl@mAZA d dlBmCZCmDZDmEZEmFZFmGZG d dlHmIZI d dlJmKZKmLZL d dlMmNZNmOZO d dlPmQZQmRZR d dlSmTZTmUZUmVZVmWZWmXZX d dlYmZZZ d dl[m\Z\ ed\Z]Z^Z_eQdZ`dd Zadd Zbdd Zcd d! Zdd"d# Zed$d% Zfd&d' Zgd(d) Zhd*d+ Zid,d- Zjd.d/ ZkG d0d1 d1eZld2d3 Zmd4d5 Znd6d7 Zod8d9 Zpd:d; Zqd<d= Zrd>d? Zsd@dA ZtdBdC ZudDdE ZvdFdG ZwdHdI ZxdJdK ZydLS )M    )Not)
Assignment)none)expm1log1p)cosm1)MatrixSolve)
ExprModsymbolsEqLeGtzooooRationalPow)
Derivative)pi)S)acosKroneckerDelta	PiecewisesignsqrtMinMaxcotacschaseccothseclogsincostanasinatansinhcoshtanhasinhacoshatanh)atan2)AndOr)SparseMatrixMatrixSymbolIdentity)PrintMethodNotImplementedError)MpmathPrinterCmathPrinterPythonCodePrinterpycodeSymPyPrinter)TensorflowPrinter)NumPyPrinterSciPyPrinter)raisesskip)IndexedBaseIdx)ArraySymbolArrayDiagonalArrayContraction	ZeroArrayOneArray)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sXJ | tt tdksrJ | tttdksJ | tttdksJ | dtt  d	ksJ | ttd
ksJ | jrJ | t	dksJ | jddhiksJ | tt
dd dks"J | ttdks:J | jdddhiksRJ | ttdksjJ | 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sJ | tdttdfdttdfdddksBJ | ttdksZJ | td dksrJ | tttd ksJ | d!d"ksJ | ddgd#ksJ | tttd$ksJ | tttd%ksJ d S )&Nzx**y   zx % 2z-(x % y)z(-x) % yzx and yzx or y   z	1/(x + y)znot xzmath.pimathr   math.sqrt(x)r   zmath.acos(x)z(1/math.tan(x))z;((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   rJ   zp[0, 1]z(1 if x == y else 0)rI   rN   z(2, 3)z[2, 3]z	min(x, y)z	max(x, y))r7   module_importsdoprintxyr
   r/   r0   r   r   r   r   r   r   r    r   r   r   r   r   r   r   r   rH   r   r   r   prntr rY   N/var/www/auris/lib/python3.9/site-packages/sympy/printing/tests/test_pycode.pytest_PythonCodePrinter!   sN    




r[   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standardZpython4)r7   rY   rY   rY   rZ   <lambda>S       z1test_PythonCodePrinter_standard.<locals>.<lambda>)r7   r]   r=   
ValueErrorrW   rY   rY   rZ   test_PythonCodePrinter_standardN   s    ra   c                  C   sD  t  } | ttdksJ | ttdks2J | ttdksHJ | ttdks^J | ttdkstJ | 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ksJ | ttdksJ | ttdks(J | ttdks@J d S )Nzcmath.sqrt(x)zcmath.log(x)zcmath.sin(x)zcmath.cos(x)zcmath.tan(x)zcmath.asin(x)zcmath.acos(x)zcmath.atan(x)zcmath.sinh(x)zcmath.cosh(x)zcmath.tanh(x)zcmath.asinh(x)zcmath.acosh(x)zcmath.atanh(x))r6   rT   r   rU   r"   r#   r$   r%   r&   r   r'   r(   r)   r*   r+   r,   r-   rH   rY   rY   rZ   test_CmathPrinterV   s    rc   c                  C   s   t  } | ttdksJ | tdddks4J | tjdksHJ | tjdks\J | tjdkspJ | tj	dksJ | tj
d	ksJ | tjd
ksJ | tjdksJ | ttdksJ d S )Nzmpmath.sign(x)rJ   rI   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))r5   rT   r   rU   r   r   Exp1PiGoldenRatio
EulerGammaNaNInfinityNegativeInfinityrG   rb   rY   rY   rZ   test_MpmathPrinterm   s    rk   c               	   C   s6  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sJ tddd}tddd}tddd}tddd}||d dksJ ||d dksJ |tddksJ tddd}tddd}|t||dks0J |t||| dksNJ ||	dddkshJ ||dddksJ ||dd| |
|f|
| dksJ ||||dksJ ||||d ksJ |||d!ksJ |||d"ksJ |||d#ks(J |||d$ks@J |td d%ksXJ |td& d'kspJ tddd(d)}||d*ksJ |tjd+ksJ |tjd,ksJ |tjd-ksJ |tj d.ksJ |tj!d/ks J |tj"d0ksJ |t#td d1ks2J d S )2Nr   )Lambda)Adjoint)
DiagMatrixDiagonalMatrix
DiagonalOf)FunctionMatrix)HadamardProduct)KroneckerProduct)	OneMatrix
ZeroMatrix)abznumpy.sign(x)ArI   BCrJ      DrN      znumpy.linalg.inv(A)znumpy.linalg.matrix_power(A, 5)znumpy.eye(3)rU   rV   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)FrO   z	2**(-1.0)numpy.eznumpy.piznumpy.euler_gammaz	numpy.nanz	numpy.infz
-numpy.infz(numpy.cos(x)**(-1.0))**2)$sympy.core.functionrl   Z"sympy.matrices.expressions.adjointrm   Z#sympy.matrices.expressions.diagonalrn   ro   rp   Z%sympy.matrices.expressions.funcmatrixrq   Z#sympy.matrices.expressions.hadamardrr   Z$sympy.matrices.expressions.kroneckerrs   Z"sympy.matrices.expressions.specialrt   ru   Z	sympy.abcrv   rw   r;   rT   r   rU   r2   r3   r   r   r   rd   re   rg   rh   ri   rj   r!   )rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rH   rx   ry   rz   r|   uvexprrY   rY   rZ   test_NumPyPrinter|   sV    
r   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sJ |td td }|t|d}|| dddg dk	 sJ |ddksJ d S )Nnumpyznumpy not installed.r   )r   r   )lambdifyg?rN   rJ   g      ?)g      ?g      ?g      @r}   rI   r~   )rJ   r   rJ      @   )
rF   r>   Z(sympy.functions.elementary.miscellaneousr   r   Zsympy.utilities.lambdifyr   rU   Zlinspaceall)r   r   r   r   expr1funcrY   rY   rZ   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 rTJ tddddi}| |d	ksvJ d
| jv sJ | tjdksJ | tj	dksJ | tj
dksJ d S )Nr   znumpy.arccos(x)c                 s   s   | ]}| d V  qdS )ZscipyN)
startswith).0mrY   rY   rZ   	<genexpr>   r_   z$test_SciPyPrinter.<locals>.<genexpr>rI   r{   rQ   rN   z8scipy.sparse.coo_matrix(([3], ([0], [1])), shape=(2, 5))zscipy.sparsezscipy.constants.golden_ratiozscipy.constants.pir   )r<   r   rU   rS   rT   anyr1   r   rf   re   rd   )rH   r   ZsmatrY   rY   rZ   test_SciPyPrinter   s    r   c                     s<   t d\ tt fdd t  } | dv s8J d S )Nzif elsec                      s   t   ddS )NT)Zerror_on_reserved)r8   rY   s1s2rY   rZ   r^      r_   z,test_pycode_reserved_words.<locals>.<lambda>)zelse_ + if_zif_ + else_)r   r=   r`   r8   )Zpy_strrY   r   rZ   test_pycode_reserved_words   s    r   c                  C   sB   t d\} }}| | }t|dks&J || }t|dks>J d S )Nza_{b} b a_{11}za_b*bza_11*b)r   r8   )Za_brw   Za_11r   rY   rY   rZ   test_issue_20762   s
    r   c                  C   sv  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sdJ | jd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sJ | jttddd	ks4J t } | jttdddksVJ | jttddd	ksrJ d S )NF)ZrationalrL   rJ   z1/math.sqrt(x)r]   r\   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))r7   Z
_print_Powr   rU   r5   r;   r<   r9   rW   rY   rY   rZ   	test_sqrt   s(    r   c                  C   s   ddl m}  | t}t }||dks,J t }||dksDJ t }||dks\J t }||dkstJ t }||dksJ d S )Nr   )fracznumpy.mod(x, 1)zx % 1zmpmath.frac(x)z+sympy.functions.elementary.integers.frac(x))	Z#sympy.functions.elementary.integersr   rU   r;   rT   r<   r7   r5   r9   )r   r   rX   rY   rY   rZ   	test_frac   s    r   c                   @   s   e Zd Zdd Zdd ZdS )CustomPrintedObjectc                 C   s   dS )Nr   rY   selfprinterrY   rY   rZ   
_numpycode  s    zCustomPrintedObject._numpycodec                 C   s   dS )NmpmathrY   r   rY   rY   rZ   _mpmathcode  s    zCustomPrintedObject._mpmathcodeN)__name__
__module____qualname__r   r   rY   rY   rY   rZ   r     s   r   c                  C   s2   t  } t | dksJ t | dks.J d S )Nr   r   )r   r;   rT   r5   )objrY   rY   rZ   test_printmethod  s    r   c                   C   s   t tdksJ d S )NNone)r8   r   rY   rY   rY   rZ   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'))r7   rT   r   r   rW   rY   rY   rZ   test_issue_14283&  s    r   c                  C   s    t  } | tddksJ d S )NrI   z(0, 1,))r;   Z
_print_seqrange)nrY   rY   rZ   test_NumPyPrinter_print_seq-  s    r   c            	   	   C   s   ddl m} m} td}| |t}||t}t }||dksDJ ||dksVJ t }tddi}||fD ]H}t	t
 || W d    n1 s0    Y  d||v spJ qpd S )	Nr   )
lowergamma
uppergammarv   z3scipy.special.gamma(a)*scipy.special.gammainc(a, x)z4scipy.special.gamma(a)*scipy.special.gammaincc(a, x)strictFNot supported)'sympy.functions.special.gamma_functionsr   r   r   rU   r<   rT   r;   r7   r=   NotImplementedError)	r   r   rv   r   expr2rX   p_numpyp_pycoder   rY   rY   rZ   test_issue_16535_165363  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sJ 	|dksJ t
tfdd	 t
t fd
d	 t 	|dksJ 	|dksJ t
tfdd	 t
t fdd	 d S )Nr   )exp)IntegralrI   rJ   z>scipy.integrate.quad(lambda x: numpy.exp(-x), 0, numpy.inf)[0]zMscipy.integrate.nquad(lambda x, y: x**2*numpy.exp(x*y), ((-z, z), (0, z)))[0]c                      s
     S NrT   rY   
indefiniterX   rY   rZ   r^   S  r_   ztest_Integral.<locals>.<lambda>c                      s
     S r   r   rY   
evaluateatrX   rY   rZ   r^   T  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                      s
     S r   r   rY   r   rY   rZ   r^   Y  r_   c                      s
     S r   r   rY   r   rY   rZ   r^   Z  r_   )Z&sympy.functions.elementary.exponentialr   Zsympy.integrals.integralsr   rU   r   rV   zr<   rT   r=   r   r5   )r   r   singledoublerY   )r   r   rX   rZ   test_IntegralG  s     *r   c            	   	   C   s   ddl m} m} | t}|t}t }||dks8J ||dksJJ t }t }t }||fD ]h}t	t
 || W d    n1 s0    Y  t	t
 || W d    qd1 s0    Y  qd||dksJ ||dksJ d S )Nr   )fresnelcfresnelszscipy.special.fresnel(x)[1]zscipy.special.fresnel(x)[0]zmpmath.fresnelc(x)zmpmath.fresnels(x))Z'sympy.functions.special.error_functionsr   r   rU   r<   rT   r;   r7   r5   r=   r   )	r   r   r   r   rX   r   r   Zp_mpmathr   rY   rY   rZ   test_fresnel_integrals]  s     
(
*r   c                  C   s   ddl m}  | tt}t }||dks.J t }||dksFJ t }||dks^J tddi}||dks|J t }||dksJ 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))	Z&sympy.functions.special.beta_functionsr   rU   rV   r<   rT   r;   r7   r5   )r   r   rX   rY   rY   rZ   	test_betat  s    
r   c                  C   s   ddl m} m} | t}|t}t }||dks8J ||dksJJ tddi}d||v shJ d||v szJ tddi}d||v sJ d||v sJ d S )Nr   )airyaiairybizscipy.special.airy(x)[0]zscipy.special.airy(x)[2]r   Fr   )sympy.functions.special.besselr   r   rU   r<   rT   r;   r7   )r   r   r   r   rX   rY   rY   rZ   	test_airy  s    r   c                  C   s   ddl m} m} | t}|t}t }||dks8J ||dksJJ tddi}d||v shJ d||v szJ tddi}d||v sJ d||v sJ d S )Nr   )airyaiprimeairybiprimezscipy.special.airy(x)[1]zscipy.special.airy(x)[3]r   Fr   )r   r   r   rU   r<   rT   r;   r7   )r   r   r   r   rX   rY   rY   rZ   test_airy_prime  s    r   c                  C   sL   t  } | ttdksJ | ttdks2J | ttdksHJ d S )Nznumpy.expm1(x)znumpy.log1p(x)zscipy.special.cosm1(x))r<   rT   r   rU   r   r   rW   rY   rY   rZ   !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sPJ |td
dksfJ |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 t
 }|td
dks<J |td
dksTJ |t| ddgdksrJ ||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 )Nrx   )r}   r}   rM   rM   rM   IirQ   jrR   k)r}   r{   r{   znumpy.zeros((5,))znumpy.ones((5,))rI   rN   znumpy.einsum("abccd->abd", A))rI   rN   r}   znumpy.einsum("abccc->abc", A)r   rJ   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))rA   r?   r@   r;   rT   rD   rE   rC   rB   r   r:   )rx   r   r   r   r   rX   rY   rY   rZ   test_array_printer  s*    
"".$ r   c               
   C   s"  G dd dt } |  }ttjtdd}||dks:J tttjtdtddd}||dksfJ z|ttjtdd W n4 ty } zd	t	|v sJ W Y d }~nd }~0 0 dsJ z|t
ttd t W n6 ty } zd
t	|v sJ W Y d }~nd }~0 0 dsJ d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z1test_custom_Derivative_methods.<locals>.MyPrinterc                 S   s   |\}|\}d|  ||f S )Nz#my_custom_cosm1(%s, deriv_order=%d)Z_print)r   args
seq_ordersargorderrY   rY   rZ   _print_Derivative_cosm1  s    zItest_custom_Derivative_methods.<locals>.MyPrinter._print_Derivative_cosm1c                 S   s,   |\}}|\}}d|  ||  |||f S )Nz-my_custom_atan2(%s, %s, deriv1=%d, deriv2=%d)r   )r   r   r   Zarg1Zarg2Zord1Zord2rY   rY   rZ   _print_Derivative_atan2  s
    zItest_custom_Derivative_methods.<locals>.MyPrinter._print_Derivative_atan2N)r   r   r   r   r   rY   rY   rY   rZ   	MyPrinter  s   r   FrO   z!my_custom_cosm1(x, deriv_order=1)rI   rN   z)my_custom_atan2(x, y, deriv1=2, deriv2=3)Z_print_Derivative_expm1z_print_Derivative()r<   r   rU   diffrT   r.   rV   r   r4   reprr   r`   )r   rH   Zcosm1_1Z	atan2_2_3erY   rY   rZ   test_custom_Derivative_methods  s     &&r   N)zZsympyr   Z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   r   r   Zsympy.core.numbersr   Zsympy.core.singletonr   Zsympy.functionsr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   Z(sympy.functions.elementary.trigonometricr.   Zsympy.logicr/   r0   Zsympy.matricesr1   r2   r3   Zsympy.printing.codeprinterr4   Zsympy.printing.pycoder5   r6   r7   r8   r9   Zsympy.printing.tensorflowr:   Zsympy.printing.numpyr;   r<   Zsympy.testing.pytestr=   r>   Zsympy.tensorr?   r@   Z0sympy.tensor.array.expressions.array_expressionsrA   rB   rC   rD   rE   Zsympy.externalrF   r   rG   rU   rV   r   rH   r[   ra   rc   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rY   rY   rY   rZ   <module>   sb   0h-5	