Skip to main content

interp

Function interp 

Source
pub fn interp<T>(
    x: &Array<T>,
    xp: &Array<T>,
    fp: &Array<T>,
    left: Option<T>,
    right: Option<T>,
    period: Option<T>,
) -> Result<Array<T>>
where T: Clone + PartialOrd + Sub<Output = T> + Mul<Output = T> + Add<Output = T> + Div<Output = T> + Float,
Expand description

One-dimensional linear interpolation.

Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.

§Parameters

  • x - The x-coordinates at which to evaluate the interpolated values.
  • xp - The x-coordinates of the data points, must be increasing.
  • fp - The y-coordinates of the data points, same length as xp.
  • left - Value to return for x < xp[0]. If not provided, defaults to fp[0].
  • right - Value to return for x > xp[last]. If not provided, defaults to fp[last].
  • period - A period for the x-coordinates. This parameter allows making the interpolation periodic in the specified period.

§Returns

The interpolated values, same shape as x.

§Examples

use numrs2::prelude::*;
use numrs2::array_ops::conditional::interp;

let xp = Array::from_vec(vec![1.0, 2.0, 3.0]);
let fp = Array::from_vec(vec![3.0, 2.0, 0.0]);
let x = Array::from_vec(vec![0.0, 1.5, 2.0, 2.5, 3.0, 4.0]);

// Without explicitly specifying `left` and `right`
let y = interp(&x, &xp, &fp, None, None, None).expect("operation should succeed");
assert_eq!(y.to_vec(), vec![3.0, 2.5, 2.0, 1.0, 0.0, 0.0]);

// With explicit `left` and `right` values
let y = interp(&x, &xp, &fp, Some(-5.0), Some(-1.0), None).expect("operation should succeed");
assert_eq!(y.to_vec(), vec![-5.0, 2.5, 2.0, 1.0, 0.0, -1.0]);