U
    L?h                     @   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|| ||  sLtd S )Nz	x dx atol   )r   r	   r   hasr
   diffAssertionError)xZdxatolexpralgo r%   U/var/www/html/venv/lib/python3.8/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 P}td	d
t	| fdd
dd fg||d\}}t|dd dk stW 5 Q R X 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_newtonr    )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 sLtd	 t|gd
}t	 F}t
d|fdddd fg|d\}}t|dd dk stW 5 Q R X d S )Nr!   Tr(   r   Znewton)name)attrsr*   zNo Fortran compiler found.Z
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#   r<   Zf_modr>   r?   r@   r%   r%   r&   #test_newtons_method_function__fcode4   s$    


rE   c                  C   s`   t ddd} t| | d  }t|| }t|}i }t||| td|}t|d dk s\td S )Nr!   Tr(   r   znewton(0.5)r5   r6   )r   r	   r   	py_moduleexecevalr:   r    )r!   r#   r<   py_mod	namespaceresr%   r%   r&   $test_newtons_method_function__pycodeK   s    

rL   c               
      sD  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 5 Q R X n
|d}t|d dk st|std |
 |  }
}|dks(t|
dks6tW 5 Q R X d S )Nzx A k pr   c                      s
   t  S N)r   r%   r#   r!   r%   r&   <lambda>Z       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   Zpipesr;   r:   r    read)argsAkpZuse_wurlitzerr<   r=   r>   r?   r@   outerrresultr%   rN   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 sxtqxqd S )!Nza b c N_geo N_totT)r)   nonnegativei)integerr]      c                 S   s   t t| ||S rM   )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!   )rc   acr%   r&   rO      s   z<test_newtons_method_function__rtol_cse_nan.<locals>.<lambda>c                 S   s4   d| |  |  d|  |d  | |  |d   S )N   r   rd   r%   r%   r&   rO      rP   c                 S   s<   |  |  | d| |  | |  |d d |  |   S )Nr`   ri   rj   rd   r%   r%   r&   rO      rP   )Zclamped_newtonZhalleyZ
halley_alt)FT<   g0.++gؗҬ<zencountered NaN.)	paramsZitermaxrQ   Zcsecounterr"   ZrtolZboundsZ
handle_nanc              	      s2   i | ]*\}}|t  d | ditf|qS )	func_name_b)r   dict).0rW   kw)rU   kwargsr%   r&   
<dictcomp>   s      z>test_newtons_method_function__rtol_cse_nan.<locals>.<dictcomp>c                 S   s   i | ]\}}|t |qS r%   )rF   )rq   rW   vr%   r%   r&   rt      s      ro   z"13.2261515064168768938151923226496gMg<g      @g{Gz?g      Y@2   d   ri   )r   r   r   r   r   r   r   itemsrG   r   r:   r    )bZN_geoZN_totr^   ZN_ariZ	delta_ariZln_delta_geoZeqb_logZmeth_kwZuse_cser<   rI   rJ   Zroot_find_brW   ru   nsrefZreftolguessmethr[   reqr%   )rc   rf   rU   rg   rs   r&   *test_newtons_method_function__rtol_cse_nan   sP    &

	
      r   )3r7   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   rD   Zsympy.codegen.pyutilsrF   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'   rA   rE   rL   r\   r   r%   r%   r%   r&   <module>   s4   


1