[][src]Struct piecewise_linear::PiecewiseLinearFunction

pub struct PiecewiseLinearFunction<T: CoordinateType> {
    pub coordinates: Vec<Coordinate<T>>,
}

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<Coordinate<T>>

Vector of points that make up the function.

Methods

impl<T: CoordinateType> PiecewiseLinearFunction<T>[src]

pub fn new(coordinates: Vec<Coordinate<T>>) -> Option<Self>[src]

Creates a new PiecewiseLinearFunction from a vector of Coordinates.

Returns a new PicewiseLinearFunction, or None if the invariants were not respected.

pub fn constant(domain: (T, T), value: T) -> Option<Self>[src]

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

pub fn domain(&self) -> (T, T)[src]

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

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

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

Important traits for SegmentsIterator<'a, T>
pub fn segments_iter(&self) -> SegmentsIterator<T>[src]

Returns an iterator over the segments of f.

This iterator is guaranteed to have at least one element.

pub fn points_of_inflection_iter<'a>(
    &'a self,
    other: &'a PiecewiseLinearFunction<T>
) -> Option<PointsOfInflectionIterator<T>>
[src]

Returns an iterator over the joint points of inflection of self and other.

See points_of_inflection_iter() in this module for details.

pub fn segment_at_x(&self, x: T) -> Option<Line<T>>[src]

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.

pub fn y_at_x(&self, x: T) -> Option<T>[src]

Computes the value f(x) for this piecewise linear function.

Returns None if x is outside the domain of f.

pub fn shrink_domain(
    &self,
    to_domain: (T, T)
) -> Option<PiecewiseLinearFunction<T>>
[src]

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.

pub fn expand_domain(
    &self,
    to_domain: (T, T),
    strategy: ExpandDomainStrategy
) -> PiecewiseLinearFunction<T>
[src]

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.

pub fn add(
    &self,
    other: &PiecewiseLinearFunction<T>
) -> Option<PiecewiseLinearFunction<T>>
[src]

Sums this method with another piecewise linear function.

Both functions must have the same domain; returns None otherwise.

pub fn max(
    &self,
    other: &PiecewiseLinearFunction<T>
) -> Option<PiecewiseLinearFunction<T>>
[src]

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.

impl<T: CoordinateType + Signed> PiecewiseLinearFunction<T>[src]

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

Returns -f.

pub fn min(
    &self,
    other: &PiecewiseLinearFunction<T>
) -> Option<PiecewiseLinearFunction<T>>
[src]

Computes the minimum of this function and other.

Returns None in case of a domain error.

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

Computes the absolute value of this function.

impl<T: CoordinateType + Sum> PiecewiseLinearFunction<T>[src]

pub fn integrate(&self) -> T[src]

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

Trait Implementations

impl<T: CoordinateType> Into<Vec<(T, T)>> for PiecewiseLinearFunction<T>[src]

impl<T: Clone + CoordinateType> Clone for PiecewiseLinearFunction<T>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: PartialEq + CoordinateType> PartialEq<PiecewiseLinearFunction<T>> for PiecewiseLinearFunction<T>[src]

impl<T: Debug + CoordinateType> Debug for PiecewiseLinearFunction<T>[src]

impl<T: CoordinateType> TryFrom<LineString<T>> for PiecewiseLinearFunction<T>[src]

type Error = ()

The type returned in the event of a conversion error.

impl<T: CoordinateType> TryFrom<Vec<Coordinate<T>>> for PiecewiseLinearFunction<T>[src]

type Error = ()

The type returned in the event of a conversion error.

impl<T: CoordinateType> TryFrom<Vec<Point<T>>> for PiecewiseLinearFunction<T>[src]

type Error = ()

The type returned in the event of a conversion error.

impl<T: CoordinateType> TryFrom<Vec<(T, T)>> for PiecewiseLinearFunction<T>[src]

type Error = ()

The type returned in the event of a conversion error.

impl<T: CoordinateType + Signed> Neg for PiecewiseLinearFunction<T>[src]

type Output = Self

The resulting type after applying the - operator.

Auto Trait Implementations

impl<T> Unpin for PiecewiseLinearFunction<T> where
    T: Unpin

impl<T> Send for PiecewiseLinearFunction<T> where
    T: Send

impl<T> Sync for PiecewiseLinearFunction<T> where
    T: Sync

impl<T> RefUnwindSafe for PiecewiseLinearFunction<T> where
    T: RefUnwindSafe

impl<T> UnwindSafe for PiecewiseLinearFunction<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]