I am very new to Mathematica. I am dealing with the multiplication of matrix as the following code


Do[s=s+a[[i1,i2,i3]]*b[[i1,i2,i3,;;,;;,;;]],{i1,1,c},{i2,1,c},{i3,1,c}] //AbsoluteTiming

{4.26894, Null}

For me, the computation time is too high because there are many such multiplication in the program.

Any suggestions really appreciated.

  • 2
    $\begingroup$ Better try s = Total[a b, 3] or s = Flatten[a].ArrayReshape[b, {c^3, d, d, d}];. $\endgroup$ – Henrik Schumacher Jun 2 at 5:37

The code can be faster if we compile:

cf = Compile[{{a, _Real, 3}, {b, _Real, 6}}, Flatten[a].Flatten[b, 2]];

test = cf[a, b]; // AbsoluteTiming

(* {0.492055, Null} *)

and even faster if compile to C and extract the LibraryFunction[…]:

cfc = 
  Compile[{{a, _Real, 3}, {b, _Real, 6}}, Flatten[a].Flatten[b, 2], 
    CompilationTarget -> C][[-1]];

testc = cfc[a, b]; // AbsoluteTiming
(* {0.234145, Null} *)

Tested on v9.0.1, with TDM-GCC-5.1.0-2 64-bit compiler, "SystemCompileOptions"->"-Ofast".

| improve this answer | |
  • 1
    $\begingroup$ Huh? Why does extracting the LibraryFunction speed this up? $\endgroup$ – Henrik Schumacher Jun 2 at 6:42
  • 1
    $\begingroup$ @Henrik Copying large list is slow. I learned this here. $\endgroup$ – xzczd Jun 2 at 6:48
  • $\begingroup$ @xzczd Thanks, very useful answer. $\endgroup$ – Shi Jun 11 at 9:07

Better try

s = Total[a b, 3];


s = Flatten[a].ArrayReshape[b, {c^3, d, d, d}];

On my machine, the latter is faster. In general, rephrasing summations in terms of Dot (.) should lead to more efficient code as Dot is highly optimized.

| improve this answer | |
  • $\begingroup$ In v9.0.1 Flatten[a].Flatten[b, 2] seems to be faster and more memory-efficient. $\endgroup$ – xzczd Jun 2 at 5:46
  • 2
    $\begingroup$ Good point. Both lead to the same AbsoluteTiming and MaxMemoryUsed on my machine under version 12.0 for macos. I recall that ArrayReshape used to have some performance degradation when it was introduced. They seem to have been resolved. $\endgroup$ – Henrik Schumacher Jun 2 at 6:00
  • $\begingroup$ @HenrikSchumacher Thanks a lot. $\endgroup$ – Shi Jun 11 at 9:10
  • $\begingroup$ You're welcome. $\endgroup$ – Henrik Schumacher Jun 11 at 9:10

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.