[Numpy-svn] r3191 - trunk/numpy/core/src

numpy-svn at scipy.org numpy-svn at scipy.org
Tue Sep 19 19:03:25 EDT 2006


Author: oliphant
Date: 2006-09-19 18:03:14 -0500 (Tue, 19 Sep 2006)
New Revision: 3191

Modified:
   trunk/numpy/core/src/arraymethods.c
Log:
Fix mean, std, and var methods so that they reduce over double data-type with integer inputs.

Modified: trunk/numpy/core/src/arraymethods.c
===================================================================
--- trunk/numpy/core/src/arraymethods.c	2006-09-19 17:29:34 UTC (rev 3190)
+++ trunk/numpy/core/src/arraymethods.c	2006-09-19 23:03:14 UTC (rev 3191)
@@ -1353,6 +1353,27 @@
 	return ret;
 }
 
+/* Return typenumber from dtype2 unless it is NULL, then return 
+   NPY_DOUBLE if dtype1->type_num is integer or bool
+   and dtype1->type_num otherwise. 
+*/
+static int
+_get_type_num_double(PyArray_Descr *dtype1, PyArray_Descr *dtype2)
+{
+        if (dtype2 == NULL) { /* Use floating point reduction 
+                                 on integer data-types */
+                if (dtype1->type_num < NPY_FLOAT) {
+                        return NPY_DOUBLE;
+                }
+                else {
+                        return dtype1->type_num;
+                }
+        }
+        else {
+                return dtype2->type_num;
+        }
+}
+
 #define _CHKTYPENUM(typ) ((typ) ? (typ)->type_num : PyArray_NOTYPE)
 
 static PyObject *
@@ -1361,6 +1382,7 @@
 	int axis=MAX_DIMS;
 	PyArray_Descr *dtype=NULL;
         PyArrayObject *out=NULL;
+        int num;
 	static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
@@ -1368,9 +1390,10 @@
 					 &axis, PyArray_DescrConverter2,
 					 &dtype,
                                          PyArray_OutputConverter,
-                                         &out)) return NULL;
+                                         &out)) return NULL;        
 
-	return PyArray_Mean(self, axis, _CHKTYPENUM(dtype), out);
+        num = _get_type_num_double(self->descr, dtype);
+	return PyArray_Mean(self, axis, num, out);
 }
 
 static PyObject *
@@ -1489,6 +1512,7 @@
 	int axis=MAX_DIMS;
 	PyArray_Descr *dtype=NULL;
         PyArrayObject *out=NULL;
+        int num;
 	static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
@@ -1498,7 +1522,8 @@
                                          PyArray_OutputConverter,
                                          &out)) return NULL;
 
-	return PyArray_Std(self, axis, _CHKTYPENUM(dtype), out, 0);
+        num = _get_type_num_double(self->descr, dtype);
+        return PyArray_Std(self, axis, num, out, 0);
 }
 
 
@@ -1508,6 +1533,7 @@
 	int axis=MAX_DIMS;
 	PyArray_Descr *dtype=NULL;
         PyArrayObject *out=NULL;
+        int num;
 	static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
@@ -1517,7 +1543,8 @@
                                          PyArray_OutputConverter,
                                          &out)) return NULL;
 
-	return PyArray_Std(self, axis, _CHKTYPENUM(dtype), out, 1);
+        num = _get_type_num_double(self->descr, dtype);
+	return PyArray_Std(self, axis, num, out, 1);
 }
 
 




More information about the Numpy-svn mailing list