Skip to main content

InterpN

Writeup | Repo | Python Docs | Rust Docs

N-dimensional interpolation/extrapolation methods, no-std and no-alloc compatible, prioritizing correctness, performance, and compatibility with memory-constrained environments.

Available as a rust crate and python library.

Features

Feature →
↓ Interpolant Method
Regular
Grid
Rectilinear
Grid
Json
Serialization
Thread Parallelism
Nearest-Neighbor
Linear
Cubic Hermite
Cubic B-spline

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

For larger jobs such as image processing, the non-allocating evaluation pattern results in a nearly-linear thread speedup, limited only by thread spawning overhead and memory bandwidth.

See here for more info about quality-of-fit, throughput, and memory usage.

Installation

pip install interpn

Profile-Guided Optimization

To build the extension with profile-guided optimization, do sh ./scripts/distr_pgo.sh after installing this extra compiler dependency:

rustup component add llvm-tools-preview

An LLVM install matching the version used by rustc is also required for doing PGO; see the ./scripts/distr_pgo.sh or CI workflows for the exact version.

Rust Examples

Regular Grid

use interpn::{multilinear, multicubic};

// Define a grid
let x = [1.0_f64, 2.0, 3.0, 4.0];
let y = [0.0_f64, 1.0, 2.0, 3.0];

// Grid input for rectilinear method
let grids = &[&x[..], &y[..]];

// Grid input for regular grid method
let dims = [x.len(), y.len()];
let starts = [x[0], y[0]];
let steps = [x[1] - x[0], y[1] - y[0]];

// Values at grid points
let z = [2.0; 16];

// Observation points to interpolate/extrapolate
let xobs = [0.0_f64, 5.0];
let yobs = [-1.0, 3.0];
let obs = [&xobs[..], &yobs[..]];

// Storage for output
let mut out = [0.0; 2];

// Do interpolation
multilinear::regular::interpn(&dims, &starts, &steps, &z, &obs, &mut out);
multicubic::regular::interpn(&dims, &starts, &steps, &z, false, &obs, &mut out);

Rectilinear Grid

use interpn::{multilinear, multicubic};

// Define a grid
let x = [1.0_f64, 2.0, 3.0, 4.0];
let y = [0.0_f64, 1.0, 2.0, 3.0];

// Grid input for rectilinear method
let grids = &[&x[..], &y[..]];

// Values at grid points
let z = [2.0; 16];

// Points to interpolate/extrapolate
let xobs = [0.0_f64, 5.0];
let yobs = [-1.0, 3.0];
let obs = [&xobs[..], &yobs[..]];

// Storage for output
let mut out = [0.0; 2];

// Do interpolation
multilinear::rectilinear::interpn(grids, &z, &obs, &mut out).unwrap();
multicubic::rectilinear::interpn(grids, &z, false, &obs, &mut out).unwrap();

Python Examples

Available Methods

import interpn
import numpy as np

# Build grid
x = np.linspace(0.0, 10.0, 5)
y = np.linspace(20.0, 30.0, 4)
grids = [x, y]

xgrid, ygrid = np.meshgrid(x, y, indexing="ij")
zgrid = (xgrid + 2.0 * ygrid)  # Values at grid points

# Grid inputs for true regular grid
dims = [x.size, y.size]
starts = np.array([x[0], y[0]])
steps = np.array([x[1] - x[0], y[1] - y[0]])

# Initialize different interpolators
# Call like `linear_regular.eval([xs, ys])`
linear_regular = interpn.MultilinearRegular.new(dims, starts, steps, zgrid)
cubic_regular = interpn.MulticubicRegular.new(dims, starts, steps, zgrid)
linear_rectilinear = interpn.MultilinearRectilinear.new(grids, zgrid)
cubic_rectilinear = interpn.MulticubicRectilinear.new(grids, zgrid)

Multilinear Interpolation

import interpn
import numpy as np

# Build grid
x = np.linspace(0.0, 10.0, 5)
y = np.linspace(20.0, 30.0, 4)

xgrid, ygrid = np.meshgrid(x, y, indexing="ij")
zgrid = (xgrid + 2.0 * ygrid)  # Values at grid points

# Grid inputs for true regular grid
dims = [x.size, y.size]
starts = np.array([x[0], y[0]])
steps = np.array([x[1] - x[0], y[1] - y[0]])

# Observation points pointed back at the grid
obs = [xgrid.flatten(), ygrid.flatten()]

# Initialize
interpolator = interpn.MultilinearRegular.new(dims, starts, steps, zgrid.flatten())

# Interpolate
out = interpolator.eval(obs)

# Check result
assert np.allclose(out, zgrid.flatten(), rtol=1e-13)

# Serialize and deserialize
roundtrip_interpolator = interpn.MultilinearRegular.model_validate_json(
    interpolator.model_dump_json()
)
out2 = roundtrip_interpolator.eval(obs)

# Check result from roundtrip serialized/deserialized interpolator
assert np.all(out == out2)

License

Licensed under either of

at your option.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

