U
    9%e=                     @   s*  d dl Z d dlmZmZ d dlmZ 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mZ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 d dlmZm Z m!Z!m"Z" d dl#m$Z$m%Z% d dl&m'Z'm(Z( d dl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 d dl:m;Z;m<Z< d dl=m>Z> d dl?m@Z@mAZA d dlBmCZC edZDedZEedZFdd ZGdd ZHdd ZIdd  ZJd!d" ZKd#d$ ZLd%d& ZMdBd(d)ZNd*d+ ZOd,d- ZPd.d/ ZQd0d1 ZRd2d3 ZSd4d5 ZTd6d7 ZUd8d9 ZVd:d; ZWd<d= ZXe;d>d? ZYeCd@dA ZZdS )C    N)piRational)Pow)S)Symbol)Abs)explog)cossinsinc)MatrixSymbol)assumingQ)import_module)ccode)MatrixSolve)log2exp2expm1log1p)	logaddexp
logaddexp2)cosm1powm1)optimize	cosm1_optlog2_optexp2_opt	expm1_opt	log1p_opt	powm1_opt
optims_c99create_expand_pow_optimization
matinv_optlogaddexp_optlogaddexp2_optoptims_numpyoptims_scipy	sinc_optsFuncMinusOneOptim)XFAILskip)lambdify)compile_link_import_stringshas_c)	may_xfailcythonnumpyscipyc                  C   s  t d} dtd|  d  td }t|tg}|dtd|  d  ksLt|t|ks^tdtd|  d  dtd  }t|tg}|dtd|  d  d kst|t|kstt| td }t|tg}|t| kst|t|kstt| td t| d  }t|tg}|t| tdt| d   ksHt|t|ks\ttd}	t|	tg}
|
|	ks~tt| d td }t|tg}t|d	kst|t|kstd S )
Nx                     zlog2(x + 3))r   r	   r   r   r   AssertionErrorrewritestr)r4   expr1opt1expr2opt2expr3opt3expr4opt4expr5opt5expr6opt6 rK   a/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/codegen/tests/test_rewriting.pytest_log2_opt    s0      &rM   c                  C   sj   t d} dd|   }t|tg}|dt|  ks4t|t|ksFtdd|   }|t|tgksftd S )Nr4   r:   r8   r6   )r   r   r   r   r<   r=   r   )r4   r?   r@   rA   rK   rK   rL   test_exp2_opt@   s    rN   c                     s  t d t d } t| tg}t | dks4t|t| ksFtdt  d }t|tg}dt  |ksvt|t|kstdt  d }t|tg}dt  d |kst|t|ksttttdd}|t||gkst|t| |gkst|t||gkstdt  t  d }t|tg}dt  t  |ks^t|t|ksrtdtd   d }	t|	tg}
dtd   |
kst|
t|	kstdt  d t d  d }t|tg}|	 |	 kst fd	d
}t
|||| dk s,tt d}dt  d dt|  ddt|   }t|tg}dt  t| |kst|t|  dkstdt  d d }t|tg}t d t  }dt  td   }|| t dkst|||fks t|t|  dks<tt|}t|tg}t|}t|}|||fksrt|t|t  jstd S )Nr4   r:   r   r6   r7   r8   FZopportunisticc                    s   |   d S )Nr6   )subsevalf)er4   rK   rL   evo   s    ztest_expm1_opt.<locals>.evgV瞯<y   )r   r   r   r   r   r<   r=   r*   r	   Z	count_opsabsfactorr   is_zero)r?   r@   rA   rB   rC   rD   Zexpm1_opt_non_opportunisticrE   rF   rG   rH   rI   rJ   rT   rU   expr7opt7expr8opt8Ztgt8aZtgt8bZexpr9Zopt9Ztgt9aZtgt9brK   rS   rL   test_expm1_optK   s`     ,r_   c                  C   sN   t td \} }t| t| d }t|tg}|t| t| ksJtd S Nx yr8   )mapr   splitr   r   r   r   r<   r4   rU   r?   r@   rK   rK   rL   test_expm1_two_exp_terms   s    re   c                  C   s
  t d} t| d }t|tg}t| | dks4t|t|ksFtdt|  d }t|tg}dt|  |ksvt|t|kstdt|  d }t|tg}dt|  d |kst|t|ksttttdd}|t||gkst|t||gkst|t||gkstdt|  t|  d }t|tg}	dt|  t|  |	ks^t|	t|ksrtdtd|   d }
t|
tg}dtd|   |kst|t|
kstddt|   }t|tg}d	t|  |kst|t|kstd S )
Nr4   r:   r   r6   r7   r8   FrO   rV   )	r   r
   r   r   r   r<   r=   r*   r	   )r4   r?   r@   rA   rB   rC   rD   Zcosm1_opt_non_opportunisticrE   rF   rG   rH   rI   rJ   rK   rK   rL   test_cosm1_opt   s:    rf   c                  C   sN   t td \} }t| t| d }t|tg}|t| t| ksJtd S r`   )rb   r   rc   r
   r   r   r   r<   rd   rK   rK   rL   test_cosm1_two_cos_terms   s    rg   c                  C   sF   t d} t| t|  d }t|ttg}|t| t|  ksBtd S )Nr4   r8   )	r   r   r
   r   r   r   r   r   r<   )r4   r?   r@   rK   rK   rL   test_expm1_cosm1_mixed   s    rh       _Bc                 C   s   |  | }ttjj}t|| || k s4ttt	|
 |f|pJi }ttt| }	t||	 | }
