Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top