24 #ifndef NDARRAY_SWIG_ufunctors_h_INCLUDED
25 #define NDARRAY_SWIG_ufunctors_h_INCLUDED
36 template <
typename TUnaryFunctor,
37 typename TArgument=
typename TUnaryFunctor::argument_type,
38 typename TResult=
typename TUnaryFunctor::result_type>
41 static PyObject*
_call_(TUnaryFunctor
const&
self, PyObject* input, PyObject* output) {
44 if (input_array == NULL)
return NULL;
45 PyObject* output_array;
46 if (output == NULL || output == Py_None) {
47 output_array = PyArray_SimpleNew(PyArray_NDIM(input_array),
48 PyArray_DIMS(input_array),
52 NPY_ALIGNED | NPY_WRITEABLE | NPY_UPDATEIFCOPY);
54 if (output_array == NULL) {
55 Py_DECREF(input_array);
58 PyObject*
iter = PyArray_MultiIterNew(2,input_array,output_array);
60 Py_DECREF(input_array);
61 Py_DECREF(output_array);
64 int size = ((PyArrayMultiIterObject*)(iter))->size;
66 TArgument* arg = (TArgument*)PyArray_MultiIter_DATA(iter,0);
67 TResult* res = (TResult*)PyArray_MultiIter_DATA(iter,1);
69 PyArray_MultiIter_NEXT(iter);
71 Py_DECREF(input_array);
73 return PyArray_Return(reinterpret_cast<PyArrayObject*>(output_array));
79 template <
typename TBinaryFunctor,
80 typename TArgument1=
typename TBinaryFunctor::first_argument_type,
81 typename TArgument2=
typename TBinaryFunctor::second_argument_type,
82 typename TResult=
typename TBinaryFunctor::result_type>
85 static PyObject*
_call_(TBinaryFunctor
const&
self, PyObject* input1, PyObject* input2,
91 if (input1_array == NULL || input2_array == NULL) {
92 Py_XDECREF(input1_array);
93 Py_XDECREF(input2_array);
96 PyObject* output_array;
97 if (output == NULL || output == Py_None) {
98 PyObject* tmp = PyArray_MultiIterNew(2,input1_array,input2_array);
100 Py_XDECREF(input1_array);
101 Py_XDECREF(input2_array);
104 PyArrayMultiIterObject* tmp_iter = (PyArrayMultiIterObject*)tmp;
105 output_array = PyArray_SimpleNew(tmp_iter->nd,tmp_iter->dimensions,
110 NPY_ALIGNED | NPY_WRITEABLE | NPY_UPDATEIFCOPY);
112 if (output_array == NULL) {
113 Py_DECREF(input1_array);
114 Py_DECREF(input2_array);
117 PyObject*
iter = PyArray_MultiIterNew(3,input1_array,input2_array,output_array);
119 Py_DECREF(input1_array);
120 Py_DECREF(input2_array);
121 Py_DECREF(output_array);
124 int size = ((PyArrayMultiIterObject*)(iter))->size;
126 TArgument1* arg1 = (TArgument1*)PyArray_MultiIter_DATA(iter,0);
127 TArgument2* arg2 = (TArgument2*)PyArray_MultiIter_DATA(iter,1);
128 TResult* res = (TResult*)PyArray_MultiIter_DATA(iter,2);
129 *res =
self(*arg1,*arg2);
130 PyArray_MultiIter_NEXT(iter);
132 Py_DECREF(input1_array);
133 Py_DECREF(input2_array);
135 return PyArray_Return(reinterpret_cast<PyArrayObject*>(output_array));
142 #endif // !NDARRAY_SWIG_ufunctors_h_INCLUDED
Python C-API conversions between ndarray and numpy.
static PyObject * _call_(TUnaryFunctor const &self, PyObject *input, PyObject *output)
static PyObject * _call_(TBinaryFunctor const &self, PyObject *input1, PyObject *input2, PyObject *output)