From 9e0543dd8fde3a150198acb19b0a5bcc5781d243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Thu, 27 Mar 2025 09:45:10 +0100 Subject: [PATCH] typing annotation in Chow ring ideal --- src/sage/matroids/chow_ring_ideal.py | 70 ++++++++++++++-------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/src/sage/matroids/chow_ring_ideal.py b/src/sage/matroids/chow_ring_ideal.py index f7b55ad40d7..b8b7abe45eb 100644 --- a/src/sage/matroids/chow_ring_ideal.py +++ b/src/sage/matroids/chow_ring_ideal.py @@ -15,12 +15,13 @@ # https://www.gnu.org/licenses/ # **************************************************************************** -from sage.rings.polynomial.multi_polynomial_ideal import MPolynomialIdeal +from itertools import product + +from sage.combinat.posets.posets import Poset from sage.matroids.utilities import cmp_elements_key -from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing +from sage.rings.polynomial.multi_polynomial_ideal import MPolynomialIdeal from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence -from sage.combinat.posets.posets import Poset -from itertools import product +from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing class ChowRingIdeal(MPolynomialIdeal): @@ -37,7 +38,7 @@ def matroid(self): """ return self._matroid - def _lattice_flats(self): + def _lattice_flats(self) -> tuple: r""" Return the ranks and chains of lattice of flats of the matroid. @@ -65,7 +66,7 @@ def _lattice_flats(self): chains = lattice_flats.chains() # Only chains return (ranks, chains) - def flats_to_generator_dict(self): + def flats_to_generator_dict(self) -> dict: r""" Return the corresponding generators of flats/groundset elements of Chow ring ideal. @@ -146,7 +147,7 @@ class ChowRingIdeal_nonaug(ChowRingIdeal): Chow ring ideal of Fano: Binary matroid of rank 3 on 7 elements, type (3, 0) - non augmented """ - def __init__(self, M, R): + def __init__(self, M, R) -> None: r""" Initialize ``self``. @@ -167,7 +168,7 @@ def __init__(self, M, R): self._flats_generator = dict(zip(flats, gens)) MPolynomialIdeal.__init__(self, poly_ring, self._gens_constructor(poly_ring)) - def _gens_constructor(self, poly_ring): + def _gens_constructor(self, poly_ring) -> list: r""" Return the generators of ``self``. @@ -215,7 +216,7 @@ def _gens_constructor(self, poly_ring): J = list(atoms_gen.values()) # Linear generators return I + J - def _repr_(self): + def _repr_(self) -> str: r""" Return a string representation of ``self``. @@ -228,7 +229,7 @@ def _repr_(self): """ return "Chow ring ideal of {} - non augmented".format(self._matroid) - def _latex_(self): + def _latex_(self) -> str: r""" Return a LaTeX representation of ``self``. @@ -289,7 +290,7 @@ def groebner_basis(self, algorithm='', *args, **kwargs): term += flats_gen[G] for G in lattice_flats.order_ideal([F]): if G != F: - gb.append(flats_gen[G]*(term) ** (ranks[F] - ranks[G])) + gb.append(flats_gen[G] * (term) ** (ranks[F] - ranks[G])) gb.append(term ** ranks[F]) @@ -420,7 +421,7 @@ class AugmentedChowRingIdeal_fy(ChowRingIdeal): Augmented Chow ring ideal of Wheel(3): Regular matroid of rank 3 on 6 elements with 16 bases of Feitchner-Yuzvinsky presentation """ - def __init__(self, M, R): + def __init__(self, M, R) -> None: r""" Initialize ``self``. @@ -450,7 +451,7 @@ def __init__(self, M, R): self._flats_containing[x].append(F) MPolynomialIdeal.__init__(self, poly_ring, self._gens_constructor(poly_ring)) - def _gens_constructor(self, poly_ring): + def _gens_constructor(self, poly_ring) -> list: r""" Return the generators of ``self``. @@ -488,12 +489,13 @@ def _gens_constructor(self, poly_ring): A4*B, A3*B, A2*B, A1*B, A0*B] """ E = list(self._matroid.groundset()) - Q = [] L = [] lattice_flats = Poset((self._flats, lambda x, y: x <= y)) antichains = lattice_flats.antichains().elements_of_depth_iterator(2) - for F, G in antichains: - Q.append(self._flats_generator[F] * self._flats_generator[G]) # Quadratic generators + + # Quadratic generators + Q = [self._flats_generator[F] * self._flats_generator[G] + for F, G in antichains] for x in E: term = poly_ring.zero() @@ -508,7 +510,7 @@ def _gens_constructor(self, poly_ring): L.append(term1) return Q + L - def _repr_(self): + def _repr_(self) -> str: r""" EXAMPLES:: @@ -519,7 +521,7 @@ def _repr_(self): """ return "Augmented Chow ring ideal of {} of Feitchner-Yuzvinsky presentation".format(self._matroid) - def _latex_(self): + def _latex_(self) -> str: r""" Return a LaTeX representation of ``self``. @@ -554,11 +556,12 @@ def groebner_basis(self, algorithm='', *args, **kwargs): gb = [] # reduced groebner basis with two eliminated cases E = list(self._matroid.groundset()) poly_ring = self.ring() - reln = lambda x,y: x <= y - lattice_flats = Poset((self._flats, reln)) + + lattice_flats = Poset((self._flats, lambda x, y: x <= y)) antichains = lattice_flats.antichains().elements_of_depth_iterator(2) for F, G in antichains: - gb.append(self._flats_generator[F] * self._flats_generator[G]) # non-nested flats + # non-nested flats + gb.append(self._flats_generator[F] * self._flats_generator[G]) for i in E: term = poly_ring.zero() for H in self._flats_containing[i]: @@ -575,7 +578,7 @@ def groebner_basis(self, algorithm='', *args, **kwargs): order_ideal_modified = lattice_flats.order_ideal([F]) order_ideal_modified.remove(F) for G in order_ideal_modified: # nested flats - gb.append(self._flats_generator[G]*term1**(self._matroid.rank(F) - self._matroid.rank(G))) + gb.append(self._flats_generator[G] * term1**(self._matroid.rank(F) - self._matroid.rank(G))) return PolynomialSequence(poly_ring, [gb]) @@ -611,10 +614,9 @@ def normal_basis(self, algorithm='', *args, **kwargs): monomial_basis.append(R.one()) else: k = len(subset) - max_powers = [] - max_powers.append(ranks[subset[0]]) - for i in range(1, k): - max_powers.append(ranks[subset[i]] - ranks[subset[i-1]]) + max_powers = [ranks[subset[0]]] + max_powers.extend(ranks[subset[i]] - ranks[subset[i - 1]] + for i in range(1, k)) ranges = [range(1, p) for p in max_powers] ranges[0] = range(1, max_powers[0] + 1) for combination in product(*(r for r in ranges)): @@ -674,7 +676,7 @@ class AugmentedChowRingIdeal_atom_free(ChowRingIdeal): Augmented Chow ring ideal of Wheel(3): Regular matroid of rank 3 on 6 elements with 16 bases in the atom-free presentation """ - def __init__(self, M, R): + def __init__(self, M, R) -> None: r""" Initialize ``self``. @@ -695,7 +697,7 @@ def __init__(self, M, R): self._flats_generator = dict(zip(self._flats, gens)) MPolynomialIdeal.__init__(self, poly_ring, self._gens_constructor(poly_ring)) - def _gens_constructor(self, poly_ring): + def _gens_constructor(self, poly_ring) -> list: r""" Return the generators of ``self``. @@ -714,8 +716,7 @@ def _gens_constructor(self, poly_ring): for F in self._flats: for x in F: flats_containing[x].append(F) - reln = lambda x,y: x <= y - lattice_flats = Poset((self._flats, reln)) + lattice_flats = Poset((self._flats, lambda x, y: x <= y)) antichains = lattice_flats.antichains().elements_of_depth_iterator(2) for F, G in antichains: Q.append(self._flats_generator[F] * self._flats_generator[G]) @@ -727,11 +728,12 @@ def _gens_constructor(self, poly_ring): if term**2 not in Q: Q.append(term**2) - if F not in flats_containing[x]: # generators for every set of flats not containing element - Q.append(self._flats_generator[F]*term) + if F not in flats_containing[x]: + # generators for every set of flats not containing element + Q.append(self._flats_generator[F] * term) return Q - def _repr_(self): + def _repr_(self) -> str: r""" EXAMPLES:: @@ -742,7 +744,7 @@ def _repr_(self): """ return "Augmented Chow ring ideal of {} in the atom-free presentation".format(self._matroid) - def _latex_(self): + def _latex_(self) -> str: r""" Return the LaTeX output of the ring and generators of ``self``.