[SciPy-dev] [Numpy-discussion] Re: How to handle a[...] in numpy?

Sasha ndarray at mac.com
Mon Jan 9 21:28:11 CST 2006


Sorry, forgot to attach.

On 1/9/06, Sasha <ndarray at mac.com> wrote:
> Attached patch implements indexing of zero rank arrays:
>
> >>> x = array(42)
> >>> x[...]
> 42
> >>> x[()]
> 42
> >>> x[()].dtype
> <type 'int32_arrtype'>
> >>> type(x[()])
> <type 'int32_arrtype'>
>
> Ellipsis and empty tuple are the only two valid indices.
>
> So far there was only one +1 vote for the feature (from Francesc
> Altet, who also suggested  that x[()] be allowed).  Does anyone object
> to this feature?
>
> I have also proposed to support x[...,newaxis] (see comment by
> sdementen at http://www.scipy.org/wikis/numdesign/).  I will add this
> feature if there is interest.
>
> Finally, if we make x[...] valid for rank-0 arrays, should x[...] =
> value be allowed as well? I think it should, applying the principle of
> least surprized.  An additional consideration is that rank-0 arrays
> are unhashable, but there is no obvious way to change their values.
>
> The following example show that rank-0 arrays are in fact mutable (in
> a non-obvious way):
>
> >>> x = array(1)
> >>> x.shape=(1,); x[0] = 42; x.shape=()
> >>> x
>
> Please comment on the following proposals:
>
> 1. x[...]
> 2. x[...] = value
> 3. x[..., newaxis]
>
>
> -- sasha
>
-------------- next part --------------
Index: numpy/core/src/arrayobject.c
===================================================================
--- numpy/core/src/arrayobject.c	(revision 1863)
+++ numpy/core/src/arrayobject.c	(working copy)
@@ -1796,6 +1796,9 @@
 		return NULL;
 	}
         if (self->nd == 0) {
+		if (op == Py_Ellipsis ||
+		    PyTuple_Check(op) && 0 == PyTuple_GET_SIZE(op))
+			return PyArray_ToScalar(self->data, self);
                 PyErr_SetString(PyExc_IndexError, 
                                 "0-d arrays can't be indexed.");
                 return NULL;
Index: numpy/core/tests/test_multiarray.py
===================================================================
--- numpy/core/tests/test_multiarray.py	(revision 1863)
+++ numpy/core/tests/test_multiarray.py	(working copy)
@@ -69,5 +69,32 @@
         d2 = dtypedescr('f8')
         assert_equal(d2, dtypedescr(float64))
         
+class test_zero_rank(ScipyTestCase):
+    def setUp(self):
+        self.d = array(0), array('x', object)
         
-    
+    def check_ellipsis_subscript(self):
+        a,b = self.d
+
+        self.failUnlessEqual(a[...], 0)
+        self.failUnlessEqual(b[...].item(), 'x')
+        self.failUnless(type(a[...]) is a.dtype)
+        self.failUnless(type(b[...]) is b.dtype)
+        
+    def check_empty_subscript(self):
+        a,b = self.d
+
+        self.failUnlessEqual(a[()], 0)
+        self.failUnlessEqual(b[()].item(), 'x')
+        self.failUnless(type(a[()]) is a.dtype)
+        self.failUnless(type(b[()]) is b.dtype)
+
+    def check_invalid_subscript(self):
+        a,b = self.d
+        self.failUnlessRaises(IndexError, lambda x: x[0], a)
+        self.failUnlessRaises(IndexError, lambda x: x[0], b)
+        self.failUnlessRaises(IndexError, lambda x: x[array([], int)], a)
+        self.failUnlessRaises(IndexError, lambda x: x[array([], int)], b)
+
+if __name__ == "__main__":
+        ScipyTest('numpy.core.multiarray').run()


More information about the Scipy-dev mailing list