Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View 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));
}

View 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));
}
}