Skip to main content

lib_jsl_interpolation/
nd.rs

1//! N-dimensional interpolation on rectilinear grids.
2//!
3//! See the book entry [Interpolation on grids](https://jwkunz.github.io/lib_jsl/book/interpolation/grids.html).
4//!
5//! A grid is given by one strictly increasing axis per dimension and a value array whose shape is
6//! the axis lengths. Queries are slices with one coordinate per dimension.
7
8use lib_jsl_core::*;
9use ndarray::ArrayD;
10
11/// Evaluation interface shared by the N-dimensional interpolators.
12pub trait InterpolationND {
13    /// Interpolated value at `point` (one coordinate per dimension).
14    fn interpolate_at(&self, point: &[f64]) -> Result<f64, ErrorsJSL>;
15}
16
17/// Checks axes and shape consistency; returns the axes as owned vectors.
18pub(crate) fn validate_grid_nd(axes: &[&[f64]], values: &ArrayD<f64>, min_points: usize) -> Result<Vec<Vec<f64>>, ErrorsJSL> {
19    if axes.is_empty() || values.ndim() != axes.len() {
20        return Err(ErrorsJSL::IncompatibleArraySizes((values.ndim(), axes.len())));
21    }
22    for (d, axis) in axes.iter().enumerate() {
23        if axis.len() != values.shape()[d] {
24            return Err(ErrorsJSL::IncompatibleArraySizes((values.shape()[d], axis.len())));
25        }
26        if axis.len() < min_points {
27            return Err(ErrorsJSL::InvalidInputRange("not enough grid points along an axis"));
28        }
29        if axis.windows(2).any(|w| w[1] <= w[0]) {
30            return Err(ErrorsJSL::InvalidInputRange("axes must be strictly increasing"));
31        }
32    }
33    Ok(axes.iter().map(|a| a.to_vec()).collect())
34}
35
36pub(crate) fn check_point(dims: usize, point: &[f64]) -> Result<(), ErrorsJSL> {
37    if point.len() != dims {
38        return Err(ErrorsJSL::IncompatibleArraySizes((point.len(), dims)));
39    }
40    Ok(())
41}
42
43pub mod grid_data;
44pub mod nearest;
45pub mod cubic_basis;
46pub mod rbf;
47pub mod linear_nd;