# [Numpy-discussion] Consider improving numpy.outer's behavior with zero-dimensional vectors

Sebastian Berg sebastian at sipsolutions.net
Thu Apr 9 01:40:51 EDT 2015

```On Mi, 2015-04-08 at 19:34 -0400, Neil Girdhar wrote:
> Numpy's outer product works fine with vectors.  However, I seem to
> always want len(outer(a, b).shape) to be equal to len(a.shape) +
> len(b.shape).  Wolfram-alpha seems to agree
> https://reference.wolfram.com/language/ref/Outer.html with respect to
> matrix outer products.  My suggestion is to define outer as defined
> below.  I've contrasted it with numpy's current outer product.
>

Actually this behaviour already exists (I was not aware it was
different) for all ufuncs, though it is not that well known, and I tend
to broadcast manually which may be a pity ;):

np.multiply.outer(a, b)

does exactly what you want, though I am not sure if np.outer might
leverage blas or not.

- Sebastian

>
> In [36]: def a(n): return np.ones(n)
>
>
> In [37]: b = a(())
>
>
> In [38]: c = a(4)
>
>
> In [39]: d = a(5)
>
>
> In [40]: np.outer(b, d).shape
> Out[40]: (1, 5)
>
>
> In [41]: np.outer(c, d).shape
> Out[41]: (4, 5)
>
>
> In [42]: np.outer(c, b).shape
> Out[42]: (4, 1)
>
>
> In [43]: def outer(a, b):
>     return a[(...,) + len(b.shape) * (np.newaxis,)] * b
>    ....:
>
>
> In [44]: outer(b, d).shape
> Out[44]: (5,)
>
>
> In [45]: outer(c, d).shape
> Out[45]: (4, 5)
>
>
> In [46]: outer(c, b).shape
> Out[46]: (4,)
>
>
> Best,
>
>
> Neil
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150409/038673ea/attachment.sig>
```