o
    *ήc^                     @   s   d dl Z d dl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 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 Z!edd Z"dS )    N)
Assignment)newtons_methodnewtons_method_function)bind_C)render_as_module)import_module)ccode)compile_link_import_stringshas_chas_fortran)	may_xfail)skipraisescython	wurlitzerc                  C   sT   t d\} }}t | | d  }t|| ||}|t|| ||  s(J d S )Nz	x dx atol   )spsymbolscosr   hasr   diff)xdxatolexpralgo r   J/tmp/pip-target-vg8gfxp4/lib/python/sympy/codegen/tests/test_algorithms.pytest_newtons_method   s   $r   c                  C   s   t jddd} t | | d  }t|| }tstd t s"td tdd}t	 .}t
d	d
t| 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   Trealr   cython not installed.No C compiler found.c99stdz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   Symbolr   r   r   r   r
   dict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 j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 sSJ 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'   r(   r)   zRcdef extern double newton(double*)
def py_newton(double x):
    return newton(&x)
)r*   r,   r-   r.   )r   r/   r   r   r   r   r   r   f_moduler1   r2   r	   r3   r4   r5   )r   r   r6   f_modr8   r9   r:   r   r   r   #test_newtons_method_function__fcode0   s&   


"rB   c                  C   sd   t jddd} t | | d  }t|| }t|}i }t||| td|}t|d dk s0J d S )Nr   Tr   r   znewton(0.5)r-   r.   )r   r/   r   r   	py_moduleexecevalr4   )r   r   r6   py_mod	namespaceresr   r   r   $test_newtons_method_function__pycodeG   s   

rI   c               	      sn  t d } \}}}|t |  |d    tt fdd t}t | |d}t s5td t	s;td t
dd	}t i}td
dt| fdddd fg||d\}}	|r{t \}
}|d}W d    n1 suw   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>V   s   
 z@test_newtons_method_function__ccode_parameters.<locals>.<lambda>)debugr"   r!   r#   r$   znewton_par.cr&   z_newton_par.pyxr(   r)   zycdef extern double newton(double, double, double, double)
def py_newton(x, A=1, k=1, p=1):
    return newton(x, A, k, p)
)r+   r*   r,   r-   r.   zAC-level output only tested when package 'wurlitzer' is available. zx=         0.5 d_x=     0.61214
x=      1.1121 d_x=    -0.20247
x=     0.90967 d_x=   -0.042409
x=     0.86726 d_x=  -0.0017867
x=     0.86548 d_x= -3.1022e-06
x=     0.86547 d_x= -9.3421e-12
x=     0.86547 d_x=  3.6902e-17
)r   r   r   r   
ValueErrorr   r   r
   r   r   r0   r1   r2   r	   r   r3   pipesr5   r4   read)argsAkpuse_wurlitzerr6   r7   r8   r9   r:   outerrresultr   rJ   r   .test_newtons_method_function__ccode_parametersR   sD   



	
"rY   )#r1   sympyr   sympy.codegen.astr   sympy.codegen.algorithmsr   r   sympy.codegen.fnodesr   sympy.codegen.futilsr   r@   sympy.codegen.pyutilsrC   sympy.externalr   sympy.printing.codeprinterr   sympy.utilities._compilationr	   r
   r   !sympy.utilities._compilation.utilr   sympy.testing.pytestr   r   r   r   r   r;   rB   rI   rY   r   r   r   r   <module>   s,    

