@thi.ng/tensors
Version:
0D/1D/2D/3D/4D tensors with extensible polymorphic operations and customizable storage
30 lines (29 loc) • 704 B
JavaScript
import { peek } from "@thi.ng/arrays/peek";
import { isPlainObject } from "@thi.ng/checks";
import { tensor, Tensor1 } from "./tensor.js";
function range(...args) {
const opts = isPlainObject(peek(args)) ? args.pop() : void 0;
const type = opts?.type ?? "num";
let [from, to, step] = args;
if (to === void 0) {
to = from;
from = 0;
}
step = step ?? (from < to ? 1 : -1);
const data = [];
if (step > 0) {
while (from < to) {
data.push(from);
from += step;
}
} else if (step < 0) {
while (from > to) {
data.push(from);
from += step;
}
}
return tensor(type, [data.length], { ...opts, copy: type !== "num", data });
}
export {
range
};