2 ms·
This reminds me of the time I wrote a class for representing mutable arrays of any dimension, where you could add/remove dimensions at runtime on those array ob
by layer8 4y ago
This reminds me of the time I wrote a class for representing mutable arrays of any dimension, where you could add/remove dimensions at runtime on those array objects. It confused me for a while that when removing the last dimension, the now 0-dimensional array would always contain one cell (because x^0 = 1), so be a singular value, whereas for an n-dimensional array with n > 0 I could set one of the dimensions to length 0, resulting in no values. But with 0 dimensions, you couldn’t get to no values, it was always 1 value.
The final insight was that setting the length of a dimension to 1 basically removes that dimension (if you ignore the change in element indexing), so setting the length to zero doesn’t really makes sense. In the context of shapes in continuous space, the analogon of setting the dimension to one is setting it to zero: if you set the length of a rectangle to zero, you get a line, and you can’t reduce the dimension to below zero, because it has already vanished. In the array case, setting it to below 1 (so to 0) removes all elements, so arguably there is no array anymore, and shouldn’t be a mutation allowed within an array.
- _kulang 4y agoThis is why I dislike NumPy, sizes (100,) and (100,1) are different. I understand conceptually representing it as a “slice” but this is in contrast to something like MATLAB where slicing a 100x100 for a column gives you (sensibly) a 100x1 array
- layer8 4y agoOne difficulty is, if (100,) and (100,1) are the same, then (100,1) and (1,100) arguably are also the same, as well as (1,100,1), etc. (They all have the same shape, a “stick” of size 100.) You could disallow dimensions of size 1, but then why disallow (100,1) and (1,100) being different if (2,100) and (100,2) are allowed and different? One way to see it is that you always have infinitely many dimensions, almost all of which have size 1, and you just choose which index positions you use for those that are not size 1. Hence (100,) is really (100,1,1,1,1,1,…), and so is (100,1), while (1,100) is really (1,100,1,1,1,1,…), same as (1,100,1), but different from (1,1,100), and so on. But it’s not entirely intuitive, because (1,100) and (100,1) have still the same shape. It’s just that, as soon as you have two non-singular dimensions, there’s the incidental necessity that you have to choose a defined order for the dimensions somehow, because [1,2] is a different element from [2,1]. The problem arguably falls away if the dimensions are named instead of numbered.