rmatrix-multiplicationvector-multiplication

fast multiplication matrix by matrix by matrix


I need to multiply three matrices X(Nxk), FF(kxk) and X(Nxk) (again). Which is t(xi) * FF * xi, where xi is the ith row of X, i=1:N. The result will be a single column matrix with N rows. The multiplication also can be regarded also as X * FF * t(X).

(Nxk) stands for "N rows, k columns", * is algebraic multiplication, t() transposing.

The problem is that N is quite large (more than 100k). I found some advises for fast multiplication by using drop and sweep. But they consider only the half of the problem - multiplication of vector by matrix.

I want to avoid the multiplication on two stages A=XFF and then At(X) because of the size of X. So what I need is some function or hint, which multiply the three matrices at once (well, as much as possible) in order to make the calculation as fast as possible in R.


Solution

  • If you just need XFX', drop and sweep are red herrings. Those posts describe different problems.

    You can see if Matrix gives you enough speed, before going to anything more involved.

    library(Matrix)
    library(microbenchmark)
    
    # sparse matrix from Matrix
    data(CAex)
    
    # create a possible FF
    set.seed(1)
    FF = matrix(rnorm(length(CAex)), nrow = nrow(CAex), ncol = nrow(CAex))
    
    # not a sparse matrix
    CA = as.matrix(CAex)
    
    microbenchmark(
      matrix = CA %*% crossprod(FF, CA),
      Matrix = CAex %*% crossprod(FF, CAex))
    
    # Unit: microseconds
    #    expr     min      lq     mean   median      uq      max neval cld
    #  matrix 561.170 563.952 654.8408 588.1250 651.673 1403.389   100   b
    #  Matrix  94.356 102.866 173.1130 119.9435 165.542 1815.316   100  a