a
    kh                     @   sD  d dl Z d dlmZ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mZmZ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 d dlmZ d dl m!Z! d dl"m#Z#m$Z$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+ edZ,edZ-dd Z.e'dd Z/e'dd Z0dd Z1e'e+ddd Z2dd Z3dS )    N)logMinMaxsqrt)Float)Symbolsymbols)cos)
AssignmentRaiseRuntimeError_QuotedString)newtons_methodnewtons_method_function)expm1)bind_C)render_as_module)import_module)ccode)compile_link_import_stringshas_chas_fortran)	may_xfail)skipraisesskip_under_pyodidecython	wurlitzerc                  C   sP   t d\} }}t| | d  }t|| ||}|t|| ||  sLJ d S )Nz	x dx atol   )r   r	   r   Zhasr
   diff)xZdxatolexpralgo r$   Q/var/www/auris/lib/python3.9/site-packages/sympy/codegen/tests/test_algorithms.pytest_newtons_method   s    r&   c                  C   s   t ddd} t| | d  }t|| }ts2td t s@td ddi}t Z}td	d
t	| fdd
dd fg||d\}}t|dd dk sJ W d    n1 s0    Y  d S )Nr    Trealr   cython not installed.No C compiler found.stdc99znewton.c%#include <math.h>
#include <stdio.h>
_newton.pyx#cython: language_level={}
3zIcdef extern double newton(double)
def py_newton(x):
    return newton(x)
)	build_dircompile_kwargs      ?ߺv?-q=)r   r	   r   r   r   r   tempfileTemporaryDirectoryr   r   formatabs	py_newton)r    r"   func
compile_kwfoldermodinfor$   r$   r%   #test_newtons_method_function__ccode   s(    



r@   c                  C   s   t ddd} t| | d  }t|| tddgd}ts>td t sLtd	 t|gd
}t	 P}t
d|fdddd fg|d\}}t|dd dk sJ W d    n1 s0    Y  d S )Nr    Tr'   r   Znewton)name)attrsr)   zNo Fortran compiler found.Z
mod_newtonz
newton.f90r.   r/   r0   zRcdef extern double newton(double*)
def py_newton(double x):
    return newton(&x)
)r1   r3   r4   r5   )r   r	   r   r   r   r   r   f_moduler6   r7   r   r8   r9   r:   )r    r"   r;   Zf_modr=   r>   r?   r$   r$   r%   #test_newtons_method_function__fcode4   s$    


rD   c                  C   s`   t ddd} t| | d  }t|| }t|}i }t||| td|}t|d dk s\J d S )Nr    Tr'   r   znewton(0.5)r4   r5   )r   r	   r   	py_moduleexecevalr9   )r    r"   r;   py_mod	namespaceresr$   r$   r%   $test_newtons_method_function__pycodeK   s    

rK   z,Emscripten does not support process spawningc               	      sp  t d } \}}}|t|  |d    tt fdd t}t | |d}t sftd tsrtd dd	i}t	
 }td
dt| fdddd fg||d\}}	|rt \}
}|d}W d    q1 s0    Y  n
|d}t|d dk sJ |std |
 |  }
}|dks>J |
dksLJ W d    n1 sb0    Y  d S )Nzx A k pr   c                      s
   t  S N)r   r$   r"   r    r$   r%   <lambda>[       z@test_newtons_method_function__ccode_parameters.<locals>.<lambda>)debugr*   r)   r+   r,   znewton_par.cr-   z_newton_par.pyxr/   r0   zycdef extern double newton(double, double, double, double)
def py_newton(x, A=1, k=1, p=1):
    return newton(x, A, k, p)
)r2   r1   r3   r4   r5   zAC-level output only tested when package 'wurlitzer' is available. zx=         0.5
x=      1.1121 d_x=     0.61214
x=     0.90967 d_x=    -0.20247
x=     0.86726 d_x=   -0.042409
x=     0.86548 d_x=  -0.0017867
x=     0.86547 d_x= -3.1022e-06
x=     0.86547 d_x= -9.3421e-12
x=     0.86547 d_x=  3.6902e-17
)r   r	   r   
ValueErrorr   r   r   r   r   r6   r7   r   r   r8   Zpipesr:   r9   read)argsAkpZuse_wurlitzerr;   r<   r=   r>   r?   outerrresultr$   rM   r%   .test_newtons_method_function__ccode_parametersV   s>    


	*
r[   c                     s  t dddd\} }}tdddd}|| d }|  | }t| tttt|  |   }|t| }dd  d	 fd
did	dd id	dd id}|| fdD ]}	| ||fdd|	|ddftttdd	fdd| D }
dd |
 D }i }i }| D ]4\}}i  }||< t||| || d ||< q"t	d}dddd}d}| D ]J\}}
|
|dddd}|||  }|	r|d 9 }t
|| |k sxJ qxqd S )!Nza b c N_geo N_totT)r(   nonnegativei)integerr\      c                 S   s   t t| ||S rL   )r   r   )lowr"   highr$   r$   r%   _clamp   s    z:test_newtons_method_function__rtol_cse_nan.<locals>._clampZdelta_fnc                    s8    t | | d |  | | t | | d S )NgGz?)r   r   er    )rb   acr$   r%   rN      s   z<test_newtons_method_function__rtol_cse_nan.<locals>.<lambda>c                 S   s4   d| |  |  d|  |d  | |  |d   S )N   r   rc   r$   r$   r%   rN      rO   c                 S   s<   |  |  | d| |  | |  |d d |  |   S )Nr_   rh   ri   rc   r$   r$   r%   rN      rO   )Zclamped_newtonZhalleyZ
halley_alt)FT<   g0.++gؗҬ<zencountered NaN.)	paramsZitermaxrP   Zcsecounterr!   ZrtolZboundsZ
handle_nanc              
      s6   i | ].\}}|t  d | ditfi |qS )	func_name_b)r   dict).0rV   kw)rT   kwargsr$   r%   
<dictcomp>   rO   z>test_newtons_method_function__rtol_cse_nan.<locals>.<dictcomp>c                 S   s   i | ]\}}|t |qS r$   )rE   )rp   rV   vr$   r$   r%   rs      rO   rn   z"13.2261515064168768938151923226496gMg<g      @g{Gz?g      Y@2   d   rh   )r   r   r   r   r   r   r   itemsrF   r   r9   )bZN_geoZN_totr]   ZN_ariZ	delta_ariZln_delta_geoZeqb_logZmeth_kwZuse_cser;   rH   rI   Zroot_find_brV   rt   nsrefZreftolguessmethrZ   reqr$   )rb   re   rT   rf   rr   r%   *test_newtons_method_function__rtol_cse_nan   sD    &

	
r~   )4r6   Zsympyr   r   r   r   Zsympy.core.numbersr   Zsympy.core.symbolr   r   Z(sympy.functions.elementary.trigonometricr	   Zsympy.codegen.astr
   r   r   r   Zsympy.codegen.algorithmsr   r   Zsympy.codegen.cfunctionsr   Zsympy.codegen.fnodesr   Zsympy.codegen.futilsr   rC   Zsympy.codegen.pyutilsrE   Zsympy.externalr   Zsympy.printing.codeprinterr   Zsympy.utilities._compilationr   r   r   Z!sympy.utilities._compilation.utilr   Zsympy.testing.pytestr   r   r   r   r   r&   r@   rD   rK   r[   r~   r$   r$   r$   r%   <module>   s6   

1