Anonymous user
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
package Matrix_Ops is▼
▲ type Matrix is array (Natural range <>, Natural range <>) of Float;
▲ function "*" (Left, Right : Matrix) return Matrix;
</lang>▼
=={{header|ALGOL 68}}==
Line 97 ⟶ 93:
result
);
▲ # 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>
# 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
=={{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>
#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);
Output:
Product of a and b:
Line 407 ⟶ 399:
=={{header|Common Lisp}}==
;; example use:▼
(matrix-multiply '((1 2) (3 4)) '((-3 -8 3) (-2 1 4)))▼
(apply #'+ (mapcar #'* row column))) matrix2)) matrix1))</lang>
=={{header|D}}==
<lang d>import std.stdio: writefln;▼
▲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>
=={{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:▼
▲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>
=={{header|Factor}}==
<lang
Example:
<lang
Result:
<lang
=={{header|Forth}}==
{{libheader|Forth Scientific Library}}
▲ include fsl-util.f
3 3 float matrix A{{
Line 540 ⟶ 527:
A{{ B{{ C{{ mat*
C{{ }}print</lang>
=={{header|Fortran}}==
In ISO Fortran 90 or later, use the SIZE and MATMUL intrinsic functions:
<lang fortran>
=={{header|Haskell}}==
Line 574 ⟶ 560:
A somewhat inefficient version with lists (''transpose'' is expensive):
▲ 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>
A more efficient version, based on arrays:
▲ 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>
=={{header|IDL}}==
=={{header|J}}==
Matrix multiply in J is just <code>+/ .*</code>. For example:
<lang j> mp =: +/ .* NB. Matrix product▼
▲ 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>
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")
etc.
Line 633 ⟶ 611:
=={{header|Java}}==
▲}</lang>
=={{header|Mathematica}}==
Or without the variables:
The result is:
=={{header|MATLAB}}==
Line 673 ⟶ 651:
=={{header|Nial}}==
=={{header|OCaml}}==
Line 764 ⟶ 742:
=={{header|Pop11}}==
<lang pop11>define matmul(a, b) -> c;▼
▲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>
=={{header|Python}}==
<lang python>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>
Another one, {{trans|Scheme}}
Line 846 ⟶ 818:
=={{header|R}}==
=={{header|Ruby}}==
Line 913 ⟶ 885:
=={{header|SQL}}==
=={{header|Tcl}}==
Line 963 ⟶ 935:
=={{header|TI-83 BASIC}}==
Store your matrices in <tt>[A]</tt> and <tt>[B]</tt>.
An error will show if the matrices have invalid dimensions for multiplication.
Line 970 ⟶ 942:
{{trans|Mathematica}}
Or without the variables:
The result (without prettyprinting) is:
=={{header|Ursala}}==
|