Multiply many-matrices by many-vectors

If all your matrices are the same size, and you do store them in a rank 3 tensor, you can do

N1 = 7; # Always
N2 = 8; # Always
nMatrix = 1000; # Usually varies between 1e4 - 1e7, sometimes as high as 1e8
nVector  = 1000; # Usually varies between 1e4 and 2e5
M = rand(N1,N2,nMatrix);
V = rand(N2,nVector);

using TensorOperations
@tensor res[a,b,c] := M[a,x,b]*V[x,c]

e.g.

using BenchmarkTools
@btime (@tensor res[a,b,c] := $M[a,x,b]*$V[x,c]);
  #-> 14.611 ms (14 allocations: 53.83 MiB)

with the current master version of TensorOperations.jl (which will soon be v1.0)

3 Likes