6

A is an MxK matrix, B is a vector of size K, and C is a KxN matrix. What set of BLAS operators should I use to compute the matrix below?

M = A*diag(B)*C

One way to implement this would be using three for loops like below

for (int i=0; i<M; ++i)
    for (int j=0; j<N; ++j)
        for (int k=0; k<K; ++k)
            M(i,j) = A(i,k)*B(k)*C(k,j);

Is it actually worth implementing this in BLAS in order to gain better speed efficiency?

D R
  • 21,936
  • 38
  • 112
  • 149

1 Answers1

4

First compute D = diag(B)*C, then use the appropriate BLAS matrix-multiply to compute A*D.

You can implement diag(B)*C using a loop over elements of B and calling to the appropriate BLAS scalar-multiplication routine.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
  • 12
    There's no optimised routine for matrix-matrix and matrix-vector multiplication when one of the matrices is diagonal? That's incredible. – enigmaticPhysicist Mar 12 '14 at 10:21
  • See also this answer which points to the *?sbmv* functions, for working with band matrices such as diagonal matrices. From the sound of it though it's unlikely to give good performance. https://stackoverflow.com/a/13433038/ – Max Barraclough Mar 30 '21 at 12:06