<br><br><div class="gmail_quote">On Wed, Dec 28, 2011 at 1:57 PM, Dag Sverre Seljebotn <span dir="ltr"><<a href="mailto:d.s.seljebotn@astro.uio.no">d.s.seljebotn@astro.uio.no</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On 12/28/2011 01:52 PM, Dag Sverre Seljebotn wrote:<br>
> On 12/28/2011 09:33 AM, Ralf Gommers wrote:<br>
>><br>
>><br>
>> 2011/12/27 Jordi Gutiérrez Hermoso<<a href="mailto:jordigh@octave.org">jordigh@octave.org</a><br>
>> <mailto:<a href="mailto:jordigh@octave.org">jordigh@octave.org</a>>><br>
>><br>
>>      On 26 December 2011 14:56, Ralf Gommers<<a href="mailto:ralf.gommers@googlemail.com">ralf.gommers@googlemail.com</a><br>
>>      <mailto:<a href="mailto:ralf.gommers@googlemail.com">ralf.gommers@googlemail.com</a>>>  wrote:<br>
>>       ><br>
>>       ><br>
>>       >  On Mon, Dec 26, 2011 at 8:50 PM,<<a href="mailto:josef.pktd@gmail.com">josef.pktd@gmail.com</a><br>
>>      <mailto:<a href="mailto:josef.pktd@gmail.com">josef.pktd@gmail.com</a>>>  wrote:<br>
>>       >>  I have a hard time thinking through empty 2-dim arrays, and<br>
>>      don't know<br>
>>       >>  what rules should apply.<br>
>>       >>  However, in my code I might want to catch these cases rather early<br>
>>       >>  than late and then having to work my way backwards to find out where<br>
>>       >>  the content disappeared.<br>
>>       ><br>
>>       ><br>
>>       >  Same here. Almost always, my empty arrays are either due to bugs<br>
>>      or they<br>
>>       >  signal that I do need to special-case something. Silent passing<br>
>>      through of<br>
>>       >  empty arrays to all numpy functions is not what I would want.<br>
>><br>
>>      I find it quite annoying to treat the empty set with special<br>
>>      deference. "All of my great-grandkids live in Antarctica" should be<br>
>>      true for me (I'm only 30 years old). If you decide that is not true<br>
>>      for me, it leads to a bunch of other logical annoyances up there<br>
>><br>
>><br>
>> Guess you don't mean true/false, because it's neither. But I understand<br>
>> you want an empty array back instead of an error.<br>
>><br>
>> Currently the problem is that when you do get that empty array back,<br>
>> you'll then use that for something else and it will probably still<br>
>> crash. Many numpy functions do not check for empty input and will still<br>
>> give exceptions. My impression is that you're better off handling these<br>
>> where you create the empty array, rather than in some random place later<br>
>> on. The alternative is to have consistent rules for empty arrays, and<br>
>> handle them explicitly in all functions. Can be done, but is of course a<br>
>> lot of work and has some overhead.<br>
><br>
> Are you saying that the existence of other bugs means that this bug<br>
> shouldn't be fixed? I just fail to see the relevance of these other bugs<br>
> to this discussion.<br></div></div></blockquote><div><br>See below.<br> <br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="HOEnZb">
<div class="h5">
> For the record, I've encountered this bug many times myself and it's<br>
> rather irritating, since it leads to more verbose code.<br>
><br>
> It is useful whenever you want to return data that is a subset of the<br>
> input data (since the selected subset can usually be zero-sized<br>
> sometimes -- remember, in computer science the only numbers are 0, 1,<br>
> and "any number").<br>
><br>
> Here's one of the examples I've had. The Interpolative Decomposition<br>
> decomposes a m-by-n matrix A of rank k as<br>
><br>
> A = B C<br>
><br>
> where B is an m-by-k matrix consisting of a subset of the columns of A,<br>
> and C is a k-by-n matrix.<br>
><br>
> Now, if A is all zeros (which is often the case for me), then k is 0. I<br>
> would still like to create the m-by-0 matrix B by doing<br>
><br>
> B = A[:, selected_columns]<br>
><br>
> But now I have to do this instead:<br>
><br>
> if len(selected_columns) == 0:<br>
>       B = np.zeros((A.shape[0], 0), dtype=A.dtype)<br>
> else:<br>
>       B = A[:, selected_columns]<br>
><br>
> In this case, zero-sized B and C are of course perfectly valid and<br>
> useful results:<br>
><br>
> In [2]: np.dot(np.ones((3,0)), np.ones((0, 5)))<br>
> Out[2]:<br>
> array([[ 0.,  0.,  0.,  0.,  0.],<br>
>          [ 0.,  0.,  0.,  0.,  0.],<br>
>          [ 0.,  0.,  0.,  0.,  0.]])<br>
><br>
<br>
</div></div>And to answer the obvious question: Yes, this is a real usecase. It is<br>
used for something similar to image compression, where sub-sections of<br>
the images may well be all-zero and have zero rank (full story at [1]).<br>
<br></blockquote><div>Thanks for the example. I was a little surprised that dot works. Then I read what wikipedia had to say about empty arrays. It mentions dot like you do, and that the determinant of the 0-by-0 matrix is 1. So I try:<br>
<br>In [1]: a = np.zeros((0,0))<br><br>In [2]: a<br>Out[2]: array([], shape=(0, 0), dtype=float64)<br><br>In [3]: np.linalg.det(a)<br>Parameter 4 to routine DGETRF was incorrect<br><segfault><br><br> </div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

Reading the above thread I understand Ralf's reasoning better, but<br>
really, relying on NumPy's buggy behaviour to discover bugs in user code<br>
seems like the wrong approach. Tools should be dumb unless there are<br>
good reasons to make them smart. I'd be rather irritated about my hammer<br>
if it refused to drive in nails that it decided where in the wrong spot.<br></blockquote><div><br>The point is not that we shouldn't fix it, but that it's a waste of time to fix it in only one place. I remember fixing several functions to explicitly check for empty arrays and then returning an empty array or giving a sensible error.<br>
<br>So can you answer my question: do you think it's worth the time and computational overhead to handle empty arrays in all functions?<br><br>Ralf <br></div></div>