> RSiteSearch("cross product") > library(pracma) > ?cross > > Speed is usually desired in the context of many similar computations, and is > normally achieved in R by vectorizing computation, so storing the large > number of 3d vectors together in a structure like a Nx3 matrix so the code > can be vectorized is the logical approach. The cross() function takes > inputs in this form, but the current implementation (0.6-3) then fails to > take advantage of that storage since it iterates with a for loop. A better > core implementation of cross() might be: > > vcrossp <- function( a, b ) { > result <- matrix( NA, nrow( a ), 3 ) > result[,1] <- a[,2] * b[,3] - a[,3] * b[,2] > result[,2] <- a[,3] * b[,1] - a[,1] * b[,3] > result[,3] <- a[,1] * b[,2] - a[,2] * b[,1] > result > } > > which is about 20 times faster than cross() on my machine.
Thanks, Jeff, for the hint. I think most of the functions in package 'pracma' are vectorized (if appropriate), but this one was a real oversight. I has been corrected in version 0.7-1 (on R-Forge). -- Hans Werner [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org 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.