o
    8Va_9                     @   s   d dl Z 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	m
Z
mZmZmZ d dlmZmZmZmZ d dlmZ d dlmZ d#d	d
Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z dd Z!dd  Z"d!d" Z#dS )$    N)StringIO)symbolsEq)autowrapbinary_functionCythonCodeWrapperUfuncifyCodeWrapperCodeWrapper)CCodeGen
C99CodeGenCodeGenArgumentListErrormake_routine)raises)TmpFileManagerfilec                 K   s.   t  }| |||fi | | }|  |S )a5  Wrapper for dump_fn. dump_fn writes its results to a stream object and
       this wrapper returns the contents of that stream as a string. This
       auxiliary function is used by many tests below.

       The header and the empty lines are not generator to facilitate the
       testing of the output.
    )r   getvalueclose)Zdump_fnroutinesprefixkwargsoutputsource r   E/usr/lib/python3/dist-packages/sympy/utilities/tests/test_autowrap.py
get_string   s
   r   c                  C   sP   t d\} }}| | | }td|}tt }t|j|g}d}||ks&J d S )Nx,y,ztestzcdef extern from 'file.h':
    double test(double x, double y, double z)

def test_c(double x, double y, double z):

    return test(x, y, z))r   r   r   r
   r   dump_pyx)xyzexprroutinecode_genr   expectedr   r   r   #test_cython_wrapper_scalar_function"   s   

r%   c                  C   sZ   ddl m}  td\}}}tt }td| ||| }t|j|g}d}||ks+J d S )Nr   Equalityr   r   zcdef extern from 'file.h':
    void test(double x, double y, double *z)

def test_c(double x, double y):

    cdef double z = 0
    test(x, y, &z)
    return zsympyr'   r   r   r   r   r   r   r'   r   r   r    r#   r"   r   r$   r   r   r   test_cython_wrapper_outarg3   s   
	r+   c                  C   s^   ddl m}  td\}}}tt }td| ||| | }t|j|g}d}||ks-J d S )Nr   r&   r   r   zcdef extern from 'file.h':
    void test(double x, double y, double *z)

def test_c(double x, double y, double z):

    test(x, y, &z)
    return zr(   r*   r   r   r   test_cython_wrapper_inoutargF   s   
r,   c               
   C   s  ddl m}  td\}}}td| ||| }tt }ddtji }t	 }t
| tj|d}|j||d t|}	|	 }
W d    n1 sMw   Y  |
|ksXJ tt d	d
gdgddgdgddgdddiid}ddtji }|j||d t|}	|	 }
W d    n1 sw   Y  |
|ksJ ddtji }d|_|j||d t|}	|	 }
W d    n1 sw   Y  |
|ksJ t
  d S )Nr   r&   r   r   a  try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension
from Cython.Build import cythonize
cy_opts = {}

ext_mods = [Extension(
    'wrapper_module_%(num)s', ['wrapper_module_%(num)s.pyx', 'wrapped_code_%(num)s.c'],
    include_dirs=[],
    library_dirs=[],
    libraries=[],
    extra_compile_args=['-std=c99'],
    extra_link_args=[]
)]
setup(ext_modules=cythonize(ext_mods, **cy_opts))
numzsetup.py)Z	build_dirz/usr/local/includez/opt/booger/includez/user/local/libZthelibZnilibz
-slow-mathz-lswampz	-ltridentZcompiler_directivesZboundscheckF)Zinclude_dirsZlibrary_dirsZ	librariesZextra_compile_argsZextra_link_argsZcythonize_optionsa  try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension
from Cython.Build import cythonize
cy_opts = {'compiler_directives': {'boundscheck': False}}

