[Numpy-svn] r3466 - in trunk/numpy/core: . src

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Dec 1 22:11:40 CST 2006


Author: oliphant
Date: 2006-12-01 22:11:31 -0600 (Fri, 01 Dec 2006)
New Revision: 3466

Modified:
   trunk/numpy/core/fromnumeric.py
   trunk/numpy/core/src/arraymethods.c
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/multiarraymodule.c
Log:
Allow argsort and sort functions and argsort method to take None as an argument.  Add order= keyword to sort and argsort functions.

Modified: trunk/numpy/core/fromnumeric.py
===================================================================
--- trunk/numpy/core/fromnumeric.py	2006-12-02 00:35:09 UTC (rev 3465)
+++ trunk/numpy/core/fromnumeric.py	2006-12-02 04:11:31 UTC (rev 3466)
@@ -117,14 +117,20 @@
         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 @@
     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 @@
     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

Modified: trunk/numpy/core/src/arraymethods.c
===================================================================
--- trunk/numpy/core/src/arraymethods.c	2006-12-02 00:35:09 UTC (rev 3465)
+++ trunk/numpy/core/src/arraymethods.c	2006-12-02 04:11:31 UTC (rev 3466)
@@ -898,7 +898,8 @@
         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;

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-12-02 00:35:09 UTC (rev 3465)
+++ trunk/numpy/core/src/arrayobject.c	2006-12-02 04:11:31 UTC (rev 3466)
@@ -8445,7 +8445,7 @@
         outtype = _array_find_type(op, intype, MAX_DIMS);
         ret = outtype->type_num;
         Py_DECREF(outtype);
-        Py_DECREF(intype);
+        Py_XDECREF(intype);
         return ret;
 }
 

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2006-12-02 00:35:09 UTC (rev 3465)
+++ trunk/numpy/core/src/multiarraymodule.c	2006-12-02 04:11:31 UTC (rev 3466)
@@ -2388,7 +2388,7 @@
 
 	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 @@
 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 @@
 		*((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,



More information about the Numpy-svn mailing list