Matrix multiplication: Difference between revisions

Content added Content deleted
(→‎{{header|ZPL}}: Added zkl)
Line 2,054: Line 2,054:


=={{header|Mathematica}}==
=={{header|Mathematica}}==
<lang mathematica>M1 = {{1, 2},
{3, 4},
{5, 6},
{7, 8}}
M2 = {{1, 2, 3},
{4, 5, 6}}
M = M1.M2</lang>


The Wolfram Language supports both dot products and element-wise multiplication of matrices.
Or without the variables:


This computes a dot product:
<lang mathematica>{{1, 2}, {3, 4}, {5, 6}, {7, 8}}.{{1, 2, 3}, {4, 5, 6}}</lang>


<lang mathematica>Dot[{{a, b}, {c, d}}, {{w, x}, {y, z}}]</lang>
The result is:
<lang mathematica>{{9, 12, 15}, {19, 26, 33}, {29, 40, 51}, {39, 54, 69}}</lang>
<lang mathematica>matrixMul[m1_, m2_] := Table[Times @@ {a, b} // Tr, {a, m1}, {b, Transpose@m2}]
matrixMul2[m1_, m2_] :=Table[Sum[Times @@ i, {i, Transpose@{a, b}}], {a, m1}, {b, Transpose@m2}]


This also computes a dot product, using the infix . notation:
a = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};

b = {{1, 2, 3}, {4, 5, 6}};
<lang mathematica>{{a, b}, {c, d}} . {{w, x}, {y, z}}</lang>
matrixMul[a, b]

matrixMul2[a, b]</lang>
This does element-wise multiplication of matrices:

<lang mathematica>Times[{{a, b}, {c, d}}, {{w, x}, {y, z}}]</lang>

With the following infix notations '*' and ' ' (space):

<lang mathematica>{{a, b}, {c, d}}*{{w, x}, {y, z}}</lang>
<lang mathematica>{{a, b}, {c, d}} {{w, x}, {y, z}}</lang>

In all cases matrices can be fully symbolic or numeric or mixed symbolic and numeric.
Numeric matrices support arbitrary numerical magnitudes, arbitrary precision as well
as complex numbers.


=={{header|MATLAB}} / {{header|Octave }}==
=={{header|MATLAB}} / {{header|Octave }}==