summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-12-02 04:11:31 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-12-02 04:11:31 +0000
commitbf677ca3b1b02c57da75a302a0f0b5704e3f525f (patch)
tree06947d88317a5c3787ce13bd4b6250b2341e19ca /numpy
parent574fe369112ded91f80502db73022a8fd79d3fe3 (diff)
downloadnumpy-bf677ca3b1b02c57da75a302a0f0b5704e3f525f.tar.gz
Allow argsort and sort functions and argsort method to take None as an argument. Add order= keyword to sort and argsort functions.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/fromnumeric.py39
-rw-r--r--numpy/core/src/arraymethods.c3
-rw-r--r--numpy/core/src/arrayobject.c2
-rw-r--r--numpy/core/src/multiarraymodule.c20
4 files changed, 38 insertions, 26 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py
index 061b8bd9f..e153f6061 100644
--- a/numpy/core/fromnumeric.py
+++ b/numpy/core/fromnumeric.py
@@ -117,14 +117,20 @@ def transpose(a, axes=None):
return _wrapit(a, 'transpose', axes)
return transpose(axes)
-def sort(a, axis=-1, kind='quicksort'):
+def sort(a, axis=-1, kind='quicksort', order=None):
"""Returns copy of 'a' sorted along the given axis.
Keyword arguments:
- axis -- axis to be sorted (default -1)
- kind -- sorting algorithm (default 'quicksort')
- Possible values: 'quicksort', 'mergesort', or 'heapsort'.
+ axis -- axis to be sorted (default -1). Can be None
+ to indicate that a flattened and sorted array should
+ be returned (the array method does not support this).
+ kind -- sorting algorithm (default 'quicksort')
+ Possible values: 'quicksort', 'mergesort', or 'heapsort'.
+ order -- For an array with fields defined, this argument allows
+ specification of which fields to compare first, second,
+ etc. Not all fields need be specified.
+
Returns: None.
@@ -150,18 +156,27 @@ def sort(a, axis=-1, kind='quicksort'):
and use less space than sorts along other axis.
"""
- a = asanyarray(a).copy()
- a.sort(axis, kind)
+ if axis is None:
+ a = asanyarray(a).flatten()
+ axis = 0
+ else:
+ a = asanyarray(a).copy()
+ a.sort(axis, kind, order)
return a
-def argsort(a, axis=-1, kind='quicksort'):
+def argsort(a, axis=-1, kind='quicksort', order=None):
"""Returns array of indices that index 'a' in sorted order.
Keyword arguments:
- axis -- axis to be indirectly sorted (default -1)
- kind -- sorting algorithm (default 'quicksort')
- Possible values: 'quicksort', 'mergesort', or 'heapsort'
+ axis -- axis to be indirectly sorted (default -1)
+ Can be None to indicate return indices into the
+ flattened array.
+ kind -- sorting algorithm (default 'quicksort')
+ Possible values: 'quicksort', 'mergesort', or 'heapsort'
+ order -- For an array with fields defined, this argument allows
+ specification of which fields to compare first, second,
+ etc. Not all fields need be specified.
Returns: array of indices that sort 'a' along the specified axis.
@@ -190,8 +205,8 @@ def argsort(a, axis=-1, kind='quicksort'):
try:
argsort = a.argsort
except AttributeError:
- return _wrapit(a, 'argsort', axis, kind)
- return argsort(axis, kind)
+ return _wrapit(a, 'argsort', axis, kind, order)
+ return argsort(axis, kind, order)
def argmax(a, axis=None):
"""argmax(a,axis=None) returns the indices to the maximum value of the
diff --git a/numpy/core/src/arraymethods.c b/numpy/core/src/arraymethods.c
index d33fbc70e..fcde13fae 100644
--- a/numpy/core/src/arraymethods.c
+++ b/numpy/core/src/arraymethods.c
@@ -898,7 +898,8 @@ array_argsort(PyArrayObject *self, PyObject *args, PyObject *kwds)
PyArray_Descr *newd, *saved=NULL;
static char *kwlist[] = {"axis", "kind", "order", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&O", kwlist, &axis,
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O", kwlist,
+ PyArray_AxisConverter, &axis,
PyArray_SortkindConverter, &which,
&order))
return NULL;
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index ac85fcd23..bc662b7d1 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -8445,7 +8445,7 @@ PyArray_ObjectType(PyObject *op, int minimum_type)
outtype = _array_find_type(op, intype, MAX_DIMS);
ret = outtype->type_num;
Py_DECREF(outtype);
- Py_DECREF(intype);
+ Py_XDECREF(intype);
return ret;
}
diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c
index 88b063888..8d213c84a 100644
--- a/numpy/core/src/multiarraymodule.c
+++ b/numpy/core/src/multiarraymodule.c
@@ -2388,7 +2388,7 @@ PyArray_Sort(PyArrayObject *op, int axis, NPY_SORTKIND which)
n = op->nd;
if ((n==0) || (PyArray_SIZE(op)==1)) return 0;
-
+
if (axis < 0) axis += n;
if ((axis < 0) || (axis >= n)) {
PyErr_Format(PyExc_ValueError,
@@ -2468,7 +2468,7 @@ argsort_static_compare(const void *ip1, const void *ip2)
static PyObject *
PyArray_ArgSort(PyArrayObject *op, int axis, NPY_SORTKIND which)
{
- PyArrayObject *ap=NULL, *ret=NULL, *store;
+ PyArrayObject *ap=NULL, *ret=NULL, *store, *op2;
intp *ip;
intp i, j, n, m, orign;
int argsort_elsize;
@@ -2485,25 +2485,21 @@ PyArray_ArgSort(PyArrayObject *op, int axis, NPY_SORTKIND which)
*((intp *)ret->data) = 0;
return (PyObject *)ret;
}
- if (axis < 0) axis += n;
- if ((axis < 0) || (axis >= n)) {
- PyErr_Format(PyExc_ValueError,
- "axis(=%d) out of bounds", axis);
- return NULL;
- }
+
+ if ((op2=(PyAO *)_check_axis(op, &axis, 0))==NULL) return NULL;
/* Determine if we should use new algorithm or not */
- if (op->descr->f->argsort[which] != NULL) {
- return _new_argsort(op, axis, which);
+ if (op2->descr->f->argsort[which] != NULL) {
+ return _new_argsort(op2, axis, which);
}
- if ((which != PyArray_QUICKSORT) || op->descr->f->compare == NULL) {
+ if ((which != PyArray_QUICKSORT) || op2->descr->f->compare == NULL) {
PyErr_SetString(PyExc_TypeError,
"requested sort not available for type");
goto fail;
}
- SWAPAXES(ap, op);
+ SWAPAXES(ap, op2);
op = (PyArrayObject *)PyArray_ContiguousFromAny((PyObject *)ap,
PyArray_NOTYPE,