UNPKG

@thi.ng/tensors

Version:

0D/1D/2D/3D/4D tensors with extensible polymorphic operations and customizable storage

32 lines (31 loc) 647 B
import { ensureShape } from "./errors.js"; import { Tensor1 } from "./tensor.js"; import { sum } from "./sum.js"; const integrate = (out, a, fn = sum) => { const { shape, dim } = a; const odim = shape[shape.length - 1]; if (!out) { out = new Tensor1( a.type, a.storage, a.storage.alloc(odim), [odim], [1] ); } ensureShape(out, [odim]); const { data: odata, stride: [tx], offset: ox } = out; const select = new Array(dim).fill(-1); for (let i = 0; i < odim; i++) { select[dim - 1] = i; odata[ox + i * tx] = fn(a.pick(select)); } return out; }; export { integrate };