
    MZd\                         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 Zed        Zed        Z d Z!ed        Z"y)    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            	          t        j                  d      \  } }}t        j                  |       | dz  z
  }t        || ||      }|j	                  t        || |j                  |       z              sJ y )Nz	x dx atol   )spsymbolscosr   hasr   diff)xdxatolexpralgos        E/usr/lib/python3/dist-packages/sympy/codegen/tests/test_algorithms.pytest_newtons_methodr      sd    **[)KAr466!9q!tD$4,D88JrD51#56777    c                     t        j                  dd      } t        j                  |       | dz  z
  }t        ||       }t        st        d       t               st        d       ddi}t        j                         5 }t        d	d
t        |      z   fddj                  d      dz   fg||      \  }}t        |j                  d      dz
        dk  sJ 	 d d d        y # 1 sw Y   y xY w)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_newton)r   r   func
compile_kwfoldermodinfos          r   #test_newtons_method_function__ccoder<      s    
		#D!A66!9q!tD"4+D$%7#$J		$	$	& 	@&/ 149$K@ A;BB3G66 81
 J8	T 3==%67%???	@ 	@ 	@s   =ACC(c                     t        j                  dd      } t        j                  |       | dz  z
  }t        || t	        d      g      }t
        st        d       t               st        d	       t        |gd
      }t        j                         5 }t        d|fddj                  d      dz   fg|      \  }}t        |j                  d      dz
        dk  sJ 	 d d d        y # 1 sw Y   y xY w)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/   r0   )r   r1   r   r   r   r   r   r   f_moduler2   r3   r
   r4   r5   r6   )r   r   r7   f_modr9   r:   r;   s          r   #test_newtons_method_function__fcoderD   0   s    
		#D!A66!9q!tD"46x3H2IJD$%=)*dV\*E		$	$	& @&/5!;BB3G77 91
 	T 3==%67%???@ @ @s   AC((C1c                      t        j                  dd      } t        j                  |       | dz  z
  }t        ||       }t	        |      }i }t        |||       t        d|      }t        |dz
        dk  sJ y )Nr   Tr"   r   znewton(0.5)r/   r0   )r   r1   r   r   	py_moduleexecevalr5   )r   r   r7   py_mod	namespaceress         r   $test_newtons_method_function__pycoderL   G   sr    
		#D!A66!9q!tD"4+Dt_FII&
}i
(Cs^#$u,,,r    c                  *   t        j                  d      x} \  }}}|t        j                  |z        z  |dz  z  z
  t        t        fd       t
        }t        | |      }t               st        d       t        st        d       ddi}t        j                         5 }t        d	d
t        |      z   fddj                  d      dz   fg||      \  }}	|r2t
        j                         5 \  }
}|j!                  d      }d d d        n|j!                  d      }t#        dz
        dk  sJ |st        d       
j%                         j%                         }}
|dk(  sJ |
dk(  sJ 	 d d d        y # 1 sw Y   axY w# 1 sw Y   y xY w)Nzx A k pr   c                      t               S )N)r   )r   r   s   r   <lambda>z@test_newtons_method_function__ccode_parameters.<locals>.<lambda>V   s    6tQ? r    )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/   r0   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   r2   r3   r
   r	   r4   pipesr6   r5   read)argsAkpuse_wurlitzerr7   r8   r9   r:   r;   outerrresultr   r   s                @@r   .test_newtons_method_function__ccode_parametersr]   R   s   

9--D:1aARVVAaC[=1QT6!D
:?@M"4DFD7#$$%J		$	$	& &/ 149$K@ A!?!F!Fs!K"?"? A1
 %8	T " ,jsCs+, , ]]3'F6N*+e333TU88:sxxzSbyy   	 / , , s&   *AF	6E=A(F	=F	F		F)#r2   sympyr   sympy.codegen.astr   sympy.codegen.algorithmsr   r   sympy.codegen.fnodesr   sympy.codegen.futilsr   rB   sympy.codegen.pyutilsrF   sympy.externalr   sympy.printing.codeprinterr	   sympy.utilities._compilationr
   r   r   !sympy.utilities._compilation.utilr   sympy.testing.pytestr   r   r   r   r   r<   rD   rL   r]    r    r   <module>rj      s      ( L ' = ? ( , X X 7 -	x	 +&	8 @ @. @ @,- - -r    