pub struct PiecewiseLinearFunction<T: CoordFloat> {
pub coordinates: Vec<Coord<T>>,
}Expand description
A continuous piecewise linear function.
The function is represented as a list of (x, y) pairs, each representing a point of
inflection (or equivalently a limit between two linear pieces). The represented function is
assumed to be linear between each of these points.
§Invariants
All methods defined on PiecewiseLinearFunction preserve the following invariants:
- There are at least two coordinates in the
coordinatesarray - The coordinates are in strictly increasing order of
xvalue.
However, two consecutive segments do not necessarily have different slopes. These methods
will panic if invariants are broken by manually editing the coordinates vector.
This representation means that functions defined on an empty or singleton set, as well as discontinuous functions, are not supported.
§Example
use piecewise_linear::PiecewiseLinearFunction;
use std::convert::TryFrom;
let f = PiecewiseLinearFunction::try_from(vec![(0., 0.), (1., 1.), (2., 1.5)]).unwrap();
assert_eq!(f.y_at_x(1.25), Some(1.125));Fields§
§coordinates: Vec<Coord<T>>Vector of points that make up the function.
Implementations§
Source§impl<T: CoordFloat> PiecewiseLinearFunction<T>
impl<T: CoordFloat> PiecewiseLinearFunction<T>
Sourcepub fn new(coordinates: Vec<Coord<T>>) -> Option<Self>
pub fn new(coordinates: Vec<Coord<T>>) -> Option<Self>
Creates a new PiecewiseLinearFunction from a vector of Coordinates.
Returns a new PicewiseLinearFunction, or None if the invariants were not respected.
Sourcepub fn constant(domain: (T, T), value: T) -> Option<Self>
pub fn constant(domain: (T, T), value: T) -> Option<Self>
Returns a new constant PiecewiseLinearFunction with the specified domain and value.
Returns None if the domain is not valid (i.e. domain.1 <= domain.0).
Sourcepub fn has_same_domain_as(&self, other: &PiecewiseLinearFunction<T>) -> bool
pub fn has_same_domain_as(&self, other: &PiecewiseLinearFunction<T>) -> bool
Checks whether this function has the same domain as another one.
Sourcepub fn segments_iter(&self) -> SegmentsIterator<'_, T> ⓘ
pub fn segments_iter(&self) -> SegmentsIterator<'_, T> ⓘ
Returns an iterator over the segments of f.
This iterator is guaranteed to have at least one element.
Sourcepub fn points_of_inflection_iter<'a>(
&'a self,
other: &'a PiecewiseLinearFunction<T>,
) -> Option<PointsOfInflectionIterator<'_, T>>
pub fn points_of_inflection_iter<'a>( &'a self, other: &'a PiecewiseLinearFunction<T>, ) -> Option<PointsOfInflectionIterator<'_, T>>
Returns an iterator over the joint points of inflection of self and other.
See points_of_inflection_iter() in this module for details.
Sourcepub fn segment_at_x(&self, x: T) -> Option<Line<T>>
pub fn segment_at_x(&self, x: T) -> Option<Line<T>>
Returns a segment ((x1, y1), (x2, y2)) of this function such that x1 <= x <= x2.
Returns None if x is outside the domain of f.
Sourcepub fn y_at_x(&self, x: T) -> Option<T>
pub fn y_at_x(&self, x: T) -> Option<T>
Computes the value f(x) for this piecewise linear function.
Returns None if x is outside the domain of f.
Sourcepub fn shrink_domain(
&self,
to_domain: (T, T),
) -> Option<PiecewiseLinearFunction<T>>
pub fn shrink_domain( &self, to_domain: (T, T), ) -> Option<PiecewiseLinearFunction<T>>
Returns a new piecewise linear function that is the restriction of this function to the specified domain.
Returns None if to_domain is not a subset of the domain of self.
Sourcepub fn expand_domain(
&self,
to_domain: (T, T),
strategy: ExpandDomainStrategy,
) -> PiecewiseLinearFunction<T>
pub fn expand_domain( &self, to_domain: (T, T), strategy: ExpandDomainStrategy, ) -> PiecewiseLinearFunction<T>
Returns a new piecewise linear function that is the expansion of this function to the specified domain.
At most one value is added on either side. See ExpandDomainStrategy for options
determining how these added values are picked.
Sourcepub fn add(
&self,
other: &PiecewiseLinearFunction<T>,
) -> Option<PiecewiseLinearFunction<T>>
pub fn add( &self, other: &PiecewiseLinearFunction<T>, ) -> Option<PiecewiseLinearFunction<T>>
Sums this method with another piecewise linear function.
Both functions must have the same domain; returns None otherwise.
Sourcepub fn max(
&self,
other: &PiecewiseLinearFunction<T>,
) -> Option<PiecewiseLinearFunction<T>>
pub fn max( &self, other: &PiecewiseLinearFunction<T>, ) -> Option<PiecewiseLinearFunction<T>>
Returns a new piecewise linear function that is the maximum of self and other.
Note that the resulting function may have more points of inflection than either function. For instance,
§Example
use piecewise_linear::PiecewiseLinearFunction;
use std::convert::TryFrom;
let f = PiecewiseLinearFunction::try_from(vec![(0., 1.), (1., 0.)]).unwrap();
let g = PiecewiseLinearFunction::try_from(vec![(0., 0.), (1., 1.)]).unwrap();
assert_eq!(
f.max(&g).unwrap(),
PiecewiseLinearFunction::try_from(vec![(0., 1.), (0.5, 0.5), (1., 1.)]).unwrap()
);Returns None if the domains of self and other are not equal.
Source§impl<T: CoordFloat + Signed> PiecewiseLinearFunction<T>
impl<T: CoordFloat + Signed> PiecewiseLinearFunction<T>
Sourcepub fn negate(&self) -> PiecewiseLinearFunction<T>
pub fn negate(&self) -> PiecewiseLinearFunction<T>
Returns -f.
Sourcepub fn min(
&self,
other: &PiecewiseLinearFunction<T>,
) -> Option<PiecewiseLinearFunction<T>>
pub fn min( &self, other: &PiecewiseLinearFunction<T>, ) -> Option<PiecewiseLinearFunction<T>>
Computes the minimum of this function and other.
Returns None in case of a domain error.
Sourcepub fn abs(&self) -> PiecewiseLinearFunction<T>
pub fn abs(&self) -> PiecewiseLinearFunction<T>
Computes the absolute value of this function.
Source§impl<T: CoordFloat + Sum> PiecewiseLinearFunction<T>
impl<T: CoordFloat + Sum> PiecewiseLinearFunction<T>
Trait Implementations§
Source§impl<T: Clone + CoordFloat> Clone for PiecewiseLinearFunction<T>
impl<T: Clone + CoordFloat> Clone for PiecewiseLinearFunction<T>
Source§fn clone(&self) -> PiecewiseLinearFunction<T>
fn clone(&self) -> PiecewiseLinearFunction<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug + CoordFloat> Debug for PiecewiseLinearFunction<T>
impl<T: Debug + CoordFloat> Debug for PiecewiseLinearFunction<T>
Source§impl<T: CoordFloat> From<PiecewiseLinearFunction<T>> for Vec<(T, T)>
impl<T: CoordFloat> From<PiecewiseLinearFunction<T>> for Vec<(T, T)>
Source§fn from(val: PiecewiseLinearFunction<T>) -> Self
fn from(val: PiecewiseLinearFunction<T>) -> Self
Source§impl<T: CoordFloat + Signed> Neg for PiecewiseLinearFunction<T>
impl<T: CoordFloat + Signed> Neg for PiecewiseLinearFunction<T>
Source§impl<T: PartialEq + CoordFloat> PartialEq for PiecewiseLinearFunction<T>
impl<T: PartialEq + CoordFloat> PartialEq for PiecewiseLinearFunction<T>
Source§impl<T: CoordFloat> TryFrom<LineString<T>> for PiecewiseLinearFunction<T>
impl<T: CoordFloat> TryFrom<LineString<T>> for PiecewiseLinearFunction<T>
Source§impl<T: CoordFloat> TryFrom<Vec<(T, T)>> for PiecewiseLinearFunction<T>
impl<T: CoordFloat> TryFrom<Vec<(T, T)>> for PiecewiseLinearFunction<T>
Source§impl<T: CoordFloat> TryFrom<Vec<Coord<T>>> for PiecewiseLinearFunction<T>
impl<T: CoordFloat> TryFrom<Vec<Coord<T>>> for PiecewiseLinearFunction<T>
Source§impl<T: CoordFloat> TryFrom<Vec<Point<T>>> for PiecewiseLinearFunction<T>
impl<T: CoordFloat> TryFrom<Vec<Point<T>>> for PiecewiseLinearFunction<T>
impl<T: CoordFloat> StructuralPartialEq for PiecewiseLinearFunction<T>
Auto Trait Implementations§
impl<T> Freeze for PiecewiseLinearFunction<T>
impl<T> RefUnwindSafe for PiecewiseLinearFunction<T>where
T: RefUnwindSafe,
impl<T> Send for PiecewiseLinearFunction<T>where
T: Send,
impl<T> Sync for PiecewiseLinearFunction<T>where
T: Sync,
impl<T> Unpin for PiecewiseLinearFunction<T>where
T: Unpin,
impl<T> UnwindSafe for PiecewiseLinearFunction<T>where
T: UnwindSafe,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 more