diff options
Diffstat (limited to 'doc/source')
-rw-r--r-- | doc/source/user/c-info.ufunc-tutorial.rst | 146 |
1 files changed, 126 insertions, 20 deletions
diff --git a/doc/source/user/c-info.ufunc-tutorial.rst b/doc/source/user/c-info.ufunc-tutorial.rst index f1adef59a..2eb479463 100644 --- a/doc/source/user/c-info.ufunc-tutorial.rst +++ b/doc/source/user/c-info.ufunc-tutorial.rst @@ -225,7 +225,7 @@ the primary thing that must be changed to create your own ufunc. #include "math.h" #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" - #include "numpy/halffloat.h" + #include "numpy/npy_3kcompat.h" /* * single_type_logit.c @@ -243,7 +243,6 @@ the primary thing that must be changed to create your own ufunc. * docs.python.org . */ - static PyMethodDef LogitMethods[] = { {NULL, NULL, 0, NULL} }; @@ -276,12 +275,47 @@ the primary thing that must be changed to create your own ufunc. PyUFuncGenericFunction funcs[1] = {&double_logit}; /* These are the input and return dtypes of logit.*/ - char types[2] = {NPY_DOUBLE, NPY_DOUBLE}; - void *data[1] = {NULL}; + #if defined(NPY_PY3K) + static struct PyModuleDef moduledef = { + PyModuleDef HEAT_INIT, + "npspam", + NULL, + -1, + LogitMethods, + NULL, + NULL, + NULL, + NULL + }; + #endif + + #if defined(NPY_PY3K) + PyObject *PyInit__npspam(void) { + PyObject *m; + m = PyModule_Create(&moduledef); + if (!m) { + return NULL; + } + + import_array(); + import_umath(); + + logit = PyUFunc_FromFuncAndData(funcs,data, types, 1, 1, 1, + PyUFunc_None, "logit", + "logit_docstring", 0); + + d = PyModule_GetDict(m); + + PyDict_SetItemString(d, "logit", logit); + Py_DECREF(logit); + + return m; + } + #else PyMODINIT_FUNC initnpspam(void) { PyObject *m, *logit, *d; @@ -304,7 +338,7 @@ the primary thing that must be changed to create your own ufunc. PyDict_SetItemString(d, "logit", logit); Py_DECREF(logit); } - + #endif This is a setup.py file for the above code. As before, the module can be build via calling python setup.py build at the command prompt, @@ -516,15 +550,51 @@ the primary thing that must be changed to create your own ufunc. NPY_FLOAT, NPY_FLOAT, NPY_DOUBLE,NPY_DOUBLE, NPY_LONGDOUBLE, NPY_LONGDOUBLE}; + void *data[4] = {NULL, NULL, NULL, NULL}; + #if defined(NPY_PY3K) + static struct PyModuleDef moduledef = { + PyModuleDef HEAT_INIT, + "npspam", + NULL, + -1, + LogitMethods, + NULL, + NULL, + NULL, + NULL + }; + #endif - void *data[4] = {NULL, NULL, NULL, NULL}; + #if defined(NPY_PY3K) + PyObject *PyInit__npspam(void) { + PyObject *m; + m = PyModule_Create(&moduledef); + if (!m) { + return NULL; + } + + import_array(); + import_umath(); + + logit = PyUFunc_FromFuncAndData(funcs,data, types, 4, 1, 1, + PyUFunc_None, "logit", + "logit_docstring", 0); + + d = PyModule_GetDict(m); - PyMODINIT_FUNC initnpspam(void){ + PyDict_SetItemString(d, "logit", logit); + Py_DECREF(logit); + + return m; + } + #else + PyMODINIT_FUNC initnpspam(void) + { PyObject *m, *logit, *d; - m = Py_InitModule("npspam", LogitMethods); + m = Py_InitModule("npspam", LogitMethods); if (m == NULL) { return; } @@ -541,7 +611,7 @@ the primary thing that must be changed to create your own ufunc. PyDict_SetItemString(d, "logit", logit); Py_DECREF(logit); } - + #endif This is a setup.py file for the above code. As before, the module can be build via calling python setup.py build at the command prompt, @@ -700,30 +770,66 @@ as well as all other properties of a ufunc. void *data[1] = {NULL}; - PyMODINIT_FUNC initnpspam(void){ - PyObject *m, *logitprod, *d; - + #if defined(NPY_PY3K) + static struct PyModuleDef moduledef = { + PyModuleDef HEAT_INIT, + "npspam", + NULL, + -1, + LogitMethods, + NULL, + NULL, + NULL, + NULL + }; + #endif - m = Py_InitModule("npspam", LogitMethods); - if (m == NULL) { - return; + #if defined(NPY_PY3K) + PyObject *PyInit__npspam(void) { + PyObject *m; + m = PyModule_Create(&moduledef); + if (!m) { + return NULL; } import_array(); import_umath(); - logitprod = PyUFunc_FromFuncAndData(funcs,data, types, 1, 2, 2, - PyUFunc_None, "logitprod", - "logitprod_docstring", 0); + logit = PyUFunc_FromFuncAndData(funcs,data, types, 1, 2, 2, + PyUFunc_None, "logit", + "logit_docstring", 0); d = PyModule_GetDict(m); - PyDict_SetItemString(d, "logitprod", logitprod); - Py_DECREF(logitprod); + PyDict_SetItemString(d, "logit", logit); + Py_DECREF(logit); + + return m; } + #else + PyMODINIT_FUNC initnpspam(void) + { + PyObject *m, *logit, *d; + m = Py_InitModule("npspam", LogitMethods); + if (m == NULL) { + return; + } + import_array(); + import_umath(); + + logit = PyUFunc_FromFuncAndData(funcs,data, types, 1, 2, 2, + PyUFunc_None, "logit", + "logit_docstring", 0); + + d = PyModule_GetDict(m); + + PyDict_SetItemString(d, "logit", logit); + Py_DECREF(logit); + } + #endif .. _`sec:PyUFunc-spec`: |