Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.range, std.array, std.numeric, std.algorithm;
|
||||
|
||||
T[][] matMul(T)(in T[][] A, in T[][] B) /*pure nothrow*/ {
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,18 @@
|
|||
import std.stdio, std.string, std.conv, std.numeric, std.array,
|
||||
std.algorithm, std.traits;
|
||||
import std.stdio, std.range, std.numeric, std.algorithm;
|
||||
|
||||
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);
|
||||
}
|
||||
T[][] matMul(T)(immutable T[][] A, immutable T[][] B) pure nothrow {
|
||||
immutable Bt = B[0].length.iota.map!(i=> B.transversal(i).array)
|
||||
.array;
|
||||
return A.map!((immutable T[] a) => Bt.map!(b => a.dotProduct(b))
|
||||
.array).array;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable int[2][3] a = [[1, 2], [3, 4], [3, 6]];
|
||||
immutable int[3][2] b = [[-3, -8, 3,], [-2, 1, 4]];
|
||||
immutable a = [[1, 2], [3, 4], [3, 6]];
|
||||
immutable b = [[-3, -8, 3,], [-2, 1, 4]];
|
||||
|
||||
enum form = "[%([%(%d, %)],\n %)]]";
|
||||
immutable 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);
|
||||
writefln("A * B = \n" ~ form, matMul(a, b));
|
||||
}
|
||||
|
|
|
|||
29
Task/Matrix-multiplication/D/matrix-multiplication-4.d
Normal file
29
Task/Matrix-multiplication/D/matrix-multiplication-4.d
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import std.stdio, std.string, std.numeric, 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 i, const ref bi; B)
|
||||
aux[i] = bi[j];
|
||||
foreach (immutable i, const ref 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);
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
def matrix_mult(a, b)
|
||||
a.map do |ar|
|
||||
b.transpose.map do |bc|
|
||||
ar.zip(bc).map {|x,y| x*y}.inject {|z,w| z+w}
|
||||
ar.zip(bc).map(&:*).inject(&:+)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue