U
    -e\                     @   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|| ||  sPtd S )Nz	x dx atol   )spsymbolscosr   hasr   diffAssertionError)xZdxZatolexpralgo r   d/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/sympy/codegen/tests/test_algorithms.pytest_newtons_method   s    r   c               	   C   s   t jddd} t | | d  }t|| }ts6td t sDt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   Symbolr   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*    


 
r8   c               	   C   s   t jddd} t | | d  }t|| tddgd}tsBtd t sPt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%   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	   r0   r1   r2   r   )r   r   r3   Zf_modr5   r6   r7   r   r   r   #test_newtons_method_function__fcode0   s$    


r<   c                  C   sd   t j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)r+   r,   )	r   r-   r   r   	py_moduleexecevalr1   r   )r   r   r3   Zpy_mod	namespaceresr   r   r   $test_newtons_method_function__pycodeG   s    

rB   c               
      sH  t d } \}}}|t |  |d    tt fdd t}t | |d}t sjtd t	svt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s:tW 5 Q R X d S )Nzx A k pr   c                      s
   t  S )N)r   r   r   r   r   r   <lambda>V       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   r0   Zpipesr2   r1   r   read)argsAkpZuse_wurlitzerr3   r4   r5   r6   r7   outerrresultr   rC   r   .test_newtons_method_function__ccode_parametersR   s@    

 
	
rQ   )#r.   Zsympyr   Zsympy.codegen.astr   Zsympy.codegen.algorithmsr   r   Zsympy.codegen.fnodesr   Zsympy.codegen.futilsr   r;   Zsympy.codegen.pyutilsr=   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   r8   r<   rB   rQ   r   r   r   r   <module>   s*   

