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

numpy-svn at scipy.org numpy-svn at scipy.org
Thu Nov 16 17:59:22 CST 2006


Author: oliphant
Date: 2006-11-16 17:59:19 -0600 (Thu, 16 Nov 2006)
New Revision: 3437

Modified:
   trunk/numpy/core/src/arrayobject.c
Log:
Used an adapted setArrayFromSequence from numarray to increase speed of Assign_Array dramatically

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-11-13 21:49:43 UTC (rev 3436)
+++ trunk/numpy/core/src/arrayobject.c	2006-11-16 23:59:19 UTC (rev 3437)
@@ -7088,30 +7088,56 @@
         return outtype;
 }
 
+/* adapted from Numarray */
+static int 
+setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, intp offset)
+{
+        Py_ssize_t i, slen = PySequence_Length(s);
+        int res = 0;
+
+        if (dim > a->nd) {
+                PyErr_Format(PyExc_ValueError,
+                             "setArrayFromSequence: sequence/array dimensions mismatch.");
+                return -1;
+        }
+
+        if (slen != a->dimensions[dim]) {
+                PyErr_Format(PyExc_ValueError,
+                             "setArrayFromSequence: sequence/array shape mismatch.");
+                return -1;
+        }
+
+        for(i=0; i<slen; i++) {
+                PyObject *o = PySequence_GetItem(s, i);
+                if ((a->nd - dim) > 1) {
+                        res = setArrayFromSequence(a, o, dim+1, offset);
+                }
+                else {
+                        res = a->descr->f->setitem(o, (a->data + offset), a);
+                }
+                Py_DECREF(o);
+                if (res < 0) return res;
+                offset += a->strides[dim];
+        }
+        return 0;
+}
+
+
 static int
 Assign_Array(PyArrayObject *self, PyObject *v)
 {
-        PyObject *e;
-        int l, r;
-
         if (!PySequence_Check(v)) {
                 PyErr_SetString(PyExc_ValueError,
                                 "assignment from non-sequence");
                 return -1;
         }
+        if (self->nd == 0) {
+                PyErr_SetString(PyExc_ValueError,
+                                "assignment to 0-d array");
+                return -1;
+        }
 
-        l=PyObject_Length(v);
-        if(l < 0) return -1;
-
-        while(--l >= 0)
-                {
-                        e=PySequence_GetItem(v,l);
-                        if (e == NULL) return -1;
-                        r = PySequence_SetItem((PyObject*)self,l,e);
-                        Py_DECREF(e);
-                        if(r == -1) return -1;
-                }
-        return 0;
+        return setArrayFromSequence(self, v, 0, 0);
 }
 
 /* "Array Scalars don't call this code" */



More information about the Numpy-svn mailing list