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