Effective implementation of a sequence of matrix-vector products / specific "tensor" matrix products

I have a special algorithm where, as one of the last steps, I need to multiply a three-dimensional array with a two-dimensional array, so that each matrix slice of the three-dimensional array is multiplied by each column of the 2-dimensional array. In other words, if, say, Ais a matrix N x N x N, but Bis a matrix N x N, I need to calculate the Csize matrix N x N, where C(:,i) = A(:,:,i)*B(:,i);.

A naive way to implement this is through a loop, i.e.

C = zeros(N,N);
for i = 1:N
    C(:,i) = A(:,:,i)*B(:,i);
end

However, loops are not the fastest in Matlab and should be avoided. I am looking for faster ways to do this. Right now I am using the fact that (now Mathjax will be great!):

[A1 b1, A2 b2, ..., AN bN] = [A1, A2, ..., AN] * blkdiag (b1, b2, ..., bN)

This allows us to get rid of the cycle, however, we need to create a block-diagonal matrix of size N^2 x N. I do it through sparseefficient, i.e. In the following way:

A_long = reshape(A,N,N^2);
b_cell = mat2cell(B,N,ones(1,N)); % convert matrix to cell array of vectors
b_cell{1} = sparse(b_cell{1});    % make first element sparse, this is enough to trigger blkdiag into sparse mode
B_blk = blkdiag(b_cell{:});
C = A_long*B_blk;

According to my indicators, this approach is faster than the cycle, about two times (at large N), despite the necessary preparations (only multiplication is 3-4 times faster than the cycle).

Here is a quick test I did, resizing the problem Nand measuring the cycle time and an alternative approach (with and without preparation steps). For large, the Nacceleration is about 2 ... 2.5.

Benchmarking result

, . ? / , , , , .

P.S.: blkdiag(A1,...,AN)*B , N^2 x N^2, , , , .

edit: ! Matlab R2016b. , , , , . :

New test on R2016b

:

New test on R2016b, scaling

:

  • SumRepDot - , Divakar, squeeze(sum(bsxfun(@times,A,permute(B,[3,1,2])),2)), R2016b squeeze(sum(A.*permute(B,[3,1,2]),2)). , N 1,2-1,4 .
  • "" , .
  • , -, N, 3... 4 , . .
+4

Source: https://habr.com/ru/post/1668014/


All Articles