ext_mods = [Extension(
    'wrapper_module_%(num)s', ['wrapper_module_%(num)s.pyx', 'wrapped_code_%(num)s.c'],
    include_dirs=['/usr/local/include', '/opt/booger/include'],
    library_dirs=['/user/local/lib'],
    libraries=['thelib', 'nilib'],
    extra_compile_args=['-slow-math', '-std=c99'],
    extra_link_args=['-lswamp', '-ltrident']
)]
setup(ext_modules=cythonize(ext_mods, **cy_opts))
a  try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension
from Cython.Build import cythonize
cy_opts = {'compiler_directives': {'boundscheck': False}}
import numpy as np

ext_mods = [Extension(
    'wrapper_module_%(num)s', ['wrapper_module_%(num)s.pyx', 'wrapped_code_%(num)s.c'],
    include_dirs=['/usr/local/include', '/opt/booger/include', np.get_include()],
    library_dirs=['/user/local/lib'],
    libraries=['thelib', 'nilib'],
    extra_compile_args=['-slow-math', '-std=c99'],
    extra_link_args=['-lswamp', '-ltrident']
)]
setup(ext_modules=cythonize(ext_mods, **cy_opts))
T)r)   r'   r   r   r   r
   r	   _module_countertempfilemkdtempr   
tmp_folderospathjoinZ_prepare_filesopenreadZ_need_numpycleanup)r'   r   r   r    r"   r#   r$   Ztemp_dirZsetup_file_pathfZ
setup_textr   r   r   !test_cython_wrapper_compile_flagsW   sR   








r9   c                  C   s   ddl m} m} | d| d| d}}}dd |||fD \}}}|||| }td|}	tt }
t|
j|	g}d	}|j|||d
}||ksKJ d S )Nr   )Dummyr'   r   r   r    c                 S   s   g | ]}t |jqS r   )strZdummy_index).0dr   r   r   
<listcomp>   s    z8test_cython_wrapper_unique_dummyvars.<locals>.<listcomp>r   zcdef extern from 'file.h':
    void test(double x_{x_id}, double y_{y_id}, double *z_{z_id})

def test_c(double x_{x_id}, double y_{y_id}):

    cdef double z_{z_id} = 0
    test(x_{x_id}, y_{y_id}, &z_{z_id})
    return z_{z_id})x_idy_idz_id)	r)   r:   r'   r   r   r
   r   r   format)r:   r'   r   r   r    r?   r@   rA   r!   r"   r#   r   Zexpected_templater$   r   r   r   $test_cython_wrapper_unique_dummyvars   s   

	rC   c                  C   s   t d\} }}t| | dd}| t| | ksJ |jdks!J |jdks(J tt|| | dd}| t| | ks>J |jdksEJ |jdksLJ tt|| | | dd}| t| | | ksfJ |jdksmJ |jdkstJ d S )Nx y zdummybackendzx, yZnamelessr    zx, y, z)r   r   r;   argsreturnsr   )r   r   r    r8   r   r   r   test_autowrap_dummy   s   rJ   c                     s<  t d\ tt fdd tt  d gd} |  t  ks+J | jdks2J | jdks9J tt fdd tt   d gd} |  t   ksbJ | jd	ksiJ | jdkspJ tt   d fd} |  t   ksJ | jd	ksJ | jdksJ d S )
NrD   c                      s   t t  d gdS NrE   rG   rH   r   r   r   r   r   r    r   r   <lambda>   s    z$test_autowrap_args.<locals>.<lambda>rE   rL   zy, xr    c                      s    t t   d gdS rK   rM   r   rN   r   r   rO      s    zy, x, z)r   r   r   r   r   r;   rH   rI   )r8   r   rN   r   test_autowrap_args   s   ""rP   c                  C   sd   t d\} }t }t| t| | d|d}| t| | ks#J t|tj	s,J t
  d S )Nx yrE   rG   Ztempdir)r   r/   r0   r   r1   r   r;   r2   accessF_OKr7   r   r   tmpr8   r   r   r   test_autowrap_store_files   s   