|
t|| k sttt	|
 | f|pi }t||	 | }|t|| | kstdS )zU poorness=1e10 signifies that `expr` loses precision of at least ten decimal digits. N)rP   rQ   r2   ZfinfoZfloat64epsrX   r<   r-   listkeystuplerb   floatvalues)exproptZval_subsZ
approx_reflambdify_kwpoornessZnum_refrj   f1Z
args_floatZnum_err1f2Znum_err2rK   rK   rL   _check_num_lambdify   s    rv   c                  C   s@  t d} dt|  d }t|tg}|t|  t|  ks>ttrdt||| tdd idddid d	t|  d	 }t|t	}|d
t|  t|  ksttrt||| tdd idddid t
td|   t
 }t|tg}|t
 td|   td|   ksttr<t||| tdd d itdt
 ddid d S )Nr4   r:   
   ig&i3modulesr3   )rr   r8   rV   g&y3r6   )r   r
   r   r   r   r<   r3   rv   r   r(   r   rn   )r4   r?   r@   rA   rB   rC   rD   rK   rK   rL   test_cosm1_apart   s     "
"(ry   c            
      C   s   t td } \}}|| d }t|tg}|t||ks<t| D ]}||||ks@tq@trtt t	tj
j
dd d dkr|td  |ti}d}t||||dd	id
d |t|td  i}d}	t||||	dd	idd d S )NZxyr:   .r6   )r:   rw   r   g    ?g<V=rx   r3   l    h] )rr   rs   g|=ghRw=i ʚ;)rb   r   r   r!   r   r<   diffr3   rm   intversionrc   r   as_integer_ratior   rv   )
argsr4   rU   r?   r@   argZsubs1_aZ
ref1_f64_aZsubs1_bZ
ref1_f64_brK   rK   rL   
test_powm1   s    (r   c            	      C   s  t d} t| d }t|tg}t| | dks4t|t|ksFttd|  d }t|tg}t| td |kszt|t|  dksttd|  d }t|tg}td|  | dkst|t|kstt| d }t|tg}t|dkstd S )Nr4   r:   r   r6   r8   z
log(x + 3))	r   r	   r   r    r   r<   r=   simplifyr>   )	r4   r?   r@   rA   rB   rC   rD   rE   rF   rK   rK   rL   test_log1p_opt   s     r   c                  C   s(  t d} d|  t| td  t| d  t|  d }t|t }|t| t|  t|  t	|  ksnt
|ttt|kst
t| td t| d  }t|t}|t| t|  kst
|t|kst
t| td td|  d  }t|t}|t| td t|   }|dks0t
|t|  dksLt
d|  dtd|  d  d	td   d
t|   d
 td|  d  }t|t }	|	t| dtd|  d  d	  d
t	|   td t|   }
|
dkst
|	ttt|  dkst
dtd|   d }t|t}|dt	d|    }|dksXt
|t|kslt
td|  d }t|t}|t	d|  d |fkst
td|  d }t|t}|tdt|   }|dkst
|t|  dkst
td|  d }t|t}||ks$t
d S )Nr4   r8   r:   r;   r   r6   r7   r5   r9      )r   r	   r   r   r"   r   r   r   r   r   r<   r=   r   )r4   r?   r@   rA   rB   rC   rD   Zdelta3rE   rF   Zdelta4rG   rH   Zdelta5rI   rJ   r[   r\   Zdelta7r]   r^   rK   rK   rL   test_optims_c99  sF    0(
 
