Installation
Most languages and systems have a precompiled binary. Prefer it. Each section below gives the shortest install path and a small example. The language guides cover the other install routes, the options, source builds and larger examples.
Python
pip install treeweave
import math, treeweave
# Fit sin(x) on [0, 1] syntax is fit(callback, lower_bound, upper_bound, tol)
f = treeweave.fit(lambda x: math.sin(x[0]), 0.0, 1.0, tol=1e-10)
# Evaluate f on (0.5) and print the result.
print(f(0.5))
See Python.
C++
# Download treeweave at configure time and link the header-only C++ target.
CPMAddPackage("gh:DiamonDinoia/treeweave@stable")
add_executable(my_app example.cpp)
target_link_libraries(my_app PRIVATE treeweave::treeweave)
#include <treeweave/treeweave.hpp>
#include <cmath>
#include <iostream>
int main() {
// Fit sin(x) on [0, 1] syntax is fit(callback, lower_bound, upper_bound, tolerance)
const auto f = treeweave::fit([](double x) { return std::sin(x); }, 0.0, 1.0, 1e-10);
// Evaluate f on (0.5) and print the result.
std::cout << f(0.5) << "\n";
}
No CMake:
URL="https://github.com/DiamonDinoia/treeweave/releases/latest/download/treeweave-cxx-headers.tar.gz"
curl -fLO "$URL" || wget "$URL"
tar xzf treeweave-cxx-headers.tar.gz
See C++.
C
VER=stable
PLATFORM=linux-x86_64
URL="https://github.com/DiamonDinoia/treeweave/releases/download/${VER}/treeweave-${VER}-${PLATFORM}.tar.gz"
curl -fLO "$URL" || wget "$URL"
tar xzf "treeweave-${VER}-${PLATFORM}.tar.gz"
#include <math.h>
#include <stdio.h>
#include <treeweave.h>
static void fn(const double *x, double *y, void *ctx) {
(void)ctx;
/* Callback evaluated during fitting. */
y[0] = sin(x[0]);
}
int main(void) {
double a = 0.0, b = 1.0;
/* Fit sin(x) on [0, 1] syntax is treeweave_fit(callback, input_dim, output_dim, lower, upper, tolerance, context, options). */
treeweave_t f = treeweave_fit(fn, 1, 1, &a, &b, 1e-10, NULL, NULL);
/* Evaluate f on (0.5) and print the result. */
printf("%g\n", treeweave_eval_1d(f, 0.5));
treeweave_free(f);
}
gcc example.c -Iinclude -Llib -ltreeweave_c -lm -o example
LD_LIBRARY_PATH=lib ./example
On Windows the zip puts treeweave_c.dll in bin\ and the import library
treeweave_c.lib in lib\. Windows has no RPATH, so the loader finds
the DLL only through PATH:
cl example.c /I include /link /LIBPATH:lib treeweave_c.lib
set PATH=%CD%\bin;%PATH%
example.exe
A find_package(treeweave) consumer hits the same trap. Linking succeeds
without PATH, and the executable then fails to start. Put bin\ on
PATH, copy the DLL next to the executable, or link
treeweave::treeweave_c_static.
See C.
Julia
using Pkg
Pkg.add(url="https://github.com/DiamonDinoia/treeweave",
subdir="bindings/julia/Treeweave")
using Treeweave
# Fit sin(x) on [0, 1] syntax is fit(callback, lower_bound, upper_bound, tolerance)
f = fit(sin, 0.0, 1.0, 1e-10)
# Evaluate f on (0.5) and print the result.
println(f(0.5))
See Julia.
MATLAB
With mip, from the mip-org/labs channel:
mip install --channel mip-org/labs treeweave
mip load treeweave
Or download the MATLAB bundle directly:
VER=stable
PLATFORM=linux-x64
URL="https://github.com/DiamonDinoia/treeweave/releases/download/${VER}/treeweave-matlab-${VER}-${PLATFORM}.tar.gz"
curl -fLO "$URL" || wget "$URL"
tar xzf "treeweave-matlab-${VER}-${PLATFORM}.tar.gz"
% Add the extracted MEX bundle.
addpath('treeweave-matlab-stable-linux-x64')
% Fit sin(x) on [0, 1] syntax is treeweave(callback, lower_bound, upper_bound, tolerance, name/value options).
f = treeweave(@(x) sin(x(1)), [0], [1], 1e-10, 'dim', 1, 'out_dim', 1);
% Evaluate f on (0.5) and print the result.
disp(f(0.5));
delete(f);
See MATLAB / Octave.
Octave
VER=stable
URL="https://github.com/DiamonDinoia/treeweave/archive/refs/tags/${VER}.tar.gz"
curl -fL "$URL" -o "treeweave-${VER}-source.tar.gz" || wget -O "treeweave-${VER}-source.tar.gz" "$URL"
tar xzf "treeweave-${VER}-source.tar.gz"
cd "treeweave-${VER}"
cmake --preset bindings-octave
cmake --build build/bindings-octave -j
% Fit sin(x) on [0, 1] syntax is treeweave(callback, lower_bound, upper_bound, tolerance, name/value options).
f = treeweave(@(x) sin(x(1)), [0], [1], 1e-10, 'dim', 1, 'out_dim', 1);
% Evaluate f on (0.5) and print the result.
disp(f(0.5));
delete(f);
See MATLAB / Octave.
Fortran
cmake --preset bindings-fortran
cmake --build build/bindings-fortran -j
subroutine fn(x, y, context) bind(C)
use, intrinsic :: iso_c_binding
real(c_double), intent(in) :: x(*)
real(c_double), intent(out) :: y(*)
type(c_ptr), value :: context
! Callback evaluated during fitting.
y(1) = sin(x(1))
end subroutine fn
program example
use, intrinsic :: iso_c_binding
use treeweave
interface
subroutine fn(x, y, context) bind(C)
use, intrinsic :: iso_c_binding
real(c_double), intent(in) :: x(*)
real(c_double), intent(out) :: y(*)
type(c_ptr), value :: context
end subroutine fn
end interface
real(c_double) :: a(1) = [0.0_c_double], b(1) = [1.0_c_double]
real(c_double) :: x(1) = [0.5_c_double], y(1)
type(c_ptr) :: h
! Fit sin(x) on [0, 1] syntax is treeweave_fit(callback, input_dim, output_dim, lower, upper, tolerance, context, options).
h = treeweave_fit(c_funloc(fn), 1_c_int, 1_c_int, a, b, 1.0e-10_c_double, c_null_ptr, c_null_ptr)
! Evaluate f on (0.5) and print the result.
y = treeweave_eval(h, x)
print *, y(1)
h = treeweave_free(h)
end program example
See Fortran.
JavaScript / TypeScript
npm install @flatironinstitute/treeweave
import { Treeweave } from "@flatironinstitute/treeweave";
// Fit sin(x) on [0, 1] syntax is fit(callback, lower_bound, upper_bound, tolerance)
const f = await Treeweave.fit((x) => Math.sin(x[0]), 0.0, 1.0, 1e-10);
// Evaluate f on (0.5) and print the result.
console.log(f.eval(0.5));
f.free();
CMake
For profiles, build options, targets, installed packages, and source builds, see CMake.