o
    *ήc4                     @   s  d dl 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
 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& d dl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7m8Z8 d dl9m:Z:m;Z; d dl<m=Z= edZ>dd Z?dd Z@dd ZAdd ZBdd ZCd d! ZDd"d# ZEd$d% ZFd&d' ZGd(d) ZHd*d+ ZId,d- ZJd.d/ ZKd0d1 ZLd2d3 ZMe7d4d5 ZNe=d6d7 ZOdS )8    N)pi)Pow)S)Symbol)Abs)explog)cossinsinc)MatrixSymbol)assumingQ)import_module)ccode)MatrixSolve)log2exp2expm1log1p)	logaddexp
logaddexp2)cosm1)optimize	cosm1_optlog2_optexp2_opt	expm1_opt	log1p_opt
optims_c99create_expand_pow_optimization
matinv_optlogaddexp_optlogaddexp2_optoptims_numpy	sinc_optsFuncMinusOneOptim)XFAILskip)compile_link_import_stringshas_c)	may_xfailcythonc                  C   s  t d} dtd|  d  td }t|tg}|dtd|  d  ks&J |t|ks/J dtd|  d  dtd  }t|tg}|dtd|  d  d ksUJ |t|ks^J t| td }t|tg}|t| kstJ |t|ks}J t| td t| d  }t|tg}|t| tdt| d   ksJ |t|ksJ td}	t|	tg}
|
|	ksJ t| d td }t|tg}t|d	ksJ |t|ksJ d S )
Nx                     zlog2(x + 3))r   r   r   r   r   rewritestr)r-   expr1opt1expr2opt2expr3opt3expr4opt4expr5opt5expr6opt6 rC   I/tmp/pip-target-vg8gfxp4/lib/python/sympy/codegen/tests/test_rewriting.pytest_log2_opt   s0     $rE   c                  C   sj   t d} dd|   }t|tg}|dt|  ksJ |t|ks#J dd|   }|t|tgks3J d S )Nr-   r3   r1   r/   )r   r   r   r   r5   r   )r-   r7   r8   r9   rC   rC   rD   test_exp2_opt=   s   rF   c                     s  t d t d } t| tg}t | dksJ |t| ks#J dt  d }t|tg}dt  |ks;J |t|ksDJ dt  d }t|tg}dt  d |ks^J |t|ksgJ tttdd}|t||gksxJ |t| |gksJ |t||gksJ dt  t  d }t|tg}dt  t  |ksJ |t|ksJ dtd   d }	t|	tg}
dtd   |
ksJ |
t|	ksJ dt  d t d  d }t|tg}| | ksJ  fd	d
}t	|||| dk sJ t d}dt  d dt|  ddt|   }t|tg}dt  t| |ks>J |t| 
 dksLJ dt  d d }t|tg}t d t  }dt  td   }|| t
 dksJ |||fv sJ |t| 
 dksJ t|}t|tg}t|}t|}|||fv sJ |t|t 
 jsJ d S )Nr-   r3   r   r/   r0   r1   Fopportunisticc                    s   |   d S )Nr/   )subsevalf)er-   rC   rD   evl   s   ztest_expm1_opt.<locals>.evgV瞯<y   )r   r   r   r   r   r5   r&   r   	count_opsabsfactorr
   is_zero)r7   r8   r9   r:   r;   r<   expm1_opt_non_opportunisticr=   r>   r?   r@   rA   rB   rM   rN   expr7opt7expr8opt8tgt8atgt8bexpr9opt9tgt9atgt9brC   rL   rD   test_expm1_optH   s`    ,$r`   c                  C   N   t td \} }t| t| d }t|tg}|t| t| ks%J d S Nx yr1   )mapr   splitr   r   r   r   r-   rN   r7   r8   rC   rC   rD   test_expm1_two_exp_terms      rg   c                  C   s  t d} t| d }t|tg}t| | dksJ |t|ks#J dt|  d }t|tg}dt|  |ks;J |t|ksDJ dt|  d }t|tg}dt|  d |ks^J |t|ksgJ tttdd}|t||gksxJ |t||gksJ |t||gksJ dt|  t|  d }t|tg}	dt|  t|  |	ksJ |	t|ksJ dtd|   d }
