feanor_math/rings/extension/
mod.rs

1
2use crate::algorithms::extension_ops;
3use crate::algorithms::linsolve::LinSolveRing;
4use crate::algorithms::poly_factor::FactorPolyField;
5use crate::divisibility::DivisibilityRing;
6use crate::field::*;
7use crate::pid::PrincipalIdealRing;
8use crate::ring::*;
9use crate::seq::*;
10use crate::homomorphism::*;
11use crate::wrapper::RingElementWrapper;
12use super::field::{AsField, AsFieldBase};
13use super::poly::dense_poly::DensePolyRing;
14use super::poly::{PolyRingStore, PolyRing};
15
16///
17/// Contains [`extension_impl::FreeAlgebraImpl`], an implementation of [`FreeAlgebra`] based
18/// on polynomial division.
19///
20pub mod extension_impl;
21
22///
23/// Contains [`galois_field::GaloisField`], an implementation of Galois fields.
24///
25pub mod galois_field;
26
27///
28/// Contains [`number_field::NumberField`], an implementation of number fields.
29/// 
30pub mod number_field;
31
32///
33/// A table of Conway polynomials, for standardized creation of finite fields.
34///
35pub mod conway;
36
37///
38/// A ring `R` that is an extension of a base ring `S`, generated by a single element
39/// that is algebraic resp. integral over `S`.
40///
41/// This is equivalent to rings generated by a single element that is a zero of a monic polynomial over the
42/// base ring. While sounding quite technical, this includes a wide class of important rings, like number
43/// fields or galois fields.
44/// One consequence of this is that `R` is a free `S`-module, with a basis given by the powers
45/// of [`FreeAlgebra::canonical_gen()`], which is where the name "free" comes from.
46///
47/// The main implementation is [`extension_impl::FreeAlgebraImpl`].
48///
49/// # Nontrivial Automorphisms
50///
51/// Rings of this form very often have nontrivial automorphisms. In order to simplify situations
52/// where morphisms or other objects are only unique up to isomorphism, canonical morphisms between rings
53/// of this type must also preserve the canonical generator.
54///
55/// # Examples
56/// 
57/// One of the most common use cases seems to be the implementation of finite fields (sometimes
58/// called galois fields).
59/// ```rust
60/// #![feature(allocator_api)]
61/// # use std::alloc::Global;
62/// # use feanor_math::assert_el_eq;
63/// # use feanor_math::ring::*;
64/// # use feanor_math::rings::zn::*;
65/// # use feanor_math::primitive_int::*;
66/// # use feanor_math::rings::extension::*;
67/// # use feanor_math::rings::extension::extension_impl::*;
68/// # use feanor_math::algorithms::convolution::*;
69/// # use feanor_math::field::FieldStore;
70/// # use feanor_math::divisibility::*;
71/// # use feanor_math::rings::finite::*;
72/// // we have to decide for an implementation of the prime field
73/// let prime_field = zn_static::Fp::<3>::RING;
74/// let galois_field = FreeAlgebraImpl::new(prime_field, 3, [2, 1]);
75/// // this is now the finite field with 27 elements, or F_27 or GF(27) since X^3 + 2X + 1 is irreducible modulo 3
76/// let galois_field = galois_field.as_field().ok().unwrap();
77/// assert_eq!(Some(27), galois_field.size(&StaticRing::<i64>::RING));
78/// for x in galois_field.elements() {
79///     if !galois_field.is_zero(&x) {
80///         let inv_x = galois_field.div(&galois_field.one(), &x);
81///         assert_el_eq!(galois_field, galois_field.one(), galois_field.mul(x, inv_x));
82///     }
83/// }
84/// // since galois fields are so important, an efficient construction is provided by feanor-math
85/// let galois_field_2 = galois_field::GaloisField::new_with_convolution(prime_field, 3, Global, STANDARD_CONVOLUTION);
86/// // note that the generating polynomial might be different, so it is not necessarily the "same" ring
87/// assert!(galois_field_2.can_iso(&galois_field).is_none());
88/// ```
89///
90pub trait FreeAlgebra: RingExtension {
91
92    ///
93    /// Type of the canonical-basis representation of a ring element, as returned by
94    /// [`FreeAlgebra::wrt_canonical_basis()`].
95    /// 
96    type VectorRepresentation<'a>: VectorFn<El<Self::BaseRing>>
97        where Self: 'a;
98
99    ///
100    /// Returns the fixed element that generates this ring as a free module over the base ring.
101    ///
102    fn canonical_gen(&self) -> Self::Element;
103
104    ///
105    /// Returns the rank of this ring as a free module over the base ring.
106    ///
107    fn rank(&self) -> usize;
108
109    ///
110    /// Returns the representation of the element w.r.t. the canonical basis, that is the basis given
111    /// by the powers `x^i` where `x` is the canonical generator given by [`FreeAlgebra::canonical_gen()`]
112    /// and `i` goes from `0` to `rank - 1`.
113    ///
114    /// In this sense, this is the opposite function to [`FreeAlgebra::from_canonical_basis()`].
115    ///
116    fn wrt_canonical_basis<'a>(&'a self, el: &'a Self::Element) -> Self::VectorRepresentation<'a>;
117
118    ///
119    /// Returns the element that has the given representation w.r.t. the canonical basis, that is the basis given
120    /// by the powers `x^i` where `x` is the canonical generator given by [`FreeAlgebra::canonical_gen()`]
121    /// and `i` goes from `0` to `rank - 1`.
122    ///
123    /// In this sense, this is the opposite function to [`FreeAlgebra::wrt_canonical_basis()`].
124    ///
125    fn from_canonical_basis<V>(&self, vec: V) -> Self::Element
126        where V: IntoIterator<Item = El<Self::BaseRing>>,
127            V::IntoIter: DoubleEndedIterator
128    {
129        let mut given_len = 0;
130        let x = self.canonical_gen();
131        let mut result = self.zero();
132        for c in vec.into_iter().rev() {
133            self.mul_assign_ref(&mut result, &x);
134            self.add_assign(&mut result, self.from(c));
135            given_len += 1;
136        }
137        assert_eq!(given_len, self.rank());
138        return result;
139    }
140
141    ///
142    /// Like [`FreeAlgebra::from_canonical_basis()`], this computes the sum `sum_i vec[i] * x^i` where `x` is the
143    /// canonical generator given by [`FreeAlgebra::canonical_gen()`]. Unlike [`FreeAlgebra::from_canonical_basis()`],
144    /// `vec` can return any number elements.
145    /// 
146    fn from_canonical_basis_extended<V>(&self, vec: V) -> Self::Element
147        where V: IntoIterator<Item = El<Self::BaseRing>>
148    {
149        extension_ops::from_canonical_basis_extended(self, vec)
150    }
151
152    ///
153    /// Computes the characteristic polynomial of the given element.
154    /// 
155    /// The characteristic polynomial is `det(XI - B)`, where `B` is the
156    /// matrix representation of the given element `b`, or equivalently the
157    /// matrix representation of the multiplication-by-`b` map `R^n -> R^n`.
158    /// 
159    fn charpoly<P, H>(&self, el: &Self::Element, poly_ring: P, hom: H) -> El<P>
160        where P: RingStore,
161            P::Type: PolyRing,
162            <<P::Type as RingExtension>::BaseRing as RingStore>::Type: LinSolveRing,
163            H: Homomorphism<<Self::BaseRing as RingStore>::Type, <<P::Type as RingExtension>::BaseRing as RingStore>::Type>
164    {
165        extension_ops::charpoly(self, el, poly_ring, hom)
166    }
167    
168    ///
169    /// Computes the (or a) minimal polynomial of the given element.
170    /// 
171    /// The minimal polynomial is the monic polynomial of minimal degree that
172    /// has the given value as a root. Its degree is always at least 1 and at
173    /// most [`FreeAlgebra::rank()`]. If the base ring is a principal ideal domain, 
174    /// then the minimal polynomial is unique. 
175    /// 
176    /// Note that the existence of the minimal polynomial is a consequence of the
177    /// Cayley-Hamilton theorem, i.e. `det(XI - A)(A) = 0` for a square matrix over
178    /// any ring `R`. In particular, representing element of the ring `R[a]` a matrices
179    /// over `R`, we find that `det(XI - B)(b) = 0` for any element `b`, thus `b` must
180    /// be the root of a monic polynomial of degree `<= n`.
181    /// 
182    fn minpoly<P, H>(&self, el: &Self::Element, poly_ring: P, hom: H) -> El<P>
183        where P: RingStore,
184            P::Type: PolyRing,
185            <<P::Type as RingExtension>::BaseRing as RingStore>::Type: LinSolveRing,
186            H: Homomorphism<<Self::BaseRing as RingStore>::Type, <<P::Type as RingExtension>::BaseRing as RingStore>::Type>
187    {
188        extension_ops::minpoly(self, el, poly_ring, hom)
189    }
190
191    ///
192    /// Computes the trace of an element `a` in this ring extension, which is defined as the
193    /// matrix trace of the multiplication-by-`a` map.
194    /// 
195    /// In nice extensions, the trace has many characterizations. For example, in a Galois
196    /// field extension, it is the sum of `sigma(a)` as `sigma` runs through the Galois group 
197    /// of the extension. It is also equal to +/- the second largest coefficient of the
198    /// characteristic polynomial of the element.
199    /// 
200    fn trace(&self, el: Self::Element) -> El<Self::BaseRing> {
201        let mut current = el;
202        let generator = self.canonical_gen();
203        return self.base_ring().sum((0..self.rank()).map(|i| {
204            let result = self.wrt_canonical_basis(&current).at(i);
205            self.mul_assign_ref(&mut current, &generator);
206            return result;
207        }));
208    }
209
210    ///
211    /// Computes the discriminant of the canonical basis of this ring extension, 
212    /// which is defined as the determinant of the trace matrix `(Tr(a^(i + j)))`, 
213    /// where `a` is the canonical generator of this ring extension.
214    /// 
215    /// Note that the discriminant in this sense depends on the choice of the canonical 
216    /// generator. In particular, this is usually different from the discriminant of an
217    /// algebraic number field, since that is defined as the discriminant w.r.t. the basis
218    /// that generates the maximal order. On the other hand, this means that if this ring
219    /// is an order in number field, this discriminant is exactly the discriminant of the
220    /// order as considered in algebraic number theory.
221    /// 
222    fn discriminant(&self) -> El<Self::BaseRing>
223        where <Self::BaseRing as RingStore>::Type: PrincipalIdealRing
224    {
225        extension_ops::discriminant(self)
226    }
227}
228
229///
230/// [`RingStore`] for [`FreeAlgebra`].
231/// 
232pub trait FreeAlgebraStore: RingStore
233    where Self::Type: FreeAlgebra
234{
235    delegate!{ FreeAlgebra, fn canonical_gen(&self) -> El<Self> }
236    delegate!{ FreeAlgebra, fn rank(&self) -> usize }
237    delegate!{ FreeAlgebra, fn trace(&self, el: El<Self>) -> El<<Self::Type as RingExtension>::BaseRing> }
238
239    ///
240    /// See [`FreeAlgebra::wrt_canonical_basis()`].
241    ///
242    fn wrt_canonical_basis<'a>(&'a self, el: &'a El<Self>) -> <Self::Type as FreeAlgebra>::VectorRepresentation<'a> {
243        self.get_ring().wrt_canonical_basis(el)
244    }
245
246    ///
247    /// See [`FreeAlgebra::from_canonical_basis()`].
248    ///
249    fn from_canonical_basis<V>(&self, vec: V) -> El<Self>
250        where V: IntoIterator<Item = El<<Self::Type as RingExtension>::BaseRing>>,
251            V::IntoIter: DoubleEndedIterator
252    {
253        self.get_ring().from_canonical_basis(vec)
254    }
255
256    ///
257    /// See [`FreeAlgebra::from_canonical_basis_extended()`].
258    ///
259    fn from_canonical_basis_extended<V>(&self, vec: V) -> El<Self>
260        where V: IntoIterator<Item = El<<Self::Type as RingExtension>::BaseRing>>
261    {
262        self.get_ring().from_canonical_basis_extended(vec)
263    }
264
265    ///
266    /// Returns the generating polynomial of this ring, i.e. the monic polynomial `f(X)` such that this ring is isomorphic
267    /// to `R[X]/(f(X))`, where `R` is the base ring.
268    ///
269    fn generating_poly<P, H>(&self, poly_ring: P, hom: H) -> El<P>
270        where P: PolyRingStore,
271            P::Type: PolyRing,
272            H: Homomorphism<<<Self::Type as RingExtension>::BaseRing as RingStore>::Type, <<P::Type as RingExtension>::BaseRing as RingStore>::Type>
273    {
274        assert!(hom.domain().get_ring() == self.base_ring().get_ring());
275        poly_ring.sub(
276            poly_ring.from_terms([(poly_ring.base_ring().one(), self.rank())].into_iter()),
277            self.poly_repr(&poly_ring, &self.pow(self.canonical_gen(), self.rank()), hom)
278        )
279    }
280
281    ///
282    /// If this ring is a field, returns a wrapper around this ring that implements [`crate::field::FieldStore`].
283    ///
284    /// For details, see [`crate::rings::field::AsField`].
285    ///
286    fn as_field(self) -> Result<AsField<Self>, Self>
287        where Self::Type: DivisibilityRing,
288            <<Self::Type as RingExtension>::BaseRing as RingStore>::Type: Field + FactorPolyField
289    {
290        let poly_ring = DensePolyRing::new(self.base_ring(), "X");
291        if <_ as FactorPolyField>::factor_poly(&poly_ring, &self.generating_poly(&poly_ring, self.base_ring().identity())).0.len() > 1 {
292            return Err(self);
293        } else {
294            return Ok(RingValue::from(AsFieldBase::promise_is_perfect_field(self)));
295        }
296    }
297
298    ///
299    /// Returns the polynomial representation of the given element `y`, i.e. the polynomial `f(X)` of degree at most
300    /// [`FreeAlgebraStore::rank()`] such that `f(x) = y`, where `y` is the canonical generator of this ring, as given by
301    /// [`FreeAlgebraStore::canonical_gen()`].
302    ///
303    fn poly_repr<P, H>(&self, to: P, el: &El<Self>, hom: H) -> El<P>
304        where P: PolyRingStore,
305            P::Type: PolyRing,
306            H: Homomorphism<<<Self::Type as RingExtension>::BaseRing as RingStore>::Type, <<P::Type as RingExtension>::BaseRing as RingStore>::Type>
307    {
308        let coeff_vec = self.wrt_canonical_basis(el);
309        to.from_terms(
310            (0..self.rank()).map(|i| coeff_vec.at(i)).enumerate()
311                .filter(|(_, x)| !self.base_ring().is_zero(x))
312                .map(|(j, x)| (hom.map(x), j))
313        )
314    }
315    
316    ///
317    /// Computes the discriminant of the canonical basis of this ring extension, 
318    /// which is defined as the determinant of the trace matrix `(Tr(a^(i + j)))`, 
319    /// where `a` is the canonical generator of this ring extension.
320    /// 
321    /// See also [`FreeAlgebra::discriminant()`].
322    /// 
323    fn discriminant(&self) -> El<<Self::Type as RingExtension>::BaseRing>
324        where <<Self::Type as RingExtension>::BaseRing as RingStore>::Type: PrincipalIdealRing
325    {
326        self.get_ring().discriminant()
327    }
328
329    /// 
330    /// See also [`FreeAlgebra::charpoly()`].
331    /// 
332    fn charpoly<P, H>(&self, el: &El<Self>, poly_ring: P, hom: H) -> El<P>
333        where P: RingStore,
334            P::Type: PolyRing,
335            <<P::Type as RingExtension>::BaseRing as RingStore>::Type: LinSolveRing,
336            H: Homomorphism<<<Self::Type as RingExtension>::BaseRing as RingStore>::Type, <<P::Type as RingExtension>::BaseRing as RingStore>::Type>
337    {
338        self.get_ring().charpoly(el, poly_ring, hom)
339    }
340
341    ///
342    /// Temporarily wraps the canonical generator in a [`RingElementWrapper`], for more
343    /// natural creation of ring elements.
344    /// 
345    #[stability::unstable(feature = "enable")]
346    fn with_wrapped_generator<'a, F, const M: usize>(&'a self, f: F) -> [El<Self>; M]
347        where F: FnOnce(&RingElementWrapper<&'a Self>) -> [RingElementWrapper<&'a Self>; M]
348    {
349        let wrapped_indet = RingElementWrapper::new(self, self.canonical_gen());
350        let mut result_it = f(&wrapped_indet).into_iter();
351        return std::array::from_fn(|_| result_it.next().unwrap().unwrap());
352    }
353}
354
355impl<R: RingStore> FreeAlgebraStore for R
356    where R::Type: FreeAlgebra
357{}
358
359#[stability::unstable(feature = "enable")]
360pub struct FreeAlgebraHom<R, S>
361    where R: RingStore,
362        R::Type: FreeAlgebra,
363        S: RingStore,
364        S::Type: FreeAlgebra,
365        <S::Type as RingExtension>::BaseRing: RingStore<Type = <<R::Type as RingExtension>::BaseRing as RingStore>::Type>
366{
367    from: R,
368    to: S,
369    image_of_generator: El<S>
370}
371
372impl<R> FreeAlgebraHom<R, R>
373    where R: RingStore + Clone,
374        R::Type: FreeAlgebra
375{
376    #[stability::unstable(feature = "enable")]
377    pub fn identity(ring: R) -> Self {
378        Self {
379            image_of_generator: ring.canonical_gen(),
380            from: ring.clone(),
381            to: ring
382        }
383    }
384}
385
386impl<R, S> FreeAlgebraHom<R, S>
387    where R: RingStore,
388        R::Type: FreeAlgebra,
389        S: RingStore,
390        S::Type: FreeAlgebra,
391        <S::Type as RingExtension>::BaseRing: RingStore<Type = <<R::Type as RingExtension>::BaseRing as RingStore>::Type>
392{
393    ///
394    /// Creates a new [`FreeAlgebraHom`] from `R` to `S`, mapping the canonical
395    /// generator of `R` to the given element of `S`. This assumes that the resulting
396    /// homomorphism is well-defined, i.e. the generating polynomial of `R` evaluated
397    /// at `image_of_generator` gives zero in `S`.
398    /// 
399    /// The checked variant of this function is [`FreeAlgebraHom::new()`].
400    /// 
401    #[stability::unstable(feature = "enable")]
402    pub fn promise_is_well_defined(from: R, to: S, image_of_generator: El<S>) -> Self {
403        assert!(from.base_ring().get_ring() == to.base_ring().get_ring());
404        Self {
405            from: from,
406            to: to,
407            image_of_generator: image_of_generator
408        }
409    }
410
411    ///
412    /// Creates a new [`FreeAlgebraHom`] from `R` to `S`, mapping the canonical
413    /// generator of `R` to the given element of `S`.
414    /// 
415    /// As opposed to [`FreeAlgebraHom::promise_is_well_defined()`], this function
416    /// checks that the resulting homomorphism is well-defined.
417    /// 
418    #[stability::unstable(feature = "enable")]
419    pub fn new(from: R, to: S, image_of_generator: El<S>) -> Self {
420        assert!(from.base_ring().get_ring() == to.base_ring().get_ring());
421        let poly_ring = DensePolyRing::new(to.base_ring(), "X");
422        assert!(to.is_zero(&poly_ring.evaluate(&from.generating_poly(&poly_ring, poly_ring.base_ring().identity()), &image_of_generator, to.inclusion())));
423        Self {
424            from: from,
425            to: to,
426            image_of_generator: image_of_generator
427        }
428    }
429
430    ///
431    /// Consumes this object, producing the domain ring store, the codomain ring store
432    /// and the image of the canonical generator of the domain number field.
433    /// 
434    #[stability::unstable(feature = "enable")]
435    pub fn destruct(self) -> (R, S, El<S>) {
436        (self.from, self.to, self.image_of_generator)
437    }
438}
439
440impl<R, S> Homomorphism<R::Type, S::Type> for FreeAlgebraHom<R, S>
441    where R: RingStore,
442        R::Type: FreeAlgebra,
443        S: RingStore,
444        S::Type: FreeAlgebra,
445        <S::Type as RingExtension>::BaseRing: RingStore<Type = <<R::Type as RingExtension>::BaseRing as RingStore>::Type>
446{
447    type DomainStore = R;
448    type CodomainStore = S;
449
450    fn domain<'a>(&'a self) -> &'a Self::DomainStore {
451        &self.from
452    }
453
454    fn codomain<'a>(&'a self) -> &'a Self::CodomainStore {
455        &self.to
456    }
457
458    fn map(&self, x: El<R>) -> El<S> {
459        self.map_ref(&x)
460    }
461
462    fn map_ref(&self, x: &El<R>) -> El<S> {
463        let poly_ring = DensePolyRing::new(self.to.base_ring(), "X");
464        return poly_ring.evaluate(
465            &self.from.poly_repr(&poly_ring, &x, self.to.base_ring().identity()),
466            &self.image_of_generator,
467            self.to.inclusion()
468        )
469    }
470}
471
472#[cfg(any(test, feature = "generic_tests"))]
473pub mod generic_tests {
474    use super::*;
475
476    pub fn test_free_algebra_axioms<R: FreeAlgebraStore>(ring: R)
477        where R::Type: FreeAlgebra
478    {
479        let x = ring.canonical_gen();
480        let n = ring.rank();
481
482        let xn_original = ring.pow(ring.clone_el(&x), n);
483        let xn_vec = ring.wrt_canonical_basis(&xn_original);
484        let xn = ring.sum(Iterator::map(0..n, |i| ring.mul(ring.inclusion().map(xn_vec.at(i)), ring.pow(ring.clone_el(&x), i))));
485        assert_el_eq!(ring, xn_original, xn);
486
487        let x_n_1_vec_expected = (0..n).map_fn(|i| if i > 0 {
488            ring.base_ring().add(ring.base_ring().mul(xn_vec.at(n - 1), xn_vec.at(i)), xn_vec.at(i - 1))
489        } else {
490            ring.base_ring().mul(xn_vec.at(n - 1), xn_vec.at(0))
491        });
492        let x_n_1 = ring.pow(ring.clone_el(&x), n + 1);
493        let x_n_1_vec_actual = ring.wrt_canonical_basis(&x_n_1);
494        for i in 0..n {
495            assert_el_eq!(ring.base_ring(), x_n_1_vec_expected.at(i), x_n_1_vec_actual.at(i));
496        }
497
498        // test basis wrt_root_of_unity_basis linearity and compatibility from_root_of_unity_basis/wrt_root_of_unity_basis
499        for i in (0..ring.rank()).step_by(5) {
500            for j in (1..ring.rank()).step_by(7) {
501                if i == j {
502                    continue;
503                }
504                let element = ring.from_canonical_basis((0..n).map(|k| if k == i { ring.base_ring().one() } else if k == j { ring.base_ring().int_hom().map(2) } else { ring.base_ring().zero() }));
505                let expected = ring.add(ring.pow(ring.clone_el(&x), i), ring.int_hom().mul_map(ring.pow(ring.clone_el(&x), j), 2));
506                assert_el_eq!(ring, expected, element);
507                let element_vec = ring.wrt_canonical_basis(&expected);
508                for k in 0..ring.rank() {
509                    if k == i {
510                        assert_el_eq!(ring.base_ring(), ring.base_ring().one(), element_vec.at(k));
511                    } else if k == j {
512                        assert_el_eq!(ring.base_ring(), ring.base_ring().int_hom().map(2), element_vec.at(k));
513                    } else {
514                        assert_el_eq!(ring.base_ring(), ring.base_ring().zero(), element_vec.at(k));
515                    }
516                }
517            }
518        }
519    }
520}