Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -0,0 +1,21 @@
|
|||
proc *(a:[], b:[]) {
|
||||
|
||||
if (a.eltType != b.eltType) then
|
||||
writeln("type mismatch: ", a.eltType, " ", b.eltType);
|
||||
|
||||
var ad = a.domain.dims();
|
||||
var bd = b.domain.dims();
|
||||
var (arows, acols) = ad;
|
||||
var (brows, bcols) = bd;
|
||||
if (arows != bcols) then
|
||||
writeln("dimension mismatch: ", ad, " ", bd);
|
||||
|
||||
var c:[{arows, bcols}] a.eltType = 0;
|
||||
|
||||
for i in arows do
|
||||
for j in bcols do
|
||||
for k in acols do
|
||||
c(i,j) += a(i,k) * b(k,j);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
var m1:[{1..2, 1..2}] int;
|
||||
m1(1,1) = 1; m1(1,2) = 2;
|
||||
m1(2,1) = 3; m1(2,2) = 4;
|
||||
writeln(m1);
|
||||
|
||||
var m2:[{1..2, 1..2}] int;
|
||||
m2(1,1) = 2; m2(1,2) = 3;
|
||||
m2(2,1) = 4; m2(2,2) = 5;
|
||||
writeln(m2);
|
||||
|
||||
var m3 = m1 * m2;
|
||||
writeln(m3);
|
||||
|
||||
var m4:[{1..2, 1..3}] int;
|
||||
m4(1, 1) = 1; m4(1, 2) = 2; m4(1, 3) = 3;
|
||||
m4(2, 1) = 4; m4(2, 2) = 5; m4(2, 3) = 6;
|
||||
writeln(m4);
|
||||
|
||||
var m5:[{1..3, 1..2}] int;
|
||||
m5(1, 1) = 6; m5(1, 2) = -1;
|
||||
m5(2, 1) = 3; m5(2, 2) = 2;
|
||||
m5(3, 1) = 0; m5(3, 2) = -3;
|
||||
writeln(m5);
|
||||
|
||||
writeln(m4 * m5);
|
||||
34
Task/Matrix-multiplication/D/matrix-multiplication-1.d
Normal file
34
Task/Matrix-multiplication/D/matrix-multiplication-1.d
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import std.stdio, std.string, std.conv, std.numeric,
|
||||
std.array, std.algorithm;
|
||||
|
||||
bool isRectangular(T)(in T[][] M) pure nothrow {
|
||||
return M.all!(row => row.length == M[0].length);
|
||||
}
|
||||
|
||||
T[][] matrixMul(T)(in T[][] A, in T[][] B) pure nothrow
|
||||
in {
|
||||
assert(A.isRectangular && B.isRectangular &&
|
||||
!A.empty && !B.empty && A[0].length == B.length);
|
||||
} body {
|
||||
auto result = new T[][](A.length, B[0].length);
|
||||
auto aux = new T[B.length];
|
||||
|
||||
foreach (immutable j; 0 .. B[0].length) {
|
||||
foreach (immutable k, const row; B)
|
||||
aux[k] = row[j];
|
||||
foreach (immutable i, const ai; A)
|
||||
result[i][j] = dotProduct(ai, aux);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable a = [[1, 2], [3, 4], [3, 6]];
|
||||
immutable b = [[-3, -8, 3,], [-2, 1, 4]];
|
||||
|
||||
immutable form = "[%([%(%d, %)],\n %)]]";
|
||||
writefln("A = \n" ~ form ~ "\n", a);
|
||||
writefln("B = \n" ~ form ~ "\n", b);
|
||||
writefln("A * B = \n" ~ form, matrixMul(a, b));
|
||||
}
|
||||
16
Task/Matrix-multiplication/D/matrix-multiplication-2.d
Normal file
16
Task/Matrix-multiplication/D/matrix-multiplication-2.d
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import std.stdio, std.range, std.array, std.numeric, std.algorithm;
|
||||
|
||||
T[][] matMul(T)(in T[][] A, in T[][] B) /*pure nothrow*/ {
|
||||
const Bt = B[0].length.iota.map!(i=> B.transversal(i).array).array;
|
||||
return A.map!(a => Bt.map!(b => a.dotProduct(b)).array).array;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable a = [[1, 2], [3, 4], [3, 6]];
|
||||
immutable b = [[-3, -8, 3,], [-2, 1, 4]];
|
||||
|
||||
immutable form = "[%([%(%d, %)],\n %)]]";
|
||||
writefln("A = \n" ~ form ~ "\n", a);
|
||||
writefln("B = \n" ~ form ~ "\n", b);
|
||||
writefln("A * B = \n" ~ form, matMul(a, b));
|
||||
}
|
||||
30
Task/Matrix-multiplication/D/matrix-multiplication-3.d
Normal file
30
Task/Matrix-multiplication/D/matrix-multiplication-3.d
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import std.stdio, std.string, std.conv, std.numeric, std.array,
|
||||
std.algorithm, std.traits;
|
||||
|
||||
alias TMMul_helper(M1, M2) = Unqual!(ForeachType!(ForeachType!M1))
|
||||
[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);
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
julia> [1 2 3 ; 4 5 6] * [1 2 ; 3 4 ; 5 6] # product of a 2x3 by a 3x2
|
||||
2x2 Array{Int64,2}:
|
||||
22 28
|
||||
49 64
|
||||
|
||||
julia> [1 2 3] * [1,2,3] # product of a row vector by a column vector
|
||||
1-element Array{Int64,1}:
|
||||
14
|
||||
Loading…
Add table
Add a link
Reference in a new issue