H@(



r   c                  C   sN  dd } t d}| |d dks$t| |d |d  dks@t| |d |d  d	ks\t| t|d d
kstt| |d dkst| |d dkst| |d  dkst| |d |d  dkstt ddd}| || |d  dkstt ddd}| tt|d dks tdd }||d t|d  dksJtd S )Nc                 S   s   t t| tdgS )NrW   r   r   r#   rS   rK   rK   rL   <lambda>C  s   z5test_create_expand_pow_optimization.<locals>.<lambda>r4   rW   zx*x*x*xr8   zx*x + x*x*x*xr7   zpow(x, 5) + x*x*x*xzpow(sin(x), 4)z1.0/(x*x*x*x)z
pow(x, -5)z
-(x*x*x*x)z-(x*x) + x*x*x*xiTintegerzpow(x, i) - (x*x)rU   )realzexp(y*y*y*y)c                 S   s   t t| tddd dgS )NrW   c                 S   s   | j S N)Zis_Function)brK   rK   rL   r   V      zGtest_create_expand_pow_optimization.<locals>.<lambda>.<locals>.<lambda>)Zbase_reqr   rS   rK   rK   rL   r   U  s    r6   z pow(x, 3) + sin(x)*sin(x)*sin(x))r   r<   r   r   r   )ccr4   r   rU   Zcc2rK   rK   rL   #test_create_expand_pow_optimizationB  s     r   c               	   C   s   t ddd} td| | }td| d}tt|R t|d | tgt||ksVtt|d | | tgt||| kstW 5 Q R X d S )NnTr   Ar4   r:   )	r   r   r   r   Zfullrankr   r$   r   r<   )r   r   r4   rK   rK   rL   test_matsolveZ  s    "r   c                  C   st   t td \} }tt| t| }t|tg}t| || dksHtt|| | dks^t|	t|ksptd S )Nra   r   )
rb   r   rc   r	   r   r   r%   r   r<   r=   rd   rK   rK   rL   test_logaddexp_optd  s    r   c                  C   s|   t td \} }td|  d|  td }t|tg}t| || dksPtt|| | dksft|t|ksxtd S )Nra   r8   r   )	rb   r   rc   r	   r   r&   r   r<   r=   rd   rK   rK   rL   test_logaddexp2_optm  s    r   c                  C   s  dd } t d}| t|| t|td| d|  td| td| | dtd|  |t| |t| i t d}| t|| ||  t|| |t||  | t|| tt|t| tt|tdt| t| dtdt|  t|| t|| i d S )Nc                 S   s(   |   D ]\}}t|t|kstqd S r   )itemsr   r)   r<   dkvrK   rK   rL   checkw  s    ztest_sinc_opts.<locals>.checkr4   r8   r6   rU   )r   r   r   r   r4   rU   rK   rK   rL   test_sinc_optsv  s2    
  
 
 
 
 
 
 
 
r   c                  C   s   dd } t d}| td| d|  td|  d td| td|  t|d td t|d d  t|d t|d  i d S )Nc                 S   s(   |   D ]\}}t|t|kstqd S r   r   r   r'   r<   r   rK   rK   rL   r     s    z test_optims_numpy.<locals>.checkr4   r8   r:   r6   )r   r   r   r   r   r	   r   r   )r   r4   rK   rK   rL   test_optims_numpy  s    " " r   c                  C   s   dd } t td \}}| t|| t||  t|| d  td| |  t|| t||  t||  t|t| | d t	|t| i d S )Nc                 S   s(   |   D ]\}}t|t|kstqd S r   r   r   rK   rK   rL   r     s    z%test_optims_numpy_TODO.<locals>.checkra   r:   r8   )
rb   r   rc   r	   r   r   r   r   r   r   r   rK   rK   rL   test_optims_numpy_TODO  s    6 " r   c               	   C   sx  t std t std td} ddtd  ddtd   d	d
td   ddtd   td t }dt|  | }tdd }|| |d}t	dt|  | t
g}d}dt||t||d }d}t }	td|fd|fg|	ddid\}
}t|
d| }t|
d| }d|  k rFdk sLn td|  k rddk sjn tW 5 Q R X d S ) Nzcython not installed.zNo C compiler found.r4   r8   :   u   r6   a   r7   rW   '   r5   \      rw   i      z
#include <math.h>

double func_unchanged(double x) {
    return %(unchanged)s;
}
double func_rewritten(double x) {
    return %(rewritten)s;
}
)	unchanged	rewrittenz
#cython: language_level=3
cdef extern double func_unchanged(double)
cdef extern double func_rewritten(double)
def py_unchanged(x):
    return func_unchanged(x)
def py_rewritten(x):
    return func_rewritten(x)
zfunc.cz	_func.pyxZstdZc99)	build_dircompile_kwargsgdy=g<6S:g}:gҶOɃ;gؗҜ<)r1   r,   r/   r   r   r   r   rP   r   r   r   r   tempfileTemporaryDirectoryr.   rX   Zpy_rewrittenZpy_unchangedr<   )r4   Z	about_twor   Zxvalrefr   ZNUMBER_OF_DIGITSZfunc_cZfunc_pyxfoldermodinfoZerr_rewrittenZerr_unchangedrK   rK   rL   "test_compiled_ccode_with_rewriting  s6    L		
 
r   )Nri   )[r   Zsympy.core.numbersr   r   Zsympy.core.powerr   Zsympy.core.singletonr   Zsympy.core.symbolr   Z$sympy.functions.elementary.complexesr   Z&sympy.functions.elementary.exponentialr   r	   Z(sympy.functions.elementary.trigonometricr
   r   r   Z"sympy.matrices.expressions.matexprr   Zsympy.assumptionsr   r   Zsympy.externalr   Zsympy.printing.codeprinterr   Zsympy.codegen.matrix_nodesr   Zsympy.codegen.cfunctionsr   r   r   r   Zsympy.codegen.numpy_nodesr   r   Zsympy.codegen.scipy_nodesr   r   Zsympy.codegen.rewritingr   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   Zsympy.testing.pytestr+   r,   Zsympy.utilitiesr-   Zsympy.utilities._compilationr.   r/   Z!sympy.utilities._compilation.utilr0   r1   r2   r3   rM   rN   r_   re   rf   rg   rh   rv   ry   r   r   r   r   r   r   r   r   r   r   r   rK   rK   rK   rL   <module>   sZ   H B&
.
		
