Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

np.linalg.multi_dot for R

I’m trying to do a nested dot

M <- matrix(1:9,ncol=3)
x <- c(1,2,3)

m <- M
for (op in 1:1){
m <- m %*% M
}

z = x %*% m

result is effectively x.dot(M.dot(M)):

228, 516, 804

In python this loop can be reduced by:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x.dot(np.linalg.multi_dot([M]*2))

Is there something similar for R?

>Solution :

library(expm)

x %*% (M %^% 2)

     [,1] [,2] [,3]
[1,]  228  516  804

As @akrun commented, you could also use Reduce:

Reduce('%*%', rep(list(M), 2), init = x)

     [,1] [,2] [,3]
[1,]  228  516  804
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading