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
coordinates
array - The coordinates are in strictly increasing order of
x
value.
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<PiecewiseLinearFunction<T>> for PiecewiseLinearFunction<T>
impl<T: PartialEq + CoordFloat> PartialEq<PiecewiseLinearFunction<T>> for PiecewiseLinearFunction<T>
source§fn eq(&self, other: &PiecewiseLinearFunction<T>) -> bool
fn eq(&self, other: &PiecewiseLinearFunction<T>) -> bool
self
and other
values to be equal, and is used
by ==
.