
    Rie\                        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"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            	         t          j        d          \  } }}t          j        |           | dz  z
  }t          || ||          }|                    t          || |                    |           z                      sJ d S )Nz	x dx atol   )spsymbolscosr   hasr   diff)xdxatolexpralgos        Clib/python3.11/site-packages/sympy/codegen/tests/test_algorithms.pytest_newtons_methodr      sy    *[))KAr46!99q!tD$4,,D88JrD51#5667777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                    d          dz   fg||          \  }}t          |                    d          dz
            dk     sJ 	 d d d            d S # 1 swxY w Y   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_newton)r   r   func
compile_kwfoldermodinfos          r   #test_newtons_method_function__ccoder<      s   
	#D!!!A6!99q!tD"4++D &$%%%77 %#$$$J		$	&	& 	@&/ 149$KK@ A;BB3GG66 81
 J8 8 8	T 3==%%677%?????	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   A-DD
D
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                    d          dz   fg|          \  }}t          |                    d          dz
            dk     sJ 	 d d d            d S # 1 swxY 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/   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6!99q!tD"46x3H3H3H2IJJJD &$%%%== +)***dV\**E		$	&	& @&/5!;BB3GG77 91
   	T 3==%%677%?????@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @s   )ADDDc                     t          j        dd          } t          j        |           | dz  z
  }t          ||           }t	          |          }i }t          |||           t          d|          }t          |dz
            dk     sJ d S )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   s    
	#D!!!A6!99q!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                    d          dz   fg||          \  }}	|rJt
                                          5 \  }
}|                    d          }d d d            n# 1 swxY w Y   n|                    d          }t#          |dz
            dk     sJ |st          d           |
                                |                                }}
|dk    sJ |
dk    sJ 	 d d d            d S # 1 swxY w Y   d S )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RVAaC[[=1QT6!D
:?????@@@M"4DFFFD77 %#$$$ &$%%%J		$	&	& &/ 149$KK@ A!?!F!Fs!K!K"?"? A1
 %8 8 8	T  	("" ,jsCs++, , , , , , , , , , , , , , , ]]3''F6N*++e3333 	VTUUU88::sxxzzSbyyyy      /                 s8   >AGE 4G E	GE	A:GGG)#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 L L L L L L L ' ' ' ' ' ' = = = = = = ? ? ? ? ? ? ( ( ( ( ( ( , , , , , , X X X X X X X X X X 7 7 7 7 7 7 - - - - - - - -	x	 	 M+&&	8 8 8 @ @ @. @ @ @,- - - - - - - -r    