t|
tg}dtd|   |ksJ |t|
ksJ ddt|   }t|tg}d	t|  |ksJ |t|ksJ d S )
Nr-   r3   r   r/   r0   r1   FrG   rO   )r   r	   r   r   r   r5   r&   r   )r-   r7   r8   r9   r:   r;   r<   cosm1_opt_non_opportunisticr=   r>   r?   r@   rA   rB   rC   rC   rD   test_cosm1_opt   s:   rj   c                  C   ra   rb   )rd   r   re   r	   r   r   r   rf   rC   rC   rD   test_cosm1_two_cos_terms   rh   rk   c                  C   sF   t d} t| t|  d }t|ttg}|t| t|  ks!J d S )Nr-   r1   )r   r   r	   r   r   r   r   r   )r-   r7   r8   rC   rC   rD   test_expm1_cosm1_mixed   s   rl   c            	      C   s  t d} t| d }t|tg}t| | dksJ |t|ks#J td|  d }t|tg}t| td |ks=J |t|  dksJJ td|  d }t|tg}td|  | dksdJ |t|ksmJ t| d }t|tg}t|dksJ d S )Nr-   r3   r   r/   r1   z
log(x + 3))r   r   r   r   r   r5   simplifyr6   )	r-   r7   r8   r9   r:   r;   r<   r=   r>   rC   rC   rD   test_log1p_opt   s    rn   c                  C   s"  t d} d|  t| td  t| d  t|  d }t|t }|t| t|  t|  t	|  ks7J |
t
t
t|ksFJ t| td t| d  }t|t}|t| t|  kseJ |
t|ksnJ t| td td|  d  }t|t}|t| td t|   }|dksJ |
t|  dksJ 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sJ |	
t
t
t|  dks	J dtd|   d }t|t}|dt	d|    }|dks)J |
t|ks3J td|  d }t|t}|t	d|  d |fv sOJ td|  d }t|t}|tdt|   }|dksmJ |
t|  dks{J td|  d }t|t}||ksJ d S )Nr-   r1   r3   r4   r   r/   r0   r.   r2      )r   r   r   r   r   rm   r   r   r   r   r5   r   )r-   r7   r8   r9   r:   r;   r<   delta3r=   r>   delta4r?   r@   delta5rA   rB   rV   rW   delta7rX   rY   rC   rC   rD   test_optims_c99   sF   0(
 
H@(



rt   c                  C   sJ  dd } t d}| |d dksJ | |d |d  dks J | |d |d  d	ks.J | t|d d
ks:J | |d dksDJ | |d dksNJ | |d  dksYJ | |d |d  dksgJ t ddd}| || |d  dks{J t ddd}| tt|d dksJ dd }||d t|d  dksJ d S )Nc                 S   s   t t| tdgS )NrP   r   r   r    rL   rC   rC   rD   <lambda>
  s    z5test_create_expand_pow_optimization.<locals>.<lambda>r-   rP   zx*x*x*xr1   zx*x + x*x*x*xr0   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)rN   )realzexp(y*y*y*y)c                 S   s   t t| tddd dgS )NrP   c                 S   s   | j S N)is_Function)brC   rC   rD   rv     s    zGtest_create_expand_pow_optimization.<locals>.<lambda>.<locals>.<lambda>)base_reqru   rL   rC   rC   rD   rv     s    r/   z pow(x, 3) + sin(x)*sin(x)*sin(x))r   r
   r   r   )ccr-   ry   rN   cc2rC   rC   rD   #test_create_expand_pow_optimization	  s    $r   c                  C   s   t ddd} td| | }td| d}tt|/ t|d | tgt||ks+J t|d | | tgt||| ks@J W d    d S 1 sKw   Y  d S )NnTrz   Ar-   r3   )r   r   r   r   fullrankr   r!   r   )r   r   r-   rC   rC   rD   test_matsolve!  s   ","r   c                  C   st   t td \} }tt| t| }t|tg}t| || dks$J t|| | dks/J |t|ks8J d S )Nrc   r   )	rd   r   re   r   r   r   r"   r   r5   rf   rC   rC   rD   test_logaddexp_opt+  s   r   c                  C   s|   t td \} }td|  d|  td }t|tg}t| || dks(J t|| | dks3J |t|ks<J d S )Nrc   r1   r   )rd   r   re   r   r   r#   r   r5   rf   rC   rC   rD   test_logaddexp2_opt4  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   (   |   D ]\}}t|t|ksJ qd S r}   )itemsr   r%   dkvrC   rC   rD   check>     ztest_sinc_opts.<locals>.checkr-   r1   r/   rN   )r   r
   r   r   r-   rN   rC   rC   rD   test_sinc_opts=  s    (
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   r   r}   r   r   r$   r   rC   rC   rD   r   U  r   z test_optims_numpy.<locals>.checkr-   r1   r3   r/   )r   r
   r   r   r   r   r   r   )r   r-   rC   rC   rD   test_optims_numpyT  s   88
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   r   r}   r   r   rC   rC   rD   r   b  r   z%test_optims_numpy_TODO.<locals>.checkrc   r3   r1   )
rd   r   re   r   r
   r   r   r   r   r   r   rC   rC   rD   test_optims_numpy_TODO`  s   X$
r   c                  C   s  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||t||d }d}t I}	td|fd|fg|	tddd\}
}t|
d| }t|
d| }d|  k rdk sJ  J d|  k rdk sJ  J W d    d S 1 sw   Y  d S ) Nzcython not installed.zNo C compiler found.r-   r1   :   u   r/   a   r0   rP   '   r.   \      
   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.pyxc99)std)	build_dircompile_kwargsgdy=g<6S:g}:gҶOɃ;gؗҜ<)r,   r(   r*   r   r   r   r   rI   r   r   r   dictr   tempfileTemporaryDirectoryr)   rR   py_rewrittenpy_unchanged)r-   	about_twor   xvalrefr   NUMBER_OF_DIGITSfunc_cfunc_pyxfoldermodinfoerr_rewrittenerr_unchangedrC   rC   rD   "test_compiled_ccode_with_rewritingm  s6   L	
	

"r   )Pr   sympy.core.numbersr   sympy.core.powerr   sympy.core.singletonr   sympy.core.symbolr   $sympy.functions.elementary.complexesr   &sympy.functions.elementary.exponentialr   r   (sympy.functions.elementary.trigonometricr	   r
   r   "sympy.matrices.expressions.matexprr   sympy.assumptionsr   r   sympy.externalr   sympy.printing.codeprinterr   sympy.codegen.matrix_nodesr   sympy.codegen.cfunctionsr   r   r   r   sympy.codegen.numpy_nodesr   r   sympy.codegen.scipy_nodesr   sympy.codegen.rewritingr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   sympy.testing.pytestr'   r(   sympy.utilities._compilationr)   r*   !sympy.utilities._compilation.utilr+   r,   rE   rF   r`   rg   rj   rk   rl   rn   rt   r   r   r   r   r   r   r   r   rC   rC   rC   rD   <module>   sP    @ B&.
		