interpn-0.11.2.tar.gz (2.0 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

interpn-0.11.2-cp310-abi3-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.11.2-cp310-abi3-win32.whl (1.1 MB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.11.2-cp310-abi3-musllinux_1_2_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

interpn-0.11.2-cp310-abi3-musllinux_1_2_i686.whl (1.5 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.11.2-cp310-abi3-musllinux_1_2_armv7l.whl (1.4 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.11.2-cp310-abi3-musllinux_1_2_aarch64.whl (1.2 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.11.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

interpn-0.11.2-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.3 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.11.2-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.2 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.11.2-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.1 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.11.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.11.2-cp310-abi3-macosx_11_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.11.2-cp310-abi3-macosx_10_12_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file interpn-0.11.2.tar.gz.

File metadata

  • Download URL: interpn-0.11.2.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for interpn-0.11.2.tar.gz
Algorithm Hash digest
SHA256 e69afe45f674e35c33f535fd63b42dbb74a46bb4ef9bd39e9d86be74ead414ea
MD5 24bf02baa5bc8260039c96e63edc96cd
BLAKE2b-256 6d513f2f80f1dd80d43f0aa83ea00b8c747e5d9a076189c8111aa50d16772591

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: interpn-0.11.2-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for interpn-0.11.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 81864516d4163876c692f344a6f30474af6f3e1aa3c9d1b9c28dc402b6087b0c
MD5 4b61389177ddd5257a61e21f77b96c09
BLAKE2b-256 386706a86d38ddff0d2f477f22259ad331f2ac92a9d66056af01e7305bc77dcf

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-win32.whl.

File metadata

  • Download URL: interpn-0.11.2-cp310-abi3-win32.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for interpn-0.11.2-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 a854b51008b30450a37c720beaf13163beeaf06266a9b37994034e1d63a7143a
MD5 7a5bda34ffd8359c12c2bcda0c3701cd
BLAKE2b-256 6f83fcf4da0c9967e04d419dd19ceb2a6788eef2e83b3144e7cb58391df276b8

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e52d28ba877f0d655bb65fcd1842edfc148f8e1ff4e13bab4474b12fdb51589e
MD5 31d95ad6754b7860818d5d8161b70e9c
BLAKE2b-256 126bda47f11ca0fcaeb2689b300df79068dfe59eb96035c7756572098b0e57bc

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 1899e651d8b3f2e868054573e133cdde0c26dada6c897af2b00da455a4e075cd
MD5 f3b0425fd908632c962279ebe7cd246d
BLAKE2b-256 7aa24339c75a68f2979bfcfaf1c30d5389a221c2250999c86cf49d8263a692c4

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 d4cf1a37c9372ebddc79ad8690edd9e63a07ecea6b9406c358b51ece65084784
MD5 31d5e8eea7de54c70dae2ea80c108dcb
BLAKE2b-256 0e7f6238328ff719ca9781ccba793fb168b4d9117bb40e37177aa0208d066996

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8cc1a67d088a732e69586af048fc82ea99576025a6474034189e7b320e7d4241
MD5 c673d35f3aedd60117c9d94c22d66c13
BLAKE2b-256 9c9e4aa8fd5d1a8e3271cfb14daa242c306dfa52282e45c6b1ff220cdc7349bb

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5210d31dcb9b868fa030239a2ec1e66c58b622860f01b62889176629d8582d2f
MD5 a7402c5e9001fba471bec6cc2a889b85
BLAKE2b-256 0befeb44039eaf064418c23a979c67978089765a4e824cfb43a4897075e7c235

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 06a175b0680bbcec4b9e606a2354c4a4ee1c2117a05a8a71667d0af5416dcbe3
MD5 d577ead9538e330abd7ceb12c43d0cb9
BLAKE2b-256 4bbc7bb88f09fdd2220855f4282d331feb59a7638ea99f7b82a4d715316f454c

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 0e40d7f2bfc184670f1b93733a719d6e1ba01f000427edc0614f0ca649bc5671
MD5 d1ead44610600f3d44f140f7376a5f18
BLAKE2b-256 65d2df7fe7606fdc85cd1613ee6ebab379b55873483075a4812acde1fdb36aa3

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 04787a1032d2dc4f2c5a351b2f32529203013cd1db0f2f39ff5439496ac02c8f
MD5 3fc29ff484737c69cfa8d299195931d7
BLAKE2b-256 d8142cdd820501155061d6b68694a078d18bb110212d34b6ea92c9f49972dbf7

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9568fe99e852012bde1af25cba33b59af16d37707bf849183a268899212663dc
MD5 eec3bb28b5462b44f1339a88491e87c4
BLAKE2b-256 d211e30227ac05c0282b19ec9e9f41b679f853801a5b63113c59074305aec5e2

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33bc41740752f6d41916198c92c37325a1fc52342e391d951dec13074164f7f1
MD5 3297732331c56c6b07f478f7317bf44b
BLAKE2b-256 71f877031c48be55be7faf3ed7f07748c7123c5a0e63530011359c93db086145

See more details on using hashes here.

File details

Details for the file interpn-0.11.2-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.11.2-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fbc883b911140570f63763d449c801ca250b188e82cd74f99c435de6481e9289
MD5 669f7168171bbdbb572e63776672b617
BLAKE2b-256 83949b710054559c1c7e96007960929923396622b91ffcc36e68978a44156087

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page