#multi-dimensional #interpolation #numerical #linear #multilinear

ninterp

Numerical interpolation for N-dimensional rectilinear grids

16 releases (5 breaking)

new 0.6.2 Mar 21, 2025
0.5.2 Mar 12, 2025
0.1.0 Nov 27, 2024

#168 in Math

Download history 64/week @ 2024-11-29 121/week @ 2024-12-06 49/week @ 2024-12-13 80/week @ 2024-12-20 122/week @ 2024-12-27 371/week @ 2025-01-03 178/week @ 2025-01-10 256/week @ 2025-01-17 468/week @ 2025-01-24 399/week @ 2025-01-31 166/week @ 2025-02-07 138/week @ 2025-02-14 473/week @ 2025-02-21 432/week @ 2025-02-28 533/week @ 2025-03-07 135/week @ 2025-03-14

1,573 downloads per month
Used in 3 crates (via routee-compass-powertrain)

BSD-3-Clause

135KB
3K SLoC

ninterp

docs.rs Crates.io Version GitHub

The ninterp crate provides multivariate interpolation over rectilinear grids of any dimensionality.

There are hard-coded interpolators for lower dimensionalities (up to N = 3) for better runtime performance. All interpolators work with both owned and borrowed arrays (array views) of various types.

A variety of interpolation strategies are implemented and exposed in the prelude module. Custom interpolation strategies can be defined in downstream crates.

cargo add ninterp

Cargo Features

  • serde: support for serde 1.x
    cargo add ninterp --features serde
    

Examples

See examples in new method documentation:

Also see the examples directory for advanced examples:

  • Swapping strategies at runtime: dynamic_strategy.rs

    • Using strategy enums (strategy::enums::Strategy1DEnum/etc.)
      • Compatible with serde
      • Incompatible with custom strategies
    • Using Box<dyn Strategy1D>/etc. (dynamic dispatch)
      • Incompatible with serde
      • Compatible with custom strategies
      • Runtime cost
  • Swapping interpolators at runtime: dynamic_interpolator.rs

    • Using InterpolatorEnum
      • Compatible with serde
      • Incompatible with custom strategies
    • Using Box<dyn Interpolator> (dynamic dispatch)
      • Incompatible with serde
      • Compatible with custom strategies
      • Runtime cost
  • Defining custom strategies: custom_strategy.rs

  • Using transmutable (transparent) types, such as uom::si::Quantity: uom.rs

Overview

A prelude module has been defined:

use ninterp::prelude::*;

This exposes all strategies and a variety of interpolators:

There is also a constant-value 'interpolator': Interp0D. This is useful when working with an InterpolatorEnum or Box<dyn Interpolator>

Instantiation is done by calling an interpolator's new method. For dimensionalities N ≥ 1, this executes a validation step, preventing runtime panics. After editing interpolator data, call the InterpData's validate method or Interpolator::validate to rerun these checks.

To change the extrapolation setting, call set_extrapolate.

To change the interpolation strategy, supply a Strategy1DEnum/etc. or Box<dyn Strategy1D>/etc. upon instantiation, and call set_strategy.

Strategies

An interpolation strategy (e.g. Linear, Nearest, LeftNearest, RightNearest) must be specified. Not all interpolation strategies are implemented for every dimensionality. Linear and Nearest are implemented for all dimensionalities.

Custom strategies can be defined. See examples/custom_strategy.rs for an example.

Extrapolation

An Extrapolate setting must be provided in the new method. This controls what happens when a point is beyond the range of supplied coordinates. The following settings are applicable for all interpolators:

  • Extrapolate::Fill(T)
  • Extrapolate::Clamp
  • Extrapolate::Wrap
  • Extrapolate::Error

Extrapolate::Enable is valid for Linear for all dimensionalities.

If you are unsure which variant to choose, Extrapolate::Error is likely what you want.

Interpolation

Interpolation is executed by calling Interpolator::interpolate.

The length of the interpolant point slice must be equal to the interpolator dimensionality. The interpolator dimensionality can be retrieved by calling Interpolator::ndim.

Dependencies

~2–2.8MB
~56K SLoC