o
    jg                     @   s8  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* edZ+edZ,dd Z-e'dd Z.e'dd Z/dd Z0e'dd Z1dd Z2d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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%   [/var/www/html/zoom/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*   



"rA   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.f90r/   r0   r1   zRcdef extern double newton(double*)
def py_newton(double x):
    return newton(&x)
)r2   r4   r5   r6   )r   r	   r   r   r   r   r   f_moduler7   r8   r   r9   r:   r;   )r    r#   r<   f_modr>   r?   r@   r%   r%   r&   #test_newtons_method_function__fcode4   s&   


"rH   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)r5   r6   )r   r	   r   	py_moduleexecevalr:   )r    r#   r<   py_mod	namespaceresr%   r%   r&   $test_newtons_method_function__pycodeK   s   

rO   c               	      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>Z   s   
 z@test_newtons_method_function__ccode_parameters.<locals>.<lambda>)debugr+   r*   r,   r-   znewton_par.cr.   z_newton_par.pyxr0   r1   zycdef extern double newton(double, double, double, double)
def py_newton(x, A=1, k=1, p=1):
    return newton(x, A, k, p)
)r3   r2   r4   r5   r6   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   r7   r8   r   r   r9   pipesr;   r:   read)argsAkpuse_wurlitzerr<   r=   r>   r?   r@   outerrresultr%   rQ   r&   .test_newtons_method_function__ccode_parametersV   sD   


	
"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 ]\}}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)integerra      c                 S   s   t t| ||S rP   )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    )rg   acr%   r&   rR      s
    z<test_newtons_method_function__rtol_cse_nan.<locals>.<lambda>c                 S   s4   d| |  |  d|  |d  | |  |d   S )N   r   ri   r%   r%   r&   rR      s   4 c                 S   s<   |  |  | d| |  | |  |d d |  |   S )Nrd   rn   ro   ri   r%   r%   r&   rR      s   < )clamped_newtonhalley
halley_alt)FT<   g0.++gؗҬ<zencountered NaN.)	paramsitermaxrS   csecounterr"   rtolbounds
handle_nanc              
      s6   i | ]\}}|t  d | ditfi |qS )	func_name_b)r   dict).0rZ   kw)rX   kwargsr%   r&   
<dictcomp>   s   6 z>test_newtons_method_function__rtol_cse_nan.<locals>.<dictcomp>c                 S   s   i | ]	\}}|t |qS r%   )rI   )r~   rZ   vr%   r%   r&   r      s    r|   z"13.2261515064168768938151923226496gMg<g      @g{Gz?g      Y@2   d   rn   )r   r   r   r   r   r   r   itemsrJ   r   r:   )bN_geoN_totrb   N_ari	delta_ariln_delta_geoeqb_logmeth_kwuse_cser<   rL   rM   root_find_brZ   r   nsrefreftolguessmethr_   reqr%   )rg   rk   rX   rl   r   r&   *test_newtons_method_function__rtol_cse_nan   sH   &

	r   )3r7   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   rF   sympy.codegen.pyutilsrI   sympy.externalr   sympy.printing.codeprinterr   sympy.utilities._compilationr   r   r   !sympy.utilities._compilation.utilr   sympy.testing.pytestr   r   r   r   r'   rA   rH   rO   r`   r   r%   r%   r%   r&   <module>   s6    


1