Skip to content

Commit

Permalink
sagemathgh-39579: some details in schemes, typing annotation for gens
Browse files Browse the repository at this point in the history
    
adding a little bit of typing annotation for `gens` methods in schemes

There remains several such methods in `elliptic_curves` that return
lists and should be changed later to return tuples.

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
    
URL: sagemath#39579
Reported by: Frédéric Chapoton
Reviewer(s): Vincent Macri
  • Loading branch information
Release Manager committed Mar 2, 2025
2 parents f88a270 + b4eeda7 commit 3ba89b5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 73 deletions.
39 changes: 21 additions & 18 deletions src/sage/schemes/curves/plane_curve_arrangement.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
# https://www.gnu.org/licenses/
# *****************************************************************************

from itertools import combinations
from typing import Any

from sage.categories.sets_cat import Sets
from sage.groups.free_group import FreeGroup
from sage.misc.abstract_method import abstract_method
Expand All @@ -71,7 +72,7 @@ class PlaneCurveArrangementElement(Element):
"""
An ordered plane curve arrangement.
"""
def __init__(self, parent, curves, check=True):
def __init__(self, parent, curves, check=True) -> None:
"""
Construct a plane curve arrangement.
Expand Down Expand Up @@ -120,7 +121,7 @@ def __getitem__(self, i):
"""
return self._curves[i]

def __hash__(self):
def __hash__(self) -> int:
r"""
TESTS::
Expand All @@ -130,7 +131,7 @@ def __hash__(self):
"""
return hash(self.curves())

def ncurves(self):
def ncurves(self) -> int:
r"""
Return the number of curves in the arrangement.
Expand All @@ -149,7 +150,7 @@ def ncurves(self):

__len__ = ncurves

def curves(self):
def curves(self) -> tuple:
r"""
Return the curves in the arrangement as a tuple.
Expand All @@ -170,7 +171,7 @@ def curves(self):
"""
return self._curves

def _repr_(self):
def _repr_(self) -> str:
r"""
String representation for a curve arrangement.
Expand All @@ -195,11 +196,11 @@ def _repr_(self):
curves = ', '.join(h.defining_polynomial()._repr_()
for h in self._curves)
return 'Arrangement ({}) in {}'.format(curves,
self.parent().ambient_space())
self.parent().ambient_space())
return 'Arrangement of {} curves in {}'.format(len(self),
self.parent().ambient_space())
self.parent().ambient_space())

def _richcmp_(self, other, op):
def _richcmp_(self, other, op) -> bool:
"""
Compare two curve arrangements.
Expand Down Expand Up @@ -337,7 +338,7 @@ def coordinate_ring(self):
"""
return self._curves[0].defining_polynomial().parent()

