[Numpy-svn] r2983 - in trunk/numpy: core core/blasdot core/include/numpy core/src numarray oldnumeric

numpy-svn at scipy.org numpy-svn at scipy.org
Thu Aug 10 07:48:11 CDT 2006


Author: oliphant
Date: 2006-08-10 07:48:04 -0500 (Thu, 10 Aug 2006)
New Revision: 2983

Modified:
   trunk/numpy/core/blasdot/_dotblas.c
   trunk/numpy/core/defmatrix.py
   trunk/numpy/core/fromnumeric.py
   trunk/numpy/core/include/numpy/arrayobject.h
   trunk/numpy/core/src/arraymethods.c
   trunk/numpy/core/src/multiarraymodule.c
   trunk/numpy/numarray/functions.py
   trunk/numpy/oldnumeric/functions.py
   trunk/numpy/oldnumeric/misc.py
Log:
Add output arguments to a few more functions for consistency

Modified: trunk/numpy/core/blasdot/_dotblas.c
===================================================================
--- trunk/numpy/core/blasdot/_dotblas.c	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/core/blasdot/_dotblas.c	2006-08-10 12:48:04 UTC (rev 2983)
@@ -1005,7 +1005,7 @@
 	    Py_DECREF(tmp2);
 	}
 	if (PyTypeNum_ISCOMPLEX(typenum)) {
-	    op1 = PyArray_Conjugate(ap1);
+            op1 = PyArray_Conjugate(ap1, NULL);
 	    if (op1==NULL) goto fail;
 	    Py_DECREF(ap1);
 	    ap1 = (PyArrayObject *)op1;

Modified: trunk/numpy/core/defmatrix.py
===================================================================
--- trunk/numpy/core/defmatrix.py	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/core/defmatrix.py	2006-08-10 12:48:04 UTC (rev 2983)
@@ -262,14 +262,14 @@
     def max(self, axis=None, out=None):
         return N.ndarray.max(self, axis, out)._align(axis)
 
-    def argmax(self, axis=None):
-        return N.ndarray.argmax(self, axis)._align(axis)
+    def argmax(self, axis=None, out=None):
+        return N.ndarray.argmax(self, axis, out)._align(axis)
     
     def min(self, axis=None, out=None):
         return N.ndarray.min(self, axis, out)._align(axis)
     
-    def argmin(self, axis=None):
-        return N.ndarray.argmin(self, axis)._align(axis)
+    def argmin(self, axis=None, out=None):
+        return N.ndarray.argmin(self, axis, out)._align(axis)
     
     def ptp(self, axis=None, out=None):
         return N.ndarray.ptp(self, axis, out)._align(axis)

Modified: trunk/numpy/core/fromnumeric.py
===================================================================
--- trunk/numpy/core/fromnumeric.py	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/core/fromnumeric.py	2006-08-10 12:48:04 UTC (rev 2983)
@@ -141,8 +141,8 @@
         return _wrapit(a, 'argsort', axis, kind)
     return argsort(axis, kind)
 
-def argmax(a, axis=-1):
-    """argmax(a,axis=-1) returns the indices to the maximum value of the
+def argmax(a, axis=None):
+    """argmax(a,axis=None) returns the indices to the maximum value of the
     1-D arrays along the given axis.
     """
     try:
@@ -151,8 +151,8 @@
         return _wrapit(a, 'argmax', axis)
     return argmax(axis)
 
-def argmin(a, axis=-1):
-    """argmin(a,axis=-1) returns the indices to the minimum value of the
+def argmin(a, axis=None):
+    """argmin(a,axis=None) returns the indices to the minimum value of the
     1-D arrays along the given axis.
     """
     try:
@@ -250,8 +250,8 @@
         result = asarray(a).shape
     return result
 
-def compress(condition, m, axis=-1, out=None):
-    """compress(condition, x, axis=-1) = those elements of x corresponding
+def compress(condition, m, axis=None, out=None):
+    """compress(condition, x, axis=None) = those elements of x corresponding
     to those elements of condition that are "true".  condition must be the
     same size as the given dimension of x."""
     try:

Modified: trunk/numpy/core/include/numpy/arrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/arrayobject.h	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/core/include/numpy/arrayobject.h	2006-08-10 12:48:04 UTC (rev 2983)
@@ -37,7 +37,7 @@
 #define NPY_SUCCEED 1
 
         /* Helpful to distinguish what is installed */
-#define NPY_VERSION 0x01000001
+#define NPY_VERSION 0x01000002
 
 	/* Some platforms don't define bool, long long, or long double.
 	   Handle that here.

Modified: trunk/numpy/core/src/arraymethods.c
===================================================================
--- trunk/numpy/core/src/arraymethods.c	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/core/src/arraymethods.c	2006-08-10 12:48:04 UTC (rev 2983)
@@ -156,36 +156,42 @@
 	return PyArray_View(self, type, NULL);
 }
 
-static char doc_argmax[] = "a.argmax(axis=None)";
+static char doc_argmax[] = "a.argmax(axis=None, out=None)";
 
 static PyObject *
 array_argmax(PyArrayObject *self, PyObject *args, PyObject *kwds) 
 {
 	int axis=MAX_DIMS;
-	static char *kwlist[] = {"axis", NULL};
+        PyArrayObject *out=NULL;
+	static char *kwlist[] = {"axis", "out", NULL};
 	
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&", kwlist, 
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist, 
 					 PyArray_AxisConverter,
-					 &axis))
+					 &axis,
+                                         PyArray_OutputConverter,
+                                         &out))
 		return NULL;	
 	
-	return _ARET(PyArray_ArgMax(self, axis));
+	return _ARET(PyArray_ArgMax(self, axis, out));
 }
 
-static char doc_argmin[] = "a.argmin(axis=None)";
+static char doc_argmin[] = "a.argmin(axis=None, out=None)";
 
 static PyObject *
 array_argmin(PyArrayObject *self, PyObject *args, PyObject *kwds) 
 {
 	int axis=MAX_DIMS;
-	static char *kwlist[] = {"axis", NULL};
+        PyArrayObject *out=NULL;
+	static char *kwlist[] = {"axis", "out", NULL};
 	
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&", kwlist, 
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist, 
 					 PyArray_AxisConverter,
-					 &axis))
+					 &axis,
+                                         PyArray_OutputConverter,
+                                         &out))
 		return NULL;	
 	
-	return _ARET(PyArray_ArgMin(self, axis));
+	return _ARET(PyArray_ArgMin(self, axis, out));
 }
 
 static char doc_max[] = "a.max(axis=None)";
@@ -1550,19 +1556,22 @@
 #undef _CHKTYPENUM
 
 
-static char doc_clip[] = "a.clip(min=, max=)";
+static char doc_clip[] = "a.clip(min=, max=, out=None)";
 
 static PyObject *
 array_clip(PyArrayObject *self, PyObject *args, PyObject *kwds) 
 {
 	PyObject *min, *max;
-	static char *kwlist[] = {"min", "max", NULL};
+        PyArrayObject *out=NULL;
+	static char *kwlist[] = {"min", "max", "out", NULL};
 
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
-					 &min, &max)) 
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O&", kwlist,
+					 &min, &max, 
+                                         PyArray_OutputConverter,
+                                         &out)) 
 		return NULL;
 	
-	return _ARET(PyArray_Clip(self, min, max));
+	return _ARET(PyArray_Clip(self, min, max, out));
 }
 
 static char doc_conj[] = "a.conj()";
@@ -1573,9 +1582,12 @@
 array_conjugate(PyArrayObject *self, PyObject *args) 
 {
 
-	if (!PyArg_ParseTuple(args, "")) return NULL;
+        PyArrayObject *out=NULL;
+	if (!PyArg_ParseTuple(args, "|O&",
+                              PyArray_OutputConverter,
+                              &out)) return NULL;
 	
-	return PyArray_Conjugate(self);
+	return PyArray_Conjugate(self, out);
 }
 
 

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/core/src/multiarraymodule.c	2006-08-10 12:48:04 UTC (rev 2983)
@@ -216,11 +216,9 @@
 PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out)
 {
 	PyObject *f, *ret=NULL, *tmp, *op1, *op2;
-        if (out && (!PyArray_SAMESHAPE(out, a) || 
-                    !PyArray_EquivTypes(a->descr, out->descr))) {
+        if (out && (PyArray_SIZE(out) != PyArray_SIZE(a))) {
                 PyErr_SetString(PyExc_ValueError, 
-                                "output array must have the same shape"
-                                "and type");
+                                "invalid output shape");
                 return NULL;
         }
 	if (PyArray_ISCOMPLEX(a)) {
@@ -266,7 +264,7 @@
 	if (decimals >= 0) {
 		if (PyArray_ISINTEGER(a)) {
                         if (out) {
-                                if (PyArray_CopyInto(out, a) < 0) return NULL;
+                                if (PyArray_CopyAnyInto(out, a) < 0) return NULL;
                                 Py_INCREF(out);
                                 return (PyObject *)out;
                         }
@@ -897,7 +895,7 @@
  Clip
 */
 static PyObject *
-PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max)
+PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *out)
 {
 	PyObject *selector=NULL, *newtup=NULL, *ret=NULL;
 	PyObject *res1=NULL, *res2=NULL, *res3=NULL;
@@ -924,7 +922,7 @@
 
 	newtup = Py_BuildValue("(OOO)", (PyObject *)self, min, max);
 	if (newtup == NULL) {Py_DECREF(selector); return NULL;}
-	ret = PyArray_Choose((PyAO *)selector, newtup, NULL, NPY_RAISE);
+	ret = PyArray_Choose((PyAO *)selector, newtup, out, NPY_RAISE);
 	Py_DECREF(selector);
 	Py_DECREF(newtup);
 	return ret;
@@ -934,14 +932,14 @@
  Conjugate
 */
 static PyObject *
-PyArray_Conjugate(PyArrayObject *self)
+PyArray_Conjugate(PyArrayObject *self, PyArrayObject *out)
 {
 	if (PyArray_ISCOMPLEX(self)) {
 		PyObject *new;
 		intp size, i;
 		/* Make a copy */
-		new = PyArray_NewCopy(self, -1);
-		if (new==NULL) return NULL;
+                new = PyArray_NewCopy(self, -1);
+                if (new==NULL) return NULL;
 		size = PyArray_SIZE(new);
 		if (self->descr->type_num == PyArray_CFLOAT) {
 			cfloat *dptr = (cfloat *) PyArray_DATA(new);
@@ -964,11 +962,25 @@
 				dptr++;
 			}			
 		}		
+                if (out) {
+                        if (PyArray_CopyAnyInto(out, (PyArrayObject *)new)<0) 
+                                return NULL;
+                        Py_INCREF(out);
+                        Py_DECREF(new);
+                        return (PyObject *)out;
+                }
 		return new;
 	}
 	else {
-		Py_INCREF(self);
-		return (PyObject *) self;
+                PyArrayObject *ret;
+                if (out) {
+                        if (PyArray_CopyAnyInto(out, self)< 0)
+                                return NULL;
+                        ret = out;
+                }
+                else ret = self;
+                Py_INCREF(ret);
+                return (PyObject *)ret;
 	}
 }
 
@@ -1836,7 +1848,7 @@
                 PyArrayObject *obj;
                 int flags = NPY_CARRAY | NPY_UPDATEIFCOPY;
 
-                if (!PyArray_SAMESHAPE(ap, ret)) {
+                if (PyArray_SIZE(ret) != PyArray_SIZE(ap)) {
                         PyErr_SetString(PyExc_TypeError, 
                                         "invalid shape for output array.");
                         ret = NULL;
@@ -3018,7 +3030,7 @@
  ArgMin
 */
 static PyObject *
-PyArray_ArgMin(PyArrayObject *ap, int axis) 
+PyArray_ArgMin(PyArrayObject *ap, int axis, PyArrayObject *out) 
 {
 	PyObject *obj, *new, *ret;
 
@@ -3039,7 +3051,7 @@
 	new = PyArray_EnsureAnyArray(PyNumber_Subtract(obj, (PyObject *)ap));
 	Py_DECREF(obj);
 	if (new == NULL) return NULL;
-	ret = PyArray_ArgMax((PyArrayObject *)new, axis);
+	ret = PyArray_ArgMax((PyArrayObject *)new, axis, out);
 	Py_DECREF(new);
 	return ret;
 }
@@ -3117,7 +3129,7 @@
  ArgMax
 */
 static PyObject *
-PyArray_ArgMax(PyArrayObject *op, int axis) 
+PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out) 
 {
 	PyArrayObject *ap=NULL, *rp=NULL;
 	PyArray_ArgFunc* arg_func;
@@ -3125,6 +3137,7 @@
 	intp *rptr;
 	intp i, n, m;
 	int elsize;
+        int copyret=0;
 
 	NPY_BEGIN_THREADS_DEF
 	
@@ -3163,13 +3176,6 @@
 		goto fail;
 	}
 
-	rp = (PyArrayObject *)PyArray_New(ap->ob_type, ap->nd-1,
-					  ap->dimensions, PyArray_INTP,
-					  NULL, NULL, 0, 0, 
-                                          (PyObject *)ap);
-	if (rp == NULL) goto fail;
-
-
 	elsize = ap->descr->elsize;
 	m = ap->dimensions[ap->nd-1];
 	if (m == 0) {
@@ -3178,6 +3184,28 @@
 				"of an empty sequence??");
 		goto fail;
 	}
+
+        if (!out) {
+                rp = (PyArrayObject *)PyArray_New(ap->ob_type, ap->nd-1,
+                                                  ap->dimensions, PyArray_INTP,
+                                                  NULL, NULL, 0, 0, 
+                                                  (PyObject *)ap);
+                if (rp == NULL) goto fail;
+        }
+        else {
+                if (PyArray_SIZE(out) != \
+                    PyArray_MultiplyList(ap->dimensions, ap->nd-1)) {
+                        PyErr_SetString(PyExc_TypeError, 
+                                        "invalid shape for output array.");
+                }
+                rp = (PyArrayObject *)\
+                        PyArray_FromArray(out, 
+                                          PyArray_DescrFromType(PyArray_INTP),
+                                          NPY_CARRAY | NPY_UPDATEIFCOPY);
+                if (rp == NULL) goto fail;
+                if (rp != out) copyret = 1;
+        }
+
 	NPY_BEGIN_THREADS_DESCR(ap->descr)
 	n = PyArray_SIZE(ap)/m;
 	rptr = (intp *)rp->data;
@@ -3188,6 +3216,13 @@
 	NPY_END_THREADS_DESCR(ap->descr)
 
 	Py_DECREF(ap);
+        if (copyret) {
+                PyArrayObject *obj;
+                obj = (PyArrayObject *)rp->base;
+                Py_INCREF(obj);
+                Py_DECREF(rp);
+                rp = obj;
+        }
 	return (PyObject *)rp;
 	
  fail:

Modified: trunk/numpy/numarray/functions.py
===================================================================
--- trunk/numpy/numarray/functions.py	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/numarray/functions.py	2006-08-10 12:48:04 UTC (rev 2983)
@@ -40,7 +40,7 @@
 import os, sys, math, operator
 
 from numpy import dot as matrixmultiply, dot, vdot, ravel, concatenate, all,\
-     allclose, any, argmax, argmin, around, argsort, array_equal, array_equiv,\
+     allclose, any, around, argsort, array_equal, array_equiv,\
      array_str, array_repr, average, CLIP, RAISE, WRAP, clip, concatenate, \
      diagonal, e, pi, fromfunction, indices, inner as innerproduct, nonzero, \
      outer as outerproduct, kron as kroneckerproduct, lexsort, putmask, rank, \
@@ -438,3 +438,9 @@
 def cumproduct(a1, axis=0, out=None, type=None, dim=0):
     return N.asarray(a1).cumprod(axis,dtype=type,out=out)
 
+def argmax(x, axis=-1):
+    return N.argmax(x, axis)
+
+def argmin(x, axis=-1):
+    return N.argmin(x, axis)
+

Modified: trunk/numpy/oldnumeric/functions.py
===================================================================
--- trunk/numpy/oldnumeric/functions.py	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/oldnumeric/functions.py	2006-08-10 12:48:04 UTC (rev 2983)
@@ -6,10 +6,10 @@
 from typeconv import convtypecode, convtypecode2
 
 __all__ = ['take', 'repeat', 'sum', 'product', 'sometrue', 'alltrue',
-           'cumsum', 'cumproduct',
+           'cumsum', 'cumproduct', 'compress',
            'ones', 'empty', 'identity', 'zeros', 'array', 'asarray',
            'nonzero', 'reshape', 'arange', 'fromstring', 'ravel', 'trace',
-           'indices', 'where','sarray','cross_product']
+           'indices', 'where','sarray','cross_product', 'argmax', 'argmin']
 
 def take(a, indicies, axis=0):
     return N.take(a, indicies, axis)
@@ -34,6 +34,15 @@
 
 def cumproduct(x, axis=0):
     return N.cumproduct(x, axis)
+
+def argmax(x, axis=-1):
+    return N.argmax(x, axis)
+
+def argmin(x, axis=-1):
+    return N.argmax(x, axis)
+
+def compress(condition, m, axis=-1):
+    return N.compress(condition, m, axis)
     
 def ones(shape, typecode='l', savespace=0, dtype=None):
     """ones(shape, dtype=int) returns an array of the given

Modified: trunk/numpy/oldnumeric/misc.py
===================================================================
--- trunk/numpy/oldnumeric/misc.py	2006-08-10 11:55:33 UTC (rev 2982)
+++ trunk/numpy/oldnumeric/misc.py	2006-08-10 12:48:04 UTC (rev 2983)
@@ -3,11 +3,11 @@
 
 __all__ = ['load', 'sort', 'copy_reg', 'clip', 'putmask', 'Unpickler', 'rank',
            'sign', 'shape', 'types', 'allclose', 'size',
-           'argmax', 'choose', 'swapaxes', 'array_str',
-           'pi', 'math', 'compress', 'concatenate',
+           'choose', 'swapaxes', 'array_str',
+           'pi', 'math', 'concatenate',
            'around', 'vdot', 'transpose', 'array2string', 'diagonal',
            'searchsorted', 'put', 'fromfunction', 'copy', 'resize',
-           'array_repr', 'e', 'argmin', 'StringIO', 'pickle', 'average',
+           'array_repr', 'e', 'StringIO', 'pickle', 'average',
            'argsort', 'convolve', 'loads', 'cross_correlate',
            'Pickler', 'dot', 'outerproduct', 'innerproduct']
 
@@ -20,8 +20,8 @@
 from pickle import load, loads
 
 from numpy import sort, clip, putmask, rank, sign, shape, allclose, size,\
-     argmax, choose, swapaxes, array_str, array_repr, argmin, e, pi, \
-     fromfunction, resize, around, compress, concatenate, vdot, transpose, \
+     choose, swapaxes, array_str, array_repr, e, pi, \
+     fromfunction, resize, around, concatenate, vdot, transpose, \
      diagonal, searchsorted, put, average, argsort, convolve, dot, \
      outer as outerproduct, inner as innerproduct, correlate as cross_correlate
 



More information about the Numpy-svn mailing list