1
$\begingroup$

I would like to create a function to matrix multiply (dot product) n matrices together, where n is large, for example

M1*M2*M3*M4*M5*...*Mn

I know there is the 'product' function that can do this for standard multiplication but I cannot seem to find a version for dot products. I have tried using a loop but this takes forever to run when n becomes large. Is there a way around this? Thanks in advance.

$\endgroup$
  • 3
    $\begingroup$ Dot[M1, M2, M3, M4, M5, ..., Mn]? However, when the matrices vary much in size the order of Dotting should be optimized. $\endgroup$ – Henrik Schumacher Apr 25 at 19:04
  • 4
    $\begingroup$ Also Dot @@ MList comes to mind where MList is a list of matrices. $\endgroup$ – Henrik Schumacher Apr 25 at 19:12
  • $\begingroup$ That would work but I was hoping to automate this process. As you can imagine when n becomes quite, large manually entering all of the matrices would become tiresome so I am looking for a way to matrix multiply these matrices Mj with the index j running from 1 to n where i can input n to be any integer I need. $\endgroup$ – KJohn Apr 26 at 12:36
  • 3
    $\begingroup$ Fold[Dot[#1, M[#2]] &, M[1], Range[2, n]]? $\endgroup$ – Henrik Schumacher Apr 26 at 14:24
1
$\begingroup$

I found a relatively easy method that works:

matrixlist=Table[M[j],{j,1,n}]
Apply[Dot,matrixlist]
$\endgroup$
  • $\begingroup$ Dot @@ Array[M, n] $\endgroup$ – b3m2a1 May 27 at 3:19
  • $\begingroup$ Just what Henrik Schumacher recommended here $\endgroup$ – Michael E2 Sep 23 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.