feanor_math/
field.rs

1use crate::divisibility::Domain;
2use crate::ring::*;
3use crate::pid::*;
4
5///
6/// Trait for rings that are fields, i.e. where every
7/// nonzero element has an inverse.
8/// 
9/// Note that fields must be commutative.
10/// 
11pub trait Field: Domain + EuclideanRing {
12
13    fn div(&self, lhs: &Self::Element, rhs: &Self::Element) -> Self::Element {
14        assert!(!self.is_zero(rhs));
15        assert!(self.is_commutative());
16        return self.checked_left_div(lhs, rhs).unwrap();
17    }
18}
19
20///
21/// Trait for [`RingStore`]s that store [`Field`]s. Mainly used
22/// to provide a convenient interface to the `Field`-functions.
23/// 
24pub trait FieldStore: RingStore + EuclideanRingStore
25    where Self::Type: Field
26{
27    delegate!{ Field, fn div(&self, lhs: &El<Self>, rhs: &El<Self>) -> El<Self> }
28}
29
30impl<R> FieldStore for R
31    where R: RingStore, R::Type: Field
32{}
33
34///
35/// Field such that every finite degree extension field is separable.
36/// 
37/// This is equivalent to the following:
38///  - Every irreducible polynomial is square-free (over the algebraic closure)
39///  - Every finite-degree field extension is simple, i.e. generated by a single element
40/// 
41/// Note that currently, I sometimes make the assumption that a `PerfectField` is either
42/// finite, or has characteristic 0. This is clearly not the case in general, but all perfect
43/// fields that currently exist are one of those. I would even say that infinite perfect fields
44/// of positive characteristic are quite an exotic case in computer algebra, and I do not expect
45/// to implement them in the forseeable future. The simplest example of such a field would be
46/// the algebraic closure of the function field over a finite field, i.e. `AlgClosure(Fp(X))`.
47/// 
48pub trait PerfectField: Field {}
49
50#[cfg(any(test, feature = "generic_tests"))]
51pub mod generic_tests {
52    use super::*;
53
54    pub fn test_field_axioms<R, I>(R: R, edge_case_elements: I)
55        where R: FieldStore, R::Type: Field, I: Iterator<Item = El<R>>
56    {
57        let edge_case_elements = edge_case_elements.collect::<Vec<_>>();
58        for (i, a) in edge_case_elements.iter().enumerate() {
59            for (j, b) in edge_case_elements.iter().enumerate() {
60                assert!(i == j || !R.eq_el(&a, &b));
61                if !R.is_zero(&b) {
62                    assert_el_eq!(R, a, R.mul_ref_fst(&b, R.div(&a, &b)));
63                }
64            }
65        }
66    }
67}