Is there a R package or function that will multiple an arbitrary number of matrices supplied to it, preferably optimizing the sequence R_function(matA, matB, matC, matD,....) return matA %*% matB %*% matC %matD %.... and optimizing wether it is better to do A(BC) or (AB)C ? Thanks, Shiv [[alternative HTML version deleted]]
On 16.06.2013 04:35, G Vishwanath wrote:> Is there a R package or function that will multiple an arbitrary number of matrices supplied to it, preferably optimizing the sequence > > R_function(matA, matB, matC, matD,....) > return matA %*% matB %*% matC %matD %.... > > and optimizing wether it is better to do A(BC) or (AB)C ?For part 1) or your question: Reduce("%*%", list(matA, matB, matC, matD, ...) for part 2): No idea. Best, Uwe Ligges> > Thanks, > Shiv > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at 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. >
Can I have some help in vectorizing a series of matrix multiplications? Toy Example mat_size=2; num_matrices=3; num_users=2 ToyArray=array(1,dim=c(mat_size, mat_size, num_matrices, num_users)) /* So I open an 4-dim array to store 3 2 X 2 matrrices for 2 users. For each user I want to multiple the 3, 2 X 2 matrices so that at the end I have 1 matrix for each of the 2 users */ This works: output=array(NA,dim=c(mat_size, mat_size, num_users)); for ( i in 1:num_users) { output[,,i]= as.matrix(ToyArray[,,1,i]) %*% as.matrix(ba[,,2,i]) %*% as.matrix(ba[,,3,i]) }; Can I do better? Can I vectorize this over the user dimension? And even better (and a different question): what strategy can I use if the matrices are not all of the same size? GR [[alternative HTML version deleted]]