summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/setup.py7
-rw-r--r--numpy/core/src/umath/operand_flag_tests.c.src87
-rw-r--r--numpy/core/tests/test_ufunc.py8
3 files changed, 102 insertions, 0 deletions
diff --git a/numpy/core/setup.py b/numpy/core/setup.py
index 6df82b3fa..b99f1853d 100644
--- a/numpy/core/setup.py
+++ b/numpy/core/setup.py
@@ -935,6 +935,13 @@ def configuration(parent_package='',top_path=None):
config.add_extension('multiarray_tests',
sources = [join('src', 'multiarray', 'multiarray_tests.c.src')])
+ #######################################################################
+ # operand_flag_tests module #
+ #######################################################################
+
+ config.add_extension('operand_flag_tests',
+ sources = [join('src','umath', 'operand_flag_tests.c.src')])
+
config.add_data_dir('tests')
config.add_data_dir('tests/data')
diff --git a/numpy/core/src/umath/operand_flag_tests.c.src b/numpy/core/src/umath/operand_flag_tests.c.src
new file mode 100644
index 000000000..dae309683
--- /dev/null
+++ b/numpy/core/src/umath/operand_flag_tests.c.src
@@ -0,0 +1,87 @@
+#define NPY_NO_DEPRECATED_API NPY_API_VERSION
+
+#include <stdint.h>
+#include <math.h>
+#include <Python.h>
+#include <structmember.h>
+#include <numpy/arrayobject.h>
+#include <numpy/ufuncobject.h>
+#include "numpy/npy_3kcompat.h"
+
+
+static PyMethodDef TestMethods[] = {
+ {NULL, NULL, 0, NULL}
+};
+
+
+static void
+inplace_add(char **args, npy_intp *dimensions, npy_intp *steps, void *data)
+{
+ npy_intp i;
+ npy_intp n = dimensions[0];
+ char *in1 = args[0];
+ char *in2 = args[1];
+ npy_intp in1_step = steps[0];
+ npy_intp in2_step = steps[1];
+
+ for (i = 0; i < n; i++) {
+ (*(long *)in1) = *(long*)in1 + *(long*)in2;
+ in1 += in1_step;
+ in2 += in2_step;
+ }
+}
+
+
+/*This a pointer to the above function*/
+PyUFuncGenericFunction funcs[1] = {&inplace_add};
+
+/* These are the input and return dtypes of logit.*/
+static char types[2] = {NPY_LONG, NPY_LONG};
+
+static void *data[1] = {NULL};
+
+#if defined(NPY_PY3K)
+static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT,
+ "operand_flag_tests",
+ NULL,
+ -1,
+ TestMethods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
+
+PyObject *PyInit_operand_flag_tests(void)
+{
+#else
+PyMODINIT_FUNC initoperand_flag_tests(void)
+{
+#endif
+ PyObject *m, *ufunc;
+
+#if defined(NPY_PY3K)
+ m = PyModule_Create(&moduledef);
+#else
+ m = Py_InitModule("operand_flag_tests", TestMethods);
+#endif
+ if (m == NULL) {
+ return;
+ }
+
+ import_array();
+ import_umath();
+
+ ufunc = PyUFunc_FromFuncAndData(funcs, data, types, 1, 2, 0,
+ PyUFunc_None, "inplace_add",
+ "inplace_add_docstring", 0);
+
+ /* Set flags to turn off buffering for first input operand,
+ so that result can be written back to input operand. */
+ ((PyUFuncObject*)ufunc)->op_flags[0] = NPY_ITER_READWRITE;
+ ((PyUFuncObject*)ufunc)->iter_flags = NPY_ITER_REDUCE_OK;
+ PyModule_AddObject(m, "inplace_add", (PyObject*)ufunc);
+
+ return m;
+}
diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py
index dbbd15397..c969fa8ac 100644
--- a/numpy/core/tests/test_ufunc.py
+++ b/numpy/core/tests/test_ufunc.py
@@ -5,6 +5,7 @@ import sys
import numpy as np
from numpy.testing import *
import numpy.core.umath_tests as umt
+import numpy.core.operand_flag_tests as opflag_tests
from numpy.compat import asbytes
from numpy.core.test_rational import *
@@ -786,6 +787,13 @@ class TestUfunc(TestCase):
# no output type should raise TypeError
assert_raises(TypeError, test_add, a, b)
+
+ def test_operand_flags(self):
+ a = np.arange(16, dtype='i8').reshape(4,4)
+ b = np.arange(9, dtype='i8').reshape(3,3)
+ opflag_tests.inplace_add(a[:-1,:-1], b)
+ assert_equal(a, np.array([[0,2,4,3],[7,9,11,7],
+ [14,16,18,11],[12,13,14,15]], dtype='i8'))
if __name__ == "__main__":
run_module_suite()