rW   c               	   C   sh   t d\} }d}z%t| | d|d}| t| | ksJ t|tjs&J W t| d S t| w )NrQ   z./tmprE   rR   )r   r   r;   r2   rS   rT   shutilrmtreerU   r   r   r   'test_autowrap_store_files_issue_gh12939  s   rZ   c                  C   s:   t d\} }td| | dd}| t| | ksJ d S )NrQ   r8   rE   rF   )r   r   Z_imp_r;   )r   r   r8   r   r   r   test_binary_function  s   r[   c                  C   sX   t d\} }}ttd}td| | | }t|j|g}ddtji }||ks*J d S )Nr   ufuncifyr   a  #include "Python.h"
#include "math.h"
#include "numpy/ndarraytypes.h"
#include "numpy/ufuncobject.h"
#include "numpy/halffloat.h"
#include "file.h"

static PyMethodDef wrapper_module_%(num)sMethods[] = {
        {NULL, NULL, 0, NULL}
};

static void test_ufunc(char **args, npy_intp *dimensions, npy_intp* steps, void* data)
{
    npy_intp i;
    npy_intp n = dimensions[0];
    char *in0 = args[0];
    char *in1 = args[1];
    char *in2 = args[2];
    char *out0 = args[3];
    npy_intp in0_step = steps[0];
    npy_intp in1_step = steps[1];
    npy_intp in2_step = steps[2];
    npy_intp out0_step = steps[3];
    for (i = 0; i < n; i++) {
        *((double *)out0) = test(*(double *)in0, *(double *)in1, *(double *)in2);
        in0 += in0_step;
        in1 += in1_step;
        in2 += in2_step;
        out0 += out0_step;
    }
}
PyUFuncGenericFunction test_funcs[1] = {&test_ufunc};
static char test_types[4] = {NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE};
static void *test_data[1] = {NULL};

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    "wrapper_module_%(num)s",
    NULL,
    -1,
    wrapper_module_%(num)sMethods,
    NULL,
    NULL,
    NULL,
    NULL
};

PyMODINIT_FUNC PyInit_wrapper_module_%(num)s(void)
{
    PyObject *m, *d;
    PyObject *ufunc0;
    m = PyModule_Create(&moduledef);
    if (!m) {
        return NULL;
    }
    import_array();
    import_umath();
    d = PyModule_GetDict(m);
    ufunc0 = PyUFunc_FromFuncAndData(test_funcs, test_data, test_types, 1, 3, 1,
            PyUFunc_None, "wrapper_module_%(num)s", "Created in SymPy with Ufuncify", 0);
    PyDict_SetItemString(d, "test", ufunc0);
    Py_DECREF(ufunc0);
    return m;
}
#else
PyMODINIT_FUNC initwrapper_module_%(num)s(void)
{
    PyObject *m, *d;
    PyObject *ufunc0;
    m = Py_InitModule("wrapper_module_%(num)s", wrapper_module_%(num)sMethods);
    if (m == NULL) {
        return;
    }
    import_array();
    import_umath();
    d = PyModule_GetDict(m);
    ufunc0 = PyUFunc_FromFuncAndData(test_funcs, test_data, test_types, 1, 3, 1,
            PyUFunc_None, "wrapper_module_%(num)s", "Created in SymPy with Ufuncify", 0);
    PyDict_SetItemString(d, "test", ufunc0);
    Py_DECREF(ufunc0);
}
#endifr-   )r   r   r   r   r   dump_cr	   r.   )r   r   r    code_wrapperr"   r   r$   r   r   r   test_ufuncify_source  s   STr_   c                     s   t d\} }}| ||f| |d  d|d    ttd} fddttD }t|j|dd	}d
dtji }||ksAJ d S )Nr      
      r\   c                    s(   g | ]}t d | | qS )zfunc{})r   rB   Zdiff)r<   ir!   Zvar_symbolsr   r   r>   w  s   ( z4test_ufuncify_source_multioutput.<locals>.<listcomp>Z	multitest)funcnamea
  #include "Python.h"
