[R] Use crossprod() instead of t(x) %*% x !
Martin Maechler
maechler at stat.math.ethz.ch
Thu Oct 6 17:19:40 CEST 2005
>>>>> "Marc" == Marc Bernard <bernarduse1 at 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
More information about the R-help
mailing list