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.10.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.10.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (595.2 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

interpn-0.10.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (510.6 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

interpn-0.10.0-cp310-abi3-win_amd64.whl (474.8 kB view details)

Uploaded CPython 3.10+Windows x86-64

interpn-0.10.0-cp310-abi3-win32.whl (344.5 kB view details)

Uploaded CPython 3.10+Windows x86

interpn-0.10.0-cp310-abi3-musllinux_1_2_x86_64.whl (670.1 kB view details)

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

interpn-0.10.0-cp310-abi3-musllinux_1_2_i686.whl (690.1 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ i686

interpn-0.10.0-cp310-abi3-musllinux_1_2_armv7l.whl (709.2 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

interpn-0.10.0-cp310-abi3-musllinux_1_2_aarch64.whl (588.5 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

interpn-0.10.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (583.4 kB view details)

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

interpn-0.10.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (581.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

interpn-0.10.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (481.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

interpn-0.10.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (437.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

interpn-0.10.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (504.6 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

interpn-0.10.0-cp310-abi3-macosx_11_0_arm64.whl (473.4 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

interpn-0.10.0-cp310-abi3-macosx_10_12_x86_64.whl (541.5 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for interpn-0.10.0.tar.gz
Algorithm Hash digest
SHA256 984067f1b4df919c2b2157333f49128b610dc1957b42dbef1471d480dc50ae78
MD5 4a7e7cf16a1639e6a66c55a4faf48deb
BLAKE2b-256 0eea424810af54bfc3eb6ad1f2b555de1a49dfe18ceaf652994e1a5c30572f7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6759522cc9a5e51b39bbcb5902149c00a122ebd21b745a0e5f3517c7eed46dc8
MD5 ba57e2a9489989b325e71f163cea124e
BLAKE2b-256 e606190589509913fc5fba34b420845b773e6575aaa056d169376e0f9f11b496

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4a42aa28f7fdee74006f338a1db7c92282a1d16ef00111f3086d54ff3d50182d
MD5 fe5b531911d0ed6d80141f74da3f20bb
BLAKE2b-256 7eb9bae7fc9f0f4384e466b6c28e0a587213886bee393404d6d85e5992de039c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.10.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f26c009d943c4678b53aab45ac1be6b7a9bb4fb624f082b2c1591c0016e74cc7
MD5 d11e65faa57d352849bcdd7e94ef0555
BLAKE2b-256 c080c8e860c86eb6edc02cf4697e0e17c7863f21bcabbed01410b2226630fcae

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for interpn-0.10.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 ed1558a52b8a87f70c6c36fec97993b84e6ea21099aac2c5ebc4e78c4ee94ff4
MD5 d08c670cca905130f67a4c01a12cad99
BLAKE2b-256 e82c9bc4484e8402e27f01522812929156bde685760eb80922c71c3f5ae4cb14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7c06e4a905661cd3b03b50aca9a6a235293e1016c7de69dd21a94f46fb4de26d
MD5 2109d601c81a3a2e50325a34bcdf10d3
BLAKE2b-256 7225db52a1e377a21204bf88577f92039eddd85c2fbadd9ce52302e0ff2672ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 ab1553c960645543dbb8cf5d3fb25bfaa514dda7de4e14219ab2498192d03bf0
MD5 d80543ba725daf46646e3d632e31c1cc
BLAKE2b-256 e105e3097fb4bfd1a26492d42b2427aae13642a3aa6c908b8df470b88f2775ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 56b5a73f9bab7a5956e696cf3c676766331633dc04e6d5be37b9a36598b50bef
MD5 efc088ebde9a2c4d2a8900a4d8db1cc4
BLAKE2b-256 ccd18252f6482b155f2f291e667897bf0c319c8594b3df96de2d8d6d8141c274

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 aa2831239ed91d07db937a87bac023143868431d0464d88c40cbe132925bef88
MD5 7d297c3ece58ff578ff2327d2f0c31f3
BLAKE2b-256 9bb1fb93f9afa3f85ab4a7584ee218583998d38bcf463a6f493613036e834f4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4646056ad3ddb1310d7f4fac79c581ca6c3c0de813ecc15857b81d0175da659f
MD5 fd2fa0b68350f884dada138e6ac0a082
BLAKE2b-256 b91016668bcf014a5c7417ebe27943fb0a199038ee32c9fe8df355e8598a0971

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 14bf11c7e62381e1bb78198f69f282dc86a04e1fd744fb4063f1d3ed80e2536b
MD5 56d6d32d3742405cd8c30db70dcb0b9f
BLAKE2b-256 eeeba9f83cd00e8c82f39fce454b51705ce89c53a9253b1858a4aa6ec6e992d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 20c659032ad34dd882319f1bfc92dc6fa4a1f7a58f5b3a02f9de3b0e5e509f22
MD5 827779fc0be9179c2cd8242a2e27e03e
BLAKE2b-256 2bc26ca1b3a41122b5a928a61d403647a5f7a521c0903ef2371289919edbb7fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 ad91ae3ba735ee379c1b4894be5b02e315715e8f7100266041b08a3da66e8695
MD5 288d02a4a167f27f17f4139b05896eb6
BLAKE2b-256 6ca584269bdd911834775c7e1d629ef83725ece89ef9ed516310b4212ffc8f70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0d6a865fa733cf6a62c5da7f474582b821936b3a541e198ea4619b4367f77d3e
MD5 5de4a4b2f182fb9bc34085efbd27582b
BLAKE2b-256 8fa13cf02bce87b01015f4116775fce706f06cf66e5a494a558e77667af8dbe5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eb502a24c68ca4f0b601ce16226e694660f1229cac253ff2b12cdd59994e35b5
MD5 f8a91f715d4b21d91498b26d67f51d3b
BLAKE2b-256 756b76c790788c325cf0a192bb28e7836edd16f287dfc32420c0c4b4621e203b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for interpn-0.10.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 06c53d563412679733b0fa6b0cffbecb95cdd7e54163712056bc108ea239d60e
MD5 5b12bb6e0ea0f11b01efcae9226e3930
BLAKE2b-256 55d2e9407817efad155c19bae4792f2984f82b366996059165a77b5096d3823f

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