def defining_polynomials(self):
def defining_polynomials(self) -> tuple:
r"""
Return the defining polynomials of the elements of ``self``.
Expand Down Expand Up @@ -428,7 +429,7 @@ class AffinePlaneCurveArrangementElement(PlaneCurveArrangementElement):
"""
An ordered affine plane curve arrangement.
"""
def __init__(self, parent, curves, check=True):
def __init__(self, parent, curves, check=True) -> None:
"""
Construct an ordered affine plane curve arrangement.
Expand Down Expand Up @@ -584,7 +585,7 @@ def fundamental_group(self, simplified=True, vertical=True,
self._meridians_nonsimpl_nonvertical = dic
return G

def meridians(self, simplified=True, vertical=True):
def meridians(self, simplified=True, vertical=True) -> dict:
r"""
Return the meridians of each irreducible component.
Expand Down Expand Up @@ -921,7 +922,7 @@ def fundamental_group(self, simplified=True):
self._meridians_nonsimpl = dic1
return G

def meridians(self, simplified=True):
def meridians(self, simplified=True) -> dict:
r"""
Return the meridians of each irreducible component.
Expand Down Expand Up @@ -991,7 +992,7 @@ class PlaneCurveArrangements(UniqueRepresentation, Parent):
Element = PlaneCurveArrangementElement

@staticmethod
def __classcall__(cls, base, names=()):
def __classcall__(cls, base, names: tuple[str, ...] = ()):
"""
Normalize the inputs to ensure a unique representation.
Expand All @@ -1005,7 +1006,7 @@ def __classcall__(cls, base, names=()):
names = normalize_names(len(names), names)
return super().__classcall__(cls, base, names)

def __init__(self, base_ring, names=()):
def __init__(self, base_ring, names: tuple[str, ...] = ()):
"""
Initialize ``self``.
Expand Down Expand Up @@ -1061,6 +1062,8 @@ def change_ring(self, base_ring):
sage: L.change_ring(QQ) is L
True
"""
# return self.__class__(base_ring, names=self.variable_names())
# line below is ugly but line above does not work
return self.__reduce__()[1][0](base_ring, names=self.variable_names())

@abstract_method
Expand Down Expand Up @@ -1124,7 +1127,7 @@ def _element_constructor_(self, *args, **kwds):
arg = tuple(args)
ambient_space = self.ambient_space()
R = ambient_space.coordinate_ring()
curves = ()
curves: tuple[Any, ...] = ()
for h in arg:
try:
ambient = h.ambient_space()
Expand Down Expand Up @@ -1173,7 +1176,7 @@ def ngens(self):
"""
return len(self.variable_names())

def gens(self):
def gens(self) -> tuple:
"""
Return the coordinates.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/schemes/generic/ambient_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def gen(self, n=0):
"""
return self.coordinate_ring().gen(n)

def gens(self):
def gens(self) -> tuple:
"""
Return the generators of the coordinate ring of the scheme
``self``.
Expand Down
30 changes: 10 additions & 20 deletions src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,10 @@ def poly_ring(self):
"""
return self._poly_ring

def gens(self):
def gens(self) -> tuple:
"""
Return a tuple (x, T) where x and T are the generators of the ring
(as element *of this ring*).
.. NOTE::
I have no idea if this is compatible with the usual Sage
'gens' interface.
Return (x, T) where x and T are the generators of the ring
(as elements *of this ring*).
EXAMPLES::
Expand Down Expand Up @@ -2539,7 +2534,7 @@ def zero(self):
"""
return self.element_class(self, self._poly_ring.zero(), check=False)

def gens(self):
def gens(self) -> tuple:
"""
Return the generators of ``self``.
Expand Down Expand Up @@ -2680,7 +2675,7 @@ def monomial_diff_coeffs_matrices(self):
mat_2[i] = self._precomputed_diff_coeffs[i][2]
return mat_1.transpose(), mat_2.transpose()

def _precompute_monomial_diffs(self):
def _precompute_monomial_diffs(self) -> list:
r"""
Precompute coefficients of the basis representation of `d(x^iy^j)`
for small `i`, `j`.
Expand Down Expand Up @@ -3804,6 +3799,7 @@ def frob_basis_elements(self, prec, p):
F.append(F_i)
return F

@cached_method
def helper_matrix(self):
r"""
We use this to solve for the linear combination of
Expand All @@ -3821,22 +3817,16 @@ def helper_matrix(self):
[-100/2101 -125/2101 -625/8404 -64/2101 -80/2101]
[ -80/2101 -100/2101 -125/2101 -625/8404 -64/2101]
"""
try:
return self._helper_matrix
except AttributeError:
pass

# The smallest y term of (1/j) d(x^i y^j) is constant for all j.
x, y = self.base_ring().gens()
n = self.degree()
L = [(y * x**i).diff().extract_pow_y(0) for i in range(n)]
A = matrix(L).transpose()
if A.base_ring() not in IntegralDomains():
# must be using integer_mod or something to approximate
self._helper_matrix = (~A.change_ring(QQ)).change_ring(A.base_ring())
else:
self._helper_matrix = ~A
return self._helper_matrix
# must be using integer_mod or something to approximate ?
return (~A.change_ring(QQ)).change_ring(A.base_ring())

return ~A

def _element_constructor_(self, val=0, offset=0):
r"""
Expand Down
30 changes: 15 additions & 15 deletions src/sage/schemes/plane_conics/con_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def diagonalization(self, names=None):
con = Conic(D, names=names)
return con, con.hom(T, self), self.hom(T.inverse(), con)

def gens(self):
def gens(self) -> tuple:
r"""
Return the generators of the coordinate ring of ``self``.
Expand Down Expand Up @@ -945,28 +945,28 @@ def parametrization(self, point=None, morphism=True):
point = Sequence(point)
B = self.base_ring()
Q = PolynomialRing(B, 'x,y')
[x, y] = Q.gens()
x, y = Q.gens()
gens = self.ambient_space().gens()
P = PolynomialRing(B, 4, ['X', 'Y', 'T0', 'T1'])
[X, Y, T0, T1] = P.gens()
c3 = [j for j in range(2,-1,-1) if point[j] != 0][0]
c1 = [j for j in range(3) if j != c3][0]
c2 = [j for j in range(3) if j != c3 and j != c1][0]
L = [0,0,0]
L[c1] = Y*T1*point[c1] + Y*T0
L[c2] = Y*T1*point[c2] + X*T0
L[c3] = Y*T1*point[c3]
X, Y, T0, T1 = P.gens()
c3 = next(j for j in range(2, -1, -1) if point[j] != 0)
c1 = next(j for j in range(3) if j != c3)
c2 = next(j for j in range(3) if j != c3 and j != c1)
L = [0, 0, 0]
L[c1] = Y * T1 * point[c1] + Y * T0
L[c2] = Y * T1 * point[c2] + X * T0
L[c3] = Y * T1 * point[c3]
bezout = P(self.defining_polynomial()(L) / T0)
t = [bezout([x,y,0,-1]),bezout([x,y,1,0])]
par = (tuple([Q(p([x,y,t[0],t[1]])/y) for p in L]),
tuple([gens[m]*point[c3]-gens[c3]*point[m]
for m in [c2,c1]]))
t = [bezout([x, y, 0, -1]), bezout([x, y, 1, 0])]
par = (tuple([Q(p([x, y, t[0], t[1]]) / y) for p in L]),
tuple([gens[m] * point[c3] - gens[c3] * point[m]
for m in [c2, c1]]))
if self._parametrization is None:
self._parametrization = par
if not morphism:
return par
P1 = ProjectiveSpace(self.base_ring(), 1, 'x,y')
return P1.hom(par[0],self), self.Hom(P1)(par[1], check=False)
return P1.hom(par[0], self), self.Hom(P1)(par[1], check=False)

def point(self, v, check=True):
r"""
Expand Down
24 changes: 8 additions & 16 deletions src/sage/schemes/toric/chow_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
# ****************************************************************************
from __future__ import annotations

from sage.misc.cachefunc import cached_method
from sage.misc.flatten import flatten
from sage.misc.fast_methods import WithEqualityById
from sage.modules.fg_pid.fgp_module import FGP_Module_class
Expand Down Expand Up @@ -226,6 +227,7 @@ def _repr_(self) -> str:
s += ')'
return s

@cached_method
def degree(self) -> int:
r"""
Return the degree of the Chow cycle.
Expand All @@ -245,18 +247,14 @@ def degree(self) -> int:
sage: [ a.degree() for a in A.gens() ]
[2, 1, 0]
"""
if '_dim' in self.__dict__:
return self._dim

ambient_dim = self.parent()._variety.dimension()
cone_dim = None
for i, cone in enumerate(self.parent()._cones):
if self.lift()[i] != 0:
if cone_dim not in [None, cone.dim()]:
raise ValueError('Chow cycle is not of definite degree')
cone_dim = cone.dim()
self._dim = ambient_dim - cone_dim
return self._dim
return ambient_dim - cone_dim

def project_to_degree(self, degree):
r"""
Expand Down Expand Up @@ -817,6 +815,7 @@ def _cone_to_V(self, cone):
x[self._cones.index(cone)] = 1
return self._V(x)

@cached_method
def degree(self, k=None):
r"""
Return the degree-`k` Chow group.
Expand Down Expand Up @@ -911,15 +910,8 @@ def degree(self, k=None):
"""
if k is not None:
return self.degree()[k]

try:
return self._degree
except AttributeError:
pass

self._degree = tuple(ChowGroup_degree_class(self, d)
for d in range(self._variety.dimension() + 1))
return self._degree
return tuple(ChowGroup_degree_class(self, d)
for d in range(self._variety.dimension() + 1))

def coordinate_vector(self, chow_cycle, degree=None, reduce=True):
r"""
Expand Down Expand Up @@ -958,7 +950,7 @@ def coordinate_vector(self, chow_cycle, degree=None, reduce=True):
a = chow_cycle.project_to_degree(degree)
return self.degree(degree).module().coordinate_vector(a, reduce=reduce)

def gens(self, degree=None):
def gens(self, degree=None) -> tuple:
r"""
Return the generators of the Chow group.
Expand Down Expand Up @@ -1177,7 +1169,7 @@ def gen(self, i):
"""
return self._gens[i]

def gens(self):
def gens(self) -> tuple:
"""
Return the generators of the Chow group of fixed degree.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/schemes/toric/divisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ def ngens(self):
return self.scheme().fan().nrays()

@cached_method
def gens(self):
def gens(self) -> tuple:
r"""
Return the generators of the divisor group.
Expand Down
4 changes: 2 additions & 2 deletions src/sage/schemes/toric/variety.py
Original file line number Diff line number Diff line change
Expand Up @@ -3211,7 +3211,7 @@ def __call__(self, x, coerce=True):
"""
return self._element_constructor_(x)

def gens(self):
def gens(self) -> tuple:
r"""
Return the generators of the cohomology ring.
Expand All @@ -3235,7 +3235,7 @@ def gens(self):

def gen(self, i):
r"""
Return the generators of the cohomology ring.
Return a generator of the cohomology ring.
INPUT:
Expand Down

0 comments on commit 3ba89b5

Please sign in to comment.