Matrix multiplication: Difference between revisions

Restored erroneously deleted second D entry and output, and updated second D entry
m (→‎{{header|Chapel}}: remove erroneous literal)
(Restored erroneously deleted second D entry and output, and updated second D entry)
Line 932:
writefln("B = \n" ~ form ~ "\n", b);
writefln("A * B = \n" ~ form, matrixMul(a, b));
}</lang>
{{out}}
<pre>A =
[[1, 2],
[3, 4],
[3, 6]]
 
B =
[[-3, -8, 3],
[-2, 1, 4]]
 
A * B =
[[-7, -6, 11],
[-17, -20, 25],
[-21, -18, 33]]</pre>
 
===Stronger Statically Typed Version===
All array sizes are verified at compile-time (and no matrix is copied). Same output.
<lang d>import std.stdio, std.string, std.conv, std.numeric,
std.array, std.algorithm, std.traits;
 
template TMMul_helper(M1, M2) {
alias T = Unqual!(typeof(M1.init[0][0]));
alias TMMul_helper = T[M2.init[0].length][M1.length];
}
 
void matrixMul(T, T2, size_t k, size_t m, size_t n)
(in ref T[m][k] A, in ref T[n][m] B,
/*out*/ ref T2[n][k] result) pure nothrow
if (is(T2 == Unqual!T)) {
T2[m] aux;
foreach (immutable j; 0 .. n) {
foreach (immutable k, const row; B)
aux[k] = row[j];
foreach (immutable i, const ai; A)
result[i][j] = dotProduct(ai, aux);
}
}
 
void main() {
immutable int[2][3] a = [[1, 2], [3, 4], [3, 6]];
immutable int[3][2] b = [[-3, -8, 3,], [-2, 1, 4]];
 
enum form = "[%([%(%d, %)],\n %)]]";
writefln("A = \n" ~ form ~ "\n", a);
writefln("B = \n" ~ form ~ "\n", b);
TMMul_helper!(typeof(a), typeof(b)) result = void;
matrixMul(a, b, result);
writefln("A * B = \n" ~ form, result);
}</lang>