Matrix multiplication: Difference between revisions

no edit summary
No edit summary
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;
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
Line 35:
Put (A * B);
end Matrix_Product;
</adalang>
Sample output:
<pre>
Line 44:
</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;
Line 71:
end "*";
end Matrix_Ops;
</adalang>
 
=={{header|ALGOL 68}}==
Line 285:
=={{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 */
Line 342:
vprintf(result_fmt,(void*)&prod);
}
</clang>
Output:
Product of a and b:
Line 627:
 
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
Line 639:
done
done;
z</ocamllang>
 
# matrix_multiply [|[|1;2|];[|3;4|]|] [|[|-3;-8;3|];[|-2;1;4|]|];;
Line 646:
{{trans|Scheme}}
This version works on lists of lists of ints:
<lang ocaml>(* equivalent to (apply map ...) *)
let rec mapn f lists =
assert (lists <> []);
Line 662:
(List.map2 ( * ) row column))
m2)
m1</ocamllang>
 
# matrix_multiply [[1;2];[3;4]] [[-3;-8;3];[-2;1;4]];;
Line 670:
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;
Line 680:
{for (my $c = 0 ; $c < $cols ; ++$c)
{$p[$r][$c] += $a[$r][$_] * $b[$_][$c] foreach 0 .. $n;}}
return [@p];}</perllang>
 
This function takes two references to arrays of arrays and returns the product as a reference to a new anonymous array of arrays.
Line 756:
 
Another one, {{trans|Scheme}}
<lang python>from operator import mul
 
def matrixMul(m1, m2):
Line 765:
sum(map(mul, row, column)),
*m2),
m1)</pythonlang>
 
=={{header|R}}==
Line 772:
=={{header|Ruby}}==
Using the Matrix class:
<lang ruby>require 'matrix'
 
Matrix[[1, 2],
[3, 4]] * Matrix[[-3, -8, 3],
[-2, 1, 4]]</rubylang>
Output:
Matrix[[-7, -6, 11], [-17, -20, 25]]
 
Version for lists: {{trans|Haskell}}
<lang ruby>def matrix_mult(a, b)
a.map do |ar|
b.transpose.map do |bc|
Line 787:
end
end
end</rubylang>
 
=={{header|Scheme}}==
{{trans|Common Lisp}}
This version works on lists of lists:
<lang scheme>(define (matrix-multiply matrix1 matrix2)
(map
(lambda (row)
Line 799:
(apply + (map * row column)))
matrix2))
matrix1))</schemelang>
 
> (matrix-multiply '((1 2) (3 4)) '((-3 -8 3) (-2 1 4)))