I want to compute (as fast as possible)
Av = v[0] * A[0, :, :] + ... + v[M-1] * A[M-1, :, :]
where v
and A
are np.ndarrays with shape (M,)
and (M, N, N)
.
Here's a minimal example what I have so far:
import numpy as np
N = 1000
M = 100
A = np.random.randint(-10, 10, size=(M, N, N))
v = np.random.randint(-10, 10, size=(M,))
%timeit np.sum(A * v[:, None, None], axis=0)
works as expected and gives
591 ms ± 7.42 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
However, I was wondering whether there's a faster way to calculate it?