[Numpy-discussion] Is this a bug?
Jaime Fernández del Río
jaime.frio at gmail.com
Tue Sep 16 16:31:59 EDT 2014
On Tue, Sep 16, 2014 at 12:27 PM, Charles R Harris <
charlesr.harris at gmail.com> wrote:
> Hi All,
>
> It turns out that gufuncs will broadcast the last dimension if it is one.
> For instance, inner1d has signature `(n), (n) -> ()`, yet
>
> In [27]: inner1d([1,1,1], [1])
> Out[27]: 3
>
> In [28]: inner1d([1,1,1], [1,1])
> ---------------------------------------------------------------------------
> ValueError Traceback (most recent call last)
> <ipython-input-28-e53e62e35349> in <module>()
> ----> 1 inner1d([1,1,1], [1,1])
>
> ValueError: inner1d: Operand 1 has a mismatch in its core dimension 0,
> with gufunc signature (i),(i)->() (size 2 is different from 3)
>
>
> I'd think this is a bug, as the dimensions should match. Note that scalar
> 1 will be promoted to [1] in this case.
>
> Thoughts?
>
If it is a bug, it is an extended one, because it is the same behavior of
einsum:
>>> np.einsum('i,i', [1,1,1], [1])
3
>>> np.einsum('i,i', [1,1,1], [1,1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with remapped shapes
[origi
nal->remapped]: (3,)->(3,) (2,)->(2,)
And I think it is a conscious design decision, there is a comment about
broadcasting missing core dimensions here:
https://github.com/numpy/numpy/blob/master/numpy/core/src/umath/ufunc_object.c#L1940
and the code makes it very explicit that input argument dimensions with the
same label are broadcast to a common shape, see here:
https://github.com/numpy/numpy/blob/master/numpy/core/src/umath/ufunc_object.c#L1956
I kind of expect numpy to broadcast whenever possible, so this doesn't feel
wrong to me.
That said, it is hard to come up with convincing examples of how this
behavior would be useful in any practical context. But changing something
that has been working like that for so long seems like a risky thing. And I
cannot come with a convincing example of why it would be harmful either.
Jaime
--
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140916/41a4faed/attachment.html>
More information about the NumPy-Discussion
mailing list