o
    8Va^                     @   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   Zhasr   Zdiff)xZdxZatolexprZalgo r   E/usr/lib/python3/dist-packages/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Z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*   




"r5   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   Znewton)name)Zattrsr   zNo Fortran compiler found.Z
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_moduler+   r,   r	   r-   r.   r/   )r   r   r0   Zf_modr2   r3   r4   r   r   r   #test_newtons_method_function__fcode0   s&   


"r8   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evalr.   )r   r   r0   Zpy_mod	namespaceresr   r   r   $test_newtons_method_function__pycodeG   s   

r>   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   r*   r+   r,   r	   r   r-   Zpipesr/   r.   read)argsAkpZuse_wurlitzerr0   r1   r2   r3   r4   outerrresultr   r?   r   .test_newtons_method_function__ccode_parametersR   sD   



	
"rL   )#r+   Zsympyr   Zsympy.codegen.astr   Zsympy.codegen.algorithmsr   r   Zsympy.codegen.fnodesr   Zsympy.codegen.futilsr   r7   Zsympy.codegen.pyutilsr9   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   r5   r8   r>   rL   r   r   r   r   <module>   s,    

