JavaScript / TypeScript

Install

npm install @flatironinstitute/treeweave

Prebuilt native N-API binaries ship for common Linux, macOS and Windows platforms. Browsers and hosts without a matching native prebuild fall back to the bundled WASM backend.

Usage

import { Treeweave } from "@flatironinstitute/treeweave";

function zeta(x) {
    let y = 0.0;
    for (let k = 1; k <= 1000; ++k) y += Math.pow(k, -x[0]);
    return y;
}

const approx = await Treeweave.fit(zeta, 2.0, 10.0, 1e-10);
console.log(approx.eval(3.5));
approx.free();

Options

Pass a FitOptions object as the fifth argument to Treeweave.fit. Fit options documents the shared fit options. JavaScript spells them in lower camel case, such as tolKind and maxMemoryMib.

const approx = await Treeweave.fit(f, 0.0, 10.0, 1e-10, {
    tolKind: "absolute_max",
    maxDepth: 30,
    maxMemoryMib: 64,
    backend: "native",
});

JavaScript-specific fields:

  • A probe of the callback at the box midpoint infers dim and outDim; set them explicitly when that probe is not enough.

  • dtype defaults to "f64"; use "f32" for single precision.

  • backend defaults to "auto"; pass "native" or "wasm" to force one backend.

  • filename is the path of a plain-text fit file. If the file exists and its header matches the fit, the fit loads it and never calls the callback. Otherwise the fit runs and writes the file. Set outDim to skip the probe call. The option needs Node.js. The WASM backend copies the file through Node.js and throws an error in a browser.

Source build

cmake --preset bindings-js
cmake --build build/bindings-js -j

Use bindings-js-wasm for the browser WASM backend.