Matrix multiplication: Difference between revisions

Updated both D entries
(Updated D entry)
(Updated both D entries)
Line 855:
}
return result;
}
 
string prettyPrint(T)(in T[][] M) /*pure nothrow*/ {
return "[" ~ join(map!text(M), ",\n ") ~ "]";
}
 
Line 865 ⟶ 861:
immutable b = [[-3, -8, 3,], [-2, 1, 4]];
 
writeln("Aenum form =\n "[%([%(%d, prettyPrint(a%)], "\n" %)]]";
writelnwritefln("BA = \n", prettyPrint(b),~ form ~ "\n", a);
writelnwritefln("A * B = \n", prettyPrint(matrixMul(a~ form ~ "\n", b)));
writefln("A * B = \n" ~ form, matrixMul(a, b));
}</lang>
{{out}}
Output:
<pre>A =
[[1, 2],
Line 883 ⟶ 880:
[-17, -20, 25],
[-21, -18, 33]]</pre>
===Stronger Statically Typed Version===
===Alternative version===
All array sizes are verified at compile-time (and no matrix is copied),. sameSame output:.
<lang d>import std.stdio, std.string, std.conv, std.numeric,
std.array, std.algorithm, std.traits;
Line 903 ⟶ 900:
result[i][j] = dotProduct(ai, aux);
}
}
 
string prettyPrint(T, size_t m, size_t n)(const ref T[m][n] M) {
return "[" ~ array(map!text(M[])).join(",\n ") ~ "]";
}
 
void main() {
constimmutable int[2][3] a = [[1, 2], [3, 4], [3, 6]];
constimmutable int[3][2] b = [[-3, -8, 3,], [-2, 1, 4]];
 
writeln("Aenum form =\n "[%([%(%d, prettyPrint(a%)], "\n" %)]]";
writelnwritefln("BA = \n", prettyPrint(b),~ form ~ "\n", a);
writefln("B = \n" ~ form ~ "\n", b);
TMMul!(typeof(a), typeof(b)) result; // = void;
matrixMul(a, b, result);
writelnwritefln("A * B = \n" ~ form, prettyPrint(result));
}</lang>