I have a very large multiply and sum operation that I need to implement as efficiently as possible. The best method I've found so far is bsxfun
in MATLAB, where I formulate the problem as:
L = 10000;
x = rand(4,1,L+1);
A_k = rand(4,4,L);
tic
for k = 2:L
i = 2:k;
x(:,1,k+1) = x(:,1,k+1)+sum(sum(bsxfun(@times,A_k(:,:,2:k),x(:,1,k+1-i)),2),3);
end
toc
Note that L
will be larger in practice. Is there a faster method? It's strange that I need to first add the singleton dimension to x
and then sum
over it, but I can't get it to work otherwise.
It's still much faster than any other method I've tried, but not enough for our application. I've heard rumors that the Python function numpy.einsum
may be more efficient, but I wanted to ask here first before I consider porting my code.
I'm using MATLAB R2017b.