10 releases

0.5.0 Feb 26, 2025
0.4.1 Oct 12, 2023
0.3.2 Sep 21, 2023
0.3.1 Aug 29, 2023
0.1.1 Jul 8, 2023

#358 in Math

Download history 129/week @ 2024-11-19 182/week @ 2024-11-26 962/week @ 2024-12-03 411/week @ 2024-12-10 414/week @ 2024-12-17 112/week @ 2024-12-24 72/week @ 2024-12-31 135/week @ 2025-01-07 114/week @ 2025-01-14 99/week @ 2025-01-21 98/week @ 2025-01-28 173/week @ 2025-02-04 157/week @ 2025-02-11 168/week @ 2025-02-18 361/week @ 2025-02-25 157/week @ 2025-03-04

882 downloads per month

MIT license

110KB
2K SLoC

ndarray-interp

A Interpolation crate for usage with the rust ndarray crate.

Features

  • 1D-Interpolation of n-dimensional data along the first axis
  • 2D-Interpolation of n-dimensional data along the first two axes
  • Add your own Interpolation algorithms
  • Interpolation of owned arrays and array views
  • Interpolation at multiple points at once

Interpolation strategies

  • Linear interpolation with, and without extrapolation
  • Cubic spline interpolation Wikipedia
  • Bilinear interpolation with, and without extrapolation Wikipedia

Planned Features

  • More interpolation strategies
  • rayon support

lib.rs:

The ndarray-interp crate provides interpolation algorithms for interpolating n-dimesional data.

1D Interpolation

The [interp1d] module provides the Interp1D interpolator and different interpolation strategies

1D Strategies

2D Interpolation

The [interp2d] module provides the Interp2D interpolator and different interpolation strategies

2D Strategies

Custom interpolation strategy

This crate defines traits to allow implementation of user defined interpolation algorithms. A 1D interpolation strategy can be created by implementing the Interp1DStrategy and Interp1DStrategyBuilder traits. A 2D interpolation strategy can be created by implementing the Interp2DStrategy and Interp2DStrategyBuilder traits.

See also the custom_strategy.rs example.

Examples

1D Example

use ndarray_interp::interp1d::*;
use ndarray::*;

let data = array![0.0, 1.0, 1.5, 1.0, 0.0 ];
let interp = Interp1DBuilder::new(data).build().unwrap();

let result = interp.interp_scalar(3.5).unwrap();
assert!(result == 0.5);
let result = interp.interp_array(&array![0.0, 0.5, 1.5]).unwrap();
assert!(result == array![0.0, 0.5, 1.25])

1D Example with multidimensional data

use ndarray_interp::interp1d::*;
use ndarray::*;

let data = array![
    [0.0, 1.0],
    [1.0, 2.0],
    [1.5, 2.5],
    [1.0, 2.0],
];
let x = array![1.0, 2.0, 3.0, 4.0];

let interp = Interp1D::builder(data)
    .strategy(Linear::new().extrapolate(true))
    .x(x)
    .build().unwrap();

let result = interp.interp(0.5).unwrap();
assert!(result == array![-0.5, 0.5]);
let result = interp.interp_array(&array![0.5, 4.0]).unwrap();
assert!(result == array![[-0.5, 0.5], [1.0, 2.0]]);

2D Example

use ndarray_interp::interp2d::*;
use ndarray::*;

let data = array![
    [1.0, 2.0, 2.5],
    [3.0, 4.0, 3.5],
];
let interp = Interp2D::builder(data).build().unwrap();

let result = interp.interp_scalar(0.0, 0.5).unwrap();
assert!(result == 1.5);
let result = interp.interp_array(&array![0.0, 1.0], &array![0.5, 2.0]).unwrap();
assert!(result == array![1.5, 3.5]);

1D Example with multidimensional data

use ndarray_interp::interp2d::*;
use ndarray::*;

let data = array![
    // ---------------------------------> y
    [[1.0, -1.0], [2.0, -2.0], [3.0, -3.0]], // |
    [[4.0, -4.0], [5.0, -5.0], [6.0, -6.0]], // |
    [[7.0, -7.0], [8.0, -8.0], [9.0, -9.0]], // V
    [[7.5, -7.5], [8.5, -8.5], [9.5, -9.5]], // x
];
let x = array![1.0, 2.0, 3.0, 4.0];
let y = array![1.0, 2.0, 3.0];

let interp = Interp2D::builder(data)
    .x(x)
    .y(y)
    .build().unwrap();

let result = interp.interp(1.5, 2.0).unwrap();
assert!(result == array![3.5, -3.5]);
let result = interp.interp_array(&array![1.5, 1.5], &array![2.0, 2.5]).unwrap();
assert!(result == array![[3.5, -3.5],[4.0, -4.0]]);

Dependencies

~1.5–2.2MB
~45K SLoC