feanor_math/
ordered.rs

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
119
120
121
use crate::ring::*;
use std::cmp::*;

///
/// Trait for rings that have a total ordering on their elements.
/// The ordering must be compatible with addition and multiplication
/// in the usual sense.
/// 
/// In particular, this should only be implemented for rings that are
/// subrings of the real numbers.
/// 
pub trait OrderedRing: RingBase {

    ///
    /// Returns whether `lhs` is [`Ordering::Less`], [`Ordering::Equal`] or [`Ordering::Greater`]
    /// than `rhs`.
    /// 
    fn cmp(&self, lhs: &Self::Element, rhs: &Self::Element) -> Ordering;

    ///
    /// Returns whether `abs(lhs)` is [`Ordering::Less`], [`Ordering::Equal`] or [`Ordering::Greater`]
    /// than `abs(rhs)`.
    /// 
    fn abs_cmp(&self, lhs: &Self::Element, rhs: &Self::Element) -> Ordering {
        self.cmp(&self.abs(self.clone_el(lhs)), &self.abs(self.clone_el(rhs)))
    }

    ///
    /// Returns whether `lhs <= rhs`.
    /// 
    fn is_leq(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool {
        self.cmp(lhs, rhs) != Ordering::Greater
    }
    
    ///
    /// Returns whether `lhs >= rhs`.
    /// 
    fn is_geq(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool {
        self.cmp(lhs, rhs) != Ordering::Less
    }

    ///
    /// Returns whether `lhs < rhs`.
    /// 
    fn is_lt(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool {
        self.cmp(lhs, rhs) == Ordering::Less
    }
    
    ///
    /// Returns whether `lhs > rhs`.
    /// 
    fn is_gt(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool {
        self.cmp(lhs, rhs) == Ordering::Greater
    }

    ///
    /// Returns whether `value < 0`.
    /// 
    fn is_neg(&self, value: &Self::Element) -> bool {
        self.is_lt(value, &self.zero())
    }

    ///
    /// Returns whether `value > 0`.
    /// 
    fn is_pos(&self, value: &Self::Element) -> bool {
        self.is_gt(value, &self.zero())
    }

    ///
    /// Returns the absolute value of `value`, i.e. `value` if `value >= 0` and `-value` otherwise.
    /// 
    fn abs(&self, value: Self::Element) -> Self::Element {
        if self.is_neg(&value) {
            self.negate(value)
        } else {
            value
        }
    }

    ///
    /// Returns the larger one of `fst` and `snd`.
    /// 
    fn max<'a>(&self, fst: &'a Self::Element, snd: &'a Self::Element) -> &'a Self::Element {
        if self.is_geq(fst, snd) {
            return fst;
        } else {
            return snd;
        }
    }
}

///
/// Trait for [`RingStore`]s that store [`OrderedRing`]s. Mainly used
/// to provide a convenient interface to the `OrderedRing`-functions.
/// 
pub trait OrderedRingStore: RingStore
    where Self::Type: OrderedRing
{
    delegate!{ OrderedRing, fn cmp(&self, lhs: &El<Self>, rhs: &El<Self>) -> Ordering }
    delegate!{ OrderedRing, fn abs_cmp(&self, lhs: &El<Self>, rhs: &El<Self>) -> Ordering }
    delegate!{ OrderedRing, fn is_leq(&self, lhs: &El<Self>, rhs: &El<Self>) -> bool }
    delegate!{ OrderedRing, fn is_geq(&self, lhs: &El<Self>, rhs: &El<Self>) -> bool }
    delegate!{ OrderedRing, fn is_lt(&self, lhs: &El<Self>, rhs: &El<Self>) -> bool }
    delegate!{ OrderedRing, fn is_gt(&self, lhs: &El<Self>, rhs: &El<Self>) -> bool }
    delegate!{ OrderedRing, fn is_neg(&self, value: &El<Self>) -> bool }
    delegate!{ OrderedRing, fn is_pos(&self, value: &El<Self>) -> bool }
    delegate!{ OrderedRing, fn abs(&self, value: El<Self>) -> El<Self> }
    
    ///
    /// See [`OrderedRing::max()`].
    /// 
    fn max<'a>(&self, fst: &'a El<Self>, snd: &'a El<Self>) -> &'a El<Self> {
        self.get_ring().max(fst, snd)
    }
}

impl<R: ?Sized> OrderedRingStore for R
    where R: RingStore,
        R::Type: OrderedRing
{}