InterpN
Repo | Python Docs | Rust Docs
N-dimensional interpolation/extrapolation methods, no-std and no-alloc compatible, prioritizing correctness, performance, and compatiblity with memory-constrained environments.
Available as a rust crate and python library.
These methods perform zero allocation when evaluated (except, optionally, for the output). Because of this, they have minimal per-call overhead, and are particularly effective when examining small numbers of observation points. See the performance page for detailed benchmarks.
Features
| Feature →↓ Interpolant Method | RegularGrid | RectilinearGrid | JsonSerialization |
|---|---|---|---|
| Nearest-Neighbor | ✅ | ✅ | ✅ |
| Linear | ✅ | ✅ | ✅ |
| Cubic | ✅ | ✅ | ✅ |
The methods provided here, while more limited in scope than scipy's,
- are significantly faster under most conditions
- use almost no RAM (and perform no heap allocations at all)
- produce significantly improved floating-point error (by several orders of magnitude)
- are json-serializable using Pydantic
- can also be used easily in web and embedded applications via the Rust library
- are permissively licensed
See here for more info about quality-of-fit, throughput, and memory usage.
Installation
Profile-Guided Optimization
To build the extension with profile-guided optimization using pre-built profiles, do sh ./scripts/distr_pgo_install.sh.
You can also generate your own PGO profiles like sh ./scripts/distr_pgo_profile.sh.
after installing this extra compiler dependency:
Rust Examples
Regular Grid
use ;
// Define a grid
let x = ;
let y = ;
// Grid input for rectilinear method
let grids = &;
// Grid input for regular grid method
let dims = ;
let starts = ;
let steps = ;
// Values at grid points
let z = ;
// Observation points to interpolate/extrapolate
let xobs = ;
let yobs = ;
let obs = ;
// Storage for output
let mut out = ;
// Do interpolation
interpn;
interpn;
Rectilinear Grid
use ;
// Define a grid
let x = ;
let y = ;
// Grid input for rectilinear method
let grids = &;
// Values at grid points
let z = ;
// Points to interpolate/extrapolate
let xobs = ;
let yobs = ;
let obs = ;
// Storage for output
let mut out = ;
// Do interpolation
interpn.unwrap;
interpn.unwrap;
Python Examples
Available Methods
# Build grid
=
=
=
, =
= # Values at grid points
# Grid inputs for true regular grid
=
=
=
# Initialize different interpolators
# Call like `linear_regular.eval([xs, ys])`
=
=
=
=
Multilinear Interpolation
# Build grid
=
=
, =
= # Values at grid points
# Grid inputs for true regular grid
=
=
=
# Observation points pointed back at the grid
=
# Initialize
=
# Interpolate
=
# Check result
assert
# Serialize and deserialize
=
=
# Check result from roundtrip serialized/deserialized interpolator
assert
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.