[R] Use crossprod() instead of t(x) %*% x !

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Fri 07 Oct 2005 - 01:19:40 EST

>>>>> "Marc" == Marc Bernard <bernarduse1@yahoo.fr>
>>>>> on Thu, 6 Oct 2005 15:11:09 +0200 (CEST) writes:

...........           

    Marc> Here is the program:  

    Marc> nitems <- 10
    Marc> x <- array(rnorm(5*nitems,3,3), c(5,nitems))
    Marc> sigma <- t(x)%*%x

    Marc> inverse <- try(solve(sigma), TRUE)

.............

Just a side remark on your code above:

You should learn about and use
crossprod(x) rather than t(x) %*% x
because of a

  1. more efficient implementation
  2. more accurate implementation

The exact details depends on the (accelerated/optimized or not) version BLAS/Lapack your version of R is using and also on the kind of matrices.

Further note, that for

         crossprod(t(X)) == X %*% t(X)
LAPACK also provides a direc version
to which the 'Matrix' package interfaces via function

   tcrossprod()
which in particular also works *fast* for some of the sparse matrix classes.         

Martin Maechler, ETH Zurich



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Oct 07 01:23:43 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:26:02 EST