
    g(:                         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	Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#dS )    N)StringIO)symbolsEq)autowrapbinary_functionCythonCodeWrapperUfuncifyCodeWrapperCodeWrapper)CCodeGen
C99CodeGenCodeGenArgumentListErrormake_routine)raises)TmpFileManagerfilec                     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sources         _/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/utilities/tests/test_autowrap.py
get_stringr      sJ     ZZFGHff/////__F
LLNNNM    c                      t          d          \  } }}| |z   |z  }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   expecteds           r   #test_cython_wrapper_scalar_functionr)   "   sy    gGAq!E19D64((G ,,H)G955F	#  Xr   c                      ddl m}  t          d          \  }}}t          t	                                }t          d | |||z                       }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sympy.core.relationalr,   r   r   r   r   r   r!   r,   r"   r#   r$   r'   r&   r   r(   s           r   test_cython_wrapper_outargr0   3   s    ......gGAq! ..H688Aq1u#5#566G)G955F	  Xr   c                      ddl m}  t          d          \  }}}t          t	                                }t          d | |||z   |z                       }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/   s           r   test_cython_wrapper_inoutargr2   F   s    ......gGAq! ..H688Aq1uqy#9#9::G)G955F	  Xr   c                  H   ddl m}  t          d          \  }}}t          d | |||z                       }t	          t                                }ddt          j        iz  }t          j	                    }t          j        |           t          j                            |d          }|                    ||           t!          |          5 }	|	                                }
d d d            n# 1 swxY w Y   |
|k    sJ t	          t                      d	d
gdgddgdgddgdddii          }ddt          j        iz  }|                    ||           t!          |          5 }	|	                                }
d d d            n# 1 swxY w Y   |
|k    sJ ddt          j        iz  }d|_        |                    ||           t!          |          5 }	|	                                }
d d d            n# 1 swxY w Y   |
|k    sJ t          j                     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_filesopenread_need_numpycleanup)r,   r"   r#   r$   r&   r'   r(   temp_dirsetup_file_pathf
setup_texts              r   !test_cython_wrapper_compile_flagsrP   W   s
   ......gGAq!688Aq1u#5#566G ,,H k)*+H" !!Hh'''gll8Z88OGx888	o		 !VVXX
              !!!! /CEZ.[/@.A,4g+>5AN2;[1I4IM[`Ka3b# # #H k)*+H" Gx888	o		 !VVXX
              !!!!  k)*!+H$  HGx888	o		 !VVXX
              !!!!s6   C11C58C5)F

FFG<<H H c                  j   ddl m}  ddlm}  |d           |d           |d          }}}d |||fD             \  }}} | |||z             }t	          d|          }	t          t                                }
t          |
j        |	g          }d	}|	                    |||
          }||k    sJ d S )Nr   r+   )Dummyr"   r#   r$   c                 6    g | ]}t          |j                  S  )strdummy_index).0ds     r   
<listcomp>z8test_cython_wrapper_unique_dummyvars.<locals>.<listcomp>   s"    >>>qAM**>>>r   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,   sympy.core.symbolrR   r   r   r   r   r!   format)r,   rR   r"   r#   r$   rZ   r[   r\   r%   r&   r'   r   expected_templater(   s                 r   $test_cython_wrapper_unique_dummyvarsr`      s    ......''''''eCjj%%**eeCjj!qA>>Q1I>>>D$8Aq1uD64((G ,,H)G955F	  !''T4'HHHXr   c                  D   t          d          \  } }}t          | |z   d          } |            t          | |z             k    sJ |j        dk    sJ |j        dk    sJ t          t          || |z             d          } |            t          | |z             k    sJ |j        dk    sJ |j        dk    sJ t          t          || |z   |z             d          } |            t          | |z   |z             k    sJ |j        dk    sJ |j        dk    sJ d S )Nx y zdummybackendzx, ynamelessr$   zx, y, z)r   r   rU   argsreturnsr   )r"   r#   r$   rN   s       r   test_autowrap_dummyri      sN   gGAq! 	Q(((A133#a!e**6V9
""""Aq1uw///A133#a!e**6V9Aq1uqy!!7333A133#a!eai..    6Y9r   c                     t          d          \  t          t          fd           t          t	          z             dg          }  |             t          z             k    sJ | j        dk    sJ | j        dk    sJ t          t          fd           t          t	          z   z             dg          }  |             t          z   z             k    sJ | j        dk    sJ | j        dk    sJ t          t	          z   z             df          }  |             t          z   z             k    sJ | j        dk    sJ | j        dk    sJ d S )	Nrb   c                  L    t          t           z             d g          S Nrc   re   rg   r   r   r"   r#   r$   s   r   <lambda>z$test_autowrap_args.<locals>.<lambda>   s+    XbAEll!.& .& .& r   rc   rm   zy, xr$   c                  T    t          t           z   z             d g          S rl   rn   ro   s   r   rp   z$test_autowrap_args.<locals>.<lambda>   s3    XbAEAI6F6F!Q.) .) .) r   zy, x, z)r   r   r   r   r   rU   rg   rh   )rN   r"   r#   r$   s    @@@r   test_autowrap_argsrr      s   gGAq!
# && && && && && && ' ' 'Aq1uwaV<<<A133#a!e**6V9
# &) &) &) &) &) &) * * *Aq1uqy!!7!QCCCA133#a!eai..    6Y9Aq1uqy!!7!QCCCA133#a!eai..    6Y9r   c                  J   t          d          \  } }t          j                    }t          j        |           t          | |z   d|          } |            t          | |z             k    sJ t          j        |t          j	                  sJ t          j
                     d S )Nx yrc   re   tempdir)r   rA   rB   r   rC   r   rU   rD   accessF_OKrK   )r"   r#   tmprN   s       r   test_autowrap_store_filesrz      s    5>>DAq



Cc"""Q555A133#a!e**9S"'"""""r   c                     t          d          \  } }d}t          j                    }t          j                    }	 t          j        |           t          | |z   d|          } |            t          | |z             k    sJ t          j        |t          j	                  sJ 	 t          j        |           t          j        |           d S # t          j        |           t          j        |           w xY w)Nrt   z./tmprc   ru   )r   rD   getcwdrA   rB   chdirr   rU   rw   rx   shutilrmtree)r"   r#   ry   	saved_cwdtemp_cwdrN   s         r   'test_autowrap_store_files_issue_gh12939r      s    5>>DAq
C	I!!H 
QUGS999qssc!a%jj    ybg&&&&&&
h 	hs   A*C *C;c                      t          d          \  } }t          d| |z   d          }|                                t          | |z             k    sJ d S )Nrt   rN   rc   rd   )r   r   _imp_rU   )r"   r#   rN   s      r   test_binary_functionr     sR    5>>DAqQUG444A7799AE

