Multiplication of many matrices in R

r multiply matrix by vector
matrix multiplication in r
r matrix inverse
matrix multiplication in r using for loop
multiply all elements in the matrix by 2 in r
r program to multiply two matrices
vector multiplication in r
matrix transpose in r

I want to multiply several matrices of the same size with an inital vector. In the example below p.state is vector of m elements and tran.mat is list where each member is an m x m matrix.

for (i in 1:length(tran.mat)){
  p.state <- p.state %*% tran.mat[[i]]
} 

The code above gives the correct answer but can be slow when length(tran.mat) is large. I was wondering if there was a more efficient way of doing this?

Below is an example with a m=3 and length(mat)=10 that can generate this:

p.state <- c(1,0,0)
tran.mat<-lapply(1:10,function(y){apply(matrix(runif(9),3,3),1,function(x){x/sum(x)})})

for (i in 1:length(tran.mat)){
  p.state <- p.state %*% tran.mat[[i]]
}

print(p.state) 

NB: tran.mat does not have to be a list it is just currently written as one.

Edit after a few comments:

Reduce is useful when m is small. However when m=6 the loop out performed both the above solutions. library(rbenchmark)

p.state1 <- p.state <- c(1,0,0,0,0,0)
tran.mat<-lapply(1:10000,function(y){t(apply(matrix(runif(36),6,6),1,function(x){x/sum(x)}))})

tst<-do.call(c, list(list(p.state), tran.mat))

benchmark(
  'loop' = {
    for (i in 1:length(tran.mat)){
      p.state <- p.state %*% tran.mat[[i]]
    }
  },
  'reduce' = {
    p.state1 %*% Reduce('%*%', tran.mat)
   },
  'reorder' = {
    Reduce(`%*%`,tran.mat,p.state1)
  }

)

This results in

        test replications elapsed relative user.self sys.self user.child     sys.child
   1    loop          100    0.87    1.000      0.87        0         NA        NA
   2  reduce          100    1.41    1.621      1.39        0         NA        NA
   3 reorder          100    1.00    1.149      1.00        0         NA        NA

A faster way is to use Reduce() to do sequential matrix multiplication on the list of matrices.

You can get approximately a 4x speedup that way. Below is an example of your code tested, with 1000 elements in the list instead of 10 to see the performance improvement more easily.

Code
library(rbenchmark)

p.state <- c(1,0,0)
tran.mat<-lapply(1:1000,function(y){apply(matrix(runif(9),3,3),1,function(x){x/sum(x)})})

benchmark(
  'loop' = {
    for (i in 1:length(tran.mat)){
      p.state <- p.state %*% tran.mat[[i]]
    }
  },
  'reduce' = {
    p.state %*% Reduce('%*%', tran.mat)
  }
)
Output
    test replications elapsed relative user.self sys.self user.child sys.child
1   loop          100    0.23    3.833      0.23        0         NA        NA
2 reduce          100    0.06    1.000      0.07        0         NA        NA

You can see the reduce method is about 3.8 times faster.

Multiplication of many matrices in R, Matrix Multiplication. Description. Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix  Matrix Multiplication Description. Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix). Usage x %*% y Arguments


I am not sure that this will be any faster but it is shorter:

prod <- Reduce("%*%", L)

all.equal(prod, L[[1]] %*% L[[2]] %*% L[[3]] %*% L[[4]])
## [1] TRUE
Note

We used this test input:

m <- matrix(1:9, 3)
L <- list(m^0, m, m^2, m^3)

Matrix Multiplication, Matrix Multiplication. Description. Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix  So, to multiply two matrices in R special type of operator i.e., matrix multiplication operator (%*%) is used in R.


I am going to use a function from package Rfast to reduce the execution time of multiplication. Unfortunately, for loop's time can not be reduced.

The function called Rfast::eachcol.apply is a great solution for your purpose. Your multiplication is also the function crossprod but it is slow for our purpose.

Here are some helper functions:

mult.list<-function(x,y){
    for (xm in x){
        y <- y %*% xm
    }
    y
}

mult.list2<-function(x,y){
    for (xm in x){
        y <- Rfast::eachcol.apply(xm,y,oper="*",apply="sum")
    }
    y
}

Here is an example:

x<-list()
y<-rnomr(1000)
for(i in 1:100){
    x[[i]]<-Rfast::matrnorm(1000,1000)
}


microbenchmark::microbenchmark(R=a<-mult.list(x,y),Rfast=b<-mult.list2(x,y),times = 10)
 Unit: milliseconds
     expr        min         lq        mean     median         uq        max neval
        R 410.067525 532.176979 633.3700627 649.155826 699.721086 916.542414    10
    Rfast 239.987159 251.266488 352.1951486 276.382339 458.089342 741.340268    10

all.equal(as.numeric(a),as.numeric(b))
[1] TRUE

The argument oper is for the operation on each element and the apply for the operation on each column. In large matrices should be fast. I couldn't test it in my laptop for bigger matrices.

Matrix Algebra, It is built deeply into the R language. Before proceeding you many want to review the sections on Data Types and Operators. A %*% B, Matrix multiplication. If you want matrix multiplication, as the title suggests, i.e., want to multiply rows of the fist matrix by columns of the second, this is mat1 %*% mat2. But this requires the first matrix to have as many columns as the second has rows -- that is not the case in your example. – Vincent Zoonekynd Mar 15 '12 at 23:39


Matrix In R, R is an important programming language used heavily by statisticians. It is also used in machine learning, data science, research, and many  Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. They contain elements of the same atomic types. Though we can create a matrix containing only characters or only logical values, they are not of much use. We use matrices containing numeric elements to be used in mathematical calculations.


Matrix Multiplication in R, Matrix multiplication is the most useful matrix operation. in areas such as network theory, transformation of coordinates and many more uses nowadays. A matrix in R can be created using matrix() function and this function takes input vector,  R gives us an error because you can’t multiply a 3×2 and 1×3 matrix. For the matrix multiplication to work, the number of columns in the first matrix (c = 3 columns) has to be equal to the number of rows in the second matrix (x= 1 row). The previous operations were done using the default R arrays, which are matrices.


R tutorial - Matrix Arithmetic in R, This is a basic post about multiplication operations in R. We're considering element-wise multiplication versus matrix multiplication. First let's  A Matrix. (This one has 2 Rows and 3 Columns) To multiply a matrix by a single number is easy: These are the calculations: 2×4=8. 2×0=0. 2×1=2. 2×-9=-18. We call the number ("2" in this case) a scalar, so this is called "scalar multiplication".