baptiste
baptiste

Reputation: 77116

partial product of two matrices

I'm trying to find a vectorised trick to calculate the products between row i and col i of two matrices, without wasting resources on the other products (row i and col j, i!=j).

A <- matrix(rnorm(4*5), nrow=4)
B <- matrix(rnorm(5*4), ncol=4)

diag(A %*% B)

Is there a name for this product, a base R function, or a reshaping strategy that avoids a for loop?

for (ii in seq.int(nrow(A)))
    print(crossprod(A[ii,], B[,ii]))

Upvotes: 3

Views: 174

Answers (2)

Julius Vainora
Julius Vainora

Reputation: 48241

rowSums(A * t(B)) seems to be quite fast:

A <- matrix(rnorm(400*500), nrow=400)
B <- matrix(rnorm(500*400), ncol=400)

bF <- function() diag(A %*% B)
jF <- function() rowSums(A * t(B))
vF <- function() mapply(crossprod, as.data.frame(t(A)), as.data.frame(B))
lF <- function() {
  vec <- numeric(nrow(A))
  for (ii in seq.int(nrow(A)))
    vec[ii] <- crossprod(A[ii,], B[,ii])
  vec
}

library(microbenchmark)
microbenchmark(bF(), jF(), vF(), lF(), times = 100)   
# Unit: milliseconds
#  expr        min         lq     median         uq       max neval
#  bF() 137.828993 183.320782 185.823658 200.747130 207.67997   100
#  jF()   4.434627   5.300882   5.341477   5.475393  46.96347   100
#  vF()  39.110948  51.071936  54.147338  55.127911 102.17793   100
#  lF()  14.029454  18.667055  18.931154  22.166137  65.40562   100

Upvotes: 7

Victor K.
Victor K.

Reputation: 4094

How about this?

mapply(crossprod, as.data.frame(t(A)), as.data.frame(B))

Upvotes: 1

Related Questions