""""""r   c                      t          d          \  } }}t          t          d                    }t          d| |z   |z             }t	          |j        |g          }ddt          j        iz  }||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);
}
#endifr4   )r   r	   r   r   r   dump_cr
   r@   )r"   r#   r$   code_wrapperr&   r   r(   s          r   test_ufuncify_sourcer     s    gGAq!&z*'='=>>L61q519--G+gY77FW
n K/0oW1Hp Xr   c                  F   t          d          \  } }}| ||f| |dz  z   d|dz  z  z   t          t          d                    }fdt          t	                              D             }t          |j        |d          }d	d
t          j        iz  }||k    sJ d S )Nr      
      r   c           	          g | ]?}t          d                     |                              |                             @S )zfunc{})r   r^   diff)rW   ir%   var_symbolss     r   rY   z4test_ufuncify_source_multioutput.<locals>.<listcomp>s  sB    zzz]^X__Q//;q>1J1JKXXzzzr   	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);
}
#endifr4   )	r   r	   r   rangelenr   r   r
   r@   )	r"   r#   r$   r   r   r   r(   r%   r   s	          @@r    test_ufuncify_source_multioutputr   n  s    gGAq!a)Kq!t8bAgD&z*'='=>>LzzzzzbghklwhxhxbybyzzzH+XLLLF_
~ K/0_1H@ Xr   )r   )$rD   rA   r~   ior   
sympy.corer   r   sympy.utilities.autowrapr   r   r   r	   r
   sympy.utilities.codegenr   r   r   r   sympy.testing.pytestr   sympy.testing.tmpfilesr   r   r)   r0   r2   rP   r`   ri   rr   rz   r   r   r   r   rT   r   r   <module>r      s   
			         " " " " " " " "A A A A A A A A A A A A A A            ( ' ' ' ' ' 1 1 1 1 1 1     "  &  "W W Wr  *  &  ,	 	 	     # # #] ] ]@g g g g gr   