@thi.ng/tensors
Version:
1D/2D/3D/4D tensors with extensible polymorphic operations and customizable storage
85 lines (84 loc) • 1.64 kB
JavaScript
import { normal as op } from "@thi.ng/random/distributions/normal";
import { top } from "./top.js";
const randDistrib1 = (a, rnd = op(), n = 1) => {
const {
data,
offset,
shape: [sx],
stride: [tx]
} = a;
for (let x = 0; x < sx; x++) data[offset + x * tx] = rnd() * n;
return a;
};
const randDistrib2 = (a, rnd = op(), n = 1) => {
const {
data,
offset,
shape: [sx, sy],
stride: [tx, ty]
} = a;
let ox, x, y;
for (x = 0; x < sx; x++) {
ox = offset + x * tx;
for (y = 0; y < sy; y++) {
data[ox + y * ty] = rnd() * n;
}
}
return a;
};
const randDistrib3 = (a, rnd = op(), n = 1) => {
const {
data,
offset,
shape: [sx, sy, sz],
stride: [tx, ty, tz]
} = a;
let ox, oy, x, y, z;
for (x = 0; x < sx; x++) {
ox = offset + x * tx;
for (y = 0; y < sy; y++) {
oy = ox + y * ty;
for (z = 0; z < sz; z++) {
data[oy + z * tz] = rnd() * n;
}
}
}
return a;
};
const randDistrib4 = (a, rnd = op(), n = 1) => {
const {
data,
offset,
shape: [sx, sy, sz, sw],
stride: [tx, ty, tz, tw]
} = a;
let ox, oy, oz, x, y, z, w;
for (x = 0; x < sx; x++) {
ox = offset + x * tx;
for (y = 0; y < sy; y++) {
oy = ox + y * ty;
for (z = 0; z < sz; z++) {
oz = oy + z * tz;
for (w = 0; w < sw; w++) {
data[oz + w * tw] = rnd() * n;
}
}
}
}
return a;
};
const randDistrib = top(
0,
void 0,
randDistrib1,
randDistrib2,
randDistrib3,
randDistrib4
);
export {
randDistrib,
randDistrib1,
randDistrib2,
randDistrib3,
randDistrib4
};