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
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 {

    fn cmp(&self, lhs: &Self::Element, rhs: &Self::Element) -> Ordering;

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

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

    fn is_neg(&self, value: &Self::Element) -> bool {
        self.is_lt(value, &self.zero())
    }

    fn is_pos(&self, value: &Self::Element) -> bool {
        self.is_gt(value, &self.zero())
    }

    fn abs(&self, value: Self::Element) -> Self::Element {
        if self.is_neg(&value) {
            self.negate(value)
        } else {
            value
        }
    }
}

///
/// 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 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> }
}

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