KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I just discovered — by chance — that an array in numpy may be indexed by an empty tuple: In [62]: a = arange(5) In [63]: a[()] Out[63]: array([0, 1, 2, 3, 4]) I found some documentation on the numpy wiki ZeroRankArray : (Sasha) First, whatever choice is made for x[...] and x[()] they should be the same because ... is just syntactic sugar for "as many : as necessary", which in the case of zero rank leads to ... = (:,)*0 = (). Second, rank zero arrays and numpy scalar types are interchangeable within numpy, but numpy scalars can be use in some python constructs where ndarrays can't. So, for 0-d arrays a[()] and a[...] are supposed to be equivalent. Are they for higher-dimensional arrays, too? They strongly appear to be: In [65]: a = arange(25).reshape(5, 5) In [66]: a[()] is a[...] Out[66]: False In [67]: (a[()] == a[...]).all() Out[67]: True In [68]: a = arange(3**7).reshape((3,)*7) In [69]: (a[()] == a[...]).all() Out[69]: True But , it is not syntactic sugar. Not for a high-dimensional array, and not even for a 0-d array: In [76]: a[()] is a Out[76]: False In [77]: a[...] is a Out[77]: True In [79]: b = array(0) In [80]: b[()] is b Out[80]: False In [81]: b[...] is b Out[81]: True And then there is the case of indexing by an empty list , which does something else altogether, but appears equivalent to indexing with an empty ndarray : In [78]: a[[]] Out[78]: array([], shape=(0, 3, 3, 3, 3, 3, 3), dtype=int64) In [86]: a[arange(0)] Out[86]: array([], shape=(0, 3, 3, 3, 3, 3, 3), dtype=int64) In [82]: b[[]] --------------------------------------------------------------------------- IndexError Traceback
Tags (comma-separated)
Save Edits
Cancel