Matrix multiplication: Difference between revisions
Content added Content deleted
(→{{header|ZPL}}: Added zkl) |
|||
Line 2,054: | Line 2,054: | ||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
⚫ | |||
{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>Dot[{{a, b}, {c, d}}, {{w, x}, {y, z}}]</lang> |
|||
The result is: |
|||
⚫ | |||
<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}}; |
|||
⚫ | |||
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): |
|||
⚫ | |||
⚫ | |||
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 }}== |