Jump to content

Matrix multiplication: Difference between revisions

m
Fixed lang tags.
m (Fixed lang tags.)
Line 3:
=={{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:
<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;
 
Line 34 ⟶ 33:
begin
Put (A * B);
end Matrix_Product;</lang>
</lang>
Sample output:
<pre>
Line 44 ⟶ 42:
</pre>
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;
package Matrix_Ops is
function "*" (Left, Right : Matrix) return Matrix;
type Matrix is array (Natural range <>, Natural range <>) of Float;
packageend Matrix_Ops is;
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>
</lang>
 
=={{header|ALGOL 68}}==
Line 97 ⟶ 93:
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 #
[LWB a:UPB a, 2 LWB b:2 UPB b]FIELD result;
Line 104 ⟶ 99:
FOR k FROM LWB result TO UPB result DO result[k,]:=a[k,]*b OD;
result
);</lang>
</pre>
# Some sample matrices to test #
MATRIX a=((1, 1, 1, 1), # matrix A #
Line 245 ⟶ 239:
Matrix multiply in APL is just <tt>+.×</tt>. 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>
=={{header|AutoHotkey}}==
ahk [http://www.autohotkey.com/forum/topic44657-150.html discussion]
Line 341 ⟶ 335:
=={{header|C}}==
{{works with|gcc|<nowiki>4.1.2 20070925 (Red Hat 4.1.2-27) Options: gcc -std=gnu99</nowiki>}}
<lang c>#include <stdio.h>
#include <stdio.h>
#define dim 4 /* fixed length square matrices */
const int SLICE=0; /* coder hints */
Line 397 ⟶ 390:
/* finally print the result */
vprintf(result_fmt,(void*)&prod);
}</lang ada>
}
</lang>
Output:
Product of a and b:
Line 407 ⟶ 399:
 
=={{header|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)))
 
;; 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 #'+lambda (mapcar #'* row&rest column))) matrix2)) matrix1))
(apply #'+ (mapcar #'* row column))) matrix2)) matrix1))</lang>
 
=={{header|D}}==
<lang d>import std.stdio: writefln;
<pre>
import std.stdio: writefln;
import std.string: format, join;
 
Line 468 ⟶ 459:
writefln("\nB = \n", prettyPrint(b));
writefln("\nA * B = \n", prettyPrint(matrixMul(a, b)));
}</lang>
}
</pre>
=={{header|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:
<pre>
MAC ZIP = ([INT n]TYPE t: vector1 vector2) -> [n][2]t:
[INT k = 1..n](vector1[k], vector2[k]).
Line 519 ⟶ 508:
).
 
COM test: just displaysignal MOC</lang>
</pre>
=={{header|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>
 
=={{header|Forth}}==
{{libheader|Forth Scientific Library}}
<lang forth>include fsl-util.f
<pre><nowiki>
include fsl-util.f
3 3 float matrix A{{
Line 540 ⟶ 527:
A{{ B{{ C{{ mat*
C{{ }}print</lang>
</nowiki></pre>
 
=={{header|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>
 
=={{header|Haskell}}==
Line 574 ⟶ 560:
A somewhat inefficient version with lists (''transpose'' is expensive):
 
<lang haskell>import Data.List
<pre>
import Data.List
 
mmult :: Num a => [[a]] -> [[a]] -> [[a]]
Line 583 ⟶ 568:
test = [[1, 2],
[3, 4]] `mmult` [[-3, -8, 3],
[-2, 1, 4]]</lang>
</pre>
 
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
Line 601 ⟶ 584:
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>
</pre>
 
=={{header|IDL}}==
 
<lang idl>result = arr1 # arr2</lang>
 
=={{header|J}}==
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)
Line 620 ⟶ 601:
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>
</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")
<lang j>
x ~:*./ .*.= y NB. booleanwhich innerrows productof (x ~:are isthe "notsame equal"as (exclusivevector or) and *. is "and")y?
x *.+ / .= y NB. which rowsnumber of xplaces arewhere theeach samerow asof x equals vector y?</lang>
x + / .= y NB. number of places where each row of x equals vector y
</lang>
etc.
 
Line 633 ⟶ 611:
 
=={{header|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>
}
=={{header|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>
 
=={{header|MATLAB}}==
Line 673 ⟶ 651:
 
=={{header|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>
 
=={{header|OCaml}}==
Line 764 ⟶ 742:
=={{header|Pop11}}==
 
<lang pop11>define matmul(a, b) -> c;
<pre>
define matmul(a, b) -> c;
lvars ba = boundslist(a), bb = boundslist(b);
lvars i, i0 = ba(1), i1 = ba(2);
Line 787 ⟶ 764:
endfor;
endfor;
enddefine;</lang>
 
</pre>
=={{header|Python}}==
<lang python>a=((1, 1, 1, 1), # matrix A #
a=((1, 1, 1, 1), # matrix A #
(2, 4, 8, 16),
(3, 9, 27, 81),
Line 829 ⟶ 803:
print '%8.2f '%val,
print ']'
print ')'</lang>
 
</lang>
 
Another one, {{trans|Scheme}}
Line 846 ⟶ 818:
 
=={{header|R}}==
<lang r>a %*% b</lang>
 
=={{header|Ruby}}==
Line 913 ⟶ 885:
 
=={{header|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>
 
=={{header|Tcl}}==
Line 963 ⟶ 935:
=={{header|TI-83 BASIC}}==
Store your matrices in <tt>[A]</tt> and <tt>[B]</tt>.
<lang ti83b>Disp [A]*[B]</lang>
An error will show if the matrices have invalid dimensions for multiplication.
 
Line 970 ⟶ 942:
{{trans|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>
 
=={{header|Ursala}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.