[Numpy-discussion] Adding zero-dimension arrays, bug?

Robert Kern robert.kern at gmail.com
Tue Jun 9 14:48:38 EDT 2009


On Tue, Jun 9, 2009 at 09:38, David Paul
Reichert<D.P.Reichert at sms.ed.ac.uk> wrote:
> Hi,
>
> Numpy let's me define arrays with zero rows and/or
> columns, and that's wanted behaviour from what I have
> read in discussions. However, I can add an array
> with zero rows to an array with one row (but not more),
> resulting in another zero row array, like so:
>
>
> In: a = zeros((4,0))
>
> In: a
> Out: array([], shape=(4, 0), dtype=float64)
>
> In: b = zeros((4,1))
>
> In: b
> Out:
> array([[ 0.],
>        [ 0.],
>        [ 0.],
>        [ 0.]])
>
> In: a + b
> Out: array([], shape=(4, 0), dtype=float64)
>
>
> Is this a bug? This should give a shape mismatch error,
> shouldn't?

No. According to the rules of broadcasting, the axis will match iff
the sizes from each array are the same OR one of them is 1. So (4,1)
will broadcast with (4,0) to result in a (4,0) array.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list