[Numpy-discussion] Is this a bug?
Charles R Harris
charlesr.harris at gmail.com
Wed Sep 17 08:33:26 EDT 2014
On Wed, Sep 17, 2014 at 2:30 AM, Sebastian Berg <sebastian at sipsolutions.net>
wrote:
> On Di, 2014-09-16 at 16:51 -0400, Nathaniel Smith wrote:
> > On Tue, Sep 16, 2014 at 4:31 PM, Jaime Fernández del Río
> > <jaime.frio at gmail.com> wrote:
> > > 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
> >
> > "intentional" and "sensible" are not always the same thing :-). That
> > said, it isn't totally obvious to me what the correct behaviour for
> > einsum is in this case.
> >
> > > 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.
> >
> > The case Chuck is talking about is like if we allowed matrix
> > multiplication between an array with shape (n, 1) with an array with
> > (k, m), because (n, 1) can be broadcast to (n, k). This feels VERY
> > wrong to me, will certainly hide many bugs, and is definitely not how
> > it works right now (for np.dot, anyway; apparently it does work that
> > way for the brand-new gufunc np.linalg.matrix_multiply, but this must
> > be an accident).
>
> Agreed, the only argument to not change it right away would be being
> afraid of breaking user code abusing the kind of thing Josef mentioned.
>
>
It *is* a big change. I think of the gufuncs as working with matrices and
vectors, the array version of the matrix class. In that case the signature
shapes must be preserved and there should be no broadcasting within the
signature. The matrix class itself fails in that regard:
In [1]: matrix(eye(3)) + matrix([[1,1,1]])
Out[1]:
matrix([[ 2., 1., 1.],
[ 1., 2., 1.],
[ 1., 1., 2.]])
Which is all wrong for a matrix type.
It would also be nice if the order could be made part of the signature as
DGEMM and friends like one of the argument axis to be contiguous, but I
don't see a clean way to do that. The gufuncs do have an order parameter
which should probably default to 'C' if the arrays/vectors are stacked. I
think the default is currently 'K'. Hmm, we could make 'K' refer to the
last one or two dimensions in the inputs. OTOH, that isn't needed for types
not handled by BLAS. Or it could be handled in the inner loops.
<snip>
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140917/63df49a4/attachment.html>
More information about the NumPy-Discussion
mailing list