#include "math.h"
#include "numpy/ndarraytypes.h"
#include "numpy/ufuncobject.h"
#include "numpy/halffloat.h"
#include "file.h"

static PyMethodDef wrapper_module_%(num)sMethods[] = {
        {NULL, NULL, 0, NULL}
};

static void multitest_ufunc(char **args, npy_intp *dimensions, npy_intp* steps, void* data)
{
    npy_intp i;
    npy_intp n = dimensions[0];
    char *in0 = args[0];
    char *in1 = args[1];
    char *in2 = args[2];
    char *out0 = args[3];
    char *out1 = args[4];
    char *out2 = args[5];
    npy_intp in0_step = steps[0];
    npy_intp in1_step = steps[1];
    npy_intp in2_step = steps[2];
    npy_intp out0_step = steps[3];
    npy_intp out1_step = steps[4];
    npy_intp out2_step = steps[5];
    for (i = 0; i < n; i++) {
        *((double *)out0) = func0(*(double *)in0, *(double *)in1, *(double *)in2);
        *((double *)out1) = func1(*(double *)in0, *(double *)in1, *(double *)in2);
        *((double *)out2) = func2(*(double *)in0, *(double *)in1, *(double *)in2);
        in0 += in0_step;
        in1 += in1_step;
        in2 += in2_step;
        out0 += out0_step;
        out1 += out1_step;
        out2 += out2_step;
    }
}
PyUFuncGenericFunction multitest_funcs[1] = {&multitest_ufunc};
static char multitest_types[6] = {NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE};
static void *multitest_data[1] = {NULL};

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    "wrapper_module_%(num)s",
    NULL,
    -1,
    wrapper_module_%(num)sMethods,
    NULL,
    NULL,
    NULL,
    NULL
};

PyMODINIT_FUNC PyInit_wrapper_module_%(num)s(void)
{
    PyObject *m, *d;
    PyObject *ufunc0;
    m = PyModule_Create(&moduledef);
    if (!m) {
        return NULL;
    }
    import_array();
    import_umath();
    d = PyModule_GetDict(m);
    ufunc0 = PyUFunc_FromFuncAndData(multitest_funcs, multitest_data, multitest_types, 1, 3, 3,
            PyUFunc_None, "wrapper_module_%(num)s", "Created in SymPy with Ufuncify", 0);
    PyDict_SetItemString(d, "multitest", ufunc0);
    Py_DECREF(ufunc0);
    return m;
}
#else
PyMODINIT_FUNC initwrapper_module_%(num)s(void)
{
    PyObject *m, *d;
    PyObject *ufunc0;
    m = Py_InitModule("wrapper_module_%(num)s", wrapper_module_%(num)sMethods);
    if (m == NULL) {
        return;
    }
    import_array();
    import_umath();
    d = PyModule_GetDict(m);
    ufunc0 = PyUFunc_FromFuncAndData(multitest_funcs, multitest_data, multitest_types, 1, 3, 3,
            PyUFunc_None, "wrapper_module_%(num)s", "Created in SymPy with Ufuncify", 0);
    PyDict_SetItemString(d, "multitest", ufunc0);
    Py_DECREF(ufunc0);
}
#endifr-   )	r   r   r   rangelenr   r]   r	   r.   )r   r   r    r^   r   r   r$   r   rd   r    test_ufuncify_source_multioutputr  s   
[\rh   )r   )$r2   r/   rX   ior   Z
sympy.corer   r   Zsympy.utilities.autowrapr   r   r   r   r	   Zsympy.utilities.codegenr
   r   r   r   Zsympy.testing.pytestr   Zsympy.testing.tmpfilesr   r   r%   r+   r,   r9   rC   rJ   rP   rW   rZ   r[   r_   rh   r   r   r   r   <module>   s,   
e\