[Numpy-discussion] Two questions about PEP 465 dot product

Nathaniel Smith njs at pobox.com
Thu May 21 21:37:04 EDT 2015

On Thu, May 21, 2015 at 6:06 PM, Alexander Belopolsky <ndarray at mac.com> wrote:
> 1. Is there a simple expression using existing numpy functions that
> implements PEP 465 semantics for @?

Not yet.

> 2. Suppose I have a function that takes two vectors x and y, and a matrix M
> and returns x.dot(M.dot(y)).  I would like to "vectorize" this function so
> that it works with x and y of any ndim >= 1 and M of any ndim >= 2 treating
> multi-dimensional x and y as arrays of vectors and M as an array of matrices
> (broadcasting as necessary).  The result should be an array of xMy products.
> How would I achieve that using  PEP 465's @?

(x[..., np.newaxis, :] @ M @ y[..., :, np.newaxis])[..., 0, 0]

Alternatively, you might prefer something like this (though it won't
yet take advantage of BLAS):

np.einsum("...i,...ij,...j", x, M, y)

Alternatively, there's been some discussion of the possibility of
adding specialized gufuncs for broadcasted vector-vector,
vector-matrix, matrix-vector multiplication, which wouldn't do the
magic vector promotion that dot and @ do.


Nathaniel J. Smith -- http://vorpus.org

More information about the NumPy-Discussion mailing list