# [Numpy-discussion] result shape from dot for 0d, 1d, 2d scalar

Skipper Seabold jsseabold at gmail.com
Wed Nov 28 11:11:02 EST 2012

```On Tue, Nov 27, 2012 at 11:16 AM, Sebastian Berg <sebastian at sipsolutions.net
> wrote:

> On Mon, 2012-11-26 at 13:54 -0500, Skipper Seabold wrote:
> > I discovered this because scipy.optimize.fmin_powell appears to
> > squeeze 1d argmin to 0d unlike the other optimizers, but that's a
> > different story.
> >
> >
> > I would expect the 0d array to behave like the 1d array not the 2d as
> > it does below. Thoughts? Maybe too big of a pain to change this
> > behavior if indeed it's not desired, but I found it to be unexpected.
>
> I don't quite understand why it is unexpected. A 1-d array is considered
> a vector, a 0-d array is a scalar.
>
>
When you put it like this I guess it makes sense. I don't encounter 0d
arrays often and never think of a 0d array as truly a scalar like
np.array(1.).item(). See below for my intuition.

> > : np.version.full_version # same on 1.5.1
> > : '1.8.0.dev-8e0a542'
> >
> >
> > : arr = np.random.random((25,1))
> >
> >
> > [~/]
> > : np.dot(arr, np.array([1.])).shape
> > : (25,)
> >
> Matrix times vector = vector
> >
> > [~/]
> > : np.dot(arr, np.array([[1.]])).shape
> > : (25, 1)
> >
> Matrix times matrix = matrix
> >
> > [~/]
> > : np.dot(arr, np.array(1.)).shape
> > : (25, 1)
> >
> matrix times scalar = matrix (of same shape)
> >
> > [~/]
> > : np.dot(arr.squeeze(), np.array(1.)).shape
> > : (25,)
> >
> vector times scalar = vector (of same shape)
> >
> > Huh? 0d arrays broadcast with dot?
> >
> Remember a 0-d array is a scalar, there is no actual broadcasting
> involved here. (except that vectors (1-d arrays) are special)
>
>
Maybe I'm misunderstanding. How do you mean there is no broadcasting?
They're clearly not conformable. Is vector.scalar specially defined (I have
no idea)? I recall arguing once and submitting a patch such that
np.linalg.det(5) and np.linalg.inv(5) should be well-defined and work but
the counter-argument was that a scalar is not the same as a scalar matrix.
This seems to be an exception.

Here, I guess, following that counterargument, I'd expected the scalar to
fail in dot. I certainly don't expect a (N,2).scalar -> (N,2). Or I'd
expect it to follow the rules of matrix notation and be treated like the 1d
scalar vector so that (N,1).scalar -> (N,). To my mind, this follows more
closely to the expectation that (J,K).(M,N) -> (J,N), i.e., the second
dimension of the result is the same as the second dimension of whatever is
post-multiplying where the first dimension is inferred if necessary (or
should fail if non-existent). So my expectations are (were)

(N,).() -> (N,)
(N,1).() -> (N,)
(N,1).(1,) -> (N,)
(N,1).(1,1) -> (N,1)
(N,2).() -> Error

Skipper

> [~]
> > : arr = np.random.random((25,2))
> >
> >
> > [~/]
> > : np.dot(arr.squeeze(), np.array(2.)).shape
> > : (25, 2)
> >
> >
> > Skipper
> >
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121128/459f01f1/attachment.html>
```