Re: [R] fast cumulative matrix multiplication
If the matrices are not all the same size, then the order of computation will make a difference. simple example: A is 1xn, B is nx1, C is 1xn. A(BC) takes n^3 multiplies, while (AB)C requires 2n. albyn Quoting Todd Schneider todd.w.schnei...@gmail.com: Hi all, I am looking for a function like cumprod() that works for matrix multiplication. In other words, I have matrices [M1, M2, ..., Mn], and I want to calculate [M1, M1%*%M2, M1%*%M2%*%M3, ..., M1%*%...%*%Mn] as quickly as possible. Right now I'm using a for() loop but it seems like there should be a faster way. Any help is appreciated! Thanks, Todd Schneider todd.w.schnei...@gmail.com [[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. __ 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.
[R] fast cumulative matrix multiplication
Hi all, I am looking for a function like cumprod() that works for matrix multiplication. In other words, I have matrices [M1, M2, ..., Mn], and I want to calculate [M1, M1%*%M2, M1%*%M2%*%M3, ..., M1%*%...%*%Mn] as quickly as possible. Right now I'm using a for() loop but it seems like there should be a faster way. Any help is appreciated! Thanks, Todd Schneider todd.w.schnei...@gmail.com [[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.
Re: [R] fast cumulative matrix multiplication
Don't know if its any faster but try this: Reduce(%*%, list(M1, M2, M3), accumulate = TRUE) On Thu, Oct 29, 2009 at 9:56 AM, Todd Schneider todd.w.schnei...@gmail.com wrote: Hi all, I am looking for a function like cumprod() that works for matrix multiplication. In other words, I have matrices [M1, M2, ..., Mn], and I want to calculate [M1, M1%*%M2, M1%*%M2%*%M3, ..., M1%*%...%*%Mn] as quickly as possible. Right now I'm using a for() loop but it seems like there should be a faster way. Any help is appreciated! Thanks, Todd Schneider todd.w.schnei...@gmail.com [[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. __ 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.
Re: [R] fast cumulative matrix multiplication
This morning I did 10,000 replicates of multiplying 3 small matrices using a for-loop andalso with Reduce and found that the for-loop was roughly twice as fast, so I didn't post. m1 - matrix(1:9, 3) m2 - matrix(1:9, 3) m3 - matrix(1:9, 3) system.time(replicate(1000, Reduce(%*% , list(m1,m2,m3) ) ) ) user system elapsed 0.046 0.001 0.053 mlist - list(m1,m2,m3) m0 - diag(1, nrow=3,ncol=3) system.time(replicate(1000, for (i in seq_along(mlist) ) {m0 - m0 %*% mlist[[i]] } ) ) user system elapsed 0.025 0.001 0.033 -- David On Oct 29, 2009, at 6:54 PM, Gabor Grothendieck wrote: Don't know if its any faster but try this: Reduce(%*%, list(M1, M2, M3), accumulate = TRUE) On Thu, Oct 29, 2009 at 9:56 AM, Todd Schneider todd.w.schnei...@gmail.com wrote: Hi all, I am looking for a function like cumprod() that works for matrix multiplication. In other words, I have matrices [M1, M2, ..., Mn], and I want to calculate [M1, M1%*%M2, M1%*%M2%*%M3, ..., M1%*%...%*%Mn] as quickly as possible. Right now I'm using a for() loop but it seems like there should be a faster way. Any help is appreciated! Thanks, Todd Schneider todd.w.schnei...@gmail.com [[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. __ 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. David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.