Skip to main content

feanor_math/
ordered.rs

1use std::cmp::*;
2
3use crate::ring::*;
4
5/// Trait for rings that have a total ordering on their elements.
6/// The ordering must be compatible with addition and multiplication
7/// in the usual sense.
8///
9/// In particular, this should only be implemented for rings that are
10/// subrings of the real numbers.
11pub trait OrderedRing: RingBase {
12    /// Returns whether `lhs` is [`Ordering::Less`], [`Ordering::Equal`] or [`Ordering::Greater`]
13    /// than `rhs`.
14    fn cmp(&self, lhs: &Self::Element, rhs: &Self::Element) -> Ordering;
15
16    /// Returns whether `abs(lhs)` is [`Ordering::Less`], [`Ordering::Equal`] or
17    /// [`Ordering::Greater`] than `abs(rhs)`.
18    fn abs_cmp(&self, lhs: &Self::Element, rhs: &Self::Element) -> Ordering {
19        self.cmp(&self.abs(self.clone_el(lhs)), &self.abs(self.clone_el(rhs)))
20    }
21
22    /// Returns whether `lhs <= rhs`.
23    fn is_leq(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool { self.cmp(lhs, rhs) != Ordering::Greater }
24
25    /// Returns whether `lhs >= rhs`.
26    fn is_geq(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool { self.cmp(lhs, rhs) != Ordering::Less }
27
28    /// Returns whether `lhs < rhs`.
29    fn is_lt(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool { self.cmp(lhs, rhs) == Ordering::Less }
30
31    /// Returns whether `lhs > rhs`.
32    fn is_gt(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool { self.cmp(lhs, rhs) == Ordering::Greater }
33
34    /// Returns whether `value < 0`.
35    fn is_neg(&self, value: &Self::Element) -> bool { self.is_lt(value, &self.zero()) }
36
37    /// Returns whether `value > 0`.
38    fn is_pos(&self, value: &Self::Element) -> bool { self.is_gt(value, &self.zero()) }
39
40    /// Returns the absolute value of `value`, i.e. `value` if `value >= 0` and `-value` otherwise.
41    fn abs(&self, value: Self::Element) -> Self::Element {
42        if self.is_neg(&value) { self.negate(value) } else { value }
43    }
44
45    /// Returns the larger one of `fst` and `snd`.
46    fn max<'a>(&self, fst: &'a Self::Element, snd: &'a Self::Element) -> &'a Self::Element {
47        if self.is_geq(fst, snd) {
48            return fst;
49        } else {
50            return snd;
51        }
52    }
53}
54
55/// Trait for [`RingStore`]s that store [`OrderedRing`]s. Mainly used
56/// to provide a convenient interface to the `OrderedRing`-functions.
57pub trait OrderedRingStore: RingStore
58where
59    Self::Type: OrderedRing,
60{
61    delegate! { OrderedRing, fn cmp(&self, lhs: &El<Self>, rhs: &El<Self>) -> Ordering }
62    delegate! { OrderedRing, fn abs_cmp(&self, lhs: &El<Self>, rhs: &El<Self>) -> Ordering }
63    delegate! { OrderedRing, fn is_leq(&self, lhs: &El<Self>, rhs: &El<Self>) -> bool }
64    delegate! { OrderedRing, fn is_geq(&self, lhs: &El<Self>, rhs: &El<Self>) -> bool }
65    delegate! { OrderedRing, fn is_lt(&self, lhs: &El<Self>, rhs: &El<Self>) -> bool }
66    delegate! { OrderedRing, fn is_gt(&self, lhs: &El<Self>, rhs: &El<Self>) -> bool }
67    delegate! { OrderedRing, fn is_neg(&self, value: &El<Self>) -> bool }
68    delegate! { OrderedRing, fn is_pos(&self, value: &El<Self>) -> bool }
69    delegate! { OrderedRing, fn abs(&self, value: El<Self>) -> El<Self> }
70
71    /// See [`OrderedRing::max()`].
72    fn max<'a>(&self, fst: &'a El<Self>, snd: &'a El<Self>) -> &'a El<Self> { self.get_ring().max(fst, snd) }
73}
74
75impl<R: ?Sized> OrderedRingStore for R
76where
77    R: RingStore,
78    R::Type: OrderedRing,
79{
80}