diff options
-rw-r--r-- | numpy/add_newdocs.py | 26 | ||||
-rw-r--r-- | numpy/core/numeric.py | 3 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 29 | ||||
-rw-r--r-- | numpy/lib/tests/test_shape_base.py | 15 | ||||
-rw-r--r-- | numpy/lib/utils.py | 32 |
5 files changed, 73 insertions, 32 deletions
diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py index ae07f05da..39dd2205e 100644 --- a/numpy/add_newdocs.py +++ b/numpy/add_newdocs.py @@ -3648,6 +3648,32 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('min', """)) +add_newdoc('numpy.core.multiarray', 'may_share_memory', + """ + Determine if two arrays can share memory + + The memory-bounds of a and b are computed. If they overlap then + this function returns True. Otherwise, it returns False. + + A return of True does not necessarily mean that the two arrays + share any element. It just means that they *might*. + + Parameters + ---------- + a, b : ndarray + + Returns + ------- + out : bool + + Examples + -------- + >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9])) + False + + """) + + add_newdoc('numpy.core.multiarray', 'ndarray', ('newbyteorder', """ arr.newbyteorder(new_order='S') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index a187d7c5b..bb2f3d28d 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -40,7 +40,7 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', 'True_', 'bitwise_not', 'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE', 'ALLOW_THREADS', - 'ComplexWarning'] + 'ComplexWarning', 'may_share_memory'] if sys.version_info[0] < 3: __all__.extend(['getbuffer', 'newbuffer']) @@ -252,6 +252,7 @@ fromstring = multiarray.fromstring fromiter = multiarray.fromiter fromfile = multiarray.fromfile frombuffer = multiarray.frombuffer +may_share_memory = multiarray.may_share_memory if sys.version_info[0] < 3: newbuffer = multiarray.newbuffer getbuffer = multiarray.getbuffer diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index a1c9e7c30..7332a26d0 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -3544,6 +3544,32 @@ PyDataMem_RENEW(void *ptr, size_t size) return (char *)result; } +static PyObject * +array_may_share_memory(PyObject *NPY_UNUSED(ignored), PyObject *args) +{ + PyArrayObject * self = NULL; + PyArrayObject * other = NULL; + int overlap; + + if (!PyArg_ParseTuple(args, "O&O&", PyArray_Converter, &self, + PyArray_Converter, &other)) { + return NULL; + } + + overlap = arrays_overlap(self, other); + Py_XDECREF(self); + Py_XDECREF(other); + + if (overlap) { + Py_RETURN_TRUE; + } + else { + Py_RETURN_FALSE; + } +} + + + static struct PyMethodDef array_module_methods[] = { {"_get_ndarray_c_version", (PyCFunction)array__get_ndarray_c_version, @@ -3644,6 +3670,9 @@ static struct PyMethodDef array_module_methods[] = { {"result_type", (PyCFunction)array_result_type, METH_VARARGS, NULL}, + {"may_share_memory", + (PyCFunction)array_may_share_memory, + METH_VARARGS, NULL}, /* Datetime-related functions */ {"datetime_data", (PyCFunction)array_datetime_data, diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index 3c270088f..a92ddde83 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -315,6 +315,21 @@ class TestTile(TestCase): assert_equal(large, klarge) +class TestMayShareMemory(TestCase): + def test_basic(self): + d = ones((50, 60)) + d2 = ones((30, 60, 6)) + self.assertTrue(may_share_memory(d, d)) + self.assertTrue(may_share_memory(d, d[::-1])) + self.assertTrue(may_share_memory(d, d[::2])) + self.assertTrue(may_share_memory(d, d[1:, ::-1])) + + self.assertFalse(may_share_memory(d[::-1], d2)) + self.assertFalse(may_share_memory(d[::2], d2)) + self.assertFalse(may_share_memory(d[1:, ::-1], d2)) + self.assertTrue(may_share_memory(d2[1:, ::-1], d2)) + + # Utility def compare_results(res,desired): for i in range(len(desired)): diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index f94abeeab..0e4300585 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -11,7 +11,7 @@ from numpy.core import product, ndarray, ufunc __all__ = ['issubclass_', 'issubsctype', 'issubdtype', 'deprecate', 'deprecate_with_doc', 'get_numarray_include', 'get_include', 'info', 'source', 'who', 'lookfor', 'byte_bounds', - 'may_share_memory', 'safe_eval'] + 'safe_eval'] def get_include(): """ @@ -266,36 +266,6 @@ def byte_bounds(a): return a_low, a_high -def may_share_memory(a, b): - """ - Determine if two arrays can share memory - - The memory-bounds of a and b are computed. If they overlap then - this function returns True. Otherwise, it returns False. - - A return of True does not necessarily mean that the two arrays - share any element. It just means that they *might*. - - Parameters - ---------- - a, b : ndarray - - Returns - ------- - out : bool - - Examples - -------- - >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9])) - False - - """ - a_low, a_high = byte_bounds(a) - b_low, b_high = byte_bounds(b) - if b_low >= a_high or a_low >= b_high: - return False - return True - #----------------------------------------------------------------------------- # Function for output and information on the variables used. #----------------------------------------------------------------------------- |