[Numpy-discussion] Consider improving numpy.outer's behavior with zero-dimensional vectors
mistersheik at gmail.com
Sat Apr 11 12:06:16 EDT 2015
> On Wed, Apr 8, 2015 at 7:34 PM, Neil Girdhar <mistersheik at gmail.com
> <http://mail.scipy.org/mailman/listinfo/numpy-discussion>> 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
> <https://reference.wolfram.com/language/ref/Outer.html> with respect to
> matrix *>
> * outer products. * You're probably right that this is the correct
> definition of the outer
> product in an n-dimensional world. But this seems to go beyond being
> just a bug in handling 0-d arrays (which is the kind of corner case
> we've fixed in the past); np.outer is documented to always ravel its
> inputs to 1d.
> In fact the implementation is literally just:
> a = asarray(a)
> b = asarray(b)
> return multiply(a.ravel()[:, newaxis], b.ravel()[newaxis,:], out)
> Sebastian's np.multiply.outer is much more generic and effective.
> Maybe we should just deprecate np.outer? I don't see what use it
> serves. (When and whether it actually got removed after being
> deprecated would depend on how much use it actually gets in real code,
> which I certainly don't know while typing a quick email. But we could
> start telling people not to use it any time.)
+1 with everything you said.
(And thanks Sebastian for the pointer to np.multiply.outer!)
On Wed, Apr 8, 2015 at 7:34 PM, Neil Girdhar <mistersheik at gmail.com> 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.
> In : def a(n): return np.ones(n)
> In : b = a(())
> In : c = a(4)
> In : d = a(5)
> In : np.outer(b, d).shape
> Out: (1, 5)
> In : np.outer(c, d).shape
> Out: (4, 5)
> In : np.outer(c, b).shape
> Out: (4, 1)
> In : def outer(a, b):
> return a[(...,) + len(b.shape) * (np.newaxis,)] * b
> In : outer(b, d).shape
> Out: (5,)
> In : outer(c, d).shape
> Out: (4, 5)
> In : outer(c, b).shape
> Out: (4,)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion