o
    h                     @   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|| ||  s&J d S )Nz	x dx atol   )r   r	   r   hasr
   diff)xdxatolexpralgo r&   Y/var/www/html/ai/venv/lib/python3.10/site-packages/sympy/codegen/tests/test_algorithms.pytest_newtons_method   s   $r(   c                  C   s   t ddd} t| | d  }t|| }tstd t s td ddi}t .}td	d
t	| fdd
dd fg||d\}}t|dd dk sNJ W d    d S 1 sYw   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*   



"rB   c                  C   s   t ddd} t| | d  }t|| tddgd}tstd t s&td	 t|gd
}t	 )}t
d|fdddd fg|d\}}t|dd dk sQJ W d    d S 1 s\w   Y  d S )Nr!   Tr)   r   newton)name)attrsr+   zNo Fortran compiler found.
mod_newtonz
newton.f90r0   r1   r2   zRcdef extern double newton(double*)
def py_newton(double x):
    return newton(&x)
)r3   r5   r6   r7   )r   r	   r   r   r   r   r   f_moduler8   r9   r   r:   r;   r<   )r!   r$   r=   f_modr?   r@   rA   r&   r&   r'   #test_newtons_method_function__fcode4   s&   


"rI   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)r6   r7   )r   r	   r   	py_moduleexecevalr;   )r!   r$   r=   py_mod	namespaceresr&   r&   r'   $test_newtons_method_function__pycodeK   s   

rP   z,Emscripten does not support process spawningc               	      sh  t d } \}}}|t|  |d    tt fdd t}t | |d}t s3td ts9td dd	i}t	
 i}td
dt| fdddd fg||d\}}	|rxt \}
}|d}W d    n1 srw   Y  n|d}t|d dk sJ |std |
 | }
}|dksJ |
dksJ W d    d S 1 sw   Y  d S )Nzx A k pr   c                      s
   t  S N)r   r&   r$   r!   r&   r'   <lambda>[   s   
 z@test_newtons_method_function__ccode_parameters.<locals>.<lambda>)debugr,   r+   r-   r.   znewton_par.cr/   z_newton_par.pyxr1   r2   zycdef extern double newton(double, double, double, double)
def py_newton(x, A=1, k=1, p=1):
    return newton(x, A, k, p)
)r4   r3   r5   r6   r7   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   r8   r9   r   r   r:   pipesr<   r;   read)argsAkpuse_wurlitzerr=   r>   r?   r@   rA   outerrresultr&   rR   r'   .test_newtons_method_function__ccode_parametersV   sD   


	
"ra   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 ]\}}i  }||< t||| || d ||< qt	d}dddd}d}| D ]"\}}
|
|dddd}|||  }|	r|d 9 }t
|| |k sJ qqXd S )!Nza b c N_geo N_totT)r*   nonnegativei)integerrb      c                 S   s   t t| ||S rQ   )r   r   )lowr$   highr&   r&   r'   _clamp   s   z:test_newtons_method_function__rtol_cse_nan.<locals>._clampdelta_fnc                    s8    t | | d |  | | t | | d S )NgGz?)r   r    er!   )rh   acr&   r'   rS      s
    z<test_newtons_method_function__rtol_cse_nan.<locals>.<lambda>c                 S   s4   d| |  |  d|  |d  | |  |d   S )N   r    rj   r&   r&   r'   rS      s   4 c                 S   s<   |  |  | d| |  | |  |d d |  |   S )Nre   ro   rp   rj   r&   r&   r'   rS      s   < )clamped_newtonhalley
halley_alt)FT<   g0.++gؗҬ<zencountered NaN.)	paramsitermaxrT   csecounterr#   rtolbounds
handle_nanc              
      s6   i | ]\}}|t  d | ditfi |qS )	func_name_b)r   dict).0r[   kw)rY   kwargsr&   r'   
<dictcomp>   s   6 z>test_newtons_method_function__rtol_cse_nan.<locals>.<dictcomp>c                 S   s   i | ]	\}}|t |qS r&   )rJ   )r   r[   vr&   r&   r'   r      s    r}   z"13.2261515064168768938151923226496gMg<g      @g{Gz?g      Y@2   d   ro   )r   r   r   r   r   r   r   itemsrK   r   r;   )bN_geoN_totrc   N_ari	delta_ariln_delta_geoeqb_logmeth_kwuse_cser=   rM   rN   root_find_br[   r   nsrefreftolguessmethr`   reqr&   )rh   rl   rY   rm   r   r'   *test_newtons_method_function__rtol_cse_nan   sH   &

	r   )4r8   sympyr   r   r   r   sympy.core.numbersr   sympy.core.symbolr   r   (sympy.functions.elementary.trigonometricr	   sympy.codegen.astr
   r   r   r   sympy.codegen.algorithmsr   r   sympy.codegen.cfunctionsr   sympy.codegen.fnodesr   sympy.codegen.futilsr   rG   sympy.codegen.pyutilsrJ   sympy.externalr   sympy.printing.codeprinterr   sympy.utilities._compilationr   r   r   !sympy.utilities._compilation.utilr   sympy.testing.pytestr   r   r   r   r   r(   rB   rI   rP   ra   r   r&   r&   r&   r'   <module>   s8    

1