Skip to main content

N-dimensional interpolation/extrapolation methods

Project description

InterpN

Writeup | 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.

Features

Feature →
↓ Interpolant Method
Regular
Grid
Rectilinear
Grid
Json
Serialization
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, embedded and gpu applications via the Rust library
  • are permissively licensed

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

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.

Project details


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.9.1.tar.gz (1.4 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.9.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (521.6 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

interpn-0.9.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (462.0 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

interpn-0.9.1-cp310-abi3-win_amd64.whl (448.5 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.9.1-cp310-abi3-win32.whl (313.9 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.9.1-cp310-abi3-musllinux_1_2_x86_64.whl (612.3 kB view details)

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

interpn-0.9.1-cp310-abi3-musllinux_1_2_i686.whl (634.6 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.9.1-cp310-abi3-musllinux_1_2_armv7l.whl (658.6 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.9.1-cp310-abi3-musllinux_1_2_aarch64.whl (538.3 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.9.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (524.4 kB view details)

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

interpn-0.9.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (522.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.9.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (412.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.9.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (386.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.9.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (453.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.9.1-cp310-abi3-macosx_11_0_arm64.whl (440.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.9.1-cp310-abi3-macosx_10_12_x86_64.whl (515.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for interpn-0.9.1.tar.gz
Algorithm Hash digest
SHA256 50b4eb4475a9b1c5636d52de8d74bc8a33821c26b1b4e8067109812b54a8646e
MD5 b7c13410d1de6e2b4cee33116d920d9d
BLAKE2b-256 5f97998a383c70a71e9f85c6a189459f768117ee1912757367cade51e6278951

See more details on using hashes here.

File details

Details for the file interpn-0.9.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for interpn-0.9.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8c019eb47763a0e522aba19eb9889b3ee237e1465e77e859c95d70b80c2d5531
MD5 85a0552bdd9d678ca706c457a4bf0da3
BLAKE2b-256 5cd65f00e4cd70fd8298ea51b95a4341591e2becfc258b729fa3fb4888636a05

See more details on using hashes here.

File details

Details for the file interpn-0.9.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for interpn-0.9.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 dc9fc8bab621894172c1becbe16aff8addd41355c1b68d3621acbefdb331dc2b
MD5 806500275b28e5c005f4dea9a69803be
BLAKE2b-256 88c006d8bdd131ef9162b44d03aa07bc102cd0a86f38731d859ec9624da57e6a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interpn-0.9.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 448.5 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for interpn-0.9.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0fa4d7a6104084e84f1cc3dfbc51d4ef12ce46d18b0dec2c50c916f92f615f23
MD5 c5e8c30afe3f98f26d3101761bda38f6
BLAKE2b-256 d1acc732e82451e41c71e18b61af2a3b39971d75821b64a32247489ab33397d3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interpn-0.9.1-cp310-abi3-win32.whl
  • Upload date:
  • Size: 313.9 kB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for interpn-0.9.1-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 b50eb92d9f9565ca03d191f3a07f9c385cdf5fe51dbfb6e635774de1c5d7159f
MD5 efea859dc9b22117472325a5f7963632
BLAKE2b-256 ca45661727205295d12910aa9a446ecfc82017254211afb27574b677397ce560

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 be9a08eb102c8ba2a602bfc67d223a1857606725281fa0c0c910a98e437f42a6
MD5 120d245d80bfad9ac1021cdfca1229e3
BLAKE2b-256 d7357821d093c91e34d5cd4190b4a060853bb96cf1e21110954591d3c1c9371a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 491cac4f3298a1f53758d6a27580e3e64099ff9a0b10a44f4445a94efd34157f
MD5 f204795e76054fc964675dac78f4c917
BLAKE2b-256 2d2483f1219502c87694929bc5067207ad5b264822d2d2772742100dd0b9ccc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 7156b31c66d5969be2702cb55c00bb24b28e31cf9d6e06b5446bac9ccaa11fc2
MD5 c3944818569d8fa77d3857394a933ae8
BLAKE2b-256 e65d89f47455bb85b08452cac3d757e698685f2731fcf6c03be4c46da1f92d49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 627858cb927ecc0fd483903b292b131d63f2983d1ba2a67140f2361407fc4466
MD5 a2a29a303152514936a5eedf17fcaf42
BLAKE2b-256 323ecd3541c423a1197d08574fabf12acfbedd2a1c5326e8b5c108b8ae16ecc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 82b29041179f6b6497925ee3801a89dd55c4a2d1e56a667f2776a14a5f121a9c
MD5 35d746d1d29656100287e822183adeb1
BLAKE2b-256 0a17d8d7c17805d3a7616c10ced422e6987c6186bb3ff840a8fafd6744bd999d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 e8aeac78d83f4111a0130ebc46d7c2ee06b43fa7bfa3e73762065276840f3b18
MD5 73f34565e007c7358de9cdb1ba5ef9f5
BLAKE2b-256 685e8433f80f9392dd59192fc69896d5aa9aec190056423aa58f523c85563a57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 8e957aeea2e49e83ecfafa31ae92b8f6a10cb38ac37e9d300af6c9b81f8976ea
MD5 53940d4c29dafd1b6003d486b41c4e53
BLAKE2b-256 667b38e739029d6a4ea1a908bbc5337eff10a25bdc2e8e901aa4175f9431a3c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 b7dc98ce9a44871812b28090994c661af519b670f820db7cc04d259ab1e57dc5
MD5 ed030b525c38a2b5ef20cb583a7da88c
BLAKE2b-256 79ac5003b0931c04a24772ba9c4c350615ba49b4749c30cafe7aca61936390c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 13e280297d45aca5605b0a8e60e86d733a62fcc830121b0d2b146182bc2d64ec
MD5 6adb23886b23c298b627fb36f35daf3f
BLAKE2b-256 5ba805fde7d1f20254a7db4cd0b8222fd7fa048654f8db9cb2fe34e4da6b20e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ace040ca929b85d900541bd8cb9a26e6476938aa5117e691acd756120d9686e0
MD5 87a6f91a27b9c3e52b5cc4aa9178fa52
BLAKE2b-256 213ee93804c97460114ae946356737935b3fcd8f6475f1352c10e1e50a6c7b5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 aed1427d19ea835b750afc66cd8bf06bbf01020d0cd21d3293c7cb50598aad70
MD5 18feb4bfbf72dc2f1243336f01829663
BLAKE2b-256 43600dd607fc58a3ec8d09b8412bf538fae883fa3948881eb3249637c5d996fa

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