c++blascblas

BLAS Functions Dgemm but all matrices are multiplied by a Scalar


Is there any Dgemm like matrix call that scales the a and b matrix? (A, B, C are scalars).

IE c = C * c + (A*op(a)) * (B*op(b));

Bonus points is therer any reason this isn't supported or is it just uncommon in Linear-Algebra and therefor generally unwarranted?


Solution

  • The standard BLAS interface does not provide a direct mean to scale both A and B matrices independently (in this jargon, they usually devote uppercase letters for matrices/vectors and lowercase for scalars). See the MKL cblas documentation for cblas_?gemm (it will be the same across BLAS implementations).

    What you can do though is to generate the scaled version of B with an intermediate call, setting a and b to zero:

    C = c.*C + (0.*A) * (0.*B)
    

    However, if you decompose the matrix multiplication into single operations (example in row-major order):

    for( i = 0; i < N; i++ ) {
      for( j = 0; j < M; j++ ) {
        for( ii = 0; ii < K; ii++ ) {
          C[i][j] = C[i][j] + a*A[i][ii]*B[ii][j];
        }
      }
    }
    

    You will see that the scaling affects both matrices' elements. Since scalar product is commutative and associative, using an a argument equal to the product of both your scalars has the same effect.