diff options
author | Mark Wiebe <mwwiebe@gmail.com> | 2011-03-14 22:21:05 -0700 |
---|---|---|
committer | Mark Wiebe <mwwiebe@gmail.com> | 2011-03-14 22:22:28 -0700 |
commit | 7ee96bd982f98392f577ab50abd42c51de4550d2 (patch) | |
tree | 4773220a6ae96aace7a383b4fd2198ea4067c754 /numpy/lib | |
parent | 2411b7b0f1bee1472db260728de38f0dba4baf7b (diff) | |
download | numpy-7ee96bd982f98392f577ab50abd42c51de4550d2.tar.gz |
API: Rename 'coords' to 'multi-index' in ravel_coords and iterator API
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/index_tricks.py | 4 | ||||
-rw-r--r-- | numpy/lib/src/_compiled_base.c | 31 | ||||
-rw-r--r-- | numpy/lib/tests/test_index_tricks.py | 44 |
3 files changed, 40 insertions, 39 deletions
diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index d1e925ead..69539d482 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -1,4 +1,4 @@ -__all__ = ['ravel_coords', +__all__ = ['ravel_multi_index', 'unravel_index', 'mgrid', 'ogrid', @@ -17,7 +17,7 @@ import math import function_base import numpy.matrixlib as matrix from function_base import diff -from numpy.lib._compiled_base import ravel_coords, unravel_index +from numpy.lib._compiled_base import ravel_multi_index, unravel_index makemat = matrix.matrix def ix_(*args): diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c index b81d2783a..066519bf1 100644 --- a/numpy/lib/src/_compiled_base.c +++ b/numpy/lib/src/_compiled_base.c @@ -614,7 +614,7 @@ static int sequence_to_arrays(PyObject *seq, /* Inner loop for unravel_index */ static int -ravel_coords_loop(int ravel_ndim, npy_intp *ravel_dims, +ravel_multi_index_loop(int ravel_ndim, npy_intp *ravel_dims, npy_intp *ravel_strides, npy_intp count, NPY_CLIPMODE *modes, @@ -674,9 +674,9 @@ ravel_coords_loop(int ravel_ndim, npy_intp *ravel_dims, return NPY_SUCCEED; } -/* ravel_coords implementation - see add_newdocs.py */ +/* ravel_multi_index implementation - see add_newdocs.py */ static PyObject * -arr_ravel_coords(PyObject *self, PyObject *args, PyObject *kwds) +arr_ravel_multi_index(PyObject *self, PyObject *args, PyObject *kwds) { int i, s; PyObject *mode0=NULL, *coords0=NULL; @@ -692,12 +692,13 @@ arr_ravel_coords(PyObject *self, PyObject *args, PyObject *kwds) NpyIter *iter = NULL; - char *kwlist[] = {"coords", "dims", "mode", "order", NULL}; + char *kwlist[] = {"multi_index", "dims", "mode", "order", NULL}; memset(op, 0, sizeof(op)); dtype[0] = NULL; - if(!PyArg_ParseTupleAndKeywords(args, kwds, "OO&|OO&:ravel_coords", kwlist, + if(!PyArg_ParseTupleAndKeywords(args, kwds, + "OO&|OO&:ravel_multi_index", kwlist, &coords0, PyArray_IntpConverter, &dimensions, &mode0, @@ -707,7 +708,7 @@ arr_ravel_coords(PyObject *self, PyObject *args, PyObject *kwds) if (dimensions.len+1 > NPY_MAXARGS) { PyErr_SetString(PyExc_ValueError, - "too many dimensions passed to ravel_coords"); + "too many dimensions passed to ravel_multi_index"); goto fail; } @@ -736,8 +737,8 @@ arr_ravel_coords(PyObject *self, PyObject *args, PyObject *kwds) goto fail; } - /* Get the coords into op */ - if (sequence_to_arrays(coords0, op, dimensions.len, "coords") < 0) { + /* Get the multi_index into op */ + if (sequence_to_arrays(coords0, op, dimensions.len, "multi_index") < 0) { goto fail; } @@ -779,7 +780,7 @@ arr_ravel_coords(PyObject *self, PyObject *args, PyObject *kwds) countptr = NpyIter_GetInnerLoopSizePtr(iter); do { - if (ravel_coords_loop(dimensions.len, dimensions.ptr, + if (ravel_multi_index_loop(dimensions.len, dimensions.ptr, ravel_strides, *countptr, modes, dataptr, strides) != NPY_SUCCEED) { goto fail; @@ -923,7 +924,7 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds) NPY_ITER_BUFFERED| NPY_ITER_ZEROSIZE_OK| NPY_ITER_DONT_NEGATE_STRIDES| - NPY_ITER_COORDS, + NPY_ITER_MULTI_INDEX, NPY_KEEPORDER, NPY_SAME_KIND_CASTING, dtype); if (iter == NULL) { @@ -932,7 +933,7 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds) /* * Create the return array with a layout compatible with the indices - * and with a dimension added to the end for the coordinates + * and with a dimension added to the end for the multi-index */ ret_ndim = PyArray_NDIM(indices) + 1; if (NpyIter_GetShape(iter, ret_dims) != NPY_SUCCEED) { @@ -945,8 +946,8 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds) } ret_strides[ret_ndim-1] = sizeof(npy_intp); - /* Remove the coords and inner loop */ - if (NpyIter_RemoveCoords(iter) != NPY_SUCCEED) { + /* Remove the multi-index and inner loop */ + if (NpyIter_RemoveMultiIndex(iter) != NPY_SUCCEED) { goto fail; } if (NpyIter_EnableExternalLoop(iter) != NPY_SUCCEED) { @@ -1020,7 +1021,7 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds) goto fail; } - /* Now make a tuple of views, one per coordinate */ + /* Now make a tuple of views, one per index */ ret_tuple = PyTuple_New(dimensions.len); if (ret_tuple == NULL) { goto fail; @@ -1411,7 +1412,7 @@ static struct PyMethodDef methods[] = { METH_VARARGS | METH_KEYWORDS, NULL}, {"interp", (PyCFunction)arr_interp, METH_VARARGS | METH_KEYWORDS, NULL}, - {"ravel_coords", (PyCFunction)arr_ravel_coords, + {"ravel_multi_index", (PyCFunction)arr_ravel_multi_index, METH_VARARGS | METH_KEYWORDS, NULL}, {"unravel_index", (PyCFunction)arr_unravel_index, METH_VARARGS | METH_KEYWORDS, NULL}, diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index 40f75936e..8b42292a2 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -7,28 +7,28 @@ from numpy import ( array, ones, r_, mgrid, unravel_index, zeros, where, class TestRavelUnravelIndex(TestCase): def test_basic(self): assert_equal(np.unravel_index(2,(2,2)), (1,0)) - assert_equal(np.ravel_coords((1,0),(2,2)), 2) + assert_equal(np.ravel_multi_index((1,0),(2,2)), 2) assert_equal(np.unravel_index(254,(17,94)), (2,66)) - assert_equal(np.ravel_coords((2,66),(17,94)), 254) + assert_equal(np.ravel_multi_index((2,66),(17,94)), 254) assert_raises(ValueError, np.unravel_index, -1, (2,2)) assert_raises(TypeError, np.unravel_index, 0.5, (2,2)) assert_raises(ValueError, np.unravel_index, 4, (2,2)) - assert_raises(ValueError, np.ravel_coords, (-3,1), (2,2)) - assert_raises(ValueError, np.ravel_coords, (2,1), (2,2)) - assert_raises(ValueError, np.ravel_coords, (0,-3), (2,2)) - assert_raises(ValueError, np.ravel_coords, (0,2), (2,2)) - assert_raises(TypeError, np.ravel_coords, (0.1,0.), (2,2)) + assert_raises(ValueError, np.ravel_multi_index, (-3,1), (2,2)) + assert_raises(ValueError, np.ravel_multi_index, (2,1), (2,2)) + assert_raises(ValueError, np.ravel_multi_index, (0,-3), (2,2)) + assert_raises(ValueError, np.ravel_multi_index, (0,2), (2,2)) + assert_raises(TypeError, np.ravel_multi_index, (0.1,0.), (2,2)) assert_equal(np.unravel_index((2*3 + 1)*6 + 4, (4,3,6)), [2,1,4]) - assert_equal(np.ravel_coords([2,1,4], (4,3,6)), (2*3 + 1)*6 + 4) + assert_equal(np.ravel_multi_index([2,1,4], (4,3,6)), (2*3 + 1)*6 + 4) arr = np.array([[3,6,6],[4,5,1]]) - assert_equal(np.ravel_coords(arr, (7,6)), [22,41,37]) - assert_equal(np.ravel_coords(arr, (7,6), order='F'), [31,41,13]) - assert_equal(np.ravel_coords(arr, (4,6), mode='clip'), [22,23,19]) - assert_equal(np.ravel_coords(arr, (4,4), mode=('clip','wrap')), + assert_equal(np.ravel_multi_index(arr, (7,6)), [22,41,37]) + assert_equal(np.ravel_multi_index(arr, (7,6), order='F'), [31,41,13]) + assert_equal(np.ravel_multi_index(arr, (4,6), mode='clip'), [22,23,19]) + assert_equal(np.ravel_multi_index(arr, (4,4), mode=('clip','wrap')), [12,13,13]) - assert_equal(np.ravel_coords((3,1,4,1), (6,7,8,9)), 1621) + assert_equal(np.ravel_multi_index((3,1,4,1), (6,7,8,9)), 1621) assert_equal(np.unravel_index(np.array([22, 41, 37]), (7,6)), [[3, 6, 6],[4, 5, 1]]) @@ -43,30 +43,30 @@ class TestRavelUnravelIndex(TestCase): coords = np.array([[1,0,1,2,3,4],[1,6,1,3,2,0]], dtype=dtype) shape = (5,8) uncoords = 8*coords[0]+coords[1] - assert_equal(np.ravel_coords(coords, shape), uncoords) + assert_equal(np.ravel_multi_index(coords, shape), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape)) uncoords = coords[0]+5*coords[1] - assert_equal(np.ravel_coords(coords, shape, order='F'), uncoords) + assert_equal(np.ravel_multi_index(coords, shape, order='F'), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape, order='F')) coords = np.array([[1,0,1,2,3,4],[1,6,1,3,2,0],[1,3,1,0,9,5]], dtype=dtype) shape = (5,8,10) uncoords = 10*(8*coords[0]+coords[1])+coords[2] - assert_equal(np.ravel_coords(coords, shape), uncoords) + assert_equal(np.ravel_multi_index(coords, shape), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape)) uncoords = coords[0]+5*(coords[1]+8*coords[2]) - assert_equal(np.ravel_coords(coords, shape, order='F'), uncoords) + assert_equal(np.ravel_multi_index(coords, shape, order='F'), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape, order='F')) def test_clipmodes(self): # Test clipmodes - assert_equal(np.ravel_coords([5,1,-1,2], (4,3,7,12), mode='wrap'), - np.ravel_coords([1,1,6,2], (4,3,7,12))) - assert_equal(np.ravel_coords([5,1,-1,2], (4,3,7,12), + assert_equal(np.ravel_multi_index([5,1,-1,2], (4,3,7,12), mode='wrap'), + np.ravel_multi_index([1,1,6,2], (4,3,7,12))) + assert_equal(np.ravel_multi_index([5,1,-1,2], (4,3,7,12), mode=('wrap','raise','clip','raise')), - np.ravel_coords([1,1,0,2], (4,3,7,12))) - assert_raises(ValueError, np.ravel_coords, [5,1,-1,2], (4,3,7,12)) + np.ravel_multi_index([1,1,0,2], (4,3,7,12))) + assert_raises(ValueError, np.ravel_multi_index, [5,1,-1,2], (4,3,7,12)) class TestGrid(TestCase): def test_basic(self): |