[R] Sparse matrix calculation
Tamas K Papp
tpapp at Princeton.EDU
Wed Nov 15 20:21:45 CET 2006
On Wed, Nov 15, 2006 at 01:22:19PM -0500, YONGWAN CHUN wrote:
> I work on large matrices and found something interesting. For multiplication of matrices, the order has a huge influence on computing time when one of them is a sparse matrix. In the below example, M is a full matrix and A is a sparse matrix in a regular matrix class. A %*% M takes much more time than M %*% A; moreover, t(t(M) %*% t(A)) is much faster than A %*% M with same result. I would like to know how it is possible. Even though I do not have an exact reason, this fact may be helpful to others.
>
> > n <- 1000
> > M <- diag(n) - matrix(1/n,n,n)
> > A <- matrix(rnorm(n*n)>2,n,n)
> > system.time(M %*% A)
> [1] 0.10 0.03 0.12 NA NA
> > system.time(A %*% M)
> [1] 3.47 0.03 3.50 NA NA
> > system.time(t(t(M) %*% t(A)))
> [1] 0.23 0.00 0.23 NA NA
Hi Yongwan,
Sorry but I could not reproduce this:
> n <- 1000
> M <- diag(n) - matrix(1/n,n,n)
> A <- matrix(rnorm(n*n)>2,n,n)
> system.time(M %*% A)
[1] 3.466 0.060 3.744 0.000 0.000
> system.time(A %*% M)
[1] 3.327 0.088 4.046 0.000 0.000
> system.time(t(t(M) %*% t(A)))
[1] 3.681 0.089 3.814 0.000 0.000
What OS, R version, linalg package are you using?
Tamas
More information about the R-help
mailing list