问题
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