14

Given a matrix, A:

A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

How can I do the matrix multiplication A times A step by step?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Mats Granvik
  • 1,159
  • 5
  • 18

5 Answers5

34

If you have Mathematica 10 you can use the new Inactive functionality

step1 = MatrixForm[Inner[Inactive[Times], A, A, Inactive[Plus]], TableSpacing -> {3, 3}]

enter image description here

step2 = Activate[step1, Times]

enter image description here

Activate[step2]

enter image description here

ybeltukov
  • 43,673
  • 5
  • 108
  • 212
  • 2
    Dang, I was trying to figure out how to use Inactivate on Plus and Times to get this to work with Inner, but it wasn't working because I was placing Inactive at the wrong locations of my expression. +1 for getting it to work! – DumpsterDoofus Oct 05 '14 at 16:19
15

You can use HoldForm or Defer with Composition if you are still using Pre V10 versions:

MatrixForm[Inner[Composition[Defer, Times], A, A, 
                              Composition[Defer, Plus]], TableSpacing -> {3, 3}]

Mathematica graphics

MatrixForm[Inner[Times, A, A, Composition[HoldForm, Plus]], TableSpacing -> {3, 3}]

Mathematica graphics

MatrixForm[Inner[Times, A, A, Plus], TableSpacing -> {3, 3}]

Mathematica graphics

Of course, there's the V10 syntax for Composition i.e. @* that can make the above code shorter:

MatrixForm[Inner[Defer@*Times, A, A, Defer@*Plus], TableSpacing -> {3, 3}]
MatrixForm[Inner[Times, A, A, Defer@*Plus], TableSpacing -> {3, 3}]
MatrixForm[Inner[Times, A, A, Plus], TableSpacing -> {3, 3}]
RunnyKine
  • 33,088
  • 3
  • 109
  • 176
2
Clear[A, n, k, nn, aa]
A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Print["A"]
MatrixForm[A]
aa = Length[A];
Print["First step in matrix multiplication A times A"]
MatrixForm[
 Table[Flatten[
   Table[Table[
     StringJoin[{"(", ToString[A[[nn, k]]], ")", "\[CenterDot]", "(", 
       ToString[A[[k, n]]], ")", 
       If[k < aa, "+", If[n == aa, "", ","]]}], {k, 1, aa}], {n, 1, 
     aa}]], {nn, 1, aa}]]
Print["Multiply:"]
MatrixForm[
 Table[Flatten[
   Table[Table[
     StringJoin[{"(", ToString[A[[nn, k]]*A[[k, n]]], ")", 
       If[k < aa, " +", If[n == aa, "", ",   "]]}], {k, 1, aa}], {n, 
     1, aa}]], {nn, 1, aa}]]
Print["and add:"]
MatrixForm[A.A]
Mats Granvik
  • 1,159
  • 5
  • 18
1

Not a symbolically pure method but it does the job if one accepts that the terms are prepended with an empty space "" and wrapped with two square brackets [ ].

A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
B = Table[Table[""[A[[n, k]]], {k, 1, Length[A]}], {n, 1, Length[A]}];
TableForm[B.B]

step by step matrix multiplication

Mats Granvik
  • 1,159
  • 5
  • 18
  • Matrix multiplication spelled out: A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; B = A; TableForm[ Table[Table[Sum[A[[n, i]]*B[[i, k]], {i, 1, Length[A]}], {k, 1, Length[A]}], {n, 1, Length[A]}]] – Mats Granvik Feb 14 '21 at 09:13
1

One can get (more drawn out) steps with the WolframAlpha command:

WolframAlpha["{{1,2,3},{4,5,6},{7,8,9}}.{{1,2,3},{4,5,6},{7,8,9}}", 
  {{"Result", 2}, "Content"}, PodStates -> {"Result__Step-by-step solution"}]

enter image description here

Greg Hurst
  • 35,921
  • 1
  • 90
  • 136