Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Element-wise-operations/D/element-wise-operations-1.d
Normal file
15
Task/Element-wise-operations/D/element-wise-operations-1.d
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import std.stdio, std.typetuple, std.traits;
|
||||
|
||||
T[][] elementwise(string op, T, U)(in T[][] A, in U B) {
|
||||
auto R = new typeof(return)(A.length, A[0].length);
|
||||
foreach (r, row; A)
|
||||
R[r][] = mixin("row[] " ~ op ~ (isNumeric!U ? "B" : "B[r][]"));
|
||||
return R;
|
||||
}
|
||||
|
||||
void main() {
|
||||
const M = [[3, 5, 7], [1, 2, 3], [2, 4, 6]];
|
||||
foreach (op; TypeTuple!("+", "-", "*", "/", "^^"))
|
||||
writefln("%s:\n[%([%(%d, %)],\n %)]]\n\n[%([%(%d, %)],\n %)]]\n",
|
||||
op, elementwise!op(M, 2), elementwise!op(M, M));
|
||||
}
|
||||
36
Task/Element-wise-operations/D/element-wise-operations-2.d
Normal file
36
Task/Element-wise-operations/D/element-wise-operations-2.d
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import std.stdio, std.typetuple, std.traits;
|
||||
|
||||
T[][] elementwise(string op, T, U)(in T[][] A, in U B)
|
||||
@safe pure nothrow
|
||||
if (isNumeric!U || (isArray!U && isArray!(ForeachType!U) &&
|
||||
isNumeric!(ForeachType!(ForeachType!U)))) {
|
||||
static if (!isNumeric!U)
|
||||
assert(A.length == B.length);
|
||||
if (!A.length)
|
||||
return null;
|
||||
auto R = new typeof(return)(A.length, A[0].length);
|
||||
|
||||
foreach (immutable r, const row; A)
|
||||
static if (isNumeric!U) {
|
||||
R[r][] = mixin("row[] " ~ op ~ "B");
|
||||
} else {
|
||||
assert(row.length == B[r].length);
|
||||
R[r][] = mixin("row[] " ~ op ~ "B[r][]");
|
||||
}
|
||||
|
||||
return R;
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum scalar = 2;
|
||||
enum matFormat = "[%([%(%d, %)],\n %)]]\n";
|
||||
immutable matrix = [[3, 5, 7],
|
||||
[1, 2, 3],
|
||||
[2, 4, 6]];
|
||||
|
||||
foreach (immutable op; TypeTuple!("+", "-", "*", "/", "^^")) {
|
||||
writeln(op, ":");
|
||||
writefln(matFormat, elementwise!op(matrix, scalar));
|
||||
writefln(matFormat, elementwise!op(matrix, matrix));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue