o
    §ƒðj:  ã                   @   sú   d dl Z d dlZd dlZd dlm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)ÚPath)Ú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)Údump_fnÚroutinesÚprefixÚkwargsÚoutputÚsource© r   ú¥/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-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Útestz�cdef 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   zŸcdef 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©Úsympy.core.relationalr)   r   r   r   r   r   r   ©r)   r    r!   r"   r%   r$   r   r&   r   r   r   Útest_cython_wrapper_outarg4   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   z“cdef 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_inoutargG   s   
ÿr.   c            
   
   C   s6  ddl m}  tdƒ\}}}td| ||| ƒƒ}ttƒ ƒ}ddtji }t 	¡ }t
 |¡ tj |d¡}|j||d� t|ƒ ¡ }	|	|ksFJ ‚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|ƒ ¡ }	|	|ksxJ ‚ddtji }d|_|j||d� t|ƒ ¡ }	|	|ks•J ‚t
 ¡  d S )Nr   r(   r   r   a¹  from setuptools import setup
from setuptools import Extension
from Cython.Build import cythonize
cy_opts = {'compiler_directives': {'language_level': '3'}}

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)Ú	build_dirz/usr/local/includez/opt/booger/includez/user/local/libÚthelibÚnilibz
-slow-mathz-lswampz	-ltridentÚcompiler_directivesÚboundscheckF)Úinclude_dirsÚlibrary_dirsÚ	librariesÚextra_compile_argsÚextra_link_argsÚcythonize_optionsa)  from setuptools import setup
from setuptools 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))
aN  from setuptools import setup
from setuptools 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ÚjoinÚ_prepare_filesr   Ú	read_textÚ_need_numpyÚcleanup)
r)   r    r!   r"   r$   r%   r&   Útemp_dirÚsetup_file_pathÚ
setup_textr   r   r   Ú!test_cython_wrapper_compile_flagsX   sF   
ñ

úñðrI   c                  C   s¢   ddl m}  ddlm} |dƒ|dƒ|dƒ}}}dd„ |||fD ƒ\}}}| ||| ƒ}td	|ƒ}	ttƒ ƒ}
t|
j|	gƒ}d
}|j	|||d�}||ksOJ ‚d S )Nr   r(   )ÚDummyr    r!   r"   c                 S   s   g | ]}t |jƒ‘qS r   )ÚstrÚdummy_index)Ú.0Údr   r   r   Ú
<listcomp>²   s    z8test_cython_wrapper_unique_dummyvars.<locals>.<listcomp>r   zåcdef 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)   Úsympy.core.symbolrJ   r   r   r   r   r   Úformat)r)   rJ   r    r!   r"   rP   rQ   rR   r#   r$   r%   r   Úexpected_templater&   r   r   r   Ú$test_cython_wrapper_unique_dummyvars®   s   

ÿ	rV   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, yÚnamelessr"   zx, y, z)r   r   rK   ÚargsÚreturnsr   )r    r!   r"   Úfr   r   r   Útest_autowrap_dummyÃ   s   r_   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sŽJ ‚| jd	ks•J ‚| jdksœJ ‚d S )
NrW   c                      s   t tˆˆ ˆ ƒdˆ gd�S ©NrX   ©rZ   r\   ©r   r   r   ©r    r!   r"   r   r   Ú<lambda>Ù   s    ÿz$test_autowrap_args.<locals>.<lambda>rX   ra   zy, xr"   c                      s    t tˆˆ ˆ ˆ ƒdˆ ˆgd�S r`   rb   r   rc   r   r   rd   à   s    ÿzy, x, z)r   r   r   r   r   rK   r\   r]   )r^   r   rc   r   Útest_autowrap_argsÖ   s   ""re   c                  C   sd   t dƒ\} }t ¡ }t |¡ t| | d|d�}|ƒ t| | ƒks#J ‚t |tj	¡s,J ‚t 
¡  d S )Núx yrX   ©rZ   Útempdir)r   r<   r=   r   r>   r   rK   r?   ÚaccessÚF_OKrE   )r    r!   Útmpr^   r   r   r   Útest_autowrap_store_filesì   s   
rl   c               	   C   s–   t dƒ\} }d}t ¡ }t ¡ }z/t |¡ t| | d|d�}|ƒ t| | ƒks*J ‚t |tj	¡s3J ‚W t |¡ t
 |¡ d S t |¡ t
 |¡ w )Nrf   z./tmprX   rg   )r   r?   Úgetcwdr<   r=   Úchdirr   rK   ri   rj   ÚshutilÚrmtree)r    r!   rk   Ú	saved_cwdÚtemp_cwdr^   r   r   r   Ú'test_autowrap_store_files_issue_gh12939÷   s   


ÿrs   c                  C   s:   t dƒ\} }td| | dd�}| ¡ t| | ƒksJ ‚d S )Nrf   r^   rX   rY   )r   r   Ú_imp_rK   )r    r!   r^   r   r   r   Útest_binary_function  s   ru   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   af	  #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}
};

#ifdef NPY_1_19_API_VERSION
static void test_ufunc(char **args, const npy_intp *dimensions, const npy_intp* steps, void* data)
#else
static void test_ufunc(char **args, npy_intp *dimensions, npy_intp* steps, void* data)
#endif
{
    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   W©Xry   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   é   é
   é   rv   c                    s(   g | ]}t d  |¡ˆ  ˆ| ¡ˆƒ‘qS )zfunc{})r   rT   Údiff)rM   Úi©r#   Úvar_symbolsr   r   rO   q  s   ( z4test_ufuncify_source_multioutput.<locals>.<listcomp>Ú	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}
};

#ifdef NPY_1_19_API_VERSION
static void multitest_ufunc(char **args, const npy_intp *dimensions, const npy_intp* steps, void* data)
#else
static void multitest_ufunc(char **args, npy_intp *dimensions, npy_intp* steps, void* data)
#endif
{
    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   rw   r
   r;   )r    r!   r"   rx   r   r   r&   r   r   r   Ú test_ufuncify_source_multioutputl  s   
_¡`r…   )r   )&r?   r<   ro   Úior   Úpathlibr   Ú
sympy.corer   r   Úsympy.utilities.autowrapr   r   r   r	   r
   Úsympy.utilities.codegenr   r   r   r   Úsympy.testing.pytestr   Úsympy.testing.tmpfilesr   r   r'   r-   r.   rI   rV   r_   re   rl   rs   ru   ry   r…   r   r   r   r   Ú<module>   s.   
V`