[R] crossprod and X %*% t(X)

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Tue Jul 16 09:42:55 CEST 2002


On Sun, 14 Jul 2002, Jon McAuliffe wrote:

> the help page for crossprod states that crossprod(A,B) is faster than
> t(A) %*% B; experimentation certainly bears this out. more alarming
> is the evidence that crossprod(t(A), B) is faster than A %*% B:
>
> on a PII laptop, 128MB memory, win98, R-1.5.0.-patched precompiled
> (no ATLAS):
>
> > A <- matrix(rnorm(250000),500,500)
> > B <- matrix(rnorm(250000),500,500)
> > for (i in 1:5) { print(system.time(A %*% B)) }
> [1]   NA   NA 7.96   NA   NA
> [1]   NA   NA 7.81   NA   NA
> [1]   NA   NA 7.79   NA   NA
> [1]   NA   NA 7.81   NA   NA
> [1]   NA   NA 7.79   NA   NA
> > for (i in 1:5) { print(system.time(crossprod(t(A),B))) }
> [1]   NA   NA 4.48   NA   NA
> [1]   NA   NA 4.51   NA   NA
> [1]   NA   NA 4.48   NA   NA
> [1]   NA   NA 4.69   NA   NA
> [1]   NA   NA 4.49   NA   NA
>
> is this expected, or is something strange about my system? would
> ATLAS make this difference negligible? (i'd like to know before i spend
> the time building and installing it.)

If you upgrade to 1.5.1 you don't need to: ATLAS-based Rblas.dll are on
CRAN in the windows/contrib/ATLAS area, including for a PII.  However, I
cannot reproduce your results:

without ATLAS:

> for (i in 1:5) { print(system.time(A %*% B)) }
[1] 1.01 0.00 1.01   NA   NA
[1] 0.90 0.02 0.92   NA   NA
[1] 0.92 0.01 0.93   NA   NA
[1] 0.91 0.00 0.92   NA   NA
[1] 0.94 0.00 1.00   NA   NA
> for (i in 1:5) { print(system.time(crossprod(t(A),B))) }
[1] 0.94 0.00 0.94   NA   NA
[1] 0.91 0.00 0.91   NA   NA
[1] 0.94 0.01 0.97   NA   NA
[1] 0.93 0.01 0.94   NA   NA
[1] 0.94 0.00 0.93   NA   NA

and with ATLAS

> for (i in 1:5) { print(system.time(A %*% B)) }
[1] 0.25 0.01 0.39   NA   NA
[1] 0.15 0.00 0.16   NA   NA
[1] 0.15 0.01 0.16   NA   NA
[1] 0.16 0.00 0.16   NA   NA
[1] 0.16 0.00 0.16   NA   NA
> for (i in 1:5) { print(system.time(crossprod(t(A),B))) }
[1] 0.18 0.00 0.20   NA   NA
[1] 0.18 0.00 0.18   NA   NA
[1] 0.18 0.00 0.18   NA   NA
[1] 0.24 0.01 0.25   NA   NA
[1] 0.16 0.01 0.18   NA   NA

(1.4GHz P4)

> if it is expected, under what circumstances would one prefer %*%?

For clarity of reading?  Almost all uses of any of these are for small
matrices.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list