pub struct CubicSmoothingSpline<'a, T, D>{ /* 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>
impl<'a, T, D> CubicSmoothingSpline<'a, T, D>
Sourcepub fn new<X, Y>(x: X, y: Y) -> Self
pub fn new<X, Y>(x: X, y: Y) -> Self
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.
Sourcepub fn with_axis(self, axis: Axis) -> Self
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.
Sourcepub fn with_weights<W>(self, weights: W) -> Selfwhere
W: AsArray<'a, T>,
pub fn with_weights<W>(self, weights: W) -> Selfwhere
W: AsArray<'a, T>,
Sets the weights data vector
weights.len()
must be equal to x.len()
Sourcepub fn with_optional_weights<W>(self, weights: Option<W>) -> Selfwhere
W: AsArray<'a, T>,
pub fn with_optional_weights<W>(self, weights: Option<W>) -> Selfwhere
W: AsArray<'a, T>,
Sets the weights data vector in Option
wrap
weights.len()
must be equal to x.len()
Sourcepub fn with_smooth(self, smooth: T) -> Self
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
Sourcepub fn with_optional_smooth(self, smooth: Option<T>) -> Self
pub fn with_optional_smooth(self, smooth: Option<T>) -> Self
Sets the smoothing parameter in Option
wrap
Sourcepub fn make(self) -> Result<Self>
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
Auto Trait Implementations§
impl<'a, T, D> Freeze for CubicSmoothingSpline<'a, T, D>
impl<'a, T, D> RefUnwindSafe for CubicSmoothingSpline<'a, T, D>where
D: RefUnwindSafe,
T: RefUnwindSafe,
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>
impl<'a, T, D> UnwindSafe for CubicSmoothingSpline<'a, T, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.