[R] crossprod(x) vs crossprod(x,x)

Robin Hankin r.hankin at noc.soton.ac.uk
Tue Nov 21 11:23:15 CET 2006


I found out the other day that crossprod() will take a single matrix  
argument;
crossprod(x)  notionally returns crossprod(x,x).


The two forms do not  return identical matrices:

x <- matrix(rnorm(3000000),ncol=3)
M1 <- crossprod(x)
M2 <- crossprod(x,x)


R> max(abs(M1-M2))
[1] 1.932494e-08

But what really surprised me is that crossprod(x) is slower than  
crossprod(x,x):


R> system.time(crossprod(x))
[1] 0.079 0.206 0.292 0.000 0.000
R> system.time(crossprod(x,x))
[1] 0.035 0.001 0.041 0.000 0.000
R>



Is there any reason for anyone to use the single-argument form?






--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743



More information about the R-help mailing list