pub enum Interval<T>where
T: PartialOrd,{
TwoSided(T, T),
UpperOneSided(T),
LowerOneSided(T),
}Expand description
Interval over a partially ordered type (NB: floating point numbers are only partially ordered because of NaN values).
The interval is defined by its lower and upper bounds. One-sided intervals (with a single concrete bound) are also supported.
In this crate, intervals are considered inclusive of their (finite) bounds.
§Type parameters
T: The type over which the interval is defined. It must be partially ordered.
§Variants
TwoSided(T, T): Two-sided interval with lower and upper bounds. The interval is defined as [low, high]. The bounds are included in the interval.UpperOneSided(T): Upper one-sided interval with a lower bound. The interval is defined as [low, +∞). The lower bound is included in the interval.LowerOneSided(T): Lower one-sided interval with an upper bound. The interval is defined as (-∞, high]. The upper bound is included in the interval.
Intervals support various operations that depend on the type T over which they are defined.
§Operations
§Creation
Self::new(low, high): Create a new interval from its left and right bounds for ordered types with equality.Self::new_upper(low): Create a new upper one-sided interval from its left bound.Self::new_lower(high): Create a new lower one-sided interval from its right bound.
§Accessors
Self::low(): Get the lower bound of the interval (if any) for partially ordered types.Self::high(): Get the upper bound of the interval (if any) for partially ordered types.Self::low_f(): Get the lower bound of the interval (if any) for floating point types.Self::high_f(): Get the upper bound of the interval (if any) for floating point types.Self::low_i(): Get the lower bound of the interval (if any) for signed integer types.Self::high_i(): Get the upper bound of the interval (if any) for signed integer types.Self::low_u(): Get the lower bound of the interval (if any) for unsigned integer types.Self::high_u(): Get the upper bound of the interval (if any) for unsigned integer types.Self::low_as_ref(): Get a reference to the lower bound of the interval (if any).Self::high_as_ref(): Get a reference to the upper bound of the interval (if any).Self::left(): Get the left bound of the interval (if any).Self::right(): Get the right bound of the interval (if any).
§Characteristics
Self::is_two_sided(): Test whether the interval is two-sided.Self::is_one_sided(): Test whether the interval is one-sided.Self::is_upper(): Test whether the interval is an upper one-sided interval.Self::is_lower(): Test whether the interval is a lower one-sided interval.Self::is_degenerate(): Test whether the interval is degenerate.
§Comparison
Self::intersects(other): Test whether the interval intersects another interval.Self::is_included_in(other): Test whether the interval is included in another interval.Self::includes(other): Test whether the interval includes another interval.Self::contains(x): Test whether the interval contains a value.- approximate equality with
approxif theapproxfeature is enabled.
§Operators with a scalar value
Self::mul(rhs): Multiply the interval by a value.Self::div(rhs): Divide the interval by a value.Self::add(rhs): Add a value to the interval.Self::sub(rhs): Subtract a value from the interval.
§Operators with another interval
Self::relative_to(reference): Given two intervals, compute the relative interval compared to the reference (argument). The relative interval is defined as the interval of the ratios of the two intervals.
§Conversions
Self::try_from(value): Create a new interval from a tuple of bounds. The first element of the tuple is the lower bound, the second element is the upper bound. If the lower bound is greater than the upper bound, an error is returned.Self::from(range): Create a new interval from a range. The range must be bounded. If the lower bound is greater than the upper bound, an error is returned.
§Display
Self::fmt(): Format the interval as a string.
§Examples
§Creation
use stats_ci::*;
let interval = Interval::new(0., 10.)?;
let interval = Interval::new_upper(0.);
let interval = Interval::new_lower(10.);§Accessors
let interval = Interval::new(0., 10.)?;
assert_eq!(interval.low(), Some(0.));
assert_eq!(interval.high(), Some(10.));
assert_eq!(interval.low_f(), 0.);
assert_eq!(interval.high_f(), 10.);
assert_eq!(interval.width(), Some(10.));
assert_eq!(interval.is_one_sided(), false);
assert_eq!(interval.is_two_sided(), true);
assert_eq!(interval.is_upper(), false);
assert_eq!(interval.is_lower(), false);
assert_eq!(interval.is_degenerate(), false);§Comparison
let interval = Interval::new(0., 10.)?;
let interval2 = Interval::new(8., 15.)?;
let interval3 = Interval::new(2., 5.)?;
assert!(interval.intersects(&interval2));
assert!(interval3.is_included_in(&interval));
assert!(interval.includes(&interval3));
assert!(interval3 < interval2);
assert!(interval == Interval::new(0., 10.)?);
assert!(interval.contains(&5.));
assert!(!interval.contains(&20.));§Operations
let interval = Interval::new(2., 4.)?;
assert_eq!(interval * 2., Interval::new(4., 8.)?);
assert_eq!(interval + 2., Interval::new(4., 6.)?);
assert_eq!(interval - 2., Interval::new(0., 2.)?);
assert_eq!(interval / 2., Interval::new(1., 2.)?);§Conversions
let interval = Interval::new(2., 4.)?;
assert_eq!(interval, Interval::try_from(2. ..= 4.)?);
assert_eq!(interval, Interval::try_from((2., 4.))?);
assert_eq!(interval, Interval::try_from((Some(2.), Some(4.)))?);
assert_eq!(Interval::from(..= 10.), Interval::new_lower(10.));
assert_eq!(Interval::from(2. ..), Interval::new_upper(2.));
assert_eq!(format!("{}", interval), String::from("[2, 4]"));
assert_eq!(format!("{}", Interval::new_lower(3.)), String::from("(<-,3]"));
assert_eq!(format!("{}", Interval::new_upper(2.)), String::from("[2,->)"));Variants§
TwoSided(T, T)
Two-sided interval with lower and upper bounds. The interval is defined as [low, high]. The bounds are included in the interval.
UpperOneSided(T)
Upper one-sided interval with a lower bound. The interval is defined as [low, +∞). The lower bound is included in the interval.
LowerOneSided(T)
Lower one-sided interval with an upper bound. The interval is defined as (-∞, high]. The upper bound is included in the interval.
Implementations§
Source§impl<T: PartialOrd> Interval<T>
impl<T: PartialOrd> Interval<T>
Sourcepub fn new(low: T, high: T) -> Result<Self, IntervalError>
pub fn new(low: T, high: T) -> Result<Self, IntervalError>
Create a new interval from its left and right bounds for ordered types with equality.
§Examples
let interval = Interval::new(0., 1.)?;
assert_eq!(interval.low(), Some(0.));
assert_eq!(interval.high(), Some(1.));
let interval2 = Interval::new("A", "Z")?;
assert_eq!(interval2.low(), Some("A"));
assert_eq!(interval2.high(), Some("Z"));
let interval3 = Interval::new(0, 0_usize)?;
assert_eq!(interval3.low(), Some(0));
assert_eq!(interval3.high(), Some(0));Sourcepub fn new_upper(low: T) -> Self
pub fn new_upper(low: T) -> Self
Create a new upper one-sided interval from its left bound. The interval is defined as [low, +∞). The bound is included in the interval.
§Examples
The interval below represents [0., +∞).
let interval = Interval::new_upper(0.);
assert_eq!(interval.low(), Some(0.));
assert_eq!(interval.high(), None);Sourcepub fn new_lower(high: T) -> Self
pub fn new_lower(high: T) -> Self
Create a new lower one-sided interval from its right bound. The interval is defined as (-∞, high]. The bound is included in the interval.
§Examples
The interval below represents (-∞, 1.]
let interval = Interval::new_lower(1.);
assert_eq!(interval.low(), None);
assert_eq!(interval.high(), Some(1.));Sourcepub fn is_two_sided(&self) -> bool
pub fn is_two_sided(&self) -> bool
Test whether the interval is two-sided.
Sourcepub fn is_one_sided(&self) -> bool
pub fn is_one_sided(&self) -> bool
Test whether the interval is one-sided.
Sourcepub fn contains(&self, x: &T) -> bool
pub fn contains(&self, x: &T) -> bool
Test whether the interval contains a value.
§Examples
let interval = Interval::new(0., 1.)?;
assert!(interval.contains(&0.5));
assert!(!interval.contains(&2.));Sourcepub fn intersects(&self, other: &Self) -> bool
pub fn intersects(&self, other: &Self) -> bool
Test whether the interval intersects another interval. Two intervals are considered to intersect even if they only have a single point in common (e.g., one of their bounds).
§Examples
let interval = Interval::new(0., 1.)?;
let interval2 = Interval::new(0.5, 1.5)?;
assert!(interval.intersects(&interval2));
let interval3 = Interval::new(2., 3.)?;
assert!(!interval.intersects(&interval3));Sourcepub fn is_included_in(&self, other: &Self) -> bool
pub fn is_included_in(&self, other: &Self) -> bool
Test whether the interval is included in another interval.
The inclusion is not strict, i.e. an interval is included in itself.
Source§impl<T: PartialOrd + PartialEq> Interval<T>
impl<T: PartialOrd + PartialEq> Interval<T>
Sourcepub fn is_degenerate(&self) -> bool
pub fn is_degenerate(&self) -> bool
Test whether the interval is degenerate. A degenerate interval is an interval with a single point. For example, the interval [0, 0] is degenerate.
Source§impl<T: PartialOrd + Clone> Interval<T>
impl<T: PartialOrd + Clone> Interval<T>
Sourcepub fn low(&self) -> Option<T>
pub fn low(&self) -> Option<T>
Get the lower bound of the interval (if any) for partially ordered types.
This function clones the bound. If cloning is an issue, use Self::low_as_ref() instead.
Sourcepub fn high(&self) -> Option<T>
pub fn high(&self) -> Option<T>
Get the upper bound of the interval (if any) for partially ordered types.
This function clones the bound. If cloning is an issue, use Self::high_as_ref() instead.
Source§impl<T: Float> Interval<T>
impl<T: Float> Interval<T>
Sourcepub fn low_f(&self) -> T
pub fn low_f(&self) -> T
Get the lower bound of the interval (if any) for floating point types.
This function returns the negative infinite value for T for lower one-sided intervals.
Sourcepub fn high_f(&self) -> T
pub fn high_f(&self) -> T
Get the upper bound of the interval (if any) for floating point types.
This function returns the infinite value for T for upper one-sided intervals.
Sourcepub fn relative_to(&self, reference: &Interval<T>) -> Interval<T>
pub fn relative_to(&self, reference: &Interval<T>) -> Interval<T>
Given two intervals, compute the relative interval compared to the reference (argument). The relative interval is defined as the interval of the ratios of the two intervals.
E.g., for two two-sided intervals \( [x, y] \) and reference \( [a, b] \), the relative interval is \( [(x-b)/b, (y-a)/a] \).
Source§impl<T: PrimInt + Signed> Interval<T>
impl<T: PrimInt + Signed> Interval<T>
Source§impl<T: PrimInt + Unsigned> Interval<T>
impl<T: PrimInt + Unsigned> Interval<T>
Source§impl<T: PartialOrd> Interval<T>
impl<T: PartialOrd> Interval<T>
Sourcepub fn low_as_ref(&self) -> Option<&T>
pub fn low_as_ref(&self) -> Option<&T>
Get a reference to the lower bound of the interval (if any).
See also Self::low() if cloning is not an issue.
Sourcepub fn high_as_ref(&self) -> Option<&T>
pub fn high_as_ref(&self) -> Option<&T>
Get a reference to the upper bound of the interval (if any).
See also Self::high() if cloning is not an issue.
Trait Implementations§
Source§impl<T: AbsDiffEq + PartialOrd> AbsDiffEq for Interval<T>
Available on crate feature approx only.
impl<T: AbsDiffEq + PartialOrd> AbsDiffEq for Interval<T>
approx only.Source§fn default_epsilon() -> T::Epsilon
fn default_epsilon() -> T::Epsilon
Source§fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool
fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool
Source§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq.Source§impl<T: PartialOrd> From<RangeToInclusive<T>> for Interval<T>
impl<T: PartialOrd> From<RangeToInclusive<T>> for Interval<T>
Source§fn from(range: RangeToInclusive<T>) -> Self
fn from(range: RangeToInclusive<T>) -> Self
Create a lower one-sided interval from a range ending at a given value.
use stats_ci::Interval;
use std::ops::RangeToInclusive;
let interval = Interval::from(..=1);
assert_eq!(interval, Interval::new_lower(1));Source§impl<T: PartialOrd> PartialOrd for Interval<T>
impl<T: PartialOrd> PartialOrd for Interval<T>
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Compare two intervals.
Given two intervals a and b, a < b if and only if the upper bound of a is less than the lower bound of b.
Although interval bounds are inclusive, two intervals that overlap only at a single bound are considered ordered.
E.g., intervals [x,y] is considered less than [a,b] if y==a and x<b.
§Examples
let a = Interval::new(0, 10)?;
let b = Interval::new(10, 20)?;
let c = Interval::new(11, 20)?;
let d = Interval::new(0, 10)?;
let e = Interval::new_upper(10);
assert_eq!(a.partial_cmp(&b), Some(Ordering::Less));
assert_eq!(a.partial_cmp(&c), Some(Ordering::Less));
assert_eq!(a.partial_cmp(&d), Some(Ordering::Equal));
assert_eq!(a.partial_cmp(&e), Some(Ordering::Less));
assert_eq!(c.partial_cmp(&a), Some(Ordering::Greater));
assert_eq!(b.partial_cmp(&c), None);Source§impl<T: PartialOrd> RangeBounds<T> for Interval<T>
impl<T: PartialOrd> RangeBounds<T> for Interval<T>
Source§impl<T: RelativeEq + PartialOrd> RelativeEq for Interval<T>
Available on crate feature approx only.
impl<T: RelativeEq + PartialOrd> RelativeEq for Interval<T>
approx only.Source§fn default_max_relative() -> T::Epsilon
fn default_max_relative() -> T::Epsilon
Source§fn relative_eq(
&self,
other: &Self,
epsilon: T::Epsilon,
max_relative: T::Epsilon,
) -> bool
fn relative_eq( &self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon, ) -> bool
Source§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
RelativeEq::relative_eq.Source§impl<T: PartialOrd> TryFrom<(Option<T>, Option<T>)> for Interval<T>
impl<T: PartialOrd> TryFrom<(Option<T>, Option<T>)> for Interval<T>
Source§fn try_from(value: (Option<T>, Option<T>)) -> Result<Self, Self::Error>
fn try_from(value: (Option<T>, Option<T>)) -> Result<Self, Self::Error>
Create a new interval from a tuple of optional bounds.
The first element of the tuple is the lower bound, the second element is the upper bound.
If one of the bounds is None, the interval is one-sided.
If both bounds are None, an error is returned.
Source§type Error = IntervalError
type Error = IntervalError
Source§impl<T: PartialOrd> TryFrom<(T, T)> for Interval<T>
impl<T: PartialOrd> TryFrom<(T, T)> for Interval<T>
Source§fn try_from(value: (T, T)) -> Result<Self, Self::Error>
fn try_from(value: (T, T)) -> Result<Self, Self::Error>
Create a new interval from a tuple of bounds. The first element of the tuple is the lower bound, the second element is the upper bound. If the lower bound is greater than the upper bound, an error is returned.
Source§type Error = IntervalError
type Error = IntervalError
Source§impl<T: PartialOrd> TryFrom<RangeInclusive<T>> for Interval<T>
impl<T: PartialOrd> TryFrom<RangeInclusive<T>> for Interval<T>
Source§fn try_from(range: RangeInclusive<T>) -> Result<Self, Self::Error>
fn try_from(range: RangeInclusive<T>) -> Result<Self, Self::Error>
Create an interval from an inclusive range. The range must be non-empty or the function will return an error.
use stats_ci::Interval;
use std::ops::RangeInclusive;
let interval = Interval::try_from(1..=2);
assert!(interval.is_ok());
assert_eq!(interval.unwrap(), Interval::new(1, 2).unwrap());Source§type Error = IntervalError
type Error = IntervalError
Source§impl<T: UlpsEq + PartialOrd> UlpsEq for Interval<T>
Available on crate feature approx only.
impl<T: UlpsEq + PartialOrd> UlpsEq for Interval<T>
approx only.impl<T: PartialOrd + Copy> Copy for Interval<T>
impl<T> StructuralPartialEq for Interval<T>where
T: PartialOrd,
Auto Trait Implementations§
impl<T> Freeze for Interval<T>where
T: Freeze,
impl<T> RefUnwindSafe for Interval<T>where
T: RefUnwindSafe,
impl<T> Send for Interval<T>where
T: Send,
impl<T> Sync for Interval<T>where
T: Sync,
impl<T> Unpin for Interval<T>where
T: Unpin,
impl<T> UnwindSafe for Interval<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<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§fn to_subset_unchecked(&self) -> SS
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.