Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ivan-aksamentov opened this issue Nov 15, 2024 · 0 comments
Open

Comments

@ivan-aksamentov
Copy link

ivan-aksamentov commented Nov 15, 2024

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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant