Matrix multiplication: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Fixed lang tags.)
Line 3: Line 3:
=={{header|Ada}}==
=={{header|Ada}}==
Ada has matrix multiplication predefined for any floating-point or complex type. The implementation is provided by the standard library packages Ada.Numerics.Generic_Real_Arrays and Ada.Numerics.Generic_Complex_Arrays correspondingly. The following example illustrates use of real matrix multiplication for the type Float:
Ada has matrix multiplication predefined for any floating-point or complex type. The implementation is provided by the standard library packages Ada.Numerics.Generic_Real_Arrays and Ada.Numerics.Generic_Complex_Arrays correspondingly. The following example illustrates use of real matrix multiplication for the type Float:
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
<lang ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;


Line 34: Line 33:
begin
begin
Put (A * B);
Put (A * B);
end Matrix_Product;
end Matrix_Product;</lang>
</lang>
Sample output:
Sample output:
<pre>
<pre>
Line 44: Line 42:
</pre>
</pre>
The following code illustrates how matrix multiplication could be implemented from scratch:
The following code illustrates how matrix multiplication could be implemented from scratch:
<lang ada>
<lang ada>package Matrix_Ops is
type Matrix is array (Natural range <>, Natural range <>) of Float;
package Matrix_Ops is
function "*" (Left, Right : Matrix) return Matrix;
type Matrix is array (Natural range <>, Natural range <>) of Float;
end Matrix_Ops;
function "*" (Left, Right : Matrix) return Matrix;
end Matrix_Ops;


package body Matrix_Ops is
package body Matrix_Ops is
---------
---------
-- "*" --
-- "*" --
---------
---------
function "*" (Left, Right : Matrix) return Matrix is
function "*" (Left, Right : Matrix) return Matrix is
Temp : Matrix(Left'Range(1), Right'Range(2)) := (others =>(others => 0.0));
Temp : Matrix(Left'Range(1), Right'Range(2)) := (others =>(others => 0.0));
begin
begin
if Left'Length(2) /= Right'Length(1) then
if Left'Length(2) /= Right'Length(1) then
raise Constraint_Error;
raise Constraint_Error;
end if;
end if;
for I in Left'range(1) loop
for I in Left'range(1) loop
for J in Right'range(2) loop
for J in Right'range(2) loop
for K in Left'range(2) loop
for K in Left'range(2) loop
Temp(I,J) := Temp(I,J) + Left(I, K)*Right(K, J);
Temp(I,J) := Temp(I,J) + Left(I, K)*Right(K, J);
end loop;
end loop;
end loop;
end loop;
end loop;
end loop;
return Temp;
return Temp;
end "*";
end "*";
end Matrix_Ops;
end Matrix_Ops;</lang>
</lang>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 97: Line 93:
result
result
);
);
<lang algol68># this is the task portion #
<pre style="background-color:#ffe">
# this is the task portion #
OP * = (MATRIX a, b)MATRIX: ( # overload matrix times matrix #
OP * = (MATRIX a, b)MATRIX: ( # overload matrix times matrix #
[LWB a:UPB a, 2 LWB b:2 UPB b]FIELD result;
[LWB a:UPB a, 2 LWB b:2 UPB b]FIELD result;
Line 104: Line 99:
FOR k FROM LWB result TO UPB result DO result[k,]:=a[k,]*b OD;
FOR k FROM LWB result TO UPB result DO result[k,]:=a[k,]*b OD;
result
result
);
);</lang>
</pre>
# Some sample matrices to test #
# Some sample matrices to test #
MATRIX a=((1, 1, 1, 1), # matrix A #
MATRIX a=((1, 1, 1, 1), # matrix A #
Line 245: Line 239:
Matrix multiply in APL is just <tt>+.×</tt>. For example:
Matrix multiply in APL is just <tt>+.×</tt>. For example:


x ← +.×
<lang apl> x ← +.×
A ← ↑A*¨⊂A←⍳4 ⍝ Same A as in other examples (1 1 1 1⍪ 2 4 8 16⍪ 3 9 27 81,[0.5] 4 16 64 256)
A ← ↑A*¨⊂A←⍳4 ⍝ Same A as in other examples (1 1 1 1⍪ 2 4 8 16⍪ 3 9 27 81,[0.5] 4 16 64 256)
B ← ⌹A ⍝ Matrix inverse of A
B ← ⌹A ⍝ Matrix inverse of A
'F6.2' ⎕FMT A x B
'F6.2' ⎕FMT A x B
1.00 0.00 0.00 0.00
1.00 0.00 0.00 0.00
0.00 1.00 0.00 0.00
0.00 1.00 0.00 0.00
0.00 0.00 1.00 0.00
0.00 0.00 1.00 0.00
0.00 0.00 0.00 1.00
0.00 0.00 0.00 1.00</lang>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
ahk [http://www.autohotkey.com/forum/topic44657-150.html discussion]
ahk [http://www.autohotkey.com/forum/topic44657-150.html discussion]
Line 341: Line 335:
=={{header|C}}==
=={{header|C}}==
{{works with|gcc|<nowiki>4.1.2 20070925 (Red Hat 4.1.2-27) Options: gcc -std=gnu99</nowiki>}}
{{works with|gcc|<nowiki>4.1.2 20070925 (Red Hat 4.1.2-27) Options: gcc -std=gnu99</nowiki>}}
<lang c>
<lang c>#include <stdio.h>
#include <stdio.h>
#define dim 4 /* fixed length square matrices */
#define dim 4 /* fixed length square matrices */
const int SLICE=0; /* coder hints */
const int SLICE=0; /* coder hints */
Line 397: Line 390:
/* finally print the result */
/* finally print the result */
vprintf(result_fmt,(void*)&prod);
vprintf(result_fmt,(void*)&prod);
}</lang>
}
</lang>
Output:
Output:
Product of a and b:
Product of a and b:
Line 407: Line 399:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
(defun matrix-multiply (a b)
<lang lisp>(defun matrix-multiply (a b)
(flet ((col (mat i) (mapcar #'(lambda (row) (elt row i)) mat))
(flet ((col (mat i) (mapcar #'(lambda (row) (elt row i)) mat))
(row (mat i) (elt mat i)))
(row (mat i) (elt mat i)))
(loop for row from 0 below (length a)
(loop for row from 0 below (length a)
collect (loop for col from 0 below (length (row b 0))
collect (loop for col from 0 below (length (row b 0))
collect (apply #'+ (mapcar #'* (row a row) (col b col)))))))
collect (apply #'+ (mapcar #'* (row a row) (col b col)))))))
;; example use:
(matrix-multiply '((1 2) (3 4)) '((-3 -8 3) (-2 1 4)))


;; example use:
(matrix-multiply '((1 2) (3 4)) '((-3 -8 3) (-2 1 4)))



(defun matrix-multiply (matrix1 matrix2)
(defun matrix-multiply (matrix1 matrix2)
(mapcar
(mapcar
(lambda (row)
(lambda (row)
(apply #'mapcar
(apply #'mapcar
(lambda (&rest column)
(apply #'+ (mapcar #'* row column))) matrix2)) matrix1))
(lambda (&rest column)
(apply #'+ (mapcar #'* row column))) matrix2)) matrix1))</lang>


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio: writefln;
<pre>
import std.stdio: writefln;
import std.string: format, join;
import std.string: format, join;


Line 468: Line 459:
writefln("\nB = \n", prettyPrint(b));
writefln("\nB = \n", prettyPrint(b));
writefln("\nA * B = \n", prettyPrint(matrixMul(a, b)));
writefln("\nA * B = \n", prettyPrint(matrixMul(a, b)));
}</lang>
}
</pre>
=={{header|ELLA}}==
=={{header|ELLA}}==
Sample originally from ftp://ftp.dra.hmg.gb/pub/ella (a now dead link) - Public release.
Sample originally from ftp://ftp.dra.hmg.gb/pub/ella (a now dead link) - Public release.


Code for matrix multiplication hardware design verification:
Code for matrix multiplication hardware design verification:
<lang ella>MAC ZIP = ([INT n]TYPE t: vector1 vector2) -> [n][2]t:
<pre>
MAC ZIP = ([INT n]TYPE t: vector1 vector2) -> [n][2]t:
[INT k = 1..n](vector1[k], vector2[k]).
[INT k = 1..n](vector1[k], vector2[k]).
Line 519: Line 508:
).
).


COM test: just displaysignal MOC
COM test: just displaysignal MOC</lang>
</pre>
=={{header|Factor}}==
=={{header|Factor}}==
: m* flip swap [ dupd [ [ * ] 2map sum ] curry map ] map nip ;
<lang factor>: m* flip swap [ dupd [ [ * ] 2map sum ] curry map ] map nip ;</lang>
Example:
Example:
{ { 1 2 } { 3 4 } } { { -3 -8 3 } { -2 1 4 } } m* .
<lang factor>{ { 1 2 } { 3 4 } } { { -3 -8 3 } { -2 1 4 } } m* .</lang>
Result:
Result:
{ { -7 -6 11 } { -17 -20 25 } }
<lang factor>{ { -7 -6 11 } { -17 -20 25 } }</lang>


=={{header|Forth}}==
=={{header|Forth}}==
{{libheader|Forth Scientific Library}}
{{libheader|Forth Scientific Library}}
<lang forth>include fsl-util.f
<pre><nowiki>
include fsl-util.f
3 3 float matrix A{{
3 3 float matrix A{{
Line 540: Line 527:
A{{ B{{ C{{ mat*
A{{ B{{ C{{ mat*
C{{ }}print
C{{ }}print</lang>
</nowiki></pre>


=={{header|Fortran}}==
=={{header|Fortran}}==
In ISO Fortran 90 or later, use the SIZE and MATMUL intrinsic functions:
In ISO Fortran 90 or later, use the SIZE and MATMUL intrinsic functions:
<lang fortran> real, dimension(n,m) :: a = reshape( (/ (i, i=1, n*m) /), (/ n, m /) )
<lang fortran>real, dimension(n,m) :: a = reshape( (/ (i, i=1, n*m) /), (/ n, m /) )
real, dimension(m,k) :: b = reshape( (/ (i, i=1, m*k) /), (/ m, k /) )
real, dimension(m,k) :: b = reshape( (/ (i, i=1, m*k) /), (/ m, k /) )
real, dimension(size(a,1), size(b,2)) :: c ! C is an array whose first dimension (row) size
real, dimension(size(a,1), size(b,2)) :: c ! C is an array whose first dimension (row) size
! is the same as A's first dimension size, and
! is the same as A's first dimension size, and
! whose second dimension (column) size is the same
! whose second dimension (column) size is the same
! as B's second dimension size.
! as B's second dimension size.
c = matmul( a, b )
c = matmul( a, b )

print *, 'A'
print *, 'A'
do i = 1, n
do i = 1, n
print *, a(i,:)
print *, a(i,:)
end do
end do

print *,
print *,
print *, 'B'
print *, 'B'
do i = 1, m
do i = 1, m
print *, b(i,:)
print *, b(i,:)
end do
end do

print *,
print *,
print *, 'C = AB'
print *, 'C = AB'
do i = 1, n
do i = 1, n
print *, c(i,:)
print *, c(i,:)
end do</lang>
end do</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 574: Line 560:
A somewhat inefficient version with lists (''transpose'' is expensive):
A somewhat inefficient version with lists (''transpose'' is expensive):


<lang haskell>import Data.List
<pre>
import Data.List


mmult :: Num a => [[a]] -> [[a]] -> [[a]]
mmult :: Num a => [[a]] -> [[a]] -> [[a]]
Line 583: Line 568:
test = [[1, 2],
test = [[1, 2],
[3, 4]] `mmult` [[-3, -8, 3],
[3, 4]] `mmult` [[-3, -8, 3],
[-2, 1, 4]]
[-2, 1, 4]]</lang>
</pre>


A more efficient version, based on arrays:
A more efficient version, based on arrays:


<lang haskell>import Data.Array
<pre>
import Data.Array
mmult :: (Ix i, Num a) => Array (i,i) a -> Array (i,i) a -> Array (i,i) a
mmult :: (Ix i, Num a) => Array (i,i) a -> Array (i,i) a -> Array (i,i) a
Line 601: Line 584:
jr = range (y1,y1')
jr = range (y1,y1')
kr = range (x1,x1')
kr = range (x1,x1')
l = [((i,j), sum [x!(i,k) * y!(k,j) | k <- kr]) | i <- ir, j <- jr]
l = [((i,j), sum [x!(i,k) * y!(k,j) | k <- kr]) | i <- ir, j <- jr]</lang>
</pre>


=={{header|IDL}}==
=={{header|IDL}}==


result = arr1 # arr2
<lang idl>result = arr1 # arr2</lang>


=={{header|J}}==
=={{header|J}}==
Matrix multiply in J is just <code>+/ .*</code>. For example:
Matrix multiply in J is just <code>+/ .*</code>. For example:
<lang j> mp =: +/ .* NB. Matrix product
<lang j>
mp =: +/ .* NB. Matrix product
A =: ^/~>:i. 4 NB. Same A as in other examples (1 1 1 1, 2 4 8 16, 3 9 27 81,:4 16 64 256)
A =: ^/~>:i. 4 NB. Same A as in other examples (1 1 1 1, 2 4 8 16, 3 9 27 81,:4 16 64 256)
Line 620: Line 601:
0.00 1.00 0.00 0.00
0.00 1.00 0.00 0.00
0.00 0.00 1.00 0.00
0.00 0.00 1.00 0.00
0.00 0.00 0.00 1.00
0.00 0.00 0.00 1.00</lang>
</lang>
The notation is for a generalized inner product so that
The notation is for a generalized inner product so that
<lang j>x ~:/ .*. y NB. boolean inner product ( ~: is "not equal" (exclusive or) and *. is "and")
<lang j>
x ~:/ .*. y NB. boolean inner product ( ~: is "not equal" (exclusive or) and *. is "and")
x *./ .= y NB. which rows of x are the same as vector y?
x *./ .= y NB. which rows of x are the same as vector y?
x + / .= y NB. number of places where each row of x equals vector y</lang>
x + / .= y NB. number of places where each row of x equals vector y
</lang>
etc.
etc.


Line 633: Line 611:


=={{header|Java}}==
=={{header|Java}}==
public static double[][] mult(double a[][], double b[][]){//a[m][n], b[n][p]
<lang java>public static double[][] mult(double a[][], double b[][]){//a[m][n], b[n][p]
if(a.length == 0) return new double[0][0];
if(a.length == 0) return new double[0][0];
if(a[0].length != b.length) return null; //invalid dims
if(a[0].length != b.length) return null; //invalid dims

int n = a[0].length;
int n = a[0].length;
int m = a.length;
int m = a.length;
int p = b[0].length;
int p = b[0].length;

double ans[][] = new double[m][p];
double ans[][] = new double[m][p];

for(int i = 0;i < m;i++){
for(int i = 0;i < m;i++){
for(int j = 0;j < p;j++){
for(int j = 0;j < p;j++){
for(int k = 0;k < n;k++){
for(int k = 0;k < n;k++){
ans[i][j] += a[i][k] * b[k][j];
ans[i][j] += a[i][k] * b[k][j];
}
}
}
}
}
}
return ans;
return ans;
}</lang>
}
=={{header|Mathematica}}==
=={{header|Mathematica}}==
M1 = {{1, 2},
<lang mathematica>M1 = {{1, 2},
{3, 4},
{3, 4},
{5, 6},
{5, 6},
{7, 8}}
{7, 8}}
M2 = {{1, 2, 3},
M2 = {{1, 2, 3},
{4, 5, 6}}
{4, 5, 6}}
M = M1.M2
M = M1.M2</lang>


Or without the variables:
Or without the variables:


{{1, 2}, {3, 4}, {5, 6}, {7, 8}}.{{1, 2, 3}, {4, 5, 6}}
<lang mathematica>{{1, 2}, {3, 4}, {5, 6}, {7, 8}}.{{1, 2, 3}, {4, 5, 6}}</lang>


The result is:
The result is:
{{9, 12, 15}, {19, 26, 33}, {29, 40, 51}, {39, 54, 69}}
<lang mathematica>{{9, 12, 15}, {19, 26, 33}, {29, 40, 51}, {39, 54, 69}}</lang>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
Line 673: Line 651:


=={{header|Nial}}==
=={{header|Nial}}==
|A := 4 4 reshape 1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256
<lang nial>|A := 4 4 reshape 1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256
=1 1 1 1
=1 1 1 1
=2 4 8 16
=2 4 8 16
=3 9 27 81
=3 9 27 81
=4 16 64 256
=4 16 64 256
|B := inverse A
|B := inverse A


|A innerproduct B
|A innerproduct B
=1. 0. 8.3e-17 -2.9e-16
=1. 0. 8.3e-17 -2.9e-16
=1.3e-15 1. -4.4e-16 -3.3e-16
=1.3e-15 1. -4.4e-16 -3.3e-16
=0. 0. 1. 4.4e-16
=0. 0. 1. 4.4e-16
=0. 0. 0. 1.
=0. 0. 0. 1.</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 764: Line 742:
=={{header|Pop11}}==
=={{header|Pop11}}==


<lang pop11>define matmul(a, b) -> c;
<pre>
define matmul(a, b) -> c;
lvars ba = boundslist(a), bb = boundslist(b);
lvars ba = boundslist(a), bb = boundslist(b);
lvars i, i0 = ba(1), i1 = ba(2);
lvars i, i0 = ba(1), i1 = ba(2);
Line 787: Line 764:
endfor;
endfor;
endfor;
endfor;
enddefine;
enddefine;</lang>

</pre>
=={{header|Python}}==
=={{header|Python}}==
<lang python>
<lang python>a=((1, 1, 1, 1), # matrix A #
a=((1, 1, 1, 1), # matrix A #
(2, 4, 8, 16),
(2, 4, 8, 16),
(3, 9, 27, 81),
(3, 9, 27, 81),
Line 829: Line 803:
print '%8.2f '%val,
print '%8.2f '%val,
print ']'
print ']'
print ')'
print ')'</lang>

</lang>


Another one, {{trans|Scheme}}
Another one, {{trans|Scheme}}
Line 846: Line 818:


=={{header|R}}==
=={{header|R}}==
a %*% b
<lang r>a %*% b</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 913: Line 885:


=={{header|SQL}}==
=={{header|SQL}}==
CREATE TABLE a (x integer, y integer, e real);
<lang sql>CREATE TABLE a (x integer, y integer, e real);
CREATE TABLE b (x integer, y integer, e real);
CREATE TABLE b (x integer, y integer, e real);

-- test data
-- test data
-- A is a 2x2 matrix
-- A is a 2x2 matrix
INSERT INTO a VALUES(0,0,1); INSERT INTO a VALUES(1,0,2);
INSERT INTO a VALUES(0,0,1); INSERT INTO a VALUES(1,0,2);
INSERT INTO a VALUES(0,1,3); INSERT INTO a VALUES(1,1,4);
INSERT INTO a VALUES(0,1,3); INSERT INTO a VALUES(1,1,4);

-- B is a 2x3 matrix
-- B is a 2x3 matrix
INSERT INTO b VALUES(0,0,-3); INSERT INTO b VALUES(1,0,-8); INSERT INTO b VALUES(2,0,3);
INSERT INTO b VALUES(0,0,-3); INSERT INTO b VALUES(1,0,-8); INSERT INTO b VALUES(2,0,3);
INSERT INTO b VALUES(0,1,-2); INSERT INTO b VALUES(1,1, 1); INSERT INTO b VALUES(2,1,4);
INSERT INTO b VALUES(0,1,-2); INSERT INTO b VALUES(1,1, 1); INSERT INTO b VALUES(2,1,4);

-- C is 2x2 * 2x3 so will be a 2x3 matrix
-- C is 2x2 * 2x3 so will be a 2x3 matrix
SELECT rhs.x, lhs.y, (SELECT sum(a.e*b.e) FROM a, b
SELECT rhs.x, lhs.y, (SELECT sum(a.e*b.e) FROM a, b
WHERE a.y = lhs.y
WHERE a.y = lhs.y
AND b.x = rhs.x
AND b.x = rhs.x
AND a.x = b.y)
AND a.x = b.y)
INTO TABLE c
INTO TABLE c
FROM a AS lhs, b AS rhs
FROM a AS lhs, b AS rhs
WHERE lhs.x = 0 AND rhs.y = 0;
WHERE lhs.x = 0 AND rhs.y = 0;</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 963: Line 935:
=={{header|TI-83 BASIC}}==
=={{header|TI-83 BASIC}}==
Store your matrices in <tt>[A]</tt> and <tt>[B]</tt>.
Store your matrices in <tt>[A]</tt> and <tt>[B]</tt>.
Disp [A]*[B]
<lang ti83b>Disp [A]*[B]</lang>
An error will show if the matrices have invalid dimensions for multiplication.
An error will show if the matrices have invalid dimensions for multiplication.


Line 970: Line 942:
{{trans|Mathematica}}
{{trans|Mathematica}}


[1,2; 3,4; 5,6; 7,8] → m1
<lang ti89b>[1,2; 3,4; 5,6; 7,8] → m1
[1,2,3; 4,5,6] → m2
[1,2,3; 4,5,6] → m2
m1 * m2
m1 * m2</lang>


Or without the variables:
Or without the variables:


[1,2; 3,4; 5,6; 7,8] * [1,2,3; 4,5,6]
<lang ti89b>[1,2; 3,4; 5,6; 7,8] * [1,2,3; 4,5,6]</lang>


The result (without prettyprinting) is:
The result (without prettyprinting) is:


[[9,12,15][19,26,33][29,40,51][39,54,69]]
<lang ti89b>[[9,12,15][19,26,33][29,40,51][39,54,69]]</lang>


=={{header|Ursala}}==
=={{header|Ursala}}==

Revision as of 00:51, 21 November 2009

Task
Matrix multiplication
You are encouraged to solve this task according to the task description, using any language you may know.

Multiply two matrices together. They can be of any dimensions, so long as the number of columns of the first matrix is equal to the number of rows of the second matrix.

Ada

Ada has matrix multiplication predefined for any floating-point or complex type. The implementation is provided by the standard library packages Ada.Numerics.Generic_Real_Arrays and Ada.Numerics.Generic_Complex_Arrays correspondingly. The following example illustrates use of real matrix multiplication for the type Float: <lang ada>with Ada.Text_IO; use Ada.Text_IO; with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;

procedure Matrix_Product is

  procedure Put (X : Real_Matrix) is
     type Fixed is delta 0.01 range -100.0..100.0;
  begin
     for I in X'Range (1) loop
        for J in X'Range (2) loop
           Put (Fixed'Image (Fixed (X (I, J))));
        end loop;
        New_Line;
     end loop;
  end Put;
  
  A : constant Real_Matrix :=
        (  ( 1.0,  1.0,  1.0,   1.0),
           ( 2.0,  4.0,  8.0,  16.0),
           ( 3.0,  9.0, 27.0,  81.0),
           ( 4.0, 16.0, 64.0, 256.0)
        );
  B : constant Real_Matrix :=
        (  (  4.0,     -3.0,      4.0/3.0,  -1.0/4.0 ),
           (-13.0/3.0, 19.0/4.0, -7.0/3.0,  11.0/24.0),
           (  3.0/2.0, -2.0,      7.0/6.0,  -1.0/4.0 ),
           ( -1.0/6.0,  1.0/4.0, -1.0/6.0,   1.0/24.0)
        );

begin

  Put (A * B);

end Matrix_Product;</lang> Sample output:

 1.00 0.00 0.00 0.00
 0.00 1.00 0.00 0.00
 0.00 0.00 1.00 0.00
 0.00 0.00 0.00 1.00

The following code illustrates how matrix multiplication could be implemented from scratch: <lang ada>package Matrix_Ops is

  type Matrix is array (Natural range <>, Natural range <>) of Float;
  function "*" (Left, Right : Matrix) return Matrix;

end Matrix_Ops;

package body Matrix_Ops is

  ---------
  -- "*" --
  ---------
  function "*" (Left, Right : Matrix) return Matrix is
     Temp : Matrix(Left'Range(1), Right'Range(2)) := (others =>(others => 0.0));
  begin
     if Left'Length(2) /= Right'Length(1) then
        raise Constraint_Error;
     end if;
    
     for I in Left'range(1) loop
        for J in Right'range(2) loop
           for K in Left'range(2) loop
              Temp(I,J) := Temp(I,J) + Left(I, K)*Right(K, J);
           end loop;
        end loop;
     end loop;
     return Temp;
  end "*";

end Matrix_Ops;</lang>

ALGOL 68

An example of user defined Vector and Matrix Multiplication Operators:

MODE FIELD = LONG REAL; # field type is LONG REAL #
INT default upb:=3;
MODE VECTOR = [default upb]FIELD;
MODE MATRIX = [default upb,default upb]FIELD;

# crude exception handling #
PROC VOID raise index error := VOID: GOTO exception index error;

# define the vector/matrix operators #
OP * = (VECTOR a,b)FIELD: ( # basically the dot product #
    FIELD result:=0;
    IF LWB a/=LWB b OR UPB a/=UPB b THEN raise index error FI;
    FOR i FROM LWB a TO UPB a DO result+:= a[i]*b[i] OD;
    result
  );

OP * = (VECTOR a, MATRIX b)VECTOR: ( # overload vector times matrix #
    [2 LWB b:2 UPB b]FIELD result;
    IF LWB a/=LWB b OR UPB a/=UPB b THEN raise index error FI;
    FOR j FROM 2 LWB b TO 2 UPB b DO result[j]:=a*b[,j] OD;
    result
  );

<lang algol68># this is the task portion #

OP * = (MATRIX a, b)MATRIX: ( # overload matrix times matrix #
    [LWB a:UPB a, 2 LWB b:2 UPB b]FIELD result;
    IF 2 LWB a/=LWB b OR 2 UPB a/=UPB b THEN raise index error FI;
    FOR k FROM LWB result TO UPB result DO result[k,]:=a[k,]*b OD;
    result
  );</lang>
# Some sample matrices to test #
MATRIX a=((1,  1,  1,   1), # matrix A #
          (2,  4,  8,  16),
          (3,  9, 27,  81),
          (4, 16, 64, 256));

MATRIX b=((  4  , -3  ,  4/3,  -1/4 ), # matrix B #
          (-13/3, 19/4, -7/3,  11/24),
          (  3/2, -2  ,  7/6,  -1/4 ),
          ( -1/6,  1/4, -1/6,   1/24));

MATRIX prod = a * b; # actual multiplication example of A x B #

FORMAT real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals #
PROC real matrix printf= (FORMAT real fmt, MATRIX m)VOID:(
  FORMAT vector fmt = $"("n(2 UPB m-1)(f(real fmt)",")f(real fmt)")"$;
  FORMAT matrix fmt = $x"("n(UPB m-1)(f(vector fmt)","lxx)f(vector fmt)");"$;
  # finally print the result #
  printf((matrix fmt,m))
);
  
# finally print the result #
print(("Product of a and b: ",new line));
real matrix printf(real fmt, prod)
EXIT 

exception index error: 
  putf(stand error, $x"Exception: index error."l$)

Output:

Product of a and b: 
((  1.00, -0.00, -0.00, -0.00),
 ( -0.00,  1.00, -0.00, -0.00),
 ( -0.00, -0.00,  1.00, -0.00),
 ( -0.00, -0.00, -0.00,  1.00));

Parallel processing

Alternatively - for multicore CPUs - use the following reinvention of Strassen's O(n^log2(7)) recursive matrix multiplication algorithm:

int default upb := 3;
mode field = long real;
mode vector = [default upb]field;
mode matrix = [default upb, default upb]field;

¢ crude exception handling ¢
proc void raise index error := void: goto exception index error;

sema idle cpus = level ( 8 - 1 ); ¢ 8 = number of CPU cores minus parent CPU ¢

¢ define an operator to slice array into quarters ¢
op top = (matrix m)int: ( ⌊m + ⌈m ) %2,
   bot = (matrix m)int: top m + 1,
   left = (matrix m)int: ( 2 ⌊m + 2 ⌈m ) %2,
   right = (matrix m)int: left m + 1,
   left = (vector v)int: ( ⌊v + ⌈v ) %2,
   right = (vector v)int: left v + 1; 
prio top = 8, bot = 8, left = 8, right = 8; ¢ Operator priority - same as LWB & UPB ¢

op × = (vector a, b)field: ( ¢ dot product ¢
  if (⌊a, ⌈a) ≠ (⌊b, ⌈b) then raise index error fi;
  if ⌊a = ⌈a then
    a[⌈a] × b[⌈b]
  else
    field begin, end;
    []proc void schedule=(
      void: begin:=a[:left a] × b[:left b], 
      void: end  :=a[right a:] × b[right b:]
    );
    if level idle cpus = 0 then ¢ use current CPU ¢
      for thread to ⌈schedule do schedule[thread] od
    else 
      par ( ¢ run vector in parallel ¢
        schedule[1], ¢ assume parent CPU ¢
        ( ↓idle cpus; schedule[2]; ↑idle cpus)
      ) 
    fi;
    begin+end
  fi
);

op × = (matrix a, b)matrix: ¢ matrix multiply ¢
  if (⌊a, 2 ⌊b) = (⌈a, 2 ⌈b) then
    a[⌊a, ] × b[, 2 ⌈b] ¢ dot product ¢
  else
    [⌈a, 2 ⌈b] field out;
    if (2 ⌊a, 2 ⌈a) ≠ (⌊b, ⌈b) then raise index error fi;
    []struct(bool required, proc void thread) schedule = (
      ( true, ¢ calculate top left corner ¢
        void: out[:top a, :left b] := a[:top a, ] × b[, :left b]), 
      ( ⌊a ≠ ⌈a, ¢ calculate bottom left corner ¢
        void: out[bot a:, :left b] := a[bot a:, ] × b[, :left b]), 
      ( 2 ⌊b ≠ 2 ⌈b, ¢ calculate top right corner ¢
        void: out[:top a, right b:] := a[:top a, ] × b[, right b:]), 
      ( (⌊a, 2 ⌊b) ≠ (⌈a, 2 ⌈b) , ¢ calculate bottom right corner ¢
        void: out[bot a:, right b:] := a[bot a:, ] × b[, right b:])
    );
    if level idle cpus = 0 then ¢ use current CPU ¢
      for thread to ⌈schedule do (required →schedule[thread] | thread →schedule[thread] ) od
    else 
      par ( ¢ run vector in parallel ¢
        thread →schedule[1], ¢ thread is always required, and assume parent CPU ¢
        ( required →schedule[4] | ↓idle cpus; thread →schedule[4]; ↑idle cpus),
           ¢ try to do opposite corners of matrix in parallel if CPUs are limited ¢
        ( required →schedule[3] | ↓idle cpus; thread →schedule[3]; ↑idle cpus),
        ( required →schedule[2] | ↓idle cpus; thread →schedule[2]; ↑idle cpus)
      )
    fi;
    out
  fi;

format real fmt = $g(-6,2)$; ¢ width of 6, with no '+' sign, 2 decimals ¢
proc real matrix printf= (format real fmt, matrix m)void:(
  format vector fmt = $"("n(2 ⌈m-1)(f(real fmt)",")f(real fmt)")"$;
  format matrix fmt = $x"("n(⌈m-1)(f(vector fmt)","lxx)f(vector fmt)");"$;
  ¢ finally print the result ¢
  printf((matrix fmt,m))
);

¢ Some sample matrices to test ¢
matrix a=((1,  1,  1,   1), ¢ matrix A ¢
          (2,  4,  8,  16),
          (3,  9, 27,  81),
          (4, 16, 64, 256));

matrix b=((  4  , -3  ,  4/3,  -1/4 ), ¢ matrix B ¢
          (-13/3, 19/4, -7/3,  11/24),
          (  3/2, -2  ,  7/6,  -1/4 ),
          ( -1/6,  1/4, -1/6,   1/24));

matrix c = a × b; ¢ actual multiplication example of A x B ¢

print((" A x B =",new line));
real matrix printf(real fmt, c).

exception index error: 
  putf(stand error, $x"Exception: index error."l$)

APL

Matrix multiply in APL is just +.×. For example:

<lang apl> x ← +.×

   A  ←  ↑A*¨⊂A←⍳4   ⍝  Same  A  as in other examples (1 1 1 1⍪ 2 4 8 16⍪ 3 9 27 81,[0.5] 4 16 64 256) 
   B  ←  ⌹A          ⍝  Matrix inverse of A
   
   'F6.2' ⎕FMT A x B

1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00</lang>

AutoHotkey

ahk discussion <lang autohotkey>Matrix("b","  ; rows separated by "," , 1 2  ; entries separated by space or tab , 2 3 , 3 0") MsgBox % "B`n`n" MatrixPrint(b) Matrix("c"," , 1 2 3 , 3 2 1") MsgBox % "C`n`n" MatrixPrint(c)

MatrixMul("a",b,c) MsgBox % "B * C`n`n" MatrixPrint(a)

MsgBox % MatrixMul("x",b,b)


Matrix(_a,_v) { ; Matrix structure: m_0_0 = #rows, m_0_1 = #columns, m_i_j = element[i,j], i,j > 0

  Local _i, _j = 0
  Loop Parse, _v, `,
     If (A_LoopField != "") {
        _i := 0, _j ++
        Loop Parse, A_LoopField, %A_Space%%A_Tab%
           If (A_LoopField != "")
              _i++, %_a%_%_i%_%_j% := A_LoopField
     }
  %_a% := _a, %_a%_0_0 := _j, %_a%_0_1 := _i

} MatrixPrint(_a) {

  Local _i = 0, _t
  Loop % %_a%_0_0 {
     _i++
     Loop % %_a%_0_1
        _t .= %_a%_%A_Index%_%_i% "`t"
     _t .= "`n"
  }
  Return _t

} MatrixMul(_a,_b,_c) {

  Local _i = 0, _j, _k, _s
  If (%_b%_0_0 != %_c%_0_1)
     Return "ERROR: inner dimensions " %_b%_0_0 " != " %_c%_0_1
  %_a% := _a, %_a%_0_0 := %_b%_0_0, %_a%_0_1 := %_c%_0_1
  Loop % %_c%_0_1 {
     _i++, _j := 0
     Loop % %_b%_0_0 {
        _j++, _k := _s := 0
        Loop % %_b%_0_1
           _k++, _s += %_b%_%_k%_%_j% * %_c%_%_i%_%_k%
        %_a%_%_i%_%_j% := _s
     }
  }

}</lang>

BASIC

Works with: QuickBasic version 4.5
Translation of: Java

Assume the matrices to be multiplied are a and b

IF (LEN(a,2) = LEN(b)) 'if valid dims
       n = LEN(a,2)
       m = LEN(a)
       p = LEN(b,2)

       DIM ans(0 TO m - 1, 0 TO p - 1)

       FOR i = 0 TO m - 1
               FOR j = 0 TO p - 1
                       FOR k = 0 TO n - 1
                               ans(i, j) = ans(i, j) + (a(i, k) * b(k, j))
                       NEXT k, j, i

       'print answer
       FOR i = 0 TO m - 1
               FOR j = 0 TO p - 1
                       PRINT ans(i, j);
               NEXT j
               PRINT
       NEXT i
ELSE
       PRINT "invalid dimensions"
END IF

C

Works with: gcc version 4.1.2 20070925 (Red Hat 4.1.2-27) Options: gcc -std=gnu99

<lang c>#include <stdio.h>

  1. define dim 4 /* fixed length square matrices */

const int SLICE=0; /* coder hints */ typedef double field_t; /* field_t type is long float */ typedef field_t vec_t[dim]; typedef field_t *ref_vec_t; /* address of first element */ typedef vec_t matrix_t[dim]; typedef field_t *ref_matrix_t; /* address of first element */ typedef const char* format;

/* define the vector/matrix_t operators */

field_t v_times_v (vec_t a, vec_t b, int step_b){

   /* basically the dot product if step_b==1*/
   field_t result=0;
   for( int i=0; i<sizeof a; i++ )
     result+= a[i]*b[i*step_b];
   return result;
 }

ref_vec_t v_eq_v_times_m(vec_t result, vec_t a, matrix_t b){

   for( int j=0; j<sizeof b; j++ )
     result[j]=v_times_v(a,&b[SLICE][j],sizeof b[SLICE] / sizeof (field_t));
   return &result[SLICE];
 }

ref_matrix_t m_eq_m_times_m (matrix_t result, matrix_t a, matrix_t b){

   for( int k=0; k<sizeof result; k++ )
     v_eq_v_times_m(&result[k][SLICE],&a[k][SLICE],b); 
   return &result[SLICE][SLICE];
 }

/* Some sample matrices to test */ matrix_t a={{1, 1, 1, 1}, /* matrix_t A */

           {2,  4,  8,  16},
           {3,  9, 27,  81},
           {4, 16, 64, 256}};

matrix_t b={{ 4.0 , -3.0 , 4.0/3, -1.0/4 }, /* matrix_t B */

           {-13.0/3, 19.0/4, -7.0/3,  11.0/24},
           {  3.0/2, -2.0  ,  7.0/6,  -1.0/4 },
           { -1.0/6,  1.0/4, -1.0/6,   1.0/24}};

int main(){

 matrix_t prod;
 m_eq_m_times_m(prod,a,b); /* actual multiplication example of A x B */
 #define field_fmt "%6.2f" /* width of 6, with no '+' sign, 2 decimals */
 #define vec_fmt "{"field_fmt","field_fmt","field_fmt","field_fmt"}"
 #define matrix_fmt " {"vec_fmt",\n  "vec_fmt",\n  "vec_fmt",\n  "vec_fmt"};"

 format result_fmt = " Product of a and b: \n"matrix_fmt"\n";
 /* finally print the result */
 vprintf(result_fmt,(void*)&prod);

}</lang> Output:

Product of a and b: 
{{  1.00,  0.00, -0.00, -0.00},
 {  0.00,  1.00, -0.00, -0.00},
 {  0.00,  0.00,  1.00, -0.00},
 {  0.00,  0.00,  0.00,  1.00}};

Common Lisp

<lang lisp>(defun matrix-multiply (a b)

 (flet ((col (mat i) (mapcar #'(lambda (row) (elt row i)) mat))
        (row (mat i) (elt mat i)))
   (loop for row from 0 below (length a)
         collect (loop for col from 0 below (length (row b 0))
                       collect (apply #'+ (mapcar #'* (row a row) (col b col)))))))
example use

(matrix-multiply '((1 2) (3 4)) '((-3 -8 3) (-2 1 4)))


(defun matrix-multiply (matrix1 matrix2)

(mapcar
 (lambda (row)
  (apply #'mapcar
   (lambda (&rest column)
    (apply #'+ (mapcar #'* row column))) matrix2)) matrix1))</lang>

D

<lang d>import std.stdio: writefln; import std.string: format, join;

T[][] matrixMul(T)(T[][] m1, T[][] m2) {

   bool isRectangular(T[][] matrix) {
       foreach (row; matrix)
           if (row.length != matrix[0].length)
               return false;
       return true;
   }
   T[][] result;
   if (isRectangular(m1) && isRectangular(m2) && m1[0].length == m2.length) {
       result = new T[][](m1.length, m2[0].length);
       foreach (i, m1_row_i; m1)
           for (int j; j < m2[0].length; j++) {
               T aux = 0;
               foreach (k, m2_row_k; m2)
                   aux += m1_row_i[k] * m2_row_k[j];
               result[i][j] = aux;
           }
   } else
       throw new Exception("matrixMul Error");
   return result;

}

string prettyPrint(T)(T[][] matrix) {

   string[] result;
   foreach (row; matrix)
       result ~= format(row);
   return "[" ~ result.join(",\n ") ~ "]";

}

void main() {

   float[][] a = [[1, 2], [3, 4], [3, 6]];
   float[][] b = [[-3, -8, 3,], [-2, 1, 4]];
   writefln("A = \n", prettyPrint(a));
   writefln("\nB = \n", prettyPrint(b));
   writefln("\nA * B = \n", prettyPrint(matrixMul(a, b)));

}</lang>

ELLA

Sample originally from ftp://ftp.dra.hmg.gb/pub/ella (a now dead link) - Public release.

Code for matrix multiplication hardware design verification: <lang ella>MAC ZIP = ([INT n]TYPE t: vector1 vector2) -> [n][2]t:

 [INT k = 1..n](vector1[k], vector2[k]).
    

MAC TRANSPOSE = ([INT n][INT m]TYPE t: matrix) -> [m][n]t:

 [INT i = 1..m] [INT j = 1..n] matrix[j][i].

MAC INNER_PRODUCT{FN * = [2]TYPE t -> TYPE s, FN + = [2]s -> s}

                = ([INT n][2]t: vector) -> s:
 IF n = 1 THEN *vector[1]
 ELSE *vector[1] + INNER_PRODUCT {*,+} vector[2..n]
 FI.

MAC MATRIX_MULT {FN * = [2]TYPE t->TYPE s, FN + = [2]s->s} = ([INT n][INT m]t: matrix1, [m][INT p]t: matrix2) -> [n][p]s: BEGIN

 LET transposed_matrix2 = TRANSPOSE matrix2.

OUTPUT [INT i = 1..n][INT j = 1..p]

      INNER_PRODUCT{*,+}ZIP(matrix1[i],transposed_matrix2[j])

END.


TYPE element = NEW elt/(1..20),

    product = NEW prd/(1..1200).

FN PLUS = (product: integer1 integer2) -> product:

 ARITH integer1 + integer2.

FN MULT = (element: integer1 integer2) -> product:

 ARITH integer1 * integer2.

FN MULT_234 = ([2][3]element:matrix1, [3][4]element:matrix2) ->

            [2][4]product:  
 MATRIX_MULT{MULT,PLUS}(matrix1, matrix2).

FN TEST = () -> [2][4]product: ( LET m1 = ((elt/2, elt/1, elt/1),

           (elt/3, elt/6, elt/9)), 
     m2 = ((elt/6, elt/1, elt/3, elt/4), 
           (elt/9, elt/2, elt/8, elt/3),
           (elt/6, elt/4, elt/1, elt/2)).
 OUTPUT
   MULT_234 (m1, m2)

).

COM test: just displaysignal MOC</lang>

Factor

<lang factor>: m* flip swap [ dupd [ [ * ] 2map sum ] curry map ] map nip ;</lang> Example: <lang factor>{ { 1 2 } { 3 4 } } { { -3 -8 3 } { -2 1 4 } } m* .</lang> Result: <lang factor>{ { -7 -6 11 } { -17 -20 25 } }</lang>

Forth

<lang forth>include fsl-util.f

3 3 float matrix A{{
ATemplate:3 3fread  1e 2e 3e  4e 5e 6e  7e 8e 9e
3 3 float matrix B{{
BTemplate:3 3fread  3e 3e 3e  2e 2e 2e  1e 1e 1e
3 3 float matrix C{{    \ result

A{{ B{{ C{{ mat*
C{{ }}print</lang>

Fortran

In ISO Fortran 90 or later, use the SIZE and MATMUL intrinsic functions: <lang fortran>real, dimension(n,m) :: a = reshape( (/ (i, i=1, n*m) /), (/ n, m /) ) real, dimension(m,k) :: b = reshape( (/ (i, i=1, m*k) /), (/ m, k /) ) real, dimension(size(a,1), size(b,2)) :: c  ! C is an array whose first dimension (row) size

                                             ! is the same as A's first dimension size, and
                                             ! whose second dimension (column) size is the same
                                             ! as B's second dimension size.

c = matmul( a, b )

print *, 'A' do i = 1, n

   print *, a(i,:)

end do

print *, print *, 'B' do i = 1, m

   print *, b(i,:)

end do

print *, print *, 'C = AB' do i = 1, n

   print *, c(i,:)

end do</lang>

Haskell

A somewhat inefficient version with lists (transpose is expensive):

<lang haskell>import Data.List

mmult :: Num a => a -> a -> a 
mmult a b = [ [ sum $ zipWith (*) ar bc | bc <- (transpose b) ] | ar <- a ]

-- Example use:
test = [[1, 2],
        [3, 4]] `mmult` [[-3, -8, 3],
                         [-2,  1, 4]]</lang>

A more efficient version, based on arrays:

<lang haskell>import Data.Array

mmult :: (Ix i, Num a) => Array (i,i) a -> Array (i,i) a -> Array (i,i) a 
mmult x y 
  | x1 /= y0 || x1' /= y0'  = error "range mismatch"
  | otherwise               = array ((x0,y1),(x0',y1')) l
  where
    ((x0,x1),(x0',x1')) = bounds x
    ((y0,y1),(y0',y1')) = bounds y
    ir = range (x0,x0')
    jr = range (y1,y1')
    kr = range (x1,x1')
    l  = [((i,j), sum [x!(i,k) * y!(k,j) | k <- kr]) | i <- ir, j <- jr]</lang>

IDL

<lang idl>result = arr1 # arr2</lang>

J

Matrix multiply in J is just +/ .*. For example: <lang j> mp =: +/ .* NB. Matrix product

  A  =:  ^/~>:i. 4   NB.  Same  A  as in other examples (1 1 1 1, 2 4 8 16, 3 9 27 81,:4 16 64 256)
  B  =:  %.A         NB.  Matrix inverse of A
    
  '6.2' 8!:2 A mp B

1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00</lang> The notation is for a generalized inner product so that <lang j>x ~:/ .*. y NB. boolean inner product ( ~: is "not equal" (exclusive or) and *. is "and") x *./ .= y NB. which rows of x are the same as vector y? x + / .= y NB. number of places where each row of x equals vector y</lang> etc.

The general inner product extends to multidimensional arrays, requiring only that x and y be conformable (trailing dimension of array x equals the leading dimension of array y). For example, the matrix multiplication of two dimensional arrays requires x to have the same numbers of rows as y has columns, as you would expect.

Java

<lang java>public static double[][] mult(double a[][], double b[][]){//a[m][n], b[n][p]

  if(a.length == 0) return new double[0][0];
  if(a[0].length != b.length) return null; //invalid dims
  int n = a[0].length;
  int m = a.length;
  int p = b[0].length;
  double ans[][] = new double[m][p];
  for(int i = 0;i < m;i++){
     for(int j = 0;j < p;j++){
        for(int k = 0;k < n;k++){
           ans[i][j] += a[i][k] * b[k][j];
        }
     }
  }
  return ans;

}</lang>

Mathematica

<lang mathematica>M1 = {{1, 2},

     {3, 4},
     {5, 6},
     {7, 8}}

M2 = {{1, 2, 3},

     {4, 5, 6}}

M = M1.M2</lang>

Or without the variables:

<lang mathematica>{{1, 2}, {3, 4}, {5, 6}, {7, 8}}.{{1, 2, 3}, {4, 5, 6}}</lang>

The result is: <lang mathematica>{{9, 12, 15}, {19, 26, 33}, {29, 40, 51}, {39, 54, 69}}</lang>

MATLAB

<lang Matlab>function [output] = matrixmultiplication(matrixA, matrixB)

  output = matrixA*matrixB;</lang>

Nial

<lang nial>|A := 4 4 reshape 1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256 =1 1 1 1 =2 4 8 16 =3 9 27 81 =4 16 64 256 |B := inverse A

|A innerproduct B =1. 0. 8.3e-17 -2.9e-16 =1.3e-15 1. -4.4e-16 -3.3e-16 =0. 0. 1. 4.4e-16 =0. 0. 0. 1.</lang>

OCaml

This version works on arrays of arrays of ints: <lang ocaml>let matrix_multiply x y =

 let x0 = Array.length x
 and y0 = Array.length y in
 let y1 = if y0 = 0 then 0 else Array.length y.(0) in
 let z = Array.make_matrix x0 y1 0 in
 for i = 0 to x0-1 do
   for j = 0 to y1-1 do
     for k = 0 to y0-1 do
       z.(i).(j) <- z.(i).(j) + x.(i).(k) * y.(k).(j)
     done
   done
 done;
 z</lang>
# matrix_multiply [|[|1;2|];[|3;4|]|] [|[|-3;-8;3|];[|-2;1;4|]|];;
- : int array array = [|[|-7; -6; 11|]; [|-17; -20; 25|]|]
Translation of: Scheme

This version works on lists of lists of ints: <lang ocaml>(* equivalent to (apply map ...) *) let rec mapn f lists =

 assert (lists <> []);
 if List.mem [] lists then
   []
 else
   f (List.map List.hd lists) :: mapn f (List.map List.tl lists)

let matrix_multiply m1 m2 =

 List.map
   (fun row ->
     mapn
      (fun column ->
        List.fold_left (+) 0
         (List.map2 ( * ) row column))
      m2)
   m1</lang>
# matrix_multiply [[1;2];[3;4]] [[-3;-8;3];[-2;1;4]];;
- : int list list = [[-7; -6; 11]; [-17; -20; 25]]

Octave

<lang octave>a = zeros(4); % prepare the matrix % 1 1 1 1 % 2 4 8 16 % 3 9 27 81 % 4 16 64 256 for i = 1:4

 for j = 1:4
   a(i, j) = i^j;
 endfor

endfor b = inverse(a); a * b</lang>


Perl

For most applications involving extensive matrix arithmetic, using the CPAN module called "PDL" (that stands for "Perl Data Language") would probably be the easiest and most efficient approach. That said, here's an implementation of matrix multiplication in plain Perl.

<lang perl>sub mmult

{our @a; local *a = shift;
 our @b; local *b = shift;
 my @p = [];
 my $rows = @a;
 my $cols = @{$b[0]};
 my $n = @b - 1;
 for (my $r = 0 ; $r < $rows ; ++$r)
    {for (my $c = 0 ; $c < $cols ; ++$c)
        {$p[$r][$c] += $a[$r][$_] * $b[$_][$c] foreach 0 .. $n;}}
 return [@p];}</lang>

This function takes two references to arrays of arrays and returns the product as a reference to a new anonymous array of arrays.

Pop11

<lang pop11>define matmul(a, b) -> c;

   lvars ba = boundslist(a), bb = boundslist(b);
   lvars i, i0 = ba(1), i1 = ba(2);
   lvars j, j0 = bb(1), j1 = bb(2);
   lvars k, k0 = bb(3), k1 = bb(4);
   if length(ba) /= 4 then
       throw([need_2d_array ^a])
   endif;
   if length(bb) /= 4 then
       throw([need_2d_array ^b])
   endif;
   if ba(3) /= j0 or ba(4) /= j1 then
       throw([dimensions_do_not_match ^a ^b]);
   endif;
   newarray([^i0 ^i1 ^k0 ^k1], 0) -> c;
   for i from i0 to i1 do
       for k from k0 to k1 do
           for j from j0 to j1 do
               c(i, k) + a(i, j)*b(j, k) -> c(i, k);
           endfor;
       endfor;
   endfor;

enddefine;</lang>

Python

<lang python>a=((1, 1, 1, 1), # matrix A #

    (2,  4,  8,  16),
    (3,  9, 27,  81),
    (4, 16, 64, 256))

b=(( 4 , -3 , 4/3., -1/4. ), # matrix B #

    (-13/3., 19/4., -7/3.,  11/24.),
    (  3/2., -2.  ,  7/6.,  -1/4. ),
    ( -1/6.,  1/4., -1/6.,   1/24.))


def MatrixMul( mtx_a, mtx_b):

   tpos_b = zip( *mtx_b)
   rtn = [[ sum( ea*eb for ea,eb in zip(a,b)) for b in tpos_b] for a in mtx_a]
   return rtn


v = MatrixMul( a, b )

print 'v = (' for r in v:

   print '[', 
   for val in r:
       print '%8.2f '%val, 
   print ']'

print ')'


u = MatrixMul(b,a)

print 'u = ' for r in u:

   print '[', 
   for val in r:
       print '%8.2f '%val, 
   print ']'

print ')'</lang>

Another one,

Translation of: Scheme

<lang python>from operator import mul

def matrixMul(m1, m2):

 return map(
   lambda row:
     map(
       lambda *column:
         sum(map(mul, row, column)),
       *m2),
   m1)</lang>

R

<lang r>a %*% b</lang>

Ruby

Using

Library: matrix.rb

<lang ruby>require 'matrix'

Matrix[[1, 2],

      [3, 4]] * Matrix[[-3, -8, 3],
                       [-2,  1, 4]]</lang>

Output:

Matrix[[-7, -6, 11], [-17, -20, 25]]

Version for lists:

Translation of: Haskell

<lang ruby>def matrix_mult(a, b)

 a.map do |ar|
   b.transpose.map do |bc|
     ar.zip(bc).map {|x,y| x*y}.inject {|z,w| z+w}
   end
 end

end</lang>

Scheme

Translation of: Common Lisp

This version works on lists of lists: <lang scheme>(define (matrix-multiply matrix1 matrix2)

 (map
  (lambda (row)
   (apply map
    (lambda column
     (apply + (map * row column)))
    matrix2))
  matrix1))</lang>
> (matrix-multiply '((1 2) (3 4)) '((-3 -8 3) (-2 1 4)))
((-7 -6 11) (-17 -20 25))

Seed7

<lang seed7>const type: matrix is array array float;

const func matrix: (in matrix: left) * (in matrix: right) is func

 result
   var matrix: result is matrix.value;
 local
   var integer: i is 0;
   var integer: j is 0;
   var integer: k is 0;
   var float: accumulator is 0.0;
 begin
   if length(left[1]) <> length(right) then
     raise RANGE_ERROR;
   else
     result := length(left) times length(right[1]) times 0.0;
     for i range 1 to length(left) do
       for j range 1 to length(right) do
         accumulator := 0.0;
         for k range 1 to length(left) do
           accumulator +:= left[i][k] * right[k][j];
         end for;
         result[i][j] := accumulator;
       end for;
     end for;
   end if;
 end func;</lang>

Original source: [1]

SQL

<lang sql>CREATE TABLE a (x integer, y integer, e real); CREATE TABLE b (x integer, y integer, e real);

-- test data -- A is a 2x2 matrix INSERT INTO a VALUES(0,0,1); INSERT INTO a VALUES(1,0,2); INSERT INTO a VALUES(0,1,3); INSERT INTO a VALUES(1,1,4);

-- B is a 2x3 matrix INSERT INTO b VALUES(0,0,-3); INSERT INTO b VALUES(1,0,-8); INSERT INTO b VALUES(2,0,3); INSERT INTO b VALUES(0,1,-2); INSERT INTO b VALUES(1,1, 1); INSERT INTO b VALUES(2,1,4);

-- C is 2x2 * 2x3 so will be a 2x3 matrix SELECT rhs.x, lhs.y, (SELECT sum(a.e*b.e) FROM a, b

                            WHERE a.y = lhs.y
                              AND b.x = rhs.x
                              AND a.x = b.y)
      INTO TABLE c
      FROM a AS lhs, b AS rhs
      WHERE lhs.x = 0 AND rhs.y = 0;</lang>

Tcl

Works with: Tcl version 8.5

<lang tcl>package require Tcl 8.5 namespace path ::tcl::mathop proc matrix_multiply {a b} {

   lassign [size $a] a_rows a_cols
   lassign [size $b] b_rows b_cols
   if {$a_cols != $b_rows} {
       error "incompatible sizes: a($a_rows, $a_cols), b($b_rows, $b_cols)"
   }
   set temp [lrepeat $a_rows [lrepeat $b_cols 0]]
   for {set i 0} {$i < $a_rows} {incr i} {
       for {set j 0} {$j < $b_cols} {incr j} {
           set sum 0
           for {set k 0} {$k < $a_cols} {incr k} {
               set sum [+ $sum [* [lindex $a $i $k] [lindex $b $k $j]]]
           }
           lset temp $i $j $sum
       }
   }
   return $temp

}</lang> Using the print_matrix procedure defined in Matrix Transpose#Tcl

% print_matrix [matrix_multiply {{1 2} {3 4}} {{-3 -8 3} {-2 1 4}}]
 -7  -6 11 
-17 -20 25 

TI-83 BASIC

Store your matrices in [A] and [B]. <lang ti83b>Disp [A]*[B]</lang> An error will show if the matrices have invalid dimensions for multiplication.

TI-89 BASIC

Translation of: Mathematica

<lang ti89b>[1,2; 3,4; 5,6; 7,8] → m1 [1,2,3; 4,5,6] → m2 m1 * m2</lang>

Or without the variables:

<lang ti89b>[1,2; 3,4; 5,6; 7,8] * [1,2,3; 4,5,6]</lang>

The result (without prettyprinting) is:

<lang ti89b>[[9,12,15][19,26,33][29,40,51][39,54,69]]</lang>

Ursala

There is a library function for matrix multiplication of IEEE double precision floating point numbers. This example shows how to define and use a matrix multiplication function over any chosen field given only the relevant product and sum functions, in this case for the built in rational number type.

<lang Ursala>#import rat

a =

<

  <1/1,  1/1,  1/1,   1/1>,
  <2/1,  4/1,  8/1,  16/1>,
  <3/1,  9/1, 27/1,  81/1>,
  <4/1, 16/1, 64/1, 256/1>>

b =

<

  <  4/1, -3/1,  4/3,  -1/4>,
  <-13/3, 19/4, -7/3,  11/24>,
  <  3/2, -2/1,  7/6,  -1/4>,
  < -1/6,  1/4, -1/6,   1/24>>

mmult = *rK7lD *rlD sum:-0.+ product*p

  1. cast %qLL

test = mmult(a,b)</lang> output:

<
   <1/1,0/1,0/1,0/1>,
   <0/1,1/1,0/1,0/1>,
   <0/1,0/1,1/1,0/1>,
   <0/1,0/1,0/1,1/1>>