How to show matrix multiplication step by step?
If you have Mathematica 10 you can use the new Inactive
functionality
step1 = MatrixForm[Inner[Inactive[Times], A, A, Inactive[Plus]], TableSpacing -> {3, 3}]
step2 = Activate[step1, Times]
Activate[step2]
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}]
MatrixForm[Inner[Times, A, A, Composition[HoldForm, Plus]], TableSpacing -> {3, 3}]
MatrixForm[Inner[Times, A, A, Plus], TableSpacing -> {3, 3}]
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}]
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]