o
    FZh=                     @   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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)r4   expr1opt1expr2opt2expr3opt3expr4opt4expr5opt5expr6opt6 rJ   Q/var/www/auris/lib/python3.10/site-packages/sympy/codegen/tests/test_rewriting.pytest_log2_opt    s0     $rL   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 )Nr4   r:   r8   r6   )r   r   r   r   r<   r   )r4   r>   r?   r@   rJ   rJ   rK   test_exp2_opt@   s   rM   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 )Nr4   r:   r   r6   r7   r8   FZopportunisticc                    s   |   d S )Nr6   )subsevalf)er4   rJ   rK   evo   s   ztest_expm1_opt.<locals>.evgV瞯<y   )r   r   r   r   r   r<   r*   r	   Z	count_opsabsfactorr   is_zero)r>   r?   r@   rA   rB   rC   Zexpm1_opt_non_opportunisticrD   rE   rF   rG   rH   rI   rS   rT   expr7opt7expr8opt8Ztgt8aZtgt8bZexpr9Zopt9Ztgt9aZtgt9brJ   rR   rK   test_expm1_optK   s`    ,$r^   c                  C   N   t td \} }t| t| d }t|tg}|t| t| ks%J d S Nx yr8   )mapr   splitr   r   r   r   r4   rT   r>   r?   rJ   rJ   rK   test_expm1_two_exp_terms      re   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 )
Nr4   r:   r   r6   r7   r8   FrN   rU   )r   r
   r   r   r   r<   r*   r	   )r4   r>   r?   r@   rA   rB   rC   Zcosm1_opt_non_opportunisticrD   rE   rF   rG   rH   rI   rJ   rJ   rK   test_cosm1_opt   s:   rg   c                  C   r_   r`   )rb   r   rc   r
   r   r   r   rd   rJ   rJ   rK   test_cosm1_two_cos_terms   rf   rh   c                  C   sF   t d} t| t|  d }t|ttg}|t| t|  ks!J d S )Nr4   r8   )r   r   r
   r   r   r   r   r   )r4   r>   r?   rJ   rJ   rK   test_expm1_cosm1_mixed   s   ri       _Bc                 C   s   |  | }ttjj}t|| || k sJ tt|	 |fi |p&i }t
tt| }	t||	 | }
|
t|| k sDJ tt|	 | fi |pPi }t||	 | }|t|| | ksgJ dS )zU poorness=1e10 signifies that `expr` loses precision of at least ten decimal digits. N)rO   rP   r2   ZfinfoZfloat64epsrW   r-   listkeystuplerb   floatvalues)exproptZval_subsZ
approx_reflambdify_kwpoornessZnum_refrk   f1Z
args_floatZnum_err1f2Znum_err2rJ   rJ   rK   _check_num_lambdify   s   rw   c                  C   s@  t d} dt|  d }t|tg}|t|  t|  ksJ tr2t||| tdd idddid d	t|  d	 }t|t}|d
t|  t|  ksMJ tr`t||| tdd idddid t	td|   t	 }t|tg}|t	 td|   td|   ksJ trt||| tdd d it
dt	 ddid d S d S )Nr4   r:   
   ig&i3modulesr3   )rs   r8   rU   g&y3r6   )r   r
   r   r   r   r3   rw   r   r(   r   ro   )r4   r>   r?   r@   rA   rB   rC   rJ   rJ   rK   test_cosm1_apart   s"   "
"&2rz   c            
      C   s   t td } \}}|| d }t|tg}|t||ksJ | D ]}||||ks.J q trutt ttj	j	
dd d dkrw|td  |ti}d}t||||dd	id
d |t|td  i}d}	t||||	dd	idd d S d S d S )NZxyr:   .r6   )r:   rx   r   g    ?g<V=ry   r3   l    h] )rs   rt   g|=ghRw=i ʚ;)rb   r   r   r!   r   diffr3   rn   intversionrc   r   as_integer_ratior   rw   )
argsr4   rT   r>   r?   argZsubs1_aZ
ref1_f64_aZsubs1_bZ
ref1_f64_brJ   rJ   rK   
test_powm1   s   (r   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 )Nr4   r:   r   r6   r8   z
log(x + 3))r   r	   r   r    r   r<   simplifyr=   )	r4   r>   r?   r@   rA   rB   rC   rD   rE   rJ   rJ   rK   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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 )Nr4   r8   r:   r;   r   r6   r7   r5   r9      )r   r	   r   r   r"   r   r   r   r   r   r<   r   )r4   r>   r?   r@   rA   rB   rC   Zdelta3rD   rE   Zdelta4rF   rG   Zdelta5rH   rI   rZ   r[   Zdelta7r\   r]   rJ   rJ   rK   test_optims_c99  sF   0(
 
H@(



r   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 )NrV   r   r   r#   rR   rJ   rJ   rK   <lambda>C  s    z5test_create_expand_pow_optimization.<locals>.<lambda>r4   rV   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)rT   )realzexp(y*y*y*y)c                 S   s   t t| tddd dgS )NrV   c                 S   s   | j S N)Zis_Function)brJ   rJ   rK   r   V  s    zGtest_create_expand_pow_optimization.<locals>.<lambda>.<locals>.<lambda>)Zbase_reqr   rR   rJ   rJ   rK   r   U  s    r6   z pow(x, 3) + sin(x)*sin(x)*sin(x))r   r   r   r   )ccr4   r   rT   Zcc2rJ   rJ   rK   #test_create_expand_pow_optimizationB  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nTr   Ar4   r:   )r   r   r   r   Zfullrankr   r$   r   )r   r   r4   rJ   rJ   rK   test_matsolveZ  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 )Nra   r   )	rb   r   rc   r	   r   r   r%   r   r<   rd   rJ   rJ   rK   test_logaddexp_optd  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 )Nra   r8   r   )rb   r   rc   r	   r   r&   r   r<   rd   rJ   rJ   rK   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   (   |   D ]\}}t|t|ksJ qd S r   )itemsr   r)   dkvrJ   rJ   rK   checkw     ztest_sinc_opts.<locals>.checkr4   r8   r6   rT   )r   r   r   r   r4   rT   rJ   rJ   rK   test_sinc_optsv  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   rJ   rJ   rK   r     r   z test_optims_numpy.<locals>.checkr4   r8   r:   r6   )r   r   r   r   r   r	   r   r   )r   r4   rJ   rJ   rK   test_optims_numpy  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   rJ   rJ   rK   r     r   z%test_optims_numpy_TODO.<locals>.checkra   r:   r8   )
rb   r   rc   r	   r   r   r   r   r   r   r   rJ   rJ   rK   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||d }d}t H}	td|fd|fg|	ddi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.r4   r8   :   u   r6   a   r7   rV   '   r5   \      rx   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   rO   r   r   r   r   tempfileTemporaryDirectoryr.   rW   Zpy_rewrittenZpy_unchanged)r4   Z	about_twor   Zxvalrefr   ZNUMBER_OF_DIGITSZfunc_cZfunc_pyxfoldermodinfoZerr_rewrittenZerr_unchangedrJ   rJ   rK   "test_compiled_ccode_with_rewriting  s6   L	
	
"r   )Nrj   )[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   rL   rM   r^   re   rg   rh   ri   rw   rz   r   r   r   r   r   r   r   r   r   r   r   rJ   rJ   rJ   rK   <module>   s\    H B&
.
		
