Matrix multiplication: Difference between revisions

Content added Content deleted
(→‎{{header|ZPL}}: Added zkl)
Line 2,054:
 
=={{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>{{1a, 2b}, {3c, 4d}, {5, 6}, {7,. 8}}.{{1w, 2, 3x}, {4, 5y, 6z}}</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>M1{{a, =b}, {c, d}}*{1{w, 2x}, {y, z}}</lang>
<lang mathematica>{{9a, 12, 15b}, {19c, 26, 33d}}, {29{w, 40, 51x}, {39, 54y, 69z}}</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 }}==