Hello, 

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. 

Thanks, 

Yongwan


> 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

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to