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

#317 in Math

Download history 792/week @ 2024-12-06 507/week @ 2024-12-13 142/week @ 2024-12-20 58/week @ 2024-12-27 113/week @ 2025-01-03 126/week @ 2025-01-10 95/week @ 2025-01-17 128/week @ 2025-01-24 119/week @ 2025-01-31 157/week @ 2025-02-07 183/week @ 2025-02-14 295/week @ 2025-02-21 278/week @ 2025-02-28 269/week @ 2025-03-07 317/week @ 2025-03-14 209/week @ 2025-03-21

1,176 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