1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! Real spaces module.
use crate::prelude::*;
use std::fmt;

pub type Interval = crate::Interval<f64>;

/// Type representing the set of all real numbers.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Reals;

impl Reals {
    pub fn bounded(self, lb: f64, ub: f64) -> Interval { Interval::bounded(lb, ub) }

    pub fn lower_bounded(self, lb: f64) -> Interval { Interval::lower_bounded(lb) }

    pub fn upper_bounded(self, ub: f64) -> Interval { Interval::upper_bounded(ub) }
}

impl Space for Reals {
    const DIM: usize = 1;

    type Value = f64;

    fn card(&self) -> Card { Card::Infinite }

    fn contains(&self, _: &f64) -> bool { true }
}

impl OrderedSpace for Reals {}

impl_union_intersect!(Reals, Reals);

impl fmt::Display for Reals {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "\u{211d}") }
}

/// Type representing the set of non-negative real numbers, R(≥0).
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct NonNegativeReals;

impl Space for NonNegativeReals {
    const DIM: usize = 1;

    type Value = f64;

    fn card(&self) -> Card { Card::Infinite }

    fn contains(&self, val: &f64) -> bool { *val >= 0.0 }
}

impl OrderedSpace for NonNegativeReals {
    fn min(&self) -> Option<f64> { Some(0.0) }
}

impl_union_intersect!(NonNegativeReals, NonNegativeReals);

impl fmt::Display for NonNegativeReals {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "\u{211d}(>0)") }
}

/// Type representing the set of strictly positive real numbers, R(>0).
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct PositiveReals;

impl Space for PositiveReals {
    const DIM: usize = 1;

    type Value = f64;

    fn card(&self) -> Card { Card::Infinite }

    fn contains(&self, val: &f64) -> bool { *val > 0.0 }
}

impl OrderedSpace for PositiveReals {
    fn inf(&self) -> Option<f64> { Some(0.0) }
}

impl_union_intersect!(PositiveReals, PositiveReals);

impl fmt::Display for PositiveReals {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "\u{211d}(≥0)") }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "serialize")]
    extern crate serde_test;
    #[cfg(feature = "serialize")]
    use self::serde_test::{assert_tokens, Token};

    #[test]
    fn test_bounded() {
        let d = Reals;

        assert_eq!(d.bounded(0.0, 1.0), Interval::bounded(0.0, 1.0));
    }

    #[test]
    fn test_card() {
        let d = Reals;

        assert_eq!(d.card(), Card::Infinite);
    }

    #[cfg(feature = "serialize")]
    #[test]
    fn test_serialisation() {
        let d = Reals;

        assert_tokens(&d, &[Token::UnitStruct { name: "Reals" }]);
    }
}