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

Neil Girdhar mistersheik at gmail.com
Wed Apr 8 19:34:52 EDT 2015


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.

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150408/231693de/attachment.html>


More information about the NumPy-Discussion mailing list