Multiply array of vectors with array of matrices; return array of vectors?

Viewed 1179

I've got a numpy array of row vectors of shape (n,3) and another numpy array of matrices of shape (n,3,3). I would like to multiply each of the n vectors with the corresponding matrix and return an array of shape (n,3) of the resulting vectors.

By now I've been using a for loop to iterate through the n vectors/matrices and do the multiplication item by item.

I would like to know if there's a more numpy-ish way of doing this. A way without the for loop that might even be faster.

//edit 1:

As requested, here's my loopy code (with n = 10):

    arr_in = np.random.randn(10, 3)
    matrices = np.random.randn(10, 3, 3)

    for i in range(arr_in.shape[0]): # 10 iterations
        arr_out[i] = np.asarray(np.dot(arr_in[i], matrices[i]))
2 Answers
Related