Skip to content

Commit

Permalink
sagemathgh-35884: sage.{modular,schemes}: Modularization fixes for …
Browse files Browse the repository at this point in the history
…imports; update `sage -fiximports`, add relint pattern

    
<!-- Please provide a concise, informative and self-explanatory title.
-->
<!-- Don't put issue numbers in the title. Put it in the Description
below. -->
<!-- For example, instead of "Fixes sagemath#12345", use "Add a new method to
multiply two integers" -->

### 📚 Description

<!-- Describe your changes here in detail. -->
<!-- Why is this change required? What problem does it solve? -->
<!-- If this PR resolves an open issue, please link to it here. For
example "Fixes sagemath#12345". -->

- Part of: sagemath#29705
- Cherry-picked from: sagemath#35095

<!-- If your change requires a documentation PR, please link it
appropriately. -->

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x
]`. -->

- [x] The title is concise, informative, and self-explanatory.
- [ ] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on
- sagemath#12345: short description why this is a dependency
- sagemath#34567: ...
-->
- Depends on sagemath#35974 (to soothe the linter)

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: sagemath#35884
Reported by: Matthias Köppe
Reviewer(s): Kwankyu Lee, Matthias Köppe
  • Loading branch information
Release Manager committed Aug 10, 2023
2 parents 0fd5967 + bcd1d6e commit beafba9
Show file tree
Hide file tree
Showing 59 changed files with 489 additions and 356 deletions.
13 changes: 12 additions & 1 deletion src/.relint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@
# Keep in sync with SAGE_ROOT/src/sage/misc/replace_dot_all.py
pattern: 'from\s+sage(|[.](arith|categories|combinat|crypto|databases|data_structures|dynamics|ext|game_theory|games|geometry|graphs|groups|interfaces|manifolds|matrix|matroids|misc|modules|monoids|numerical|probability|quadratic_forms|quivers|rings|sat|schemes|sets|stats|symbolic|tensor)[a-z0-9_.]*|[.]libs)[.]all\s+import'
# imports from .all are allowed in all.py; also allow in some modules that need sage.all
filePattern: '(.*/|)(?!(all|benchmark|dev_tools|parsing|sage_eval))[^/.]*[.](py|pyx|pxi)$'
filePattern: '(.*/|)(?!(all|benchmark|dev_tools|parsing|sage_eval|explain_pickle|.*_test))[^/.]*[.](py|pyx|pxi)$'

- name: 'namespace_pkg_all_import_2: Module-level import of .all of a namespace package'
hint: |
Sage library code should not import sage.PAC.KAGE.all when sage.PAC.KAGE is an implicit
namespace package. Type import_statements("SOME_IDENTIFIER") to find a more specific import,
and rewrite the import statement as "from sage.PAC.KAGE.MODULE import ..."
or "lazy_import('sage.PAC.KAGE.MODULE', '...')".
# Keep in sync with above; but for now we ignore sage.{arith,categories}
pattern: '^import\s+sage(|[.](combinat|crypto|databases|data_structures|dynamics|ext|game_theory|games|geometry|graphs|groups|interfaces|manifolds|matrix|matroids|misc|modules|monoids|numerical|probability|quadratic_forms|quivers|rings|sat|schemes|sets|stats|symbolic|tensor)[a-z0-9_.]*|[.]libs)[.]all'
# imports from .all are allowed in all.py; also allow in some modules that need sage.all
filePattern: '(.*/|)(?!(all|benchmark|dev_tools|parsing|sage_eval|explain_pickle|.*_test))[^/.]*[.](py|pyx|pxi)$'

# Magic doctest comments

Expand Down
35 changes: 18 additions & 17 deletions src/sage/lfunctions/lcalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@
import os

from sage.structure.sage_object import SageObject
from sage.misc.lazy_import import lazy_import
from sage.misc.pager import pager
import sage.rings.all
import sage.schemes.elliptic_curves.ell_generic
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ

lazy_import('sage.rings.complex_mpfr', 'ComplexField')
lazy_import('sage.rings.real_mpfr', 'RealField')
lazy_import('sage.schemes.elliptic_curves.ell_generic', 'EllipticCurve_generic', as_='EllipticCurve')

prec = 32

Expand Down Expand Up @@ -75,9 +80,8 @@ def _compute_L(self, L):
if L == 'tau':
return '--tau'
return L
import sage.schemes.all
if sage.schemes.elliptic_curves.ell_generic.is_EllipticCurve(L):
if L.base_ring() == sage.rings.all.RationalField():
if isinstance(L, EllipticCurve):
if L.base_ring() == QQ:
L = L.minimal_model()
return '-e --a1 %s --a2 %s --a3 %s --a4 %s --a6 %s' % tuple(L.a_invariants())
raise TypeError("$L$-function of %s not known" % L)
Expand Down Expand Up @@ -127,7 +131,7 @@ def zeros(self, n, L=''):
[0.000000000, 5.00317001, 6.87039122]
"""
L = self._compute_L(L)
RR = sage.rings.all.RealField(prec)
RR = RealField(prec)
X = self('-z %s %s' % (int(n), L))
return [RR(z) for z in X.split()]

Expand Down Expand Up @@ -162,7 +166,7 @@ def zeros_in_interval(self, x, y, stepsize, L=''):
[(14.1347251, 0.184672916), (21.0220396, -0.0677893290), (25.0108576, -0.0555872781)]
"""
L = self._compute_L(L)
RR = sage.rings.all.RealField(prec)
RR = RealField(prec)
X = self('--zeros-interval -x %s -y %s --stepsize=%s %s' % (
float(x), float(y), float(stepsize), L))
return [tuple([RR(z) for z in t.split()]) for t in X.split('\n')]
Expand Down Expand Up @@ -193,7 +197,7 @@ def value(self, s, L=''):
2.69261988568132 - 0.0203860296025982*I
"""
L = self._compute_L(L)
CC = sage.rings.all.ComplexField(prec)
CC = ComplexField(prec)
s = CC(s)
x, y = self('-v -x %s -y %s %s' % (s.real(), s.imag(), L)).split()
return CC((float(x), float(y)))
Expand Down Expand Up @@ -275,7 +279,7 @@ def values_along_line(self, s0, s1, number_samples, L=''):
"""
L = self._compute_L(L)
CC = sage.rings.all.ComplexField(prec)
CC = ComplexField(prec)
s0 = CC(s0)
s1 = CC(s1)
v = self('--value-line-segment -x %s -y %s -X %s -Y %s --number-samples %s %s' % (
Expand Down Expand Up @@ -343,8 +347,7 @@ def twist_values(self, s, dmin, dmax, L=''):
0.373691713 + 0.0*I
"""
L = self._compute_L(L)
CC = sage.rings.all.ComplexField(prec)
Z = sage.rings.all.Integer
CC = ComplexField(prec)
s = CC(s)
typ = '--twist-quadratic'
dmin = int(dmin)
Expand All @@ -358,7 +361,7 @@ def twist_values(self, s, dmin, dmax, L=''):
return w
for a in v.split('\n'):
d, x, y = a.split()
w.append((Z(d), CC(x, y)))
w.append((ZZ(d), CC(x, y)))
return w

def twist_zeros(self, n, dmin, dmax, L=''):
Expand Down Expand Up @@ -393,8 +396,7 @@ def twist_zeros(self, n, dmin, dmax, L=''):
{-3: [8.03973716, 11.2492062, 15.7046192], 5: [6.64845335, 9.83144443, 11.9588456]}
"""
L = self._compute_L(L)
RR = sage.rings.all.RealField(prec)
Z = sage.rings.all.Integer
RR = RealField(prec)
typ = '--twist-quadratic'
n = int(n)
v = self('-z %s %s --start %s --finish %s %s' % (
Expand All @@ -405,7 +407,7 @@ def twist_zeros(self, n, dmin, dmax, L=''):
for a in v.split('\n'):
d, x = a.split()
x = RR(x)
d = Z(d)
d = ZZ(d)
if d in w:
w[d].append(x)
else:
Expand Down Expand Up @@ -439,10 +441,9 @@ def analytic_rank(self, L=''):
1
"""
L = self._compute_L(L)
Z = sage.rings.all.Integer
s = self('--rank-compute %s' % L)
i = s.find('equals')
return Z(s[i + 6:])
return ZZ(s[i + 6:])


# An instance
Expand Down
6 changes: 3 additions & 3 deletions src/sage/lfunctions/sympow.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from sage.structure.sage_object import SageObject
from sage.misc.pager import pager
from sage.misc.verbose import verbose
import sage.rings.all
from sage.rings.integer import Integer


class Sympow(SageObject):
Expand Down Expand Up @@ -237,7 +237,7 @@ def modular_degree(self, E):
if i == -1:
print(self._fix_err(v))
raise RuntimeError("failed to compute modular degree")
return sage.rings.all.Integer(v[i + len(s):])
return Integer(v[i + len(s):])

def analytic_rank(self, E):
r"""
Expand Down Expand Up @@ -296,7 +296,7 @@ def analytic_rank(self, E):
print(self._fix_err(v))
raise RuntimeError("failed to compute analytic rank")
j = v.rfind(':')
r = sage.rings.all.Integer(v[i + len(s):j])
r = Integer(v[i + len(s):j])
i = v.rfind(' ')
L = v[i + 1:]
return r, L
Expand Down
10 changes: 6 additions & 4 deletions src/sage/misc/replace_dot_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ def make_replacements_in_file(location, package_regex=None, verbose=False, outpu
write_file.write(replaced_content) # overwriting the old file contents with the new/replaced content


def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex=None, verbose=False):
def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex=None, verbose=False, *,
excluded_file_regex=r'auto-methods|replace_dot_all'):
r"""
Replace ``import`` statements in the files in directory ``dir`` matching the regex pattern ``file_regex``.
Expand All @@ -396,6 +397,7 @@ def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex
- ``package_regex`` -- (default: :obj:`default_package_regex`) a regular expression matching
the ``sage.PAC.KAGE.all`` package names from which we do not want to import.
- ``verbose`` -- if True, print statements when interesting examples are found
- ``excluded_file_regex`` -- a regular expression matching the file names to exclude
EXAMPLES::
Expand All @@ -404,14 +406,14 @@ def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex
"""
global numberFiles, numberFilesMatchingRegex
file_regex = re.compile(file_regex)
excluded_file_regex = re.compile(excluded_file_regex)
for root, dirs, files in os.walk(dir, topdown=False):
for name in files:
numberFiles += 1
if file_regex.search(name):
if file_regex.search(name) and not excluded_file_regex.search(name):
numberFilesMatchingRegex += 1
location = os.path.join(root, name)
if location.find('replace_dot_all') == -1: # to avoid changing anything in this file itself
make_replacements_in_file(location, package_regex, verbose)
make_replacements_in_file(location, package_regex, verbose)


# ******************************************************** EXECUTES MAIN FUNCTION **********************************************************************
Expand Down
12 changes: 7 additions & 5 deletions src/sage/modular/abvar/abvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from copy import copy
from random import randrange

import sage.rings.abc

from sage.arith.functions import lcm as LCM
from sage.arith.misc import divisors, next_prime, is_prime
from sage.categories.modular_abelian_varieties import ModularAbelianVarieties
Expand All @@ -55,7 +57,6 @@
from sage.rings.integer import Integer
from sage.rings.integer_ring import ZZ
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.qqbar import QQbar
from sage.rings.rational_field import QQ
from sage.rings.ring import is_Ring
from sage.schemes.elliptic_curves.constructor import EllipticCurve
Expand All @@ -67,6 +68,7 @@
lazy_import('sage.databases.cremona',
['cremona_letter_code', 'CremonaDatabase'])


from . import homspace
from . import lseries
from .morphism import HeckeOperator, Morphism, DegeneracyMap
Expand Down Expand Up @@ -798,8 +800,8 @@ def _Hom_(self, B, cat=None):
L = B.base_field()
if K == L:
F = K
elif K == QQbar or L == QQbar:
F = QQbar
elif isinstance(K, sage.rings.abc.AlgebraicField) or isinstance(L, sage.rings.abc.AlgebraicField):
from sage.rings.qqbar import QQbar as F
else:
# TODO -- improve this
raise ValueError("please specify a category")
Expand Down Expand Up @@ -997,7 +999,7 @@ def intersection(self, other):
for v in V.coordinate_module(S).basis()]

if A.dimension() > 0:
finitegroup_base_field = QQbar
from sage.rings.qqbar import QQbar as finitegroup_base_field
else:
finitegroup_base_field = self.base_field()
G = self.finite_subgroup(gens, field_of_definition=finitegroup_base_field)
Expand Down Expand Up @@ -3126,7 +3128,7 @@ def finite_subgroup(self, X, field_of_definition=None, check=True):
raise ValueError("X must be a subgroup of self.")

if field_of_definition is None:
field_of_definition = QQbar
from sage.rings.qqbar import QQbar as field_of_definition

return FiniteSubgroup_lattice(
self, X, field_of_definition=field_of_definition, check=check)
Expand Down
12 changes: 9 additions & 3 deletions src/sage/modular/abvar/finite_subgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
# http://www.gnu.org/licenses/
#*****************************************************************************

import sage.rings.abc

from sage.misc.lazy_import import lazy_import
from sage.modular.abvar.torsion_point import TorsionPoint
from sage.modules.module import Module
from sage.modules.free_module import is_FreeModule
Expand All @@ -105,7 +108,6 @@
from sage.structure.richcmp import richcmp_method, richcmp
from sage.rings.integer import Integer
from sage.rings.integer_ring import ZZ
from sage.rings.qqbar import QQbar
from sage.rings.rational_field import QQ
from sage.arith.functions import lcm
from sage.misc.misc_c import prod
Expand Down Expand Up @@ -521,7 +523,7 @@ def _repr_(self):
'Finite subgroup with invariants [3, 3, 3, 3, 3, 3, 3, 3, 3, 3] over QQ of Abelian variety J0(42) of dimension 5'
"""
K = self.__field_of_definition
if K == QQbar:
if isinstance(K, sage.rings.abc.AlgebraicField):
field = "QQbar"
elif K == QQ:
field = "QQ"
Expand Down Expand Up @@ -750,6 +752,8 @@ def subgroup(self, gens):
sage: H == G.subgroup([[1/11,0,0,0]])
True
"""
from sage.rings.qqbar import QQbar

if not isinstance(gens, (tuple, list)):
raise TypeError("gens must be a list or tuple")
A = self.abelian_variety()
Expand Down Expand Up @@ -815,7 +819,7 @@ def invariants(self):


class FiniteSubgroup_lattice(FiniteSubgroup):
def __init__(self, abvar, lattice, field_of_definition=QQbar, check=True):
def __init__(self, abvar, lattice, field_of_definition=None, check=True):
"""
A finite subgroup of a modular abelian variety that is defined by a
given lattice.
Expand All @@ -841,6 +845,8 @@ def __init__(self, abvar, lattice, field_of_definition=QQbar, check=True):
sage: G = J.finite_subgroup([[1/3,0], [0,1/5]]); G
Finite subgroup with invariants [15] over QQbar of Abelian variety J0(11) of dimension 1
"""
if field_of_definition is None:
from sage.rings.qqbar import QQbar as field_of_definition
if check:
from .abvar import is_ModularAbelianVariety
if not is_FreeModule(lattice) or lattice.base_ring() != ZZ:
Expand Down
6 changes: 3 additions & 3 deletions src/sage/modular/abvar/homspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@
from . import morphism

import sage.rings.integer_ring
import sage.rings.all
from sage.rings.infinity import Infinity

from sage.rings.ring import Ring
from sage.matrix.matrix_space import MatrixSpace
from sage.matrix.constructor import Matrix, identity_matrix
from sage.structure.element import is_Matrix

ZZ = sage.rings.integer_ring.ZZ
from sage.rings.integer_ring import ZZ


class Homspace(HomsetWithBase):
Expand Down Expand Up @@ -882,7 +882,7 @@ def index_in(self, other, check=True):
M = self.free_module()
N = other.free_module()
if M.rank() < N.rank():
return sage.rings.all.Infinity
return Infinity
return M.index_in(N)

def index_in_saturation(self):
Expand Down
3 changes: 2 additions & 1 deletion src/sage/modular/arithgroup/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from .congroup_gamma import Gamma_constructor as Gamma, is_Gamma
from .congroup_sl2z import SL2Z, is_SL2Z

from .arithgroup_perm import ArithmeticSubgroup_Permutation
from sage.misc.lazy_import import lazy_import
lazy_import('sage.modular.arithgroup.arithgroup_perm', 'ArithmeticSubgroup_Permutation')

from .congroup import (degeneracy_coset_representatives_gamma0,
degeneracy_coset_representatives_gamma1)
Expand Down
11 changes: 6 additions & 5 deletions src/sage/modular/arithgroup/arithgroup_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,20 +885,21 @@ def index(self):

def generalised_level(self):
r"""
Return the generalised level of self, i.e. the least common multiple of
Return the generalised level of ``self``, i.e., the least common multiple of
the widths of all cusps.
If self is *even*, Wohlfart's theorem tells us that this is equal to
the (conventional) level of self when self is a congruence subgroup.
This can fail if self is odd, but the actual level is at most twice the
If ``self`` is *even*, Wohlfart's theorem tells us that this is equal to
the (conventional) level of ``self`` when ``self`` is a congruence subgroup.
This can fail if ``self`` is odd, but the actual level is at most twice the
generalised level. See the paper by Kiming, Schuett and Verrill for
more examples.
EXAMPLES::
sage: Gamma0(18).generalised_level()
18
sage: sage.modular.arithgroup.arithgroup_perm.HsuExample18().generalised_level()
sage: from sage.modular.arithgroup.arithgroup_perm import HsuExample18
sage: HsuExample18().generalised_level()
24
In the following example, the actual level is twice the generalised
Expand Down
Loading

0 comments on commit beafba9

Please sign in to comment.