Struct CubicSmoothingSpline

Source
pub struct CubicSmoothingSpline<'a, T, D>
where T: Real, D: Dimension,
{ /* private fields */ }
Expand description

N-dimensional (univariate/multivariate) smoothing spline calculator/evaluator

The struct represents n-d smoothing cubic spline and allows you to make and evaluate the splines for given data.

CubicSmoothingSpline struct is parametrized by data type (f64 or f32) and data dimension.

The methods API of CubicSmoothingSpline is implemented as builder-loke pattern or in other words as chained API.

§Examples

use csaps::CubicSmoothingSpline;

let x = vec![1.0, 2.0, 3.0, 4.0];
let y = vec![0.5, 1.2, 3.4, 2.5];

let ys = CubicSmoothingSpline::new(&x, &y)
    .make().unwrap()
    .evaluate(&x).unwrap();
use ndarray::array;
use csaps::CubicSmoothingSpline;

let x = array![1.0, 2.0, 3.0, 4.0];
let y = array![0.5, 1.2, 3.4, 2.5];
let w = array![1.0, 0.7, 0.5, 1.0];
let smooth = 0.85;

let s = CubicSmoothingSpline::new(&x, &y)
    .with_weights(&w)
    .with_smooth(smooth)
    .make().unwrap();

let xi = array![1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0];
let yi = s.evaluate(&xi).unwrap();

Implementations§

Source§

impl<'a, T, D> CubicSmoothingSpline<'a, T, D>
where T: Real, D: Dimension,

Source

pub fn new<X, Y>(x: X, y: Y) -> Self
where X: AsArray<'a, T>, Y: AsArray<'a, T, D>,

Creates CubicSmoothingSpline struct from the given X data sites and Y data values

§Arguments
  • x – the X-data sites 1-d array-like. Must strictly increasing: x1 < x2 < x3 < ... < xN
  • y – The Y-data values n-d array-like. ndim can be from 1 to N. The splines will be computed for all data by given axis. By default the axis parameter is equal to the last axis of Y data. For example, for 1-d axis is equal to 0, for 2-d axis is equal to 1, for 3-d axis is equal to 2 and etc.
Source

pub fn with_axis(self, axis: Axis) -> Self

Sets the axis parameter

The Y-data axis. Axis along which Y-data is assumed to be varying. In other words, the axis parameter specifies Y-data axis for computing spline.

y.shape()[axis] must be equal to x.len()

§Example
use ndarray::{array, Axis};
use csaps::CubicSmoothingSpline;

let x = array![1., 2., 3., 4.];
let y = array![[1., 5., 9.],
               [2., 6., 10.],
               [3., 7., 11.],
               [4., 8., 12.]];

let ys = CubicSmoothingSpline::new(&x, &y)
    .with_axis(Axis(0))  // y.shape()[0] == x.len()
    .make().unwrap()
    .evaluate(&x).unwrap();

assert_eq!(ys, y);

In the example y data view will be reshaped from shape [4, 3] to shape [3, 4] before computing spline and reshaped back while evaluating the spline for correct shape of ys output.

Source

pub fn with_weights<W>(self, weights: W) -> Self
where W: AsArray<'a, T>,

Sets the weights data vector

weights.len() must be equal to x.len()

Source

pub fn with_optional_weights<W>(self, weights: Option<W>) -> Self
where W: AsArray<'a, T>,

Sets the weights data vector in Option wrap

weights.len() must be equal to x.len()

Source

pub fn with_smooth(self, smooth: T) -> Self

Sets the smoothing parameter

The smoothing parameter should be in range [0, 1], where bounds are:

  • 0: The smoothing spline is the least-squares straight line fit to the data
  • 1: The cubic spline interpolant with natural boundary condition
Source

pub fn with_optional_smooth(self, smooth: Option<T>) -> Self

Sets the smoothing parameter in Option wrap

Source

pub fn make(self) -> Result<Self>

Makes (computes) the spline for given data and parameters

§Errors
  • If the data or parameters are invalid
  • If reshaping Y data to 2-d view has failed
Source

pub fn evaluate<X>(&self, xi: X) -> Result<Array<T, D>>
where X: AsArray<'a, T>,

Evaluates the computed spline on the given data sites

§Errors
  • If the xi data is invalid
  • If the spline yet has not been computed
Source

pub fn smooth(&self) -> Option<T>

Returns the smoothing parameter or None

Source

pub fn spline(&self) -> Option<&NdSpline<'a, T>>

Returns the ref to NdSpline struct with data of computed spline or None

Auto Trait Implementations§

§

impl<'a, T, D> Freeze for CubicSmoothingSpline<'a, T, D>
where D: Freeze, T: Freeze,

§

impl<'a, T, D> RefUnwindSafe for CubicSmoothingSpline<'a, T, D>

§

impl<'a, T, D> Send for CubicSmoothingSpline<'a, T, D>

§

impl<'a, T, D> Sync for CubicSmoothingSpline<'a, T, D>

§

impl<'a, T, D> Unpin for CubicSmoothingSpline<'a, T, D>
where D: Unpin, T: Unpin,

§

impl<'a, T, D> UnwindSafe for CubicSmoothingSpline<'a, T, D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

unsafe fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.