| | 21 | Sasha started a [http://www.scipy.net/pipermail/scipy-dev/2006-January/004971.html discussion] on scipy-dev |
| | 22 | with the folowing proposal: |
| | 23 | {{{ |
| | 24 | ... it may be reasonable to allow a[...]. This way |
| | 25 | ellipsis can be interpereted as any number of ":"s including zero. |
| | 26 | Another subscript operation that makes sense for scalars would be |
| | 27 | a[...,newaxis] or even a[{newaxis, }* ..., {newaxis,}*], where |
| | 28 | {newaxis,}* stands for any number of comma-separated newaxis tokens. |
| | 29 | This will allow one to use ellipsis in generic code that would work on |
| | 30 | any numpy type. |
| | 31 | }}} |
| | 32 | The idea to allow increasing the rank of zero-rank arrays with [..., newaxis] was originally |
| | 33 | [http://www.scipy.org/wikis/numdesign/ proposed] by Sebastien. |
| | 34 | |
| | 35 | [http://www.carabos.com/ Francesc Altet] [http://www.scipy.net/pipermail/scipy-dev/2006-January/005022.html supported] |
| | 36 | the idea of [...] on zero-rank arrays and suggested that [()] be supported as well. |
| | 37 | |
| | 38 | Francesc's proposal was: |
| | 39 | {{{ |
| | 40 | In [65]: type(numpy.array(0)[...]) |
| | 41 | Out[65]: <type 'numpy.ndarray'> |
| | 42 | |
| | 43 | In [66]: type(numpy.array(0)[()]) # Indexing a la numarray |
| | 44 | Out[66]: <type 'int32_arrtype'> |
| | 45 | |
| | 46 | In [67]: type(numpy.array(0).item()) # already works |
| | 47 | Out[67]: <type 'int'> |
| | 48 | }}} |
| | 49 | |
| | 50 | There is a consensus that for a zero-rank array x, both x[...] and x[()] should be valid, but the question |
| | 51 | remains on what should be the type of the result - zero rank ndarray or x.dtype? |
| | 52 | |
| | 53 | (Sasha) First, whatever choice is made for x[...] and x[()] they should be the same because ... is just syntactic |
| | 54 | sugar for "as many : as necessary", which in the case of zero rank leads to ... = (:,)*0 = (). Second, rank zero |
| | 55 | arrays and numpy scalar types are interchangeable within numpy, but numpy scalars can be use in some python constructs |
| | 56 | where ndarrays can't. For example: |
| | 57 | {{{ |
| | 58 | >>> (1,)[array(0)] |
| | 59 | Traceback (most recent call last): |
| | 60 | File "<stdin>", line 1, in ? |
| | 61 | TypeError: tuple indices must be integers |
| | 62 | >>> (1,)[int32(0)] |
| | 63 | 1 |
| | 64 | }}} |
| | 65 | Since most if not all numpy function automatically convert zero-rank arrays to scalars on return, there is no reason for |
| | 66 | [...] and [()] operations to be different. |
| | 67 | |