Skip to content

Commit

Permalink
Allow extending algebraic extensions in Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
benruijl committed Aug 25, 2024
1 parent 14f2f7b commit af2fa52
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/api/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7816,6 +7816,42 @@ impl PythonNumberFieldPolynomial {
})
.into())
}

/// Get the minimal polynomial of the algebraic extension.
pub fn get_minimal_polynomial(&self) -> PythonPolynomial {
PythonPolynomial {
poly: self.poly.ring.poly().clone(),
}
}

/// Extend the coefficient ring of this polynomial `R[a]` with `b`, whose minimal polynomial
/// is `R[a][b]` and form `R[b]`. Also return the new representation of `a` and `b`.
///
/// `b` must be irreducible over `R` and `R[a]`; this is not checked.
pub fn extend(&self, b: Self) -> (Self, PythonPolynomial, PythonPolynomial) {
let (new_field, map1, map2) = self.poly.ring.extend(&b.poly);

(
Self {
poly: self.poly.map_coeff(
|f| {
let mut new_num = new_field.zero();
for (p, coeff) in f.poly.coefficients.iter().enumerate() {
new_field.add_assign(
&mut new_num,
&new_field.pow(&map1, p as u64).mul_coeff(coeff.clone()),
);
}

new_num
},
new_field.clone(),
),
},
PythonPolynomial { poly: map1.poly },
PythonPolynomial { poly: map2.poly },
)
}
}

/// A Symbolica rational polynomial.
Expand Down
8 changes: 7 additions & 1 deletion src/domains/algebraic_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ impl<R: Ring> std::fmt::Display for AlgebraicNumber<R> {
}

impl<R: Ring> AlgebraicNumber<R> {
pub fn mul_coeff(self, c: R::Element) -> Self {
AlgebraicNumber {
poly: self.poly.mul_coeff(c),
}
}

pub fn to_finite_field<UField: FiniteFieldWorkspace>(
&self,
field: &FiniteField<UField>,
Expand Down Expand Up @@ -559,7 +565,7 @@ impl<R: Field + PolynomialGCD<u16>> AlgebraicExtension<R> {
/// Extend the current algebraic extension `R[a]` with `b`, whose minimal polynomial
/// is `R[a][b]` and form `R[b]`. Also return the new representation of `a` and `b`.
///
/// `b` must be irreducible over `R` and `R[a]`; this is not checked.
/// `b` must be irreducible over `R` and `R[a]`; this is not checked.
pub fn extend(
&self,
b: &MultivariatePolynomial<AlgebraicExtension<R>>,
Expand Down
9 changes: 9 additions & 0 deletions symbolica.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,15 @@ class NumberFieldPolynomial:
>>> p.replace(x, r)
"""

def get_minimal_polynomial(self) -> Polynomial:
"""Get the minimal polynomial of the algebraic extension."""

def extend(self, b: NumberFieldPolynomial) -> Tuple[NumberFieldPolynomial, Polynomial, Polynomial]:
"""Extend the coefficient ring of this polynomial `R[a]` with `b`, whose minimal polynomial
is `R[a][b]` and form `R[b]`. Also return the new representation of `a` and `b`.
`b` must be irreducible over `R` and `R[a]`; this is not checked."""


class FiniteFieldPolynomial:
"""A Symbolica polynomial with finite field coefficients."""
Expand Down

0 comments on commit af2fa52

Please sign in to comment.