Speeding up calculation of symmetric matrices; use of outer

ε祈祈猫儿з 提交于 2020-03-05 06:55:12

问题


I need to speed up a calculation that produces a symmetric matrix. Currently I have something like this:

X <- 1:50
Y<- 1:50
M <- outer(X, Y, FUN = myfun)

where myfun is a quite complicated, vectorized, but symmetrical function (myfun(x, y) = myfun(y, x)).

So my code unnecessarily wastes time calculating the lower triangular matrix as well as the upper triangular matrix.

How can I avoid that duplication without using slow for-loops?


回答1:


If your function is slow and timing scales with size of its input, you could use combn:

X <- 1:50
Y <- 1:50

#a slow function
myfun <- function(x, y) {
  res <- x * NA
  for (i in seq_along(x)) {
    Sys.sleep(0.01)
    res[i] <- x[i] * y[i]
    }
  res
}

system.time(M <- outer(X, Y, FUN = myfun))
#user  system elapsed 
#0.00    0.00   26.41 

system.time({
  inds <- combn(seq_len(length(X)), 2)
  M1 <- matrix(ncol = length(X), nrow = length(Y))

  M1[lower.tri(M1)] <-  myfun(X[inds[1,]], Y[inds[2,]])
  M1[upper.tri(M1)] <- t(M1)[upper.tri(M1)]
  diag(M1) <- myfun(X, Y)
})
#user  system elapsed 
#0.00    0.00   13.41

all.equal(M, M1)
#[1] TRUE

However, the best solution is probably to implement this in C++ via Rcpp.



来源:https://stackoverflow.com/questions/52384294/speeding-up-calculation-of-symmetric-matrices-use-of-outer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!