[Numpy-discussion] Indexing empty dimensions with empty arrays

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Dec 26 19:56:08 EST 2011


2011/12/26 Jordi Gutiérrez Hermoso <jordigh at octave.org>:
> On 26 December 2011 14:56, Ralf Gommers <ralf.gommers at googlemail.com> wrote:
>>
>>
>> On Mon, Dec 26, 2011 at 8:50 PM, <josef.pktd at gmail.com> wrote:
>>> I have a hard time thinking through empty 2-dim arrays, and don't know
>>> what rules should apply.
>>> However, in my code I might want to catch these cases rather early
>>> than late and then having to work my way backwards to find out where
>>> the content disappeared.
>>
>>
>> Same here. Almost always, my empty arrays are either due to bugs or they
>> signal that I do need to special-case something. Silent passing through of
>> empty arrays to all numpy functions is not what I would want.
>
> I find it quite annoying to treat the empty set with special
> deference. "All of my great-grandkids live in Antarctica" should be
> true for me (I'm only 30 years old). If you decide that is not true
> for me, it leads to a bunch of other logical annoyances up there
>
> The rule that shouldn't be special cased is what I described: x[idx1,
> idx2] should be a valid construction if it's true that all elements of
> idx1 and idx2 are integers in the correct range. The sizes of the
> empty matrices are also somewhat obvious.
>
> Special-casing vacuous truth makes me write annoying special cases.
> Octave doesn't error out for those special cases, and I think it's a
> good thing it doesn't. It's logically consistent.

I don't think I ever ran into an empty matrix in matlab, and wouldn't
know how it behaves.

But it looks like the [:, empty] is a special case that doesn't work

>>> np.ones((3,0))
array([], shape=(3, 0), dtype=float64)
>>> np.ones((3,0))[1,[]]
array([], dtype=float64)
>>> np.ones((3,0))[:,[]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: invalid index

>>> np.ones((3,0))[np.arange(3),[]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape

oops, my mistake

>>> np.broadcast_arrays(np.arange(3)[:,None],[])
[array([], shape=(3, 0), dtype=int32), array([], shape=(3, 0), dtype=float64)]
>>> np.ones((3,0))[np.arange(3)[:,None],[]]
array([], shape=(3, 0), dtype=float64)
>>> np.broadcast_arrays(np.arange(3)[:,None],[[]])
[array([], shape=(3, 0), dtype=int32), array([], shape=(3, 0), dtype=float64)]

>>> np.ones((3,0))[np.arange(3)[:,None],[]]
array([], shape=(3, 0), dtype=float64)
>>> np.ones((3,0))[np.arange(3)[:,None],[[]]]
array([], shape=(3, 0), dtype=float64)
>>> np.ones((3,0))[np.arange(3)[:,None],np.array([],int)]
array([], shape=(3, 0), dtype=float64)

>>> np.take(np.ones((3,0)),[], axis=1)
array([], shape=(3, 0), dtype=float64)
>>> np.take(np.ones((3,0)),[], axis=0)
array([], shape=(0, 0), dtype=float64)

I would prefer consistent indexing, independent of whether I find it
useful to have pages of code working with nothing.

Josef
I don't think a paper where the referee or editor catches the authors
using assumptions that describe an empty set will ever get published
(maybe with a qualifier, outside of philosophy).
It might happen, though, that the empty set slips through the
refereeing process.

>
> - Jordi G. H.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list