Ranjan Maitra
2012-Dec-28 05:33 UTC
[R] efficiently multiply different matrices in 3-d array with different vectors?
Hello, I have been wondering of an efficient way to do this: I have an n x m x p array Z and a p x n matrix Y. I want to multiply each of the n matrices with the corresponding column vector of Y. In other words, I am wanting to matrix multiply: Z[i, ,] %*% Y[, i] which will give me a (two-dimensional) array or matrix of dimension n x p with the i'th row storing the above. Any pointers on an efficient way of doing this? I considered using apply, but was not successful. Many thanks again and best wishes, Ranjan -- Important Notice: This mailbox is ignored: e-mails are set to be deleted on receipt. For those needing to send personal or professional e-mail, please use appropriate addresses. ____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!
Suzen, Mehmet
2012-Dec-28 05:47 UTC
[R] efficiently multiply different matrices in 3-d array with different vectors?
I think what you are doing is a tensor algebra. You may want to try tensorA: http://cran.r-project.org/web/packages/tensorA/index.html On 28 December 2012 06:33, Ranjan Maitra <maitra.mbox.ignored at inbox.com> wrote:> Any pointers on an efficient way of doing this? I considered using > apply, but was not successful.please post that code. Why it was not successful? What was the error message?
arun
2012-Dec-28 06:03 UTC
[R] efficiently multiply different matrices in 3-d array with different vectors?
HI, Not sure if this is what you wanted: set.seed(14) Z<-array(sample(1:100,80,replace=TRUE),dim=c(5,2,8)) set.seed(21) Y<-matrix(sample(1:40,40,replace=TRUE),nrow=8) do.call(cbind,lapply(seq_len(dim(Z)[1]),function(i) Z[i,,]%*%Y[,i])) #???? [,1] [,2]? [,3]? [,4]? [,5] #[1,] 5065 6070 12328 11536 13678 #[2,] 6152 9878 11161 13777? 7991 A.K. ----- Original Message ----- From: Ranjan Maitra <maitra.mbox.ignored at inbox.com> To: r-help at r-project.org Cc: Sent: Friday, December 28, 2012 12:33 AM Subject: [R] efficiently multiply different matrices in 3-d array with different vectors? Hello, I have been wondering of an efficient way to do this: I have an n x m x p array Z and a p x n matrix Y. I want to multiply each of the n matrices with the corresponding column vector of Y. In other words, I am wanting to matrix multiply: Z[i, ,] %*% Y[, i] which will give me a (two-dimensional) array or matrix of dimension n x p with the i'th row storing the above. Any pointers on an efficient way of doing this? I considered using apply, but was not successful. Many thanks again and best wishes, Ranjan -- Important Notice: This mailbox is ignored: e-mails are set to be deleted on receipt. For those needing to send personal or professional e-mail, please use appropriate addresses. ____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more! ______________________________________________ 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.
Suzen, Mehmet
2012-Dec-29 18:19 UTC
[R] efficiently multiply different matrices in 3-d array with different vectors?
What I had in mind was a tensor multiplication. I think, using tensor arithmetic for multidimensional arrays looks more compacts and efficient (~ 2-3 times). I am guessing that performance will be much more pronounced for n-D arrays (n>3). # Component Wise set.seed(14) Z<-array(sample(1:1000000,800000,replace=TRUE),dim=c(50000,2,8)) set.seed(21) Y<-matrix(sample(1:400000,400000,replace=TRUE),nrow=8) system.time(X<-do.call(cbind,lapply(seq_len(dim(Z)[1]),function(i) Z[i,,]%*%Y[,i]))) # user system elapsed # 0.58 0.00 0.58 R<-cbind(sum(X[1,]), sum(X[2,])) # Tensor multiply library(tensorA) set.seed(14) Zt <-to.tensor(sample(1:1000000,800000,replace=TRUE), c(a=50000, b=2, c=8)) set.seed(21) Yt <-to.tensor(sample(1:400000,400000,replace=TRUE), c(c=8, a=50000)) system.time(Rt<-Zt %e% Yt) # user system elapsed # 0.124 0.000 0.126 # correct results? R # [,1] [,2] #[1,] 4.00595e+16 3.997387e+16 Rt #[1] 4.005950e+16 3.997387e+1
Suzen, Mehmet
2012-Dec-31 10:11 UTC
[R] efficiently multiply different matrices in 3-d array with different vectors?
On 30 December 2012 20:21, arun <smartpink111 at yahoo.com> wrote:> HI, > I was not aware of the algebraic relationship. > Tx for the explanation.For the record; the simple example I have shown can be reproduced with Einstein's summation rule as well. While A and x tensors have covariant (lower) indices *only*. So using %r% may make sense only if we have contravariant (upper) indices as well (mixed-variant). Just to clarify my previous post to avoid any confusion.