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.0.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.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (528.8 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

interpn-0.9.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (479.9 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

interpn-0.9.0-cp310-abi3-win_amd64.whl (500.1 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.9.0-cp310-abi3-win32.whl (293.2 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.9.0-cp310-abi3-musllinux_1_2_x86_64.whl (580.9 kB view details)

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

interpn-0.9.0-cp310-abi3-musllinux_1_2_i686.whl (615.0 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.9.0-cp310-abi3-musllinux_1_2_armv7l.whl (652.0 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.9.0-cp310-abi3-musllinux_1_2_aarch64.whl (530.5 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.9.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (563.8 kB view details)

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

interpn-0.9.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (475.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.9.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (396.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.9.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (380.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.9.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (500.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.9.0-cp310-abi3-macosx_11_0_arm64.whl (494.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.9.0-cp310-abi3-macosx_10_12_x86_64.whl (551.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: interpn-0.9.0.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.0.tar.gz
Algorithm Hash digest
SHA256 ee73ad75bd48b487b4563d7882a5d8bc1bc66437c3a7ac7f67778407a4406d15
MD5 98df8d972aa81ec566a29761aace3d3f
BLAKE2b-256 ce74e6c944575a3d7fd23ccdb7233026d9d5e67b359353490d3b6f8c03799e9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 727d05e10b117d0d3a971e3db350a27da05066e0fca7a598438316ae4407a246
MD5 f5769482f9bf1881794456632eace071
BLAKE2b-256 91ea76f08e1ed4555e3fdefdd764de96d9c0b0770e09a74bfb015ce88a2ff99f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7c9fa42f5e301cc1c1aec9aa4ca56526fd125c89771fa186a5d957543f9ebf88
MD5 c8eb3a51e6bf3f2d9d2f3f7f00c409b2
BLAKE2b-256 a04d2cc7fe23c15ee61a923fb8a14cb39354e85b41008c324d807cc999200a39

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interpn-0.9.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 500.1 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.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2ecc13dfdcae63e4de1bbd4fe0ae117fea99ea944f344176fd7041349d47d88b
MD5 943c906db8f46ab86a785c04c18cadd0
BLAKE2b-256 deaf683fdfb7101040f2d530741df9f19bd718b794557b6e902b6ac96e53cfda

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interpn-0.9.0-cp310-abi3-win32.whl
  • Upload date:
  • Size: 293.2 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.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 5d17259d978831fe445be3cc38a165cb15f72030e284d883c85056cd6cb3058a
MD5 796626ccae10298d71ac4c9960ab0035
BLAKE2b-256 af878c1823bc9fd0311c40e797ddd5c53548d8a130832b1ffafc8f2b59d91945

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 bdb925a2ae1948db6a32d355f805b51c233f0e9b199237a2886de7cc77391dcc
MD5 43ac56894f04448a8709e5f6d7d42baf
BLAKE2b-256 58f59b63c76571e50f940291e87b3719d0de29eaac1d7afdb174c03594c4accb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 7a8c7b7fead19eeb721c7798c70a339bc3c0d747ca3d8caef8795c2cd478b1ee
MD5 ee37598aef07ce7774a85906f025bdcf
BLAKE2b-256 130b6f1963963310e2b638b78388e306cad4965a16104d48c2cfe2a3a522e74a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 b498e8c8aa981f30e3323d411f10c6956dff88202b2ae37fc7a85baf6328ffb1
MD5 cb7dd37d48aedf91f52d11a73787dc8b
BLAKE2b-256 a9dbc099763a4fa085ae5e582acf6a99d282de5d1ff45d971df59992a9a8c18c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 59986ab4c1bd290fd7eab472c91146adaa1145205bf27c7c02083d253aebc1ff
MD5 f92699a8d7ec8bd26ee44b253f5f6e22
BLAKE2b-256 83ea632c69f3903711a26e710f4c526f7422ebf19250df5dc91fee8300130441

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69ef6385dc5a2574aee9a711abf58418a858d92a32ce193dbf0d8d3f89f0bd37
MD5 810ec25b3522f07e32639b8e7472012b
BLAKE2b-256 06665b71b1b4d8bddbe8e3c1eadde583ec163fb89d7cb5170a0d8942818c4255

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 b00d69d4418e83d63f84fda534b2de7c74d42b25c17f73ac6c62845ea795851a
MD5 d8866d54d414996798788bf847c162d7
BLAKE2b-256 a81e50940f25b907cc496fca7f48134e2601304687daf7343787bec415e039b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 c10ad5746f5444b908219672f26800fc468439bf2aef3590e64713078b2f1334
MD5 dd6e8cc0a054e367183817b78a3b0297
BLAKE2b-256 f21b8a52b79c2b0309a3930071321b579d1b0f57f3fe02283999e22445f4ed04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 cd6d031ea727b808a8c99e2c6c8bdaeb5587265dffbcd0cc91bc16b345a0e9d4
MD5 d8de48a71382b6f111e394e9b860e5d3
BLAKE2b-256 afc102dc1cff2cc2bfda1573f8f7459fef1a350f1b85f904737a2e9cdd187025

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 99f4b78c8ff62b9a4d92931027732cf841073b8df3ec4ee273b6ae8c88a9e1c3
MD5 b21a78906135202043069eac258eaddf
BLAKE2b-256 806209851aef4c4970dfc41fa52b0e47d757681387c96c4d64c96883e6d466ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ed52556f30a1c01f0c7caaa762620353cb10e809655ab2cd622d27b46c8c77a7
MD5 ab4c33497b6402fd3d665e233e7a8808
BLAKE2b-256 27a6399076d5d0a5f117b14c11deb0f33cc03e97982978867b4a57e1f76e3bdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.9.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3e4c52c8370d8eb182937be7459da2b4b54b1da77e68bf48e2bc4f6b06be1c04
MD5 982c08b05d00315861f471d421417afe
BLAKE2b-256 5ba7ebee44b21749483c2de1d2fddad1947b3a1e6beca9d58b26b825a4ce9fc5

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