jonasBoss / ndarray-interp

Rust Interpolation Library
MIT License
5 stars 7 forks source link

Consider allowing X and Y to be of different types (float-like) #9

Open ivan-aksamentov opened 1 week ago

ivan-aksamentov commented 1 week ago

Consider function y = f(x) to be interpolated. The types of x an y are conceptually independent and don't have to be the same.

However, in the ndarray-interp crate they are enforced to be the same type. This seems unnecessary.

If possible, please consider making types of x and y disjoint. They could be any float-like values (e.g. num_traits::Float or some of the types from ndarray).

In a more general scenario, potentially, they could be integers (with obvious limitations) or some custom types (with a set of custom traits implemented specifically for interpolation). But that might be more complex.

Repro

This is currently not allowed:

let ys: Array1<f32> = array![1.0, 1.5, 2.0];
let xs: Array1<f64> = array![1.0, 2.0, 3.0];
let query = 1.5;
let expected = 1.25;

let interpolator = Interp1DBuilder::new(ys).x(xs).build()?;
let result: f32 = interpolator.interp_scalar(query)?;

assert_eq!(result, expected);

The error is:

error[E0271]: type mismatch resolving `<OwnedRepr<f64> as RawData>::Elem == f32`
    |
15  |     let interpolator = Interp1DBuilder::new(ys).x(xs).build()?;
    |                                                 - ^^ expected `f32`, found `f64`
    |                                                 |
    |                                                 required by a bound introduced by this call
    |
note: required by a bound in `ndarray_interp::interp1d::Interp1DBuilder::<Sd, Sx, D, Strat>::x`
   --> .cargo/registry/src/index.crates.io-6f17d22bba15001f/ndarray-interp-0.4.1/src/interp1d.rs:423:21
    |
421 |     pub fn x<NewSx>(self, x: ArrayBase<NewSx, Ix1>) -> Interp1DBuilder<Sd, NewSx, D, Strat>
    |            - required by a bound in this associated function
422 |     where
423 |         NewSx: Data<Elem = Sd::Elem>,
    |                     ^^^^^^^^^^^^^^^ required by this bound in `Interp1DBuilder::<Sd, Sx, D, Strat>::x`