UNPKG

@thi.ng/tensors

Version:

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

41 lines (40 loc) 811 B
import { illegalShape } from "./errors.js"; import { tensor } from "./tensor.js"; const mulV = (out, a, b) => { const { data: adata, offset: oa, shape: [sxa, sya], stride: [txa, tya] } = a; const { data: bdata, offset: ob, shape: [sxb], stride: [txb] } = b; if (sya !== sxb) illegalShape(b.shape); if (out == null) { out = tensor(b.type, [sxa], { storage: b.storage }); } const { data: odata, offset: oo, shape: [sxo], stride: [txo] } = out; if (sxo !== sxa) illegalShape(out.shape); let oax, x, y, sum; for (x = 0; x < sxa; x++) { oax = oa + x * txa; sum = 0; for (y = 0; y < sya; y++) { sum += adata[oax + y * tya] * bdata[ob + y * txb]; } odata[oo + x * txo] = sum; } return out; }; export { mulV };