Re: [SciPy-Dev] docstring standard: parameter shape description

On Tue, 29 Jan 2013 10:09:54 -0500, Jonathan Guyer <guyer@nist.gov> wrote:
On Jan 28, 2013, at 7:35 PM, Joe Harrington wrote:
On Mon, 28 Jan 2013 13:47:26 -0800 Nathaniel Smith <njs@pobox.com> wrote:
"any size" should mean 0+. "absent" is not a size. If a function does accept an optional final dimension, can we write that like 'shape (N, D) or shape (N,)'?
An array dimension cannot have 0 elements (the total size is the product of the shape tuple's elements).
That's not true. An array dimension certainly can have 0 elements:
import numpy as np a = np.zeros((0,)) b = np.zeros((1, 0)) c = np.zeros((1, 0, 3)) a.shape, b.shape, c.shape ((0,), (1, 0), (1, 0, 3))
For most applications, there is little utility in being able to declare an "empty" array, but you can certainly do it (and for us, it's handy for FiPy to partition PDE solutions across multiple processors without having to special-case any processors that don't get any solution nodes).
As we reported in http://projects.scipy.org/numpy/ticket/1171, the treatment of 0-length dimensions is buggy, but NumPy nonetheless allows for it.
I did the following test before I posted:
import numpy as np a=np.array(((2,3),(3,4))) a array([[2, 3], [3, 4]]) a.shape (2, 2) a.shape=(2,0,2) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: total size of new array must be unchanged
So, you can't have a 0-element dimension in an array that is not already 0-size. I didn't think about 0-size arrays having some non-zero dimensions. I guess that makes mathematical sense, but then what is the point? Between this and your bug report, I wonder whether a 0-element array dimension is a feature you can rely on. Can a developer let us know if it's an accident that it works at all? I'll leave it to you to start a new thread on this and get the inconsistencies resolved, since you're the one using the feature. The report was last touched 3 years ago... --jh--

On Tue, Jan 29, 2013 at 3:06 PM, Joe Harrington <jh@physics.ucf.edu> wrote:
I did the following test before I posted:
import numpy as np a=np.array(((2,3),(3,4))) a array([[2, 3], [3, 4]]) a.shape (2, 2) a.shape=(2,0,2) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: total size of new array must be unchanged
So, you can't have a 0-element dimension in an array that is not already 0-size.
Yes, but you can't create a 7-element dimension in an array that is already not a multiple-of-7 in size, without this ruling out the idea of 7-element dimensions as a thing :-).
I didn't think about 0-size arrays having some non-zero dimensions. I guess that makes mathematical sense, but then what is the point?
Between this and your bug report, I wonder whether a 0-element array dimension is a feature you can rely on. Can a developer let us know if it's an accident that it works at all? I'll leave it to you to start a new thread on this and get the inconsistencies resolved, since you're the one using the feature. The report was last touched 3 years ago...
It's certainly intended, and very useful! Most code that can handle an arbitrary size for a dimension can automatically handle a 0 size, and often this lets you avoid having to write in special cases. This is also why e.g. people have spent a lot of effort thinking about what it means to sum 0 elements: https://en.wikipedia.org/wiki/Empty_sum (And numpy does the Right Thing for expressions like np.sum([]), np.prod([]).) I don't know about that particular bug report's current status, but just because there's some obscure corner of the fancy indexing system that has a bug it doesn't mean that we don't support 0 sized arrays in general... that would mean a lot of numpy was unsupported! ;-) -n

On Jan 29, 2013, at 6:06 PM, Joe Harrington wrote:
As we reported in http://projects.scipy.org/numpy/ticket/1171, the treatment of 0-length dimensions is buggy, but NumPy nonetheless allows for it.
I did the following test before I posted:
import numpy as np a=np.array(((2,3),(3,4))) a array([[2, 3], [3, 4]]) a.shape (2, 2) a.shape=(2,0,2) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: total size of new array must be unchanged
So, you can't have a 0-element dimension in an array that is not already 0-size.
I guess that's to be expected. That use case doesn't arise for us, so I can't say whether it would be expected or desirable to be able to do such a reshaping.
I didn't think about 0-size arrays having some non-zero dimensions. I guess that makes mathematical sense, but then what is the point?
Like I said, one point is to be able to deal with an array partitioned across multiple processors in a consistent way. If the number of processors doesn't evenly divide into the array size, then it's possible to end up with "empty" arrays that are semantically consistent with all of the other "full" arrays.
Between this and your bug report, I wonder whether a 0-element array dimension is a feature you can rely on. Can a developer let us know if it's an accident that it works at all?
Well, I don't know if it was originally an accident, but Travis agreed with my original bug report.
I'll leave it to you to start a new thread on this and get the inconsistencies resolved, since you're the one using the feature. The report was last touched 3 years ago...
I discovered after I posted that this ticket is very much alive and well at https://github.com/numpy/numpy/issues/1769 with an active pull request to fix it at https://github.com/numpy/numpy/pull/2701

So, you can't have a 0-element dimension in an array that is not already 0-size. I didn't think about 0-size arrays having some non-zero dimensions. I guess that makes mathematical sense, but then what is the point?
Between this and your bug report, I wonder whether a 0-element array dimension is a feature you can rely on. Can a developer let us know if it's an accident that it works at all? I'll leave it to you to start a new thread on this and get the inconsistencies resolved, since you're the one using the feature. The report was last touched 3 years ago. Just to say that back in 2009 I was programming a modeling tool to get
Hi Joe, Le 30/01/2013 00:06, Joe Harrington a écrit : the state space representation of any (linear) electrical circuit and empty arrays were useful in avoiding painful special casing. Within the algorithm there is a block-partioning of the incidence matrix of the circuit graph. Partitioning is dictated by the different kinds of circuit elements. Thus, some of these blocks could be empty and it was very useful that such (0,N) or (M,0) arrays could propagate smoothly in the rest of the algorithm. best, Pierre (This being said, I cannot tell how well numpy dealt with the empty array arithmetic... because it was in Matlab ! Mea culpa ;-) )
participants (4)
-
Joe Harrington
-
Jonathan Guyer
-
Nathaniel Smith
-
Pierre Haessig