feanor_math/
field.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
use crate::divisibility::Domain;
use crate::ring::*;
use crate::pid::*;

///
/// Trait for rings that are fields, i.e. where every
/// nonzero element has an inverse.
/// 
/// Note that fields must be commutative.
/// 
pub trait Field: Domain + EuclideanRing {

    fn div(&self, lhs: &Self::Element, rhs: &Self::Element) -> Self::Element {
        assert!(!self.is_zero(rhs));
        assert!(self.is_commutative());
        return self.checked_left_div(lhs, rhs).unwrap();
    }
}

///
/// Trait for [`RingStore`]s that store [`Field`]s. Mainly used
/// to provide a convenient interface to the `Field`-functions.
/// 
pub trait FieldStore: RingStore + EuclideanRingStore
    where Self::Type: Field
{
    delegate!{ Field, fn div(&self, lhs: &El<Self>, rhs: &El<Self>) -> El<Self> }
}

impl<R> FieldStore for R
    where R: RingStore, R::Type: Field
{}

///
/// Field such that every finite degree extension field is separable.
/// 
/// This is equivalent to the following:
///  - Every irreducible polynomial is square-free (over the algebraic closure)
///  - Every finite-degree field extension is simple, i.e. generated by a single element
/// 
/// Note that currently, I sometimes make the assumption that a `PerfectField` is either
/// finite, or has characteristic 0. This is clearly not the case in general, but all perfect
/// fields that currently exist are one of those. I would even say that infinite perfect fields
/// of positive characteristic are quite an exotic case in computer algebra, and I do not expect
/// to implement them in the forseeable future. The simplest example of such a field would be
/// the algebraic closure of the function field over a finite field, i.e. `AlgClosure(Fp(X))`.
/// 
pub trait PerfectField: Field {}

#[cfg(any(test, feature = "generic_tests"))]
pub mod generic_tests {
    use super::*;

    pub fn test_field_axioms<R, I>(R: R, edge_case_elements: I)
        where R: FieldStore, R::Type: Field, I: Iterator<Item = El<R>>
    {
        let edge_case_elements = edge_case_elements.collect::<Vec<_>>();
        for (i, a) in edge_case_elements.iter().enumerate() {
            for (j, b) in edge_case_elements.iter().enumerate() {
                assert!(i == j || !R.eq_el(&a, &b));
                if !R.is_zero(&b) {
                    assert_el_eq!(R, a, R.mul_ref_fst(&b, R.div(&a, &b)));
                }
            }
        }
    }
}