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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use num_traits::{One, Zero};
use std::ops::{Add, Div, Mul, Rem, Sub};
pub trait RingOperation: Sized + Add + Sub + Mul {}
impl<T> RingOperation for T where T: Sized + Add + Sub + Mul {}
pub trait RingOperationFrom:
Sized
+ for<'x> From<<&'x Self as Add>::Output>
+ for<'x> From<<&'x Self as Sub>::Output>
+ for<'x> From<<&'x Self as Mul>::Output>
where
for<'x> &'x Self: RingOperation,
{
}
impl<T> RingOperationFrom for T
where
T: Sized
+ for<'x> From<<&'x T as Add>::Output>
+ for<'x> From<<&'x T as Sub>::Output>
+ for<'x> From<<&'x T as Mul>::Output>,
for<'x> &'x T: RingOperation,
{
}
pub trait EuclideanRingOperation: RingOperation + Div + Rem {}
impl<T> EuclideanRingOperation for T where T: RingOperation + Div + Rem {}
pub trait EuclideanRingOperationFrom:
RingOperationFrom
+ for<'x> From<<&'x Self as Div>::Output>
+ for<'x> From<<&'x Self as Rem>::Output>
where
for<'x> &'x Self: EuclideanRingOperation,
{
}
impl<T> EuclideanRingOperationFrom for T
where
T: RingOperationFrom
+ for<'x> From<<&'x T as Div>::Output>
+ for<'x> From<<&'x T as Rem>::Output>,
for<'x> &'x T: EuclideanRingOperation,
{
}
pub trait RingNormalize {
#[must_use]
fn leading_unit(&self) -> Self;
fn normalize_mut(&mut self);
#[must_use]
fn into_normalize(mut self) -> Self
where
Self: Sized,
{
self.normalize_mut();
self
}
#[must_use]
fn normalize(&self) -> Self
where
Self: Clone,
{
self.clone().into_normalize()
}
fn is_similar(&self, other: &Self) -> bool
where
Self: Clone + Eq,
{
self.normalize() == other.normalize()
}
}
macro_rules! ring_normalize {
($t:ty) => {
impl RingNormalize for $t {
fn leading_unit(&self) -> Self {
if self >= &Self::zero() {
Self::one()
} else {
-Self::one()
}
}
fn normalize_mut(&mut self) {
*self = self.abs();
}
}
};
}
macro_rules! ring_normalize_unsigned {
($t:ty) => {
impl RingNormalize for $t {
fn leading_unit(&self) -> Self {
Self::one()
}
fn normalize_mut(&mut self) {}
}
};
}
ring_normalize!(i8);
ring_normalize!(i16);
ring_normalize!(i32);
ring_normalize!(i64);
ring_normalize!(i128);
ring_normalize!(isize);
ring_normalize_unsigned!(u8);
ring_normalize_unsigned!(u16);
ring_normalize_unsigned!(u32);
ring_normalize_unsigned!(u64);
ring_normalize_unsigned!(u128);
ring_normalize_unsigned!(usize);
#[cfg(feature = "num-bigint")]
impl RingNormalize for num_bigint::BigInt {
fn leading_unit(&self) -> Self {
if self.sign() == num_bigint::Sign::Minus {
-Self::one()
} else {
Self::one()
}
}
fn normalize_mut(&mut self) {
if self.sign() == num_bigint::Sign::Minus {
*self = -&*self
}
}
}
#[cfg(feature = "num-bigint")]
impl RingNormalize for num_bigint::BigUint {
fn leading_unit(&self) -> Self {
Self::one()
}
fn normalize_mut(&mut self) {}
}
#[cfg(feature = "rug")]
impl RingNormalize for rug::Integer {
fn leading_unit(&self) -> Self {
if self < &Self::zero() {
-Self::one()
} else {
Self::one()
}
}
fn normalize_mut(&mut self) {
self.abs_mut()
}
}