Expand description
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
interp1d::Linear
- Linear interpolation and extrapolationinterp1d::cubic_spline
- Cubic Spline interpolation with different boundary conditions.
§2D Interpolation
The interp2d module provides the Interp2D
interpolator
and different interpolation strategies
2D Strategies
interp2d::Bilinear
- Bilinear interpolation and extrapolation
§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]]);
Modules§
- interp1d
- A collection of structs and traits to interpolate data along the first axis
- interp2d
- A collection of structs and traits to interpolate data along the first two axis
- vector_
extensions
Enums§
- Builder
Error - Errors during Interpolator creation
- Interpolate
Error - Errors during Interpolation