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>

source

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.

source

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).

source

pub fn domain(&self) -> (T, T)

Returns a function’s domain, represented as its min and max.

source

pub fn has_same_domain_as(&self, other: &PiecewiseLinearFunction<T>) -> bool

Checks whether this function has the same domain as another one.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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>

source

pub fn negate(&self) -> PiecewiseLinearFunction<T>

Returns -f.

source

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.

source

pub fn abs(&self) -> PiecewiseLinearFunction<T>

Computes the absolute value of this function.

source§

impl<T: CoordFloat + Sum> PiecewiseLinearFunction<T>

source

pub fn integrate(&self) -> T

Returns the integral of the considered function over its entire domain.

Trait Implementations§

source§

impl<T: Clone + CoordFloat> Clone for PiecewiseLinearFunction<T>

source§

fn clone(&self) -> PiecewiseLinearFunction<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + CoordFloat> Debug for PiecewiseLinearFunction<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: CoordFloat> From<PiecewiseLinearFunction<T>> for Vec<(T, T)>

source§

fn from(val: PiecewiseLinearFunction<T>) -> Self

Converts to this type from the input type.
source§

impl<T: CoordFloat + Signed> Neg for PiecewiseLinearFunction<T>

§

type Output = PiecewiseLinearFunction<T>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<T: PartialEq + CoordFloat> PartialEq<PiecewiseLinearFunction<T>> for PiecewiseLinearFunction<T>

source§

fn eq(&self, other: &PiecewiseLinearFunction<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: CoordFloat> TryFrom<LineString<T>> for PiecewiseLinearFunction<T>

§

type Error = ()

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

fn try_from(value: LineString<T>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: CoordFloat> TryFrom<Vec<(T, T), Global>> for PiecewiseLinearFunction<T>

§

type Error = ()

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

fn try_from(value: Vec<(T, T)>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: CoordFloat> TryFrom<Vec<Coord<T>, Global>> for PiecewiseLinearFunction<T>

§

type Error = ()

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

fn try_from(value: Vec<Coord<T>>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: CoordFloat> TryFrom<Vec<Point<T>, Global>> for PiecewiseLinearFunction<T>

§

type Error = ()

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

fn try_from(value: Vec<Point<T>>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: CoordFloat> StructuralPartialEq for PiecewiseLinearFunction<T>

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<G1, G2> Within<G2> for G1where G2: Contains<G1>,

source§

fn is_within(&self, b: &G2) -> bool