Matrix multiplication: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added REXX language.)
Line 714:
 
=={{header|D}}==
<lang d>import std.stdio:, writeflnstd.string;
import std.string: format, join;
 
T[][]pure matrixMulbool isRectangular(T)(T[][] m1,const T[][] m2matrix) {
boolforeach isRectangular(T[][]row; matrix) {
foreachif (row;.length != matrix[0].length)
ifreturn (row.length != matrix[0].length)false;
return falsetrue;
return true;
}
 
T[][] result;
if (isRectangular(m1) && isRectangular(m2) && m1[0].length == m2.length) {
result = new T[][](m1.length, m2[0].length);
 
T[][] matrixMul(T)(T[][] m1, T[][] m2)
in {
if assert(isRectangular(m1) && isRectangular(m2) && m1[0].length == m2.length) {
m1[0].length == m2.length);
} body {
auto result = new T[][](m1.length, m2[0].length);
foreach (i, m1_row_i; m1)
forforeach (int j; j0 <.. m2[0].length; j++) {
T aux = 0;
foreach (k, m2_row_k; m2)
Line 736:
result[i][j] = aux;
}
} else return result;
}
throw new Exception("matrixMul Error");
return result;
 
string prettyPrint(T)(T[][] matrix) {
Line 749 ⟶ 747:
 
void main() {
float[][]enum a = [[1, 2], [3, 4], [3, 6]];
float[][]enum b = [[-3, -8, 3,], [-2, 1, 4]];
writeflnwriteln("\nA * BA = \n", prettyPrint(matrixMul(a, b)));
 
writeflnwriteln("A\nB = \n", prettyPrint(ab));
writeflnwriteln("\nBnA * B = \n", prettyPrint(matrixMul(a, b)));
writefln("\nA * B = \n", prettyPrint(matrixMul(a, b)));
}</lang>
Output:
<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>
 
=={{header|ELLA}}==
Sample originally from ftp://ftp.dra.hmg.gb/pub/ella (a now dead link) - Public release.