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

numpy-svn at scipy.org numpy-svn at scipy.org
Wed Oct 18 00:44:33 EDT 2006


Author: oliphant
Date: 2006-10-17 23:44:28 -0500 (Tue, 17 Oct 2006)
New Revision: 3354

Modified:
   trunk/numpy/core/_internal.py
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/multiarraymodule.c
Log:
Fix so that reshape copies fewer times. Fix so that arr.ctypes.data returns the integer and arr.ctypes._as_parameter_ returns the c_void_p type

Modified: trunk/numpy/core/_internal.py
===================================================================
--- trunk/numpy/core/_internal.py	2006-10-17 23:53:52 UTC (rev 3353)
+++ trunk/numpy/core/_internal.py	2006-10-18 04:44:28 UTC (rev 3354)
@@ -248,7 +248,7 @@
         return (obj*self._arr.ndim)(*self._arr.strides)
     
     def get_data(self):
-        return self._ctypes.c_void_p(self._data)
+        return self._data
 
     def get_shape(self):
         if self._zerod: 
@@ -261,7 +261,7 @@
         return (_getintp_ctype()*self._arr.ndim)(*self._arr.strides)
 
     def get_as_parameter(self):
-        return self._data
+        return self._ctypes.c_void_p(self._data)
     
     data = property(get_data, None, doc="c-types data")
     shape = property(get_shape, None, doc="c-types shape")

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-10-17 23:53:52 UTC (rev 3353)
+++ trunk/numpy/core/src/arrayobject.c	2006-10-18 04:44:28 UTC (rev 3354)
@@ -7819,7 +7819,8 @@
                    count and return the input */
                 else {
                         Py_DECREF(newtype);
-                        if ((flags & ENSUREARRAY) && !PyArray_CheckExact(arr)) {
+                        if ((flags & ENSUREARRAY) && 
+                            !PyArray_CheckExact(arr)) {
                                 Py_INCREF(arr->descr);
                                 ret = (PyArrayObject *)                 \
                                         PyArray_NewFromDescr(&PyArray_Type,
@@ -7840,7 +7841,7 @@
         }
 
         /* The desired output type is different than the input
-           array type */
+           array type and copy was not specified */
         else {
                 if ((flags & UPDATEIFCOPY) &&                   \
                     (!PyArray_ISWRITEABLE(arr))) {

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2006-10-17 23:53:52 UTC (rev 3353)
+++ trunk/numpy/core/src/multiarraymodule.c	2006-10-18 04:44:28 UTC (rev 3354)
@@ -454,6 +454,25 @@
 	return 0;
 }
 
+/* returns True if self->nd > 1 and all 
+   there is more than one dimension filled with 1.
+ */
+static int
+_nd_bigger_than_one(PyArrayObject *arr)
+{
+        int i, nd;
+        int count=0;
+        nd = arr->nd;
+        if (nd > 1) {
+                for (i=0; i<nd; i++) {
+                        if (arr->dimensions[i] > 1)
+                                count++;
+                        if (count > 1) return 1;
+                }
+        }
+        return 0;
+}
+
 /* Returns a new array
    with the new shape from the data
    in the old array --- order-perspective depends on fortran argument.
@@ -514,12 +533,16 @@
 			return NULL;
 
 		/* sometimes we have to create a new copy of the array
-		   in order to get the right orientation 
+		   in order to get the right orientation and
+                   because we can't just re-use the buffer with the 
+                   data in the order it is in.
 		*/
 		if (!(PyArray_ISONESEGMENT(self)) || 
-		    ((self->nd > 1) && 
-		     ((PyArray_ISCONTIGUOUS(self) && fortran == NPY_FORTRANORDER)
-		      || (PyArray_ISFORTRAN(self) && fortran == NPY_CORDER)))) {
+                    (((PyArray_CHKFLAGS(self, NPY_CONTIGUOUS) && 
+                       fortran == NPY_FORTRANORDER)
+                      || (PyArray_CHKFLAGS(self, NPY_FORTRAN) && 
+                          fortran == NPY_CORDER)) && 
+                     _nd_bigger_than_one(self))) {
 			PyObject *new;
 			new = PyArray_NewCopy(self, fortran);
 			if (new == NULL) return NULL;




More information about the Numpy